Modules: Support production-on-test dependency
This feature is supported in IDEA project configuration and can be used in Maven/Gradle-based projects #KT-20112 Fixed
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea;
|
||||
|
||||
import com.intellij.ide.plugins.IdeaPluginDescriptor;
|
||||
import com.intellij.ide.plugins.PluginManager;
|
||||
import com.intellij.ide.plugins.PluginManagerCore;
|
||||
import com.intellij.openapi.extensions.PluginId;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.idea.configuration.KotlinModuleTypeManager;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class KotlinPluginUtil {
|
||||
|
||||
public static final PluginId KOTLIN_PLUGIN_ID = PluginId.getId("org.jetbrains.kotlin");
|
||||
|
||||
@NotNull
|
||||
public static String getPluginVersion() {
|
||||
IdeaPluginDescriptor plugin = PluginManager.getPlugin(KOTLIN_PLUGIN_ID);
|
||||
assert plugin != null : "Kotlin plugin not found: " + Arrays.toString(PluginManagerCore.getPlugins());
|
||||
return plugin.getVersion();
|
||||
}
|
||||
|
||||
public static boolean isSnapshotVersion() {
|
||||
return "@snapshot@".equals(getPluginVersion());
|
||||
}
|
||||
|
||||
public static boolean isAndroidGradleModule(@NotNull Module module) {
|
||||
return KotlinModuleTypeManager.getInstance().isAndroidGradleModule(module);
|
||||
}
|
||||
|
||||
public static boolean isGradleModule(@NotNull Module module) {
|
||||
return KotlinModuleTypeManager.getInstance().isGradleModule(module);
|
||||
}
|
||||
|
||||
public static boolean isMavenModule(@NotNull Module module) {
|
||||
// This constant could be acquired from MavenProjectsManager, but we don't want to depend on the Maven plugin...
|
||||
// See MavenProjectsManager.isMavenizedModule()
|
||||
return "true".equals(module.getOptionValue("org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule"));
|
||||
}
|
||||
}
|
||||
+27
-10
@@ -21,6 +21,7 @@ import com.intellij.openapi.module.impl.scopes.LibraryScopeBase
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.projectRoots.Sdk
|
||||
import com.intellij.openapi.roots.*
|
||||
import com.intellij.openapi.roots.impl.ModuleOrderEntryImpl
|
||||
import com.intellij.openapi.roots.impl.libraries.LibraryEx
|
||||
import com.intellij.openapi.roots.libraries.Library
|
||||
import com.intellij.openapi.util.ModificationTracker
|
||||
@@ -35,6 +36,7 @@ import org.jetbrains.kotlin.analyzer.ModuleInfo
|
||||
import org.jetbrains.kotlin.analyzer.TrackableModuleInfo
|
||||
import org.jetbrains.kotlin.caches.resolve.LibraryModuleInfo
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.idea.KotlinPluginUtil
|
||||
import org.jetbrains.kotlin.idea.framework.getLibraryPlatform
|
||||
import org.jetbrains.kotlin.idea.project.KotlinModuleModificationTracker
|
||||
import org.jetbrains.kotlin.idea.project.TargetPlatformDetector
|
||||
@@ -58,8 +60,8 @@ interface IdeaModuleInfo : ModuleInfo {
|
||||
override fun dependencies(): List<IdeaModuleInfo>
|
||||
}
|
||||
|
||||
private fun orderEntryToModuleInfo(project: Project, orderEntry: OrderEntry, productionOnly: Boolean): List<IdeaModuleInfo> {
|
||||
fun Module.toInfos() = correspondingModuleInfos().filter { !productionOnly || it is ModuleProductionSourceInfo }
|
||||
private fun orderEntryToModuleInfo(project: Project, orderEntry: OrderEntry, forProduction: Boolean): List<IdeaModuleInfo> {
|
||||
fun Module.toInfos() = correspondingModuleInfos().filter { !forProduction || it is ModuleProductionSourceInfo }
|
||||
|
||||
if (!orderEntry.isValid) return emptyList()
|
||||
|
||||
@@ -68,7 +70,13 @@ private fun orderEntryToModuleInfo(project: Project, orderEntry: OrderEntry, pro
|
||||
orderEntry.getOwnerModule().toInfos()
|
||||
}
|
||||
is ModuleOrderEntry -> {
|
||||
orderEntry.module?.toInfos().orEmpty()
|
||||
val module = orderEntry.module ?: return emptyList()
|
||||
if (forProduction && orderEntry is ModuleOrderEntryImpl && orderEntry.isProductionOnTestDependency) {
|
||||
listOfNotNull(module.testSourceInfo())
|
||||
}
|
||||
else {
|
||||
module.toInfos()
|
||||
}
|
||||
}
|
||||
is LibraryOrderEntry -> {
|
||||
val library = orderEntry.library ?: return listOf()
|
||||
@@ -88,16 +96,25 @@ fun <T> Module.cached(provider: CachedValueProvider<T>): T {
|
||||
return CachedValuesManager.getManager(project).getCachedValue(this, provider)
|
||||
}
|
||||
|
||||
private fun ideaModelDependencies(module: Module, productionOnly: Boolean): List<IdeaModuleInfo> {
|
||||
private fun OrderEntry.acceptAsDependency(forProduction: Boolean): Boolean {
|
||||
return this !is ExportableOrderEntry
|
||||
|| !forProduction
|
||||
// this is needed for Maven/Gradle projects with "production-on-test" dependency
|
||||
|| this is ModuleOrderEntryImpl && isProductionOnTestDependency
|
||||
|| scope.isForProductionCompile
|
||||
}
|
||||
|
||||
private fun ideaModelDependencies(module: Module, forProduction: Boolean): List<IdeaModuleInfo> {
|
||||
//NOTE: lib dependencies can be processed several times during recursive traversal
|
||||
val result = LinkedHashSet<IdeaModuleInfo>()
|
||||
val dependencyEnumerator = ModuleRootManager.getInstance(module).orderEntries().compileOnly().recursively().exportedOnly()
|
||||
if (productionOnly) {
|
||||
if (forProduction && !(KotlinPluginUtil.isMavenModule(module) || KotlinPluginUtil.isGradleModule(module))) {
|
||||
dependencyEnumerator.productionOnly()
|
||||
}
|
||||
dependencyEnumerator.forEach {
|
||||
orderEntry ->
|
||||
result.addAll(orderEntryToModuleInfo(module.project, orderEntry!!, productionOnly))
|
||||
dependencyEnumerator.forEach { orderEntry ->
|
||||
if (orderEntry.acceptAsDependency(forProduction)) {
|
||||
result.addAll(orderEntryToModuleInfo(module.project, orderEntry!!, forProduction))
|
||||
}
|
||||
true
|
||||
}
|
||||
return result.toList()
|
||||
@@ -125,7 +142,7 @@ data class ModuleProductionSourceInfo internal constructor(override val module:
|
||||
|
||||
override fun dependencies() = module.cached(CachedValueProvider {
|
||||
CachedValueProvider.Result(
|
||||
ideaModelDependencies(module, productionOnly = true),
|
||||
ideaModelDependencies(module, forProduction = true),
|
||||
ProjectRootModificationTracker.getInstance(module.project))
|
||||
})
|
||||
}
|
||||
@@ -140,7 +157,7 @@ data class ModuleTestSourceInfo internal constructor(override val module: Module
|
||||
|
||||
override fun dependencies() = module.cached(CachedValueProvider {
|
||||
CachedValueProvider.Result(
|
||||
ideaModelDependencies(module, productionOnly = false),
|
||||
ideaModelDependencies(module, forProduction = false),
|
||||
ProjectRootModificationTracker.getInstance(module.project))
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user