diff --git a/jps/model-serialization/src/org/jetbrains/jps/model/serialization/JpsModelSerializationDataService.java b/jps/model-serialization/src/org/jetbrains/jps/model/serialization/JpsModelSerializationDataService.java index 546143ba35d9..a50af9c13602 100644 --- a/jps/model-serialization/src/org/jetbrains/jps/model/serialization/JpsModelSerializationDataService.java +++ b/jps/model-serialization/src/org/jetbrains/jps/model/serialization/JpsModelSerializationDataService.java @@ -42,7 +42,7 @@ public class JpsModelSerializationDataService { } public static Map computeAllPathVariables(JpsGlobal global) { - Map pathVariables = new HashMap<>(PathMacroUtil.getGlobalSystemMacros()); + Map pathVariables = new HashMap<>(PathMacroUtil.getGlobalSystemMacros(false)); JpsPathVariablesConfiguration configuration = getPathVariablesConfiguration(global); if (configuration != null) { pathVariables.putAll(configuration.getAllUserVariables()); @@ -89,7 +89,7 @@ public class JpsModelSerializationDataService { @Nullable public static String getPathVariableValue(@NotNull JpsGlobal global, @NotNull String name) { - String value = PathMacroUtil.getGlobalSystemMacroValue(name); + String value = PathMacroUtil.getGlobalSystemMacroValue(name, false); if (value != null) { return value; } diff --git a/jps/model-serialization/src/org/jetbrains/jps/model/serialization/PathMacroUtil.java b/jps/model-serialization/src/org/jetbrains/jps/model/serialization/PathMacroUtil.java index da60bda7c3ad..3d94b4c9838f 100644 --- a/jps/model-serialization/src/org/jetbrains/jps/model/serialization/PathMacroUtil.java +++ b/jps/model-serialization/src/org/jetbrains/jps/model/serialization/PathMacroUtil.java @@ -32,11 +32,8 @@ public class PathMacroUtil { @NonNls public static final String APPLICATION_PLUGINS_DIR = "APPLICATION_PLUGINS_DIR"; @NonNls public static final String USER_HOME_NAME = "USER_HOME"; - private static final Map ourGlobalMacros = ContainerUtil.immutableMapBuilder() - .put(APPLICATION_HOME_DIR, toSystemIndependentName(PathManager.getHomePath())) - .put(APPLICATION_CONFIG_DIR, toSystemIndependentName(PathManager.getConfigPath())) - .put(APPLICATION_PLUGINS_DIR, toSystemIndependentName(PathManager.getPluginsPath())) - .put(USER_HOME_NAME, StringUtil.trimEnd(toSystemIndependentName(SystemProperties.getUserHome()), "/")).build(); + private static volatile Map ourGlobalMacrosForIde; + private static volatile Map ourGlobalMacrosForStandalone; @Nullable public static String getModuleDir(@NotNull String moduleFilePath) { @@ -66,11 +63,57 @@ public class PathMacroUtil { @NotNull public static Map getGlobalSystemMacros() { - return ourGlobalMacros; + return getGlobalSystemMacros(true); + } + + @NotNull + public static Map getGlobalSystemMacros(boolean insideIde) { + if (insideIde) { + if (ourGlobalMacrosForIde == null) { + ourGlobalMacrosForIde = computeGlobalPathMacrosInsideIde(); + } + return ourGlobalMacrosForIde; + } + else { + if (ourGlobalMacrosForStandalone == null) { + ourGlobalMacrosForStandalone = computeGlobalPathMacrosForStandaloneCode(); + } + return ourGlobalMacrosForStandalone; + } + } + + private static Map computeGlobalPathMacrosForStandaloneCode() { + ContainerUtil.ImmutableMapBuilder builder = ContainerUtil.immutableMapBuilder(); + String homePath = PathManager.getHomePath(false); + if (homePath != null) { + builder.put(APPLICATION_HOME_DIR, toSystemIndependentName(homePath)) + .put(APPLICATION_CONFIG_DIR, toSystemIndependentName(PathManager.getConfigPath())) + .put(APPLICATION_PLUGINS_DIR, toSystemIndependentName(PathManager.getPluginsPath())); + } + builder.put(USER_HOME_NAME, computeUserHomePath()); + return builder.build(); + } + + private static Map computeGlobalPathMacrosInsideIde() { + return ContainerUtil.immutableMapBuilder() + .put(APPLICATION_HOME_DIR, toSystemIndependentName(PathManager.getHomePath())) + .put(APPLICATION_CONFIG_DIR, toSystemIndependentName(PathManager.getConfigPath())) + .put(APPLICATION_PLUGINS_DIR, toSystemIndependentName(PathManager.getPluginsPath())) + .put(USER_HOME_NAME, computeUserHomePath()).build(); + } + + @NotNull + private static String computeUserHomePath() { + return StringUtil.trimEnd(toSystemIndependentName(SystemProperties.getUserHome()), "/"); } @Nullable public static String getGlobalSystemMacroValue(String name) { - return ourGlobalMacros.get(name); + return getGlobalSystemMacroValue(name, true); + } + + @Nullable + public static String getGlobalSystemMacroValue(String name, boolean insideIde) { + return getGlobalSystemMacros(insideIde).get(name); } } diff --git a/platform/util/src/com/intellij/openapi/application/PathManager.java b/platform/util/src/com/intellij/openapi/application/PathManager.java index fe01e25d623c..fa38c5261087 100644 --- a/platform/util/src/com/intellij/openapi/application/PathManager.java +++ b/platform/util/src/com/intellij/openapi/application/PathManager.java @@ -65,6 +65,15 @@ public class PathManager { @NotNull public static String getHomePath() { + return getHomePath(true); + } + + /** + * @param insideIde {@code true} if the calling code is working inside IDE and {@code false} if it isn't (e.g. if it's running in a build + * process or a script) + */ + @Contract("true -> !null") + public static String getHomePath(boolean insideIde) { if (ourHomePath != null) return ourHomePath; String fromProperty = System.getProperty(PROPERTY_HOME_PATH, System.getProperty(PROPERTY_HOME)); @@ -74,7 +83,7 @@ public class PathManager { throw new RuntimeException("Invalid home path '" + ourHomePath + "'"); } } - else { + else if (insideIde) { ourHomePath = getHomePathFor(PathManager.class); if (ourHomePath == null) { String advice = SystemInfo.isMac ? "reinstall the software." : "make sure bin/idea.properties is present in the installation directory."; @@ -82,11 +91,11 @@ public class PathManager { } } - if (SystemInfo.isWindows) { + if (ourHomePath != null && SystemInfo.isWindows) { ourHomePath = canonicalPath(ourHomePath); } - ourBinDirectories = getBinDirectories(new File(ourHomePath)); + ourBinDirectories = ourHomePath != null ? getBinDirectories(new File(ourHomePath)) : ArrayUtil.EMPTY_STRING_ARRAY; return ourHomePath; }