Refactor runtime library configuration in IDE
This commit is contained in:
@@ -29,10 +29,9 @@ import org.jetbrains.kotlin.idea.framework.JavaRuntimePresentationProvider;
|
||||
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil;
|
||||
import org.jetbrains.kotlin.idea.project.TargetPlatform;
|
||||
import org.jetbrains.kotlin.idea.versions.KotlinRuntimeLibraryCoreUtil;
|
||||
import org.jetbrains.kotlin.utils.KotlinPaths;
|
||||
import org.jetbrains.kotlin.utils.PathUtil;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.jetbrains.kotlin.idea.configuration.ConfigureKotlinInProjectUtils.showInfoNotification;
|
||||
|
||||
public class KotlinJavaModuleConfigurator extends KotlinWithLibraryConfigurator {
|
||||
@@ -61,18 +60,6 @@ public class KotlinJavaModuleConfigurator extends KotlinWithLibraryConfigurator
|
||||
return JavaRuntimeLibraryDescription.LIBRARY_CAPTION;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getJarName() {
|
||||
return PathUtil.KOTLIN_JAVA_RUNTIME_JAR;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getSourcesJarName() {
|
||||
return PathUtil.KOTLIN_JAVA_RUNTIME_SRC_JAR;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getMessageForOverrideDialog() {
|
||||
@@ -97,23 +84,21 @@ public class KotlinJavaModuleConfigurator extends KotlinWithLibraryConfigurator
|
||||
return TargetPlatform.JVM;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public File getExistingJarFile() {
|
||||
return assertFileExists(PathUtil.getKotlinPathsForIdeaPlugin().getRuntimePath());
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public File getExistingSourcesJarFile() {
|
||||
return assertFileExists(PathUtil.getKotlinPathsForIdeaPlugin().getRuntimeSourcesPath());
|
||||
public RuntimeLibraryFiles getExistingJarFiles() {
|
||||
KotlinPaths paths = PathUtil.getKotlinPathsForIdeaPlugin();
|
||||
return new RuntimeLibraryFiles(
|
||||
assertFileExists(paths.getRuntimePath()),
|
||||
assertFileExists(paths.getRuntimeSourcesPath())
|
||||
);
|
||||
}
|
||||
|
||||
public void copySourcesToPathFromLibrary(@NotNull Library library) {
|
||||
String dirToJarFromLibrary = getPathFromLibrary(library, OrderRootType.SOURCES);
|
||||
assert dirToJarFromLibrary != null : "Directory to file from library should be non null";
|
||||
|
||||
copyFileToDir(getExistingSourcesJarFile(), dirToJarFromLibrary);
|
||||
copyFileToDir(getExistingJarFiles().getRuntimeSourcesJar(), dirToJarFromLibrary);
|
||||
}
|
||||
|
||||
public boolean changeOldSourcesPathIfNeeded(@NotNull Library library) {
|
||||
@@ -124,7 +109,7 @@ public class KotlinJavaModuleConfigurator extends KotlinWithLibraryConfigurator
|
||||
String parentDir = getPathFromLibrary(library, OrderRootType.CLASSES);
|
||||
assert parentDir != null : "Parent dir for classes jar should exists for Kotlin library";
|
||||
|
||||
return addSourcesToLibraryIfNeeded(library, getFileInDir(getSourcesJarName(), parentDir));
|
||||
return addSourcesToLibraryIfNeeded(library, getExistingJarFiles().getRuntimeSourcesDestination(parentDir));
|
||||
}
|
||||
|
||||
private static boolean removeOldSourcesRootIfNeeded(@NotNull Library library) {
|
||||
|
||||
@@ -22,10 +22,9 @@ import org.jetbrains.kotlin.idea.framework.JSLibraryStdDescription;
|
||||
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil;
|
||||
import org.jetbrains.kotlin.idea.project.TargetPlatform;
|
||||
import org.jetbrains.kotlin.js.JavaScript;
|
||||
import org.jetbrains.kotlin.utils.KotlinPaths;
|
||||
import org.jetbrains.kotlin.utils.PathUtil;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class KotlinJsModuleConfigurator extends KotlinWithLibraryConfigurator {
|
||||
public static final String NAME = JavaScript.LOWER_NAME;
|
||||
|
||||
@@ -70,18 +69,6 @@ public class KotlinJsModuleConfigurator extends KotlinWithLibraryConfigurator {
|
||||
return JSLibraryStdDescription.LIBRARY_CAPTION;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getJarName() {
|
||||
return PathUtil.JS_LIB_JAR_NAME;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getSourcesJarName() {
|
||||
return PathUtil.JS_LIB_SRC_JAR_NAME;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getMessageForOverrideDialog() {
|
||||
@@ -90,14 +77,12 @@ public class KotlinJsModuleConfigurator extends KotlinWithLibraryConfigurator {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public File getExistingJarFile() {
|
||||
return assertFileExists(PathUtil.getKotlinPathsForIdeaPlugin().getJsStdLibJarPath());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public File getExistingSourcesJarFile() {
|
||||
return assertFileExists(PathUtil.getKotlinPathsForIdeaPlugin().getJsStdLibSrcJarPath());
|
||||
public RuntimeLibraryFiles getExistingJarFiles() {
|
||||
KotlinPaths paths = PathUtil.getKotlinPathsForIdeaPlugin();
|
||||
return new RuntimeLibraryFiles(
|
||||
paths.getJsStdLibJarPath(),
|
||||
paths.getJsStdLibSrcJarPath()
|
||||
);
|
||||
}
|
||||
|
||||
KotlinJsModuleConfigurator() {
|
||||
|
||||
+26
-38
@@ -51,12 +51,6 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
@NotNull
|
||||
protected abstract String getLibraryName();
|
||||
|
||||
@NotNull
|
||||
public abstract String getJarName();
|
||||
|
||||
@NotNull
|
||||
public abstract String getSourcesJarName();
|
||||
|
||||
@NotNull
|
||||
protected abstract String getMessageForOverrideDialog();
|
||||
|
||||
@@ -67,10 +61,7 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
protected abstract String getLibraryCaption();
|
||||
|
||||
@NotNull
|
||||
public abstract File getExistingJarFile();
|
||||
|
||||
@NotNull
|
||||
public abstract File getExistingSourcesJarFile();
|
||||
public abstract RuntimeLibraryFiles getExistingJarFiles();
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull Module module) {
|
||||
@@ -119,16 +110,18 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
) {
|
||||
Project project = module.getProject();
|
||||
|
||||
RuntimeLibraryFiles files = getExistingJarFiles();
|
||||
LibraryState libraryState = getLibraryState(project);
|
||||
String dirToCopyJar = getPathToCopyFileTo(project, OrderRootType.CLASSES, defaultPath, pathFromDialog);
|
||||
FileState runtimeState = getJarState(project, getJarName(), OrderRootType.CLASSES, dirToCopyJar, pathFromDialog == null);
|
||||
FileState runtimeState =
|
||||
getJarState(project, files.getRuntimeDestination(dirToCopyJar), OrderRootType.CLASSES, pathFromDialog == null);
|
||||
|
||||
configureModuleWithLibraryClasses(module, libraryState, runtimeState, dirToCopyJar);
|
||||
|
||||
Library library = getKotlinLibrary(project);
|
||||
assert library != null : "Kotlin library should exists when adding sources root";
|
||||
String dirToCopySourcesJar = getPathToCopyFileTo(project, OrderRootType.SOURCES, defaultPath, pathFromDialog);
|
||||
FileState sourcesState = getJarState(project, getSourcesJarName(), OrderRootType.SOURCES, dirToCopySourcesJar,
|
||||
FileState sourcesState = getJarState(project, files.getRuntimeSourcesDestination(dirToCopySourcesJar), OrderRootType.SOURCES,
|
||||
pathFromDialog == null);
|
||||
|
||||
configureModuleWithLibrarySources(library, sourcesState, dirToCopySourcesJar);
|
||||
@@ -141,6 +134,8 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
@NotNull String dirToCopyJarTo
|
||||
) {
|
||||
Project project = module.getProject();
|
||||
RuntimeLibraryFiles files = getExistingJarFiles();
|
||||
File runtimeJar = files.getRuntimeJar();
|
||||
|
||||
switch (libraryState) {
|
||||
case LIBRARY:
|
||||
@@ -149,7 +144,7 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
break;
|
||||
}
|
||||
case COPY: {
|
||||
copyJarToDir(dirToCopyJarTo);
|
||||
copyFileToDir(runtimeJar, dirToCopyJarTo);
|
||||
break;
|
||||
}
|
||||
case DO_NOT_COPY: {
|
||||
@@ -161,16 +156,16 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
case NON_CONFIGURED_LIBRARY:
|
||||
switch (jarState) {
|
||||
case EXISTS: {
|
||||
addJarToExistingLibrary(project, getFileInDir(getJarName(), dirToCopyJarTo));
|
||||
addJarToExistingLibrary(project, files.getRuntimeDestination(dirToCopyJarTo));
|
||||
break;
|
||||
}
|
||||
case COPY: {
|
||||
File file = copyJarToDir(dirToCopyJarTo);
|
||||
File file = copyFileToDir(runtimeJar, dirToCopyJarTo);
|
||||
addJarToExistingLibrary(project, file);
|
||||
break;
|
||||
}
|
||||
case DO_NOT_COPY: {
|
||||
addJarToExistingLibrary(project, getExistingJarFile());
|
||||
addJarToExistingLibrary(project, runtimeJar);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -178,16 +173,16 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
case NEW_LIBRARY:
|
||||
switch (jarState) {
|
||||
case EXISTS: {
|
||||
addJarToNewLibrary(project, getFileInDir(getJarName(), dirToCopyJarTo));
|
||||
addJarToNewLibrary(project, files.getRuntimeDestination(dirToCopyJarTo));
|
||||
break;
|
||||
}
|
||||
case COPY: {
|
||||
File file = copyJarToDir(dirToCopyJarTo);
|
||||
File file = copyFileToDir(runtimeJar, dirToCopyJarTo);
|
||||
addJarToNewLibrary(project, file);
|
||||
break;
|
||||
}
|
||||
case DO_NOT_COPY: {
|
||||
addJarToNewLibrary(project, getExistingJarFile());
|
||||
addJarToNewLibrary(project, runtimeJar);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -201,31 +196,28 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
@NotNull FileState jarState,
|
||||
@Nullable String dirToCopyJarTo
|
||||
) {
|
||||
RuntimeLibraryFiles files = getExistingJarFiles();
|
||||
File runtimeSourcesJar = files.getRuntimeSourcesJar();
|
||||
switch (jarState) {
|
||||
case EXISTS: {
|
||||
if (dirToCopyJarTo != null) {
|
||||
addSourcesToLibraryIfNeeded(library, getFileInDir(getSourcesJarName(), dirToCopyJarTo));
|
||||
addSourcesToLibraryIfNeeded(library, files.getRuntimeSourcesDestination(dirToCopyJarTo));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case COPY: {
|
||||
assert dirToCopyJarTo != null : "Path to copy should be non-null";
|
||||
File file = copyFileToDir(getExistingSourcesJarFile(), dirToCopyJarTo);
|
||||
File file = copyFileToDir(runtimeSourcesJar, dirToCopyJarTo);
|
||||
addSourcesToLibraryIfNeeded(library, file);
|
||||
break;
|
||||
}
|
||||
case DO_NOT_COPY: {
|
||||
addSourcesToLibraryIfNeeded(library, getExistingSourcesJarFile());
|
||||
addSourcesToLibraryIfNeeded(library, runtimeSourcesJar);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static File getFileInDir(@NotNull String fileName, @NotNull String dirToJar) {
|
||||
return new File(dirToJar + "/" + fileName);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Library getKotlinLibrary(@NotNull Project project) {
|
||||
LibrariesContainer librariesContainer = LibrariesContainerFactory.createContainer(project);
|
||||
@@ -404,8 +396,10 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
return true;
|
||||
}
|
||||
|
||||
String fileName = getExistingJarFiles().getRuntimeJar().getName();
|
||||
|
||||
for (VirtualFile root : library.getFiles(OrderRootType.CLASSES)) {
|
||||
if (root.getName().equals(getJarName())) {
|
||||
if (root.getName().equals(fileName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -413,13 +407,9 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
return false;
|
||||
}
|
||||
|
||||
private File copyJarToDir(@NotNull String toDir) {
|
||||
return copyFileToDir(getExistingJarFile(), toDir);
|
||||
}
|
||||
|
||||
protected boolean needToChooseJarPath(@NotNull Project project) {
|
||||
String defaultPath = getDefaultPathToJarFile(project);
|
||||
return !isProjectLibraryPresent(project) && !getFileInDir(getJarName(), defaultPath).exists();
|
||||
return !isProjectLibraryPresent(project) && !getExistingJarFiles().getRuntimeDestination(defaultPath).exists();
|
||||
}
|
||||
|
||||
protected String getDefaultPathToJarFile(@NotNull Project project) {
|
||||
@@ -456,16 +446,14 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
|
||||
@NotNull
|
||||
protected FileState getJarState(
|
||||
@NotNull Project project,
|
||||
@NotNull String jarName,
|
||||
@NotNull File targetFile,
|
||||
@NotNull OrderRootType jarType,
|
||||
@NotNull String copyPath,
|
||||
boolean useBundled
|
||||
) {
|
||||
String pathFromLibrary = getPathFromLibrary(project, jarType);
|
||||
if (getFileInDir(jarName, copyPath).exists()) {
|
||||
if (targetFile.exists()) {
|
||||
return FileState.EXISTS;
|
||||
}
|
||||
else if (pathFromLibrary != null) {
|
||||
else if (getPathFromLibrary(project, jarType) != null) {
|
||||
return FileState.COPY;
|
||||
}
|
||||
else if (useBundled) {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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.configuration
|
||||
|
||||
import java.io.File
|
||||
|
||||
class RuntimeLibraryFiles(
|
||||
val runtimeJar: File,
|
||||
val runtimeSourcesJar: File
|
||||
) {
|
||||
fun getAllJars(): List<File> = listOf(runtimeJar, runtimeSourcesJar)
|
||||
|
||||
fun getRuntimeDestination(dirToCopyJar: String): File =
|
||||
File(dirToCopyJar + "/" + runtimeJar.name)
|
||||
|
||||
fun getRuntimeSourcesDestination(dirToCopyJar: String): File =
|
||||
File(dirToCopyJar + "/" + runtimeSourcesJar.name)
|
||||
}
|
||||
+13
-13
@@ -36,6 +36,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.idea.configuration.ConfigureKotlinInProjectUtils;
|
||||
import org.jetbrains.kotlin.idea.configuration.KotlinWithLibraryConfigurator;
|
||||
import org.jetbrains.kotlin.idea.configuration.RuntimeLibraryFiles;
|
||||
import org.jetbrains.kotlin.idea.framework.ui.CreateLibraryDialog;
|
||||
import org.jetbrains.kotlin.idea.framework.ui.FileUIUtils;
|
||||
import org.jetbrains.kotlin.idea.util.projectStructure.ProjectStructurePackage;
|
||||
@@ -46,8 +47,6 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.jetbrains.kotlin.idea.configuration.KotlinWithLibraryConfigurator.getFileInDir;
|
||||
|
||||
public abstract class CustomLibraryDescriptorWithDeferredConfig extends CustomLibraryDescription {
|
||||
|
||||
private static final String DEFAULT_LIB_DIR_NAME = "lib";
|
||||
@@ -172,44 +171,45 @@ public abstract class CustomLibraryDescriptorWithDeferredConfig extends CustomLi
|
||||
String defaultPathToJarFile = useRelativePaths ? DEFAULT_LIB_DIR_NAME
|
||||
: FileUIUtils.createRelativePath(null, contextDirectory, DEFAULT_LIB_DIR_NAME);
|
||||
|
||||
File bundledLibJarFile = configurator.getExistingJarFile();
|
||||
File bundledLibSourcesJarFile = configurator.getExistingSourcesJarFile();
|
||||
RuntimeLibraryFiles files = configurator.getExistingJarFiles();
|
||||
|
||||
File libraryFile;
|
||||
File librarySrcFile;
|
||||
|
||||
File stdJarInDefaultPath = getFileInDir(configurator.getJarName(), defaultPathToJarFile);
|
||||
File stdJarInDefaultPath = files.getRuntimeDestination(defaultPathToJarFile);
|
||||
if (!useRelativePaths && stdJarInDefaultPath.exists()) {
|
||||
libraryFile = stdJarInDefaultPath;
|
||||
|
||||
File sourcesJar = getFileInDir(configurator.getSourcesJarName(), defaultPathToJarFile);
|
||||
File sourcesJar = files.getRuntimeSourcesDestination(defaultPathToJarFile);
|
||||
if (sourcesJar.exists()) {
|
||||
librarySrcFile = sourcesJar;
|
||||
}
|
||||
else {
|
||||
deferredCopyFileRequests.addCopyWithReplaceRequest(bundledLibSourcesJarFile, libraryFile.getParent());
|
||||
librarySrcFile = bundledLibSourcesJarFile;
|
||||
librarySrcFile = files.getRuntimeSourcesJar();
|
||||
deferredCopyFileRequests.addCopyWithReplaceRequest(librarySrcFile, libraryFile.getParent());
|
||||
}
|
||||
}
|
||||
else {
|
||||
CreateLibraryDialog dialog =new CreateLibraryDialog(defaultPathToJarFile, dialogTitle, modulesSeparatorCaption);
|
||||
CreateLibraryDialog dialog = new CreateLibraryDialog(defaultPathToJarFile, dialogTitle, modulesSeparatorCaption);
|
||||
dialog.show();
|
||||
|
||||
if (!dialog.isOK()) return null;
|
||||
|
||||
String copyIntoPath = dialog.getCopyIntoPath();
|
||||
if (copyIntoPath != null) {
|
||||
deferredCopyFileRequests.addCopyWithReplaceRequest(bundledLibJarFile, copyIntoPath);
|
||||
deferredCopyFileRequests.addCopyWithReplaceRequest(bundledLibSourcesJarFile, copyIntoPath);
|
||||
for (File file : files.getAllJars()) {
|
||||
deferredCopyFileRequests.addCopyWithReplaceRequest(file, copyIntoPath);
|
||||
}
|
||||
}
|
||||
|
||||
libraryFile = bundledLibJarFile;
|
||||
librarySrcFile = bundledLibSourcesJarFile;
|
||||
libraryFile = files.getRuntimeJar();
|
||||
librarySrcFile = files.getRuntimeSourcesJar();
|
||||
}
|
||||
|
||||
return createConfiguration(libraryFile, librarySrcFile);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected NewLibraryConfiguration createConfiguration(@NotNull File libraryFile, @NotNull File librarySrcFile) {
|
||||
final String libraryFileUrl = VfsUtil.getUrlForLibraryRoot(libraryFile);
|
||||
final String libraryFileSrcUrl = VfsUtil.getUrlForLibraryRoot(librarySrcFile);
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.intellij.openapi.roots.libraries.NewLibraryConfiguration;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
import org.jetbrains.kotlin.idea.configuration.KotlinJsModuleConfigurator;
|
||||
import org.jetbrains.kotlin.idea.configuration.RuntimeLibraryFiles;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@@ -50,6 +51,7 @@ public class JSLibraryStdDescription extends CustomLibraryDescriptorWithDeferred
|
||||
KotlinJsModuleConfigurator configurator = (KotlinJsModuleConfigurator) getConfiguratorByName(NAME);
|
||||
assert configurator != null : "Cannot find configurator with name " + NAME;
|
||||
|
||||
return createConfiguration(configurator.getExistingJarFile(), configurator.getExistingSourcesJarFile());
|
||||
RuntimeLibraryFiles files = configurator.getExistingJarFiles();
|
||||
return createConfiguration(files.getRuntimeJar(), files.getRuntimeSourcesJar());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user