Drop search file with pattern
This commit is contained in:
@@ -24,9 +24,6 @@ public interface KotlinPaths {
|
||||
@NotNull
|
||||
File getHomePath();
|
||||
|
||||
@NotNull
|
||||
File getBuildVersionFile();
|
||||
|
||||
@NotNull
|
||||
File getLibPath();
|
||||
|
||||
|
||||
@@ -34,12 +34,6 @@ 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,23 +21,17 @@ 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();
|
||||
@@ -61,16 +55,5 @@ 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,12 +21,9 @@ 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 {
|
||||
|
||||
@@ -34,52 +31,10 @@ 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_FINDER = 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
|
||||
@@ -114,16 +69,6 @@ public class PathUtil {
|
||||
return new KotlinPathsFromHomeDir(new File("dist", HOME_FOLDER_NAME));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static KotlinPaths getKotlinStandaloneCompilerPaths(@NotNull String path) {
|
||||
File homePath = KOTLIN_HOME_DIRECTORY_FINDER.fun(path);
|
||||
if (homePath == null) {
|
||||
throw new IllegalArgumentException(String.format("Can't get home path from '%s'", path));
|
||||
}
|
||||
|
||||
return new KotlinPathsFromHomeDir(homePath);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static File getCompilerPathForCompilerJar() {
|
||||
File jar = getPathUtilJar();
|
||||
|
||||
Reference in New Issue
Block a user