package jetbrains.mps.core.tool.environment.util;
/*Generated by MPS */
import org.jetbrains.annotations.NonNls;
import jetbrains.mps.util.PathUtil;
import java.io.File;
import jetbrains.mps.core.tool.environment.common.SystemInfo;
import java.io.IOException;
import jetbrains.mps.core.tool.environment.common.FileUtil;
import org.jetbrains.annotations.Nullable;
import java.net.URL;
import jetbrains.mps.util.StringUtil;
import jetbrains.mps.util.URLUtil;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.PropertyResourceBundle;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
public class PathManager {
/**
* copied from idea PathManager since we do not want to depend from here (it is core) on idea
*/
@NonNls
private static final String PROPERTIES_FILE = "idea.properties.file";
@NonNls
private static final String PROPERTIES_FILE_NAME = "idea.properties";
@NonNls
private static final String PROPERTY_SYSTEM_PATH = "idea.system.path";
@NonNls
private static final String PROPERTY_CONFIG_PATH = "idea.config.path";
@NonNls
private static final String PROPERTY_PLUGINS_PATH = "idea.plugins.path";
@NonNls
private static final String PROPERTY_HOME_PATH = "idea.home.path";
@NonNls
private static final String PROPERTY_LOG_PATH = "idea.log.path";
@NonNls
public static final String PROPERTY_PATHS_SELECTOR = "idea.paths.selector";
@NonNls
private static String ourHomePath;
@NonNls
private static String ourSystemPath;
@NonNls
private static String ourConfigPath;
@NonNls
private static String ourPluginsPath;
@NonNls
private static String ourLogPath;
@NonNls
private static final String FILE = "file";
@NonNls
private static final String JAR = "jar";
@NonNls
private static final String JAR_DELIMITER = "!";
@NonNls
private static final String PROTOCOL_DELIMITER = ":";
@NonNls
public static final String DEFAULT_OPTIONS_FILE_NAME = "other";
@NonNls
private static final String LIB_FOLDER = "lib";
@NonNls
public static final String PLUGINS_FOLDER = "plugins";
@NonNls
private static final String BIN_FOLDER = "bin";
@NonNls
private static final String LOG_DIRECTORY = "log";
@NonNls
private static final String OPTIONS_FOLDER = "options";
private static final String PATHS_SELECTOR = System.getProperty(PROPERTY_PATHS_SELECTOR);
private PathManager() {
}
public static String getHomePath() {
if (PathManager.ourHomePath != null) {
return PathManager.ourHomePath;
}
if (System.getProperty(PathManager.PROPERTY_HOME_PATH) != null) {
PathManager.ourHomePath = PathUtil.getAbsolutePath(System.getProperty(PathManager.PROPERTY_HOME_PATH));
} else {
final Class aClass = PathManager.class;
String rootPath = PathManager.getResourceRoot(aClass, "/" + aClass.getName().replace('.', '/') + ".class");
if (rootPath != null) {
File root = new File(rootPath).getAbsoluteFile();
do {
final String parent = root.getParent();
if (parent == null) {
return null;
}
assert parent != null : "No parent found for " + root + "; " + PathManager.BIN_FOLDER + " folder with " + "idea.properties" + " file not found";
root = new File(parent).getAbsoluteFile();
// one step back to get folder
} while (root != null && !(PathManager.isMpsDir(root)));
PathManager.ourHomePath = (root != null ? root.getAbsolutePath() : null);
}
}
try {
if (!(SystemInfo.isFileSystemCaseSensitive)) {
PathManager.ourHomePath = (PathManager.ourHomePath == null ? null : new File(PathManager.ourHomePath).getCanonicalPath());
}
} catch (IOException e) {
// ignore
}
return PathManager.ourHomePath;
}
private static boolean isIdeaHome(final File root) {
return new File(root, FileUtil.toSystemDependentName("bin/idea.properties")).exists() || new File(root, FileUtil.toSystemDependentName("community/bin/idea.properties")).exists();
}
private static boolean isMpsDir(File file) {
return new File(file, FileUtil.toSystemDependentName("bin/" + PROPERTIES_FILE_NAME)).exists();
}
public static String getLibPath() {
return PathManager.getHomePath() + File.separator + PathManager.LIB_FOLDER;
}
public static String getSystemPath() {
if (PathManager.ourSystemPath != null) {
return PathManager.ourSystemPath;
}
if (System.getProperty(PathManager.PROPERTY_SYSTEM_PATH) != null) {
PathManager.ourSystemPath = PathUtil.getAbsolutePath(PathUtil.trimPathQuotes(System.getProperty(PathManager.PROPERTY_SYSTEM_PATH)));
} else {
PathManager.ourSystemPath = PathManager.getHomePath() + File.separator + "system";
}
try {
File file = new File(PathManager.ourSystemPath);
file.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
return PathManager.ourSystemPath;
}
public static boolean ensureConfigFolderExists(final boolean createIfNotExists) {
PathManager.getConfigPathWithoutDialog();
File file = new File(PathManager.ourConfigPath);
if (createIfNotExists && !(file.exists())) {
file.mkdirs();
return true;
}
return false;
}
public static String getConfigPath(boolean createIfNotExists) {
PathManager.ensureConfigFolderExists(createIfNotExists);
return PathManager.ourConfigPath;
}
public static String getConfigPath() {
return PathManager.getConfigPath(true);
}
private static String getConfigPathWithoutDialog() {
if (PathManager.ourConfigPath != null) {
return PathManager.ourConfigPath;
}
if (System.getProperty(PathManager.PROPERTY_CONFIG_PATH) != null) {
PathManager.ourConfigPath = PathUtil.getAbsolutePath(PathUtil.trimPathQuotes(System.getProperty(PathManager.PROPERTY_CONFIG_PATH)));
} else {
PathManager.ourConfigPath = PathManager.getHomePath() + File.separator + "config";
}
return PathManager.ourConfigPath;
}
public static String getBinPath() {
return PathManager.getHomePath() + File.separator + PathManager.BIN_FOLDER;
}
public static String getOptionsPath() {
return PathManager.getConfigPath() + File.separator + PathManager.OPTIONS_FOLDER;
}
public static String getOptionsPathWithoutDialog() {
return PathManager.getConfigPathWithoutDialog() + File.separator + PathManager.OPTIONS_FOLDER;
}
public static File getIndexRoot() {
File file = new File(PathManager.getSystemPath(), "index");
try {
file = file.getCanonicalFile();
} catch (IOException ignored) {
}
file.mkdirs();
return file;
}
public static String getPluginsPath() {
if (ourPluginsPath != null) {
return ourPluginsPath;
}
if (System.getProperty(PROPERTY_PLUGINS_PATH) != null) {
ourPluginsPath = System.getProperty(PROPERTY_PLUGINS_PATH);
} else
if (SystemInfo.isMac && PATHS_SELECTOR != null) {
ourPluginsPath = System.getProperty("user.home") + File.separator + "Library/Application Support" + File.separator + PATHS_SELECTOR;
} else {
ourPluginsPath = getConfigPath() + File.separatorChar + PLUGINS_FOLDER;
}
return ourPluginsPath;
}
public static String getPreInstalledPluginsPath() {
return jetbrains.mps.util.PathManager.getPreInstalledPluginsPath();
}
public static String getLogPath() {
if (PathManager.ourLogPath == null) {
if (System.getProperty(PathManager.PROPERTY_LOG_PATH) != null) {
PathManager.ourLogPath = PathUtil.getAbsolutePath(PathUtil.trimPathQuotes(System.getProperty(PathManager.PROPERTY_LOG_PATH)));
} else {
PathManager.ourLogPath = PathManager.getSystemPath() + File.separatorChar + PathManager.LOG_DIRECTORY;
}
}
return PathManager.ourLogPath;
}
@NonNls
public static File getOptionsFile(@NonNls String fileName) {
return new File(PathManager.getOptionsPath() + File.separatorChar + fileName + ".xml");
}
/**
* * Attempts to detect classpath entry which contains given resource
*/
@Nullable
public static String getResourceRoot(Class context, @NonNls String path) {
URL url = context.getResource(path);
if (url == null) {
url = ClassLoader.getSystemResource(path.substring(1));
}
if (url == null) {
return null;
}
return PathManager.extractRoot(url, path);
}
/**
* * Attempts to extract classpath entry part from passed URL.
*/
@NonNls
private static String extractRoot(URL resourceURL, String resourcePath) {
if (!((StringUtil.startsWithChar(resourcePath, '/') || StringUtil.startsWithChar(resourcePath, '\\')))) {
// noinspection HardCodedStringLiteral
System.err.println("precondition failed: " + resourcePath);
return null;
}
String protocol = resourceURL.getProtocol();
String resultPath = null;
if (PathManager.FILE.equals(protocol)) {
String path = resourceURL.getFile();
final String testPath = path.replace('\\', '/');
final String testResourcePath = resourcePath.replace('\\', '/');
if (jetbrains.mps.core.tool.environment.common.StringUtil.endsWithIgnoreCase(testPath, testResourcePath)) {
resultPath = path.substring(0, path.length() - resourcePath.length());
}
} else
if (PathManager.JAR.equals(protocol)) {
String fullPath = resourceURL.getFile();
int delimiter = fullPath.indexOf(PathManager.JAR_DELIMITER);
if (delimiter >= 0) {
String archivePath = fullPath.substring(0, delimiter);
if (jetbrains.mps.core.tool.environment.common.StringUtil.startsWithConcatenationOf(archivePath, PathManager.FILE, PathManager.PROTOCOL_DELIMITER)) {
resultPath = archivePath.substring(PathManager.FILE.length() + PathManager.PROTOCOL_DELIMITER.length());
}
}
}
if (resultPath == null) {
// noinspection HardCodedStringLiteral
System.err.println("cannot extract: " + resultPath + " from " + resourceURL);
return null;
}
resultPath = jetbrains.mps.core.tool.environment.common.StringUtil.trimEnd(resultPath, File.separator);
resultPath = URLUtil.unescapePercentSequences(resultPath);
return resultPath;
}
@NonNls
public static File getDefaultOptionsFile() {
return new File(PathManager.getOptionsPath(), PathManager.DEFAULT_OPTIONS_FILE_NAME + ".xml");
}
public static void loadProperties() {
File propFile = FileUtil.findFirstThatExist(System.getProperty(PathManager.PROPERTIES_FILE), System.getProperty("user.home") + "/idea.properties", PathManager.getHomePath() + "/bin/idea.properties", PathManager.getHomePath() + "/community/bin/idea.properties");
if (propFile != null) {
InputStream fis = null;
try {
fis = new BufferedInputStream(new FileInputStream(propFile));
final PropertyResourceBundle bundle = new PropertyResourceBundle(fis);
final Enumeration keys = bundle.getKeys();
String home = (String) bundle.handleGetObject("idea.home");
if (home != null && PathManager.ourHomePath == null) {
PathManager.ourHomePath = PathUtil.getAbsolutePath(PathManager.substituteVars(home));
}
final Properties sysProperties = System.getProperties();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
if (sysProperties.getProperty(key, null) == null) {
// load the property from the property file only if it is not defined yet
final String value = PathManager.substituteVars(bundle.getString(key));
sysProperties.setProperty(key, value);
}
}
} catch (IOException e) {
// noinspection HardCodedStringLiteral
System.err.println("Problem reading from property file: " + propFile.getPath());
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
}
}
}
}
public static String substituteVars(String s) {
final String ideaHomePath = PathManager.getHomePath();
return PathManager.substituteVars(s, ideaHomePath);
}
public static String substituteVars(String s, final String ideaHomePath) {
if (s == null) {
return null;
}
if (s.startsWith("..")) {
s = ideaHomePath + File.separatorChar + PathManager.BIN_FOLDER + File.separatorChar + s;
}
s = StringUtil.replace(s, "${idea.home}", ideaHomePath);
final Properties props = System.getProperties();
final Set keys = props.keySet();
for (final Object key1 : keys) {
String key = (String) key1;
String value = props.getProperty(key);
s = StringUtil.replace(s, "${" + key + "}", value);
}
return s;
}
public static String getPluginTempPath() {
String systemPath = PathManager.getSystemPath();
return systemPath + File.separator + PathManager.PLUGINS_FOLDER;
}
public static File findFileInLibDirectory(@NotNull String relativePath) {
File file = new File(PathManager.getLibPath() + File.separator + relativePath);
if (file.exists()) {
return file;
}
return new File(PathManager.getHomePath() + File.separator + "community" + File.separator + "lib" + File.separator + relativePath);
}
public static Collection<String> getBootstrapPaths() {
return jetbrains.mps.util.PathManager.getBootstrapPaths();
}
public static String getLanguagesPath() {
return jetbrains.mps.util.PathManager.getLanguagesPath();
}
}