4
4
import io .projectenv .commons .gson .GsonFactory ;
5
5
import io .projectenv .core .cli .ProjectEnvException ;
6
6
import io .projectenv .core .commons .process .ProcessOutput ;
7
+ import io .projectenv .core .commons .system .CpuArchitecture ;
7
8
import io .projectenv .core .commons .system .EnvironmentVariables ;
8
9
import io .projectenv .core .commons .system .OperatingSystem ;
9
10
import io .projectenv .core .toolsupport .commons .ToolVersionHelper ;
10
- import io .projectenv .core .toolsupport .spi .index .ToolsIndex ;
11
11
import io .projectenv .core .toolsupport .spi .index .ToolsIndexException ;
12
12
import io .projectenv .core .toolsupport .spi .index .ToolsIndexManager ;
13
+ import io .projectenv .core .toolsupport .spi .index .ToolsIndexV2 ;
13
14
import org .apache .commons .io .FileUtils ;
14
15
import org .apache .commons .io .IOUtils ;
15
16
import org .apache .commons .lang3 .StringUtils ;
27
28
28
29
public class DefaultToolsIndexManager implements ToolsIndexManager {
29
30
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 PROJECT_ENV_TOOL_INDEX_ENV = "PROJECT_ENV_TOOL_INDEX " ;
32
- private static final String TOOLS_INDEX_FILE = "tools-index.json" ;
31
+ private static final String DEFAULT_TOOLS_INDEX_URL = "https://raw.githubusercontent.com/Project-Env/project-env-tools/main/index-v2 .json" ;
32
+ private static final String PROJECT_ENV_TOOL_INDEX_ENV = "PROJECT_ENV_TOOL_INDEX_V2 " ;
33
+ private static final String TOOLS_INDEX_FILE = "tools-index-v2 .json" ;
33
34
34
35
private static final Gson GSON = GsonFactory .createGsonBuilder ().create ();
35
36
@@ -39,13 +40,13 @@ public class DefaultToolsIndexManager implements ToolsIndexManager {
39
40
40
41
private final File toolsRoot ;
41
42
42
- private ToolsIndex toolsIndex ;
43
+ private ToolsIndexV2 toolsIndexV2 ;
43
44
44
45
public DefaultToolsIndexManager (File toolsRoot ) {
45
46
this .toolsRoot = toolsRoot ;
46
47
}
47
48
48
- private ToolsIndex loadToolsIndex () throws IOException {
49
+ private ToolsIndexV2 loadToolsIndex () throws IOException {
49
50
var toolsIndexFile = new File (toolsRoot , TOOLS_INDEX_FILE );
50
51
FileUtils .forceMkdirParent (toolsIndexFile );
51
52
@@ -64,7 +65,7 @@ private ToolsIndex loadToolsIndex() throws IOException {
64
65
}
65
66
66
67
try (Reader reader = new FileReader (toolsIndexFile , StandardCharsets .UTF_8 )) {
67
- return GSON .fromJson (reader , ToolsIndex .class );
68
+ return GSON .fromJson (reader , ToolsIndexV2 .class );
68
69
}
69
70
}
70
71
@@ -133,6 +134,7 @@ public Set<String> getGradleVersions() {
133
134
public String resolveNodeJsDistributionUrl (String version ) {
134
135
return Optional .ofNullable (getToolsIndex ().getNodeVersions ().get (ToolVersionHelper .getVersionWithoutPrefix (version )))
135
136
.map (versionEntry -> versionEntry .get (OperatingSystem .getCurrentOperatingSystem ()))
137
+ .map (this ::resolveDownloadUrlForCpuArchitecture )
136
138
.orElseThrow (() -> new ToolsIndexException ("failed to resolve NodeJS " + version + " from tool index" ));
137
139
}
138
140
@@ -147,6 +149,7 @@ public String resolveJdkDistributionUrl(String jdkDistribution, String version)
147
149
.map (jdkDistributionId -> getToolsIndex ().getJdkVersions ().get (jdkDistributionId ))
148
150
.map (jdkDistributionEntry -> jdkDistributionEntry .get (ToolVersionHelper .getVersionWithoutPrefix (version )))
149
151
.map (versionEntry -> versionEntry .get (OperatingSystem .getCurrentOperatingSystem ()))
152
+ .map (this ::resolveDownloadUrlForCpuArchitecture )
150
153
.orElseThrow (() -> new ToolsIndexException ("failed to resolve " + jdkDistribution + " " + version + " from tool index" ));
151
154
}
152
155
@@ -158,6 +161,19 @@ public Set<String> getJdkDistributionVersions(String jdkDistribution) {
158
161
.orElse (Collections .emptySet ());
159
162
}
160
163
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
+
161
177
private Optional <String > resolveJdkDistributionId (String jdkDistribution ) {
162
178
var jdkDistributionSynonyms = getToolsIndex ().getJdkDistributionSynonyms ();
163
179
if (jdkDistributionSynonyms .containsKey (jdkDistribution )) {
@@ -173,13 +189,13 @@ private Optional<String> resolveJdkDistributionId(String jdkDistribution) {
173
189
return Optional .empty ();
174
190
}
175
191
176
- private ToolsIndex getToolsIndex () {
192
+ private ToolsIndexV2 getToolsIndex () {
177
193
try {
178
- if (toolsIndex == null ) {
179
- toolsIndex = loadToolsIndex ();
194
+ if (toolsIndexV2 == null ) {
195
+ toolsIndexV2 = loadToolsIndex ();
180
196
}
181
197
182
- return toolsIndex ;
198
+ return toolsIndexV2 ;
183
199
} catch (IOException e ) {
184
200
throw new ProjectEnvException ("failed to load tool index" , e );
185
201
}
0 commit comments