simplify - CommonPathRelativizer.basePath is not-null

GitOrigin-RevId: 3af6fa8ecbec64408bfa6ea5508d1dcd11b4e58a
This commit is contained in:
Vladimir Krivosheev
2024-09-12 13:24:54 +02:00
committed by intellij-monorepo-bot
parent c6d0c5344a
commit e3ec1aab09
5 changed files with 65 additions and 80 deletions

View File

@@ -5,30 +5,15 @@ import com.intellij.openapi.util.SystemInfoRt
import com.intellij.openapi.util.io.FileUtil
internal open class CommonPathRelativizer @JvmOverloads constructor(
private val basePath: String?, private val identifier: String,
/**
* can be null when basePath is null, or it is impossible to detect the sensitivity of the file system
*/
private val isCaseSensitive: Boolean? = null
private val basePath: String,
private val identifier: String,
private val isCaseSensitive: Boolean = SystemInfoRt.isFileSystemCaseSensitive,
) : PathRelativizer {
override fun toRelativePath(path: String): String? {
if (basePath == null ||
!(if (isCaseSensitive == null)
FileUtil.startsWith(path, basePath, SystemInfoRt.isFileSystemCaseSensitive)
else
FileUtil.startsWith(path, basePath, isCaseSensitive))
) {
return null
}
return identifier + path.substring(basePath.length)
return if (FileUtil.startsWith(path, basePath, isCaseSensitive)) identifier + path.substring(basePath.length) else null
}
override fun toAbsolutePath(path: String): String? {
if (basePath == null || !path.startsWith(identifier)) {
return null
}
else {
return basePath + path.substring(identifier.length)
}
return if (path.startsWith(identifier)) basePath + path.substring(identifier.length) else null
}
}

View File

@@ -1,27 +1,25 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.jps.incremental.relativizer;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.SystemProperties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
final class GradlePathRelativizer extends CommonPathRelativizer {
private static final String IDENTIFIER = "$GRADLE_REPOSITORY$";
private static final String GRADLE_USER_HOME = "GRADLE_USER_HOME";
GradlePathRelativizer() {
super(initializeGradleRepositoryPath(), IDENTIFIER);
GradlePathRelativizer(@NotNull String gradleRepositoryPath) {
super(gradleRepositoryPath, IDENTIFIER);
}
private static @Nullable String initializeGradleRepositoryPath() {
static @Nullable String initializeGradleRepositoryPath() {
String gradleUserHomePath = System.getenv(GRADLE_USER_HOME);
String gradleUserHome = gradleUserHomePath == null ? SystemProperties.getUserHome() + File.separator + ".gradle" : gradleUserHomePath;
if (FileUtil.exists(gradleUserHome)) {
return PathRelativizerService.normalizePath(gradleUserHome);
}
return null;
return Files.exists(Path.of(gradleUserHome)) ? PathRelativizerService.normalizePath(gradleUserHome) : null;
}
}

View File

@@ -18,24 +18,18 @@ import java.util.stream.Collectors;
* {@link PathRelativizerService#toRelative} or {@link PathRelativizerService#toFull} with any path.
*/
final class JavaSdkPathRelativizer implements PathRelativizer {
private @Nullable Map<String, String> javaSdkPathMap;
private final @NotNull Map<String, String> javaSdkPathMap;
JavaSdkPathRelativizer(@Nullable Set<? extends JpsSdk<?>> javaSdks) {
if (javaSdks != null) {
JavaSdkPathRelativizer(@NotNull Set<? extends JpsSdk<?>> javaSdks) {
javaSdkPathMap = javaSdks.stream()
.collect(Collectors.toMap(sdk -> {
JavaVersion version = JavaVersion.tryParse(sdk.getVersionString());
return "$JDK_" + (version != null ? version.toString() : "0") + "$";
return "$JDK_" + (version == null ? "0" : version.toString()) + "$";
}, sdk -> PathRelativizerService.normalizePath(sdk.getHomePath()), (sdk1, sdk2) -> sdk1));
}
}
@Override
public @Nullable String toRelativePath(@NotNull String path) {
if (javaSdkPathMap == null || javaSdkPathMap.isEmpty()) {
return null;
}
for (Map.Entry<String, String> entry : javaSdkPathMap.entrySet()) {
if (FileUtil.startsWith(path, entry.getValue())) {
return entry.getKey() + path.substring(entry.getValue().length());
@@ -46,10 +40,6 @@ final class JavaSdkPathRelativizer implements PathRelativizer {
@Override
public @Nullable String toAbsolutePath(@NotNull String path) {
if (javaSdkPathMap == null || javaSdkPathMap.isEmpty()) {
return null;
}
for (Map.Entry<String, String> it : javaSdkPathMap.entrySet()) {
if (path.startsWith(it.getKey())) {
return it.getValue() + path.substring(it.getKey().length());

View File

@@ -1,22 +1,23 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.jps.incremental.relativizer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
import org.jetbrains.jps.model.serialization.JpsMavenSettings;
@VisibleForTesting
@ApiStatus.Internal
public final class MavenPathRelativizer extends CommonPathRelativizer {
private static final String IDENTIFIER = "$MAVEN_REPOSITORY$";
public MavenPathRelativizer() {
super(getNormalizedMavenRepositoryPath(), IDENTIFIER);
public MavenPathRelativizer(@NotNull String mavenRepositoryPath) {
super(mavenRepositoryPath, IDENTIFIER);
}
private static String getNormalizedMavenRepositoryPath() {
public static @Nullable String getNormalizedMavenRepositoryPath() {
String path = JpsMavenSettings.getMavenRepositoryPath();
if (path != null) {
return PathRelativizerService.normalizePath(path);
}
return null;
return path == null ? null : PathRelativizerService.normalizePath(path);
}
}

View File

@@ -2,6 +2,7 @@
package org.jetbrains.jps.incremental.relativizer;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.SystemInfoRt;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
@@ -17,10 +18,7 @@ import org.jetbrains.jps.util.JpsPathUtil;
import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
public final class PathRelativizerService {
@@ -29,7 +27,7 @@ public final class PathRelativizerService {
private static final String PROJECT_DIR_IDENTIFIER = "$PROJECT_DIR$";
private static final String BUILD_DIR_IDENTIFIER = "$BUILD_DIR$";
private final List<PathRelativizer> relativizers;
private final PathRelativizer[] relativizers;
private final Set<String> unhandledPaths = Collections.synchronizedSet(new LinkedHashSet<>());
public PathRelativizerService(@Nullable String projectPath) {
@@ -45,16 +43,12 @@ public final class PathRelativizerService {
}
public PathRelativizerService(@NotNull JpsProject project, @Nullable Boolean projectDirIsCaseSensitive) {
Set<JpsSdk<?>> javaSdks = project.getModules().stream()
.map(module -> module.getSdk(JpsJavaSdkType.INSTANCE))
.filter(sdk -> sdk != null && sdk.getVersionString() != null && sdk.getHomePath() != null)
.collect(Collectors.toSet());
Set<JpsSdk<?>> javaSdks = project.getModules().stream().map(module -> module.getSdk(JpsJavaSdkType.INSTANCE))
.filter(sdk -> sdk != null && sdk.getVersionString() != null && sdk.getHomePath() != null).collect(Collectors.toSet());
File projectBaseDirectory = JpsModelSerializationDataService.getBaseDirectory(project);
relativizers = initialize(projectBaseDirectory == null ? null : projectBaseDirectory.getAbsolutePath(),
getBuildDirPath(project),
projectDirIsCaseSensitive,
javaSdks);
relativizers = initialize(projectBaseDirectory == null ? null : projectBaseDirectory.getAbsolutePath(), getBuildDirPath(project),
projectDirIsCaseSensitive, javaSdks);
}
@TestOnly
@@ -111,19 +105,36 @@ public final class PathRelativizerService {
}
}
private static @NotNull List<PathRelativizer> initialize(@Nullable String projectPath,
private static PathRelativizer[] initialize(@Nullable String projectPath,
@Nullable String buildDirPath,
@Nullable Boolean projectDirIsCaseSensitive,
@Nullable Set<? extends JpsSdk<?>> javaSdks) {
String normalizedProjectPath = projectPath == null ? null : normalizePath(projectPath);
String normalizedBuildDirPath = buildDirPath == null ? null : normalizePath(buildDirPath);
return List.of(
new CommonPathRelativizer(normalizedBuildDirPath, BUILD_DIR_IDENTIFIER, projectDirIsCaseSensitive),
new CommonPathRelativizer(normalizedProjectPath, PROJECT_DIR_IDENTIFIER),
new JavaSdkPathRelativizer(javaSdks),
new MavenPathRelativizer(),
new GradlePathRelativizer()
);
List<PathRelativizer> result = new ArrayList<>(5);
if (normalizedBuildDirPath != null) {
result.add(new CommonPathRelativizer(normalizedBuildDirPath, BUILD_DIR_IDENTIFIER, projectDirIsCaseSensitive == null
? SystemInfoRt.isFileSystemCaseSensitive
: projectDirIsCaseSensitive));
}
if (normalizedProjectPath != null) {
result.add(new CommonPathRelativizer(normalizedProjectPath, PROJECT_DIR_IDENTIFIER));
}
if (javaSdks != null && !javaSdks.isEmpty()) {
result.add(new JavaSdkPathRelativizer(javaSdks));
}
String mavenRepositoryPath = MavenPathRelativizer.getNormalizedMavenRepositoryPath();
if (mavenRepositoryPath != null) {
result.add(new MavenPathRelativizer(mavenRepositoryPath));
}
String gradleRepositoryPath = GradlePathRelativizer.initializeGradleRepositoryPath();
if (gradleRepositoryPath != null) {
result.add(new GradlePathRelativizer(gradleRepositoryPath));
}
return result.toArray(new PathRelativizer[0]);
}
static @NotNull String normalizePath(@NotNull String path) {