Add an ability to create libraries from standalone compiler
This commit is contained in:
committed by
Nikolay Krasko
parent
9b9f9a50d3
commit
eed215893f
@@ -24,6 +24,9 @@ public interface KotlinPaths {
|
||||
@NotNull
|
||||
File getHomePath();
|
||||
|
||||
@NotNull
|
||||
File getBuildVersionFile();
|
||||
|
||||
@NotNull
|
||||
File getLibPath();
|
||||
|
||||
|
||||
@@ -34,6 +34,12 @@ public class KotlinPathsFromHomeDir implements KotlinPaths {
|
||||
return homePath;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public File getBuildVersionFile() {
|
||||
return new File(homePath, PathUtil.BUILD_VERSION_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public File getLibPath() {
|
||||
|
||||
@@ -21,17 +21,23 @@ import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.util.io.URLUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/*
|
||||
* This code is essentially copied from IntelliJ IDEA's VfsUtil, and slightly restructured
|
||||
*/
|
||||
public class KotlinVfsUtil {
|
||||
private static final String FILE = "file";
|
||||
private static final String JAR = "jar";
|
||||
private static final String PROTOCOL_DELIMITER = ":";
|
||||
|
||||
/*
|
||||
* This code is essentially copied from IntelliJ IDEA's VfsUtil, and slightly restructured
|
||||
*/
|
||||
@NotNull
|
||||
public static String convertFromUrl(@NotNull URL url) throws MalformedURLException {
|
||||
String protocol = url.getProtocol();
|
||||
@@ -55,5 +61,16 @@ public class KotlinVfsUtil {
|
||||
return protocol + "://" + path;
|
||||
}
|
||||
|
||||
public static List<File> getFilesInDirectoryByPattern(@NotNull File dir, @NotNull final Pattern pattern) {
|
||||
File[] files = dir.listFiles(new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(@NotNull File dir, @NotNull String name) {
|
||||
return pattern.matcher(name).matches();
|
||||
}
|
||||
});
|
||||
return files != null ? Arrays.asList(files) : Collections.<File>emptyList();
|
||||
}
|
||||
|
||||
|
||||
private KotlinVfsUtil() {}
|
||||
}
|
||||
|
||||
@@ -21,9 +21,12 @@ import com.intellij.openapi.util.SystemInfo;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
import com.intellij.util.Function;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PathUtil {
|
||||
|
||||
@@ -31,9 +34,52 @@ public class PathUtil {
|
||||
public static final String JS_LIB_JS_NAME = "kotlinEcma3.js";
|
||||
public static final String JDK_ANNOTATIONS_JAR = "kotlin-jdk-annotations.jar";
|
||||
public static final String KOTLIN_JAVA_RUNTIME_JAR = "kotlin-runtime.jar";
|
||||
public static final String BUILD_VERSION_NAME = "build.txt";
|
||||
public static final String HOME_FOLDER_NAME = "kotlinc";
|
||||
|
||||
private static final File NO_PATH = new File("<no_path>");
|
||||
|
||||
public static final Function<String, File> KOTLIN_HOME_DIRECTORY_ADAPTER = new Function<String, File>() {
|
||||
private final Pattern homeDirPattern = Pattern.compile(HOME_FOLDER_NAME);
|
||||
private final Pattern buildFilePattern = Pattern.compile(BUILD_VERSION_NAME);
|
||||
|
||||
@Override
|
||||
public File fun(String path) {
|
||||
if (path == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
File directory = new File(path);
|
||||
if (!(directory.exists() || directory.isDirectory())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (checkIsHomeDirectory(directory)) {
|
||||
return directory;
|
||||
}
|
||||
|
||||
List<File> homeSubfolders = KotlinVfsUtil.getFilesInDirectoryByPattern(directory, homeDirPattern);
|
||||
if (!homeSubfolders.isEmpty()) {
|
||||
assert homeSubfolders.size() == 1;
|
||||
File homeNamedDir = homeSubfolders.get(0);
|
||||
if (checkIsHomeDirectory(homeNamedDir)) {
|
||||
return homeNamedDir;
|
||||
}
|
||||
}
|
||||
|
||||
File parentDirectory = directory.getParentFile();
|
||||
if (parentDirectory != null && checkIsHomeDirectory(parentDirectory)) {
|
||||
return parentDirectory;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean checkIsHomeDirectory(File dir) {
|
||||
return dir.getName().equals(HOME_FOLDER_NAME) && !KotlinVfsUtil.getFilesInDirectoryByPattern(dir, buildFilePattern).isEmpty();
|
||||
}
|
||||
};
|
||||
|
||||
private PathUtil() {}
|
||||
|
||||
@NotNull
|
||||
@@ -65,7 +111,17 @@ public class PathUtil {
|
||||
|
||||
@NotNull
|
||||
public static KotlinPaths getKotlinPathsForDistDirectory() {
|
||||
return new KotlinPathsFromHomeDir(new File("dist/kotlinc"));
|
||||
return new KotlinPathsFromHomeDir(new File("dist", HOME_FOLDER_NAME));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static KotlinPaths getKotlinStandaloneCompilerPaths(@NotNull String path) {
|
||||
File homePath = KOTLIN_HOME_DIRECTORY_ADAPTER.fun(path);
|
||||
if (homePath == null) {
|
||||
throw new IllegalArgumentException(String.format("Can't get home path from '%s'", path));
|
||||
}
|
||||
|
||||
return new KotlinPathsFromHomeDir(homePath);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -90,7 +146,7 @@ public class PathUtil {
|
||||
|
||||
if (jar.getName().equals("kotlin-jps-plugin.jar")) {
|
||||
File pluginHome = jar.getParentFile().getParentFile().getParentFile();
|
||||
return new File(pluginHome, "kotlinc");
|
||||
return new File(pluginHome, HOME_FOLDER_NAME);
|
||||
}
|
||||
|
||||
return NO_PATH;
|
||||
@@ -106,7 +162,7 @@ public class PathUtil {
|
||||
File lib = jar.getParentFile();
|
||||
File pluginHome = lib.getParentFile();
|
||||
|
||||
return new File(pluginHome, "kotlinc");
|
||||
return new File(pluginHome, HOME_FOLDER_NAME);
|
||||
}
|
||||
|
||||
return NO_PATH;
|
||||
|
||||
Reference in New Issue
Block a user