Add test dependency when kotlin file are only under test root
This commit is contained in:
@@ -25,7 +25,6 @@ import com.intellij.openapi.module.ModuleManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.search.FileTypeIndex;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.util.Function;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -60,14 +59,18 @@ public class ConfigureKotlinInProjectUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean hasKotlinFiles(@NotNull Module module) {
|
||||
return !FileTypeIndex.getFiles(JetFileType.INSTANCE, GlobalSearchScope.moduleScope(module)).isEmpty();
|
||||
public static boolean hasKotlinFilesInSources(@NotNull Module module) {
|
||||
return !FileTypeIndex.getFiles(JetFileType.INSTANCE, module.getModuleScope(false)).isEmpty();
|
||||
}
|
||||
|
||||
public static boolean hasKotlinFilesOnlyInTests(@NotNull Module module) {
|
||||
return !hasKotlinFilesInSources(module) && !FileTypeIndex.getFiles(JetFileType.INSTANCE, module.getModuleScope(true)).isEmpty();
|
||||
}
|
||||
|
||||
public static Collection<Module> getModulesWithKotlinFiles(@NotNull Project project) {
|
||||
List<Module> modulesWithKotlin = Lists.newArrayList();
|
||||
for (Module module : ModuleManager.getInstance(project).getModules()) {
|
||||
if (hasKotlinFiles(module)) {
|
||||
if (hasKotlinFilesInSources(module) || hasKotlinFilesOnlyInTests(module)) {
|
||||
modulesWithKotlin.add(module);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,8 @@ package org.jetbrains.jet.plugin.configuration;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.ModuleRootModificationUtil;
|
||||
import com.intellij.openapi.roots.OrderEnumerator;
|
||||
import com.intellij.openapi.roots.OrderRootType;
|
||||
import com.intellij.openapi.roots.*;
|
||||
import com.intellij.openapi.roots.impl.OrderEntryUtil;
|
||||
import com.intellij.openapi.roots.libraries.Library;
|
||||
import com.intellij.openapi.roots.libraries.LibraryTable;
|
||||
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
|
||||
@@ -231,13 +230,43 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
}
|
||||
|
||||
private void addLibraryToModuleIfNeeded(Module module) {
|
||||
if (getKotlinLibrary(module) == null) {
|
||||
DependencyScope expectedDependencyScope = getDependencyScope(module);
|
||||
Library kotlinLibrary = getKotlinLibrary(module);
|
||||
if (kotlinLibrary == null) {
|
||||
Library library = getKotlinLibrary(module.getProject());
|
||||
assert library != null : "Kotlin project library should exists";
|
||||
|
||||
ModuleRootModificationUtil.addDependency(module, library);
|
||||
ModuleRootModificationUtil.addDependency(module, library, expectedDependencyScope, false);
|
||||
showInfoNotification(library.getName() + " library was added to module " + module.getName());
|
||||
}
|
||||
else {
|
||||
final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
|
||||
LibraryOrderEntry libraryEntry = OrderEntryUtil.findLibraryOrderEntry(model, kotlinLibrary);
|
||||
if (libraryEntry != null) {
|
||||
DependencyScope libraryDependencyScope = libraryEntry.getScope();
|
||||
if (!expectedDependencyScope.equals(libraryDependencyScope)) {
|
||||
libraryEntry.setScope(expectedDependencyScope);
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
model.commit();
|
||||
}
|
||||
});
|
||||
|
||||
showInfoNotification(kotlinLibrary.getName() + " library scope has changed from " + libraryDependencyScope +
|
||||
" to " + expectedDependencyScope + " for module " + module.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static DependencyScope getDependencyScope(@NotNull Module module) {
|
||||
if (ConfigureKotlinInProjectUtils.hasKotlinFilesOnlyInTests(module)) {
|
||||
return DependencyScope.TEST;
|
||||
}
|
||||
return DependencyScope.COMPILE;
|
||||
}
|
||||
|
||||
private void addJarToExistedLibrary(@NotNull Project project, @NotNull File jarFile) {
|
||||
|
||||
@@ -41,6 +41,7 @@ import com.intellij.util.Processor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.plugin.configuration.ConfigureKotlinInProjectUtils;
|
||||
import org.jetbrains.jet.plugin.versions.KotlinRuntimeLibraryUtil;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
|
||||
@@ -59,7 +60,8 @@ public class KotlinFrameworkDetector {
|
||||
}
|
||||
|
||||
public static boolean isJavaKotlinModule(@NotNull Module module) {
|
||||
GlobalSearchScope scope = module.getModuleWithDependenciesAndLibrariesScope(false);
|
||||
GlobalSearchScope scope = module.getModuleWithDependenciesAndLibrariesScope(
|
||||
ConfigureKotlinInProjectUtils.hasKotlinFilesOnlyInTests(module));
|
||||
return KotlinRuntimeLibraryUtil.getKotlinRuntimeMarkerClass(scope) != null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user