Skip to content

Commit

Permalink
devonfw#759: added missing changes
Browse files Browse the repository at this point in the history
changed return type of readCustomToolsFromJson to CustomToolsJson
  • Loading branch information
jan-vcapgemini committed Dec 18, 2024
1 parent b614c6d commit 6408d55
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
*/
public interface CustomToolRepository extends ToolRepository {

/** The filename of the configuration file in the settings for this {@link CustomToolRepository}. */
String FILE_CUSTOM_TOOLS = "ide-custom-tools.json";

/**
* @return the {@link Collection} with the {@link CustomTool}s. Will be {@link Collection#isEmpty() empty} if no custom tools are configured.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,39 +162,46 @@ public static CustomToolRepository of(IdeContext context) {
Path settingsPath = context.getSettingsPath();
Path customToolsJson = null;
if (settingsPath != null) {
customToolsJson = settingsPath.resolve(FILE_CUSTOM_TOOLS);
customToolsJson = settingsPath.resolve(IdeContext.FILE_CUSTOM_TOOLS);
}
List<CustomTool> tools = new ArrayList<>();
if ((customToolsJson != null) && Files.exists(customToolsJson)) {
try (InputStream in = Files.newInputStream(customToolsJson);
Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {

JsonReader jsonReader = Json.createReader(new BufferedReader(reader));
JsonStructure json = jsonReader.read();
JsonObject jsonRoot = requireObject(json);
String defaultUrl = getString(jsonRoot, "url", "");
JsonArray jsonTools = requireArray(jsonRoot.get("tools"));
for (JsonValue jsonTool : jsonTools) {
JsonObject jsonToolObject = requireObject(jsonTool);
String name = getString(jsonToolObject, "name");
String version = getString(jsonToolObject, "version");
String url = getString(jsonToolObject, "url", defaultUrl);
boolean osAgnostic = getBoolean(jsonToolObject, "os-agnostic", Boolean.FALSE);
boolean archAgnostic = getBoolean(jsonToolObject, "arch-agnostic", Boolean.TRUE);
if (url.isEmpty()) {
throw new IllegalStateException("Missing 'url' property for tool '" + name + "'!");
}
// TODO
String checksum = null;
CustomTool customTool = new CustomTool(name, VersionIdentifier.of(version), osAgnostic, archAgnostic, url,
checksum, context.getSystemInfo());
tools.add(customTool);
readCustomToolsFromJson(context, customToolsJson);
}
return new CustomToolRepositoryImpl(context, tools);
}

public static CustomToolsJson readCustomToolsFromJson(IdeContext context, Path customToolsJsonPath) {
try (InputStream in = Files.newInputStream(customToolsJsonPath);
Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {

JsonReader jsonReader = Json.createReader(new BufferedReader(reader));
JsonStructure json = jsonReader.read();
JsonObject jsonRoot = requireObject(json);
String defaultUrl = getString(jsonRoot, "url", "");
JsonArray jsonTools = requireArray(jsonRoot.get("tools"));
List<CustomTool> customTools = new ArrayList<>();

for (JsonValue jsonTool : jsonTools) {
JsonObject jsonToolObject = requireObject(jsonTool);
String name = getString(jsonToolObject, "name");
String version = getString(jsonToolObject, "version");
String url = getString(jsonToolObject, "url", defaultUrl);
boolean osAgnostic = getBoolean(jsonToolObject, "os-agnostic", Boolean.FALSE);
boolean archAgnostic = getBoolean(jsonToolObject, "arch-agnostic", Boolean.TRUE);
if (url.isEmpty()) {
throw new IllegalStateException("Missing 'url' property for tool '" + name + "'!");
}
} catch (Exception e) {
throw new IllegalStateException("Failed to read JSON from " + customToolsJson, e);
// TODO
String checksum = null;
CustomTool customTool = new CustomTool(name, VersionIdentifier.of(version), osAgnostic, archAgnostic, url,
checksum, context.getSystemInfo());
customTools.add(customTool);
}
return new CustomToolsJson("tools", defaultUrl, customTools);
} catch (Exception e) {
throw new IllegalStateException("Failed to read JSON from " + customToolsJsonPath, e);
}
return new CustomToolRepositoryImpl(context, tools);
}

private static boolean getBoolean(JsonObject json, String property, Boolean defaultValue) {
Expand Down

0 comments on commit 6408d55

Please sign in to comment.