Skip to content

Commit 406c315

Browse files
committed
Use index V2
1 parent bab815e commit 406c315

File tree

14 files changed

+66
-177
lines changed

14 files changed

+66
-177
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Note that the flag needs to be added as prefix (e.g. `version = "^11.0.16.1+1"`)
124124
After running the `upgrade` command, the `install` command needs to be executed again to install the upgraded tools.
125125

126126
## Custom index
127-
The `PROJECT_ENV_TOOL_INDEX` env var can be used to specify a custom index URL.
127+
The `PROJECT_ENV_TOOL_INDEX` env var can be used to specify a custom index URL. Note that the custom index needs to follow the format of the V2 format (see https://github.com/Project-Env/project-env-tools/blob/main/index-v2.json).
128128

129129
## Installation
130130

code/cli/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
<groupId>org.apache.commons</groupId>
7575
<artifactId>commons-collections4</artifactId>
7676
</dependency>
77+
<dependency>
78+
<groupId>org.apache.commons</groupId>
79+
<artifactId>commons-text</artifactId>
80+
</dependency>
7781

7882
<dependency>
7983
<groupId>info.picocli</groupId>

code/cli/src/main/java/io/projectenv/core/cli/index/DefaultToolsIndexManager.java

+25-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import io.projectenv.commons.gson.GsonFactory;
55
import io.projectenv.core.cli.ProjectEnvException;
66
import io.projectenv.core.commons.process.ProcessOutput;
7+
import io.projectenv.core.commons.system.CpuArchitecture;
78
import io.projectenv.core.commons.system.EnvironmentVariables;
89
import io.projectenv.core.commons.system.OperatingSystem;
910
import io.projectenv.core.toolsupport.commons.ToolVersionHelper;
10-
import io.projectenv.core.toolsupport.spi.index.ToolsIndex;
1111
import io.projectenv.core.toolsupport.spi.index.ToolsIndexException;
1212
import io.projectenv.core.toolsupport.spi.index.ToolsIndexManager;
13+
import io.projectenv.core.toolsupport.spi.index.ToolsIndexV2;
1314
import org.apache.commons.io.FileUtils;
1415
import org.apache.commons.io.IOUtils;
1516
import org.apache.commons.lang3.StringUtils;
@@ -27,7 +28,7 @@
2728

2829
public class DefaultToolsIndexManager implements ToolsIndexManager {
2930

30-
private static final String DEFAULT_TOOLS_INDEX_URL = "https://raw.githubusercontent.com/Project-Env/project-env-tools/main/index.json";
31+
private static final String DEFAULT_TOOLS_INDEX_URL = "https://raw.githubusercontent.com/Project-Env/project-env-tools/main/index-v2.json";
3132
private static final String PROJECT_ENV_TOOL_INDEX_ENV = "PROJECT_ENV_TOOL_INDEX";
3233
private static final String TOOLS_INDEX_FILE = "tools-index.json";
3334

@@ -39,13 +40,13 @@ public class DefaultToolsIndexManager implements ToolsIndexManager {
3940

4041
private final File toolsRoot;
4142

42-
private ToolsIndex toolsIndex;
43+
private ToolsIndexV2 toolsIndexV2;
4344

4445
public DefaultToolsIndexManager(File toolsRoot) {
4546
this.toolsRoot = toolsRoot;
4647
}
4748

48-
private ToolsIndex loadToolsIndex() throws IOException {
49+
private ToolsIndexV2 loadToolsIndex() throws IOException {
4950
var toolsIndexFile = new File(toolsRoot, TOOLS_INDEX_FILE);
5051
FileUtils.forceMkdirParent(toolsIndexFile);
5152

@@ -64,7 +65,7 @@ private ToolsIndex loadToolsIndex() throws IOException {
6465
}
6566

6667
try (Reader reader = new FileReader(toolsIndexFile, StandardCharsets.UTF_8)) {
67-
return GSON.fromJson(reader, ToolsIndex.class);
68+
return GSON.fromJson(reader, ToolsIndexV2.class);
6869
}
6970
}
7071

@@ -133,6 +134,7 @@ public Set<String> getGradleVersions() {
133134
public String resolveNodeJsDistributionUrl(String version) {
134135
return Optional.ofNullable(getToolsIndex().getNodeVersions().get(ToolVersionHelper.getVersionWithoutPrefix(version)))
135136
.map(versionEntry -> versionEntry.get(OperatingSystem.getCurrentOperatingSystem()))
137+
.map(this::resolveDownloadUrlForCpuArchitecture)
136138
.orElseThrow(() -> new ToolsIndexException("failed to resolve NodeJS " + version + " from tool index"));
137139
}
138140

@@ -147,6 +149,7 @@ public String resolveJdkDistributionUrl(String jdkDistribution, String version)
147149
.map(jdkDistributionId -> getToolsIndex().getJdkVersions().get(jdkDistributionId))
148150
.map(jdkDistributionEntry -> jdkDistributionEntry.get(ToolVersionHelper.getVersionWithoutPrefix(version)))
149151
.map(versionEntry -> versionEntry.get(OperatingSystem.getCurrentOperatingSystem()))
152+
.map(this::resolveDownloadUrlForCpuArchitecture)
150153
.orElseThrow(() -> new ToolsIndexException("failed to resolve " + jdkDistribution + " " + version + " from tool index"));
151154
}
152155

@@ -158,6 +161,19 @@ public Set<String> getJdkDistributionVersions(String jdkDistribution) {
158161
.orElse(Collections.emptySet());
159162
}
160163

164+
private String resolveDownloadUrlForCpuArchitecture(Map<CpuArchitecture, String> downloadUrls) {
165+
String downloadUrl = downloadUrls.get(CpuArchitecture.getCurrentCpuArchitecture());
166+
167+
// In case there is no download URL for Apple Silicon, we use the amd64 as fallback
168+
if (downloadUrl == null &&
169+
CpuArchitecture.getCurrentCpuArchitecture() == CpuArchitecture.AARCH64 &&
170+
OperatingSystem.getCurrentOperatingSystem() == OperatingSystem.MACOS) {
171+
downloadUrl = downloadUrls.get(CpuArchitecture.AMD64);
172+
}
173+
174+
return downloadUrl;
175+
}
176+
161177
private Optional<String> resolveJdkDistributionId(String jdkDistribution) {
162178
var jdkDistributionSynonyms = getToolsIndex().getJdkDistributionSynonyms();
163179
if (jdkDistributionSynonyms.containsKey(jdkDistribution)) {
@@ -173,13 +189,13 @@ private Optional<String> resolveJdkDistributionId(String jdkDistribution) {
173189
return Optional.empty();
174190
}
175191

176-
private ToolsIndex getToolsIndex() {
192+
private ToolsIndexV2 getToolsIndex() {
177193
try {
178-
if (toolsIndex == null) {
179-
toolsIndex = loadToolsIndex();
194+
if (toolsIndexV2 == null) {
195+
toolsIndexV2 = loadToolsIndex();
180196
}
181197

182-
return toolsIndex;
198+
return toolsIndexV2;
183199
} catch (IOException e) {
184200
throw new ProjectEnvException("failed to load tool index", e);
185201
}

code/commons/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<modules>
1919
<module>native-image</module>
2020
<module>process</module>
21-
<module>string-substitutor</module>
2221
<module>archive</module>
2322
<module>system</module>
2423
<module>system-test</module>

code/commons/string-substitutor/pom.xml

-47
This file was deleted.

code/commons/string-substitutor/src/main/java/io/projectenv/core/commons/download/DownloadUrlDictionary.java

-18
This file was deleted.

code/commons/string-substitutor/src/main/java/io/projectenv/core/commons/download/DownloadUrlSubstitutorFactory.java

-17
This file was deleted.

code/commons/string-substitutor/src/main/java/io/projectenv/core/commons/download/impl/DownloadUrlVariableLookup.java

-53
This file was deleted.

code/commons/system/src/main/java/io/projectenv/core/commons/system/CPUArchitecture.java

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.projectenv.core.commons.system;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.apache.commons.lang3.SystemUtils;
6+
7+
import java.util.Arrays;
8+
9+
public enum CpuArchitecture {
10+
11+
@SerializedName("amd64")
12+
AMD64("amd64", "x86_64"),
13+
@SerializedName("aarch64")
14+
AARCH64("aarch64");
15+
16+
private final String[] identifiers;
17+
18+
CpuArchitecture(String... identifiers) {
19+
this.identifiers = identifiers;
20+
}
21+
22+
public static CpuArchitecture getCurrentCpuArchitecture() {
23+
return Arrays.stream(values())
24+
.filter(value -> Arrays
25+
.stream(value.identifiers)
26+
.anyMatch(identifier -> StringUtils.equalsIgnoreCase(SystemUtils.OS_ARCH, identifier)))
27+
.findFirst()
28+
.orElseThrow(() -> new IllegalStateException("unsupported CPU architecture " + SystemUtils.OS_ARCH));
29+
}
30+
31+
32+
}

code/tool-support/jdk-support/pom.xml

-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
<version>${project.version}</version>
2626
</dependency>
2727

28-
<dependency>
29-
<groupId>io.projectenv.core.commons</groupId>
30-
<artifactId>string-substitutor</artifactId>
31-
<version>${project.version}</version>
32-
</dependency>
33-
3428
<dependency>
3529
<groupId>org.immutables</groupId>
3630
<artifactId>value</artifactId>

code/tool-support/nodejs-support/pom.xml

-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
<version>${project.version}</version>
2626
</dependency>
2727

28-
<dependency>
29-
<groupId>io.projectenv.core.commons</groupId>
30-
<artifactId>string-substitutor</artifactId>
31-
<version>${project.version}</version>
32-
</dependency>
33-
3428
<dependency>
3529
<groupId>org.immutables</groupId>
3630
<artifactId>value</artifactId>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.projectenv.core.toolsupport.spi.index;
22

3+
import io.projectenv.core.commons.system.CpuArchitecture;
34
import io.projectenv.core.commons.system.OperatingSystem;
45
import org.immutables.gson.Gson;
56
import org.immutables.value.Value;
@@ -9,16 +10,16 @@
910

1011
@Gson.TypeAdapters
1112
@Value.Immutable
12-
public interface ToolsIndex {
13+
public interface ToolsIndexV2 {
1314

14-
Map<String, Map<String, Map<OperatingSystem, String>>> getJdkVersions();
15+
Map<String, Map<String, Map<OperatingSystem, Map<CpuArchitecture, String>>>> getJdkVersions();
1516

1617
Map<String, Set<String>> getJdkDistributionSynonyms();
1718

1819
Map<String, String> getGradleVersions();
1920

2021
Map<String, String> getMavenVersions();
2122

22-
Map<String, Map<OperatingSystem, String>> getNodeVersions();
23+
Map<String, Map<OperatingSystem, Map<CpuArchitecture, String>>> getNodeVersions();
2324

2425
}

tools/sonar-aggregator/pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@
6969
<artifactId>process</artifactId>
7070
<version>${project.version}</version>
7171
</dependency>
72-
<dependency>
73-
<groupId>io.projectenv.core.commons</groupId>
74-
<artifactId>string-substitutor</artifactId>
75-
<version>${project.version}</version>
76-
</dependency>
7772
<dependency>
7873
<groupId>io.projectenv.core.commons</groupId>
7974
<artifactId>archive</artifactId>

0 commit comments

Comments
 (0)