• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 13327588889

14 Feb 2025 10:44AM UTC coverage: 67.947% (-0.5%) from 68.469%
13327588889

Pull #1021

github

web-flow
Merge d03159bfe into 52609dacb
Pull Request #1021: #786: support ide upgrade to automatically update to the latest version of IDEasy

2964 of 4791 branches covered (61.87%)

Branch coverage included in aggregate %.

7688 of 10886 relevant lines covered (70.62%)

3.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

18.64
cli/src/main/java/com/devonfw/tools/ide/tool/repository/CustomToolRepositoryImpl.java
1
package com.devonfw.tools.ide.tool.repository;
2

3
import java.net.URLDecoder;
4
import java.nio.charset.StandardCharsets;
5
import java.nio.file.Files;
6
import java.nio.file.Path;
7
import java.util.ArrayList;
8
import java.util.Collection;
9
import java.util.Collections;
10
import java.util.HashMap;
11
import java.util.List;
12
import java.util.Map;
13

14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.tool.ToolCommandlet;
16
import com.devonfw.tools.ide.url.model.file.UrlDownloadFileMetadata;
17
import com.devonfw.tools.ide.url.model.file.json.ToolDependency;
18
import com.devonfw.tools.ide.version.GenericVersionRange;
19
import com.devonfw.tools.ide.version.VersionIdentifier;
20

21
/**
22
 * Implementation of {@link CustomToolRepository}.
23
 */
24
public class CustomToolRepositoryImpl extends AbstractToolRepository implements CustomToolRepository {
25

26
  private final String id;
27

28
  private final Map<String, CustomToolMetadata> toolsMap;
29

30
  private final Collection<CustomToolMetadata> tools;
31

32
  /**
33
   * The constructor.
34
   *
35
   * @param context the owning {@link IdeContext}.
36
   * @param tools the {@link CustomToolMetadata}s.
37
   */
38
  public CustomToolRepositoryImpl(IdeContext context, Collection<CustomToolMetadata> tools) {
39

40
    super(context);
3✔
41
    this.toolsMap = new HashMap<>(tools.size());
7✔
42
    String repoId = null;
2✔
43
    for (CustomToolMetadata tool : tools) {
6!
44
      String name = tool.getTool();
×
45
      CustomToolMetadata duplicate = this.toolsMap.put(name, tool);
×
46
      if (duplicate != null) {
×
47
        throw new IllegalStateException("Duplicate custom tool '" + name + "'!");
×
48
      }
49
      if (repoId == null) {
×
50
        repoId = computeId(tool.getRepositoryUrl());
×
51
      }
52
    }
×
53
    if (repoId == null) {
2!
54
      repoId = "custom";
2✔
55
    }
56
    this.id = repoId;
3✔
57
    this.tools = Collections.unmodifiableCollection(this.toolsMap.values());
6✔
58
  }
1✔
59

60
  private static String computeId(String url) {
61

62
    String id = url;
×
63
    int schemaIndex = id.indexOf("://");
×
64
    if (schemaIndex > 0) {
×
65
      id = id.substring(schemaIndex + 3); // remove schema like "https://"
×
66
      id = URLDecoder.decode(id, StandardCharsets.UTF_8);
×
67
    }
68
    id = id.replace('\\', '/').replace("//", "/"); // normalize slashes
×
69
    if (id.startsWith("/")) {
×
70
      id = id.substring(1);
×
71
    }
72
    StringBuilder sb = new StringBuilder(id.length());
×
73
    if (schemaIndex > 0) { // was a URL?
×
74
      int slashIndex = id.indexOf('/');
×
75
      if (slashIndex > 0) {
×
76
        sb.append(id.substring(0, slashIndex).replace(':', '_'));
×
77
        sb.append('/');
×
78
        id = id.substring(slashIndex + 1);
×
79
      }
80
    }
81
    int length = id.length();
×
82
    for (int i = 0; i < length; i++) {
×
83
      char c = id.charAt(i);
×
84
      if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == '.')
×
85
          || (c == '-')) {
86
        sb.append(c);
×
87
      } else {
88
        sb.append('_');
×
89
      }
90
    }
91
    return sb.toString();
×
92
  }
93

94
  @Override
95
  public String getId() {
96

97
    return this.id;
×
98
  }
99

100
  @Override
101
  protected UrlDownloadFileMetadata getMetadata(String tool, String edition, VersionIdentifier version, ToolCommandlet toolCommandlet) {
102

103
    CustomToolMetadata customTool = getCustomTool(tool);
×
104
    if (!version.equals(customTool.getVersion())) {
×
105
      throw new IllegalArgumentException("Undefined version '" + version + "' for custom tool '" + tool
×
106
          + "' - expected version '" + customTool.getVersion() + "'!");
×
107
    }
108
    if (!edition.equals(customTool.getEdition())) {
×
109
      throw new IllegalArgumentException("Undefined edition '" + edition + "' for custom tool '" + tool + "'!");
×
110
    }
111
    return customTool;
×
112
  }
113

114
  private CustomToolMetadata getCustomTool(String tool) {
115
    CustomToolMetadata customTool = this.toolsMap.get(tool);
×
116
    if (customTool == null) {
×
117
      throw new IllegalArgumentException("Undefined custom tool '" + tool + "'!");
×
118
    }
119
    return customTool;
×
120
  }
121

122
  @Override
123
  public VersionIdentifier resolveVersion(String tool, String edition, GenericVersionRange version, ToolCommandlet toolCommandlet) {
124

125
    CustomToolMetadata customTool = getCustomTool(tool);
×
126
    VersionIdentifier customToolVersion = customTool.getVersion();
×
127
    if (!version.contains(customToolVersion)) {
×
128
      throw new IllegalStateException(customTool + " does not satisfy version to install " + version);
×
129
    }
130
    return customToolVersion;
×
131
  }
132

133
  @Override
134
  public List<VersionIdentifier> getSortedVersions(String tool, String edition, ToolCommandlet toolCommandlet) {
135

136
    CustomToolMetadata customTool = getCustomTool(tool);
×
137
    return List.of(customTool.getVersion());
×
138
  }
139

140
  @Override
141
  public Collection<CustomToolMetadata> getTools() {
142

143
    return this.tools;
3✔
144
  }
145

146
  @Override
147
  public Collection<ToolDependency> findDependencies(String tool, String edition, VersionIdentifier version) {
148

149
    return Collections.emptyList();
×
150
  }
151

152
  @Override
153
  public List<String> getSortedEditions(String tool) {
154

155
    return List.of(tool);
×
156
  }
157

158
  /**
159
   * @param context the owning {@link IdeContext}.
160
   * @return the {@link CustomToolRepository}.
161
   */
162
  public static CustomToolRepository of(IdeContext context) {
163

164
    Path settingsPath = context.getSettingsPath();
3✔
165
    Path customToolsJsonFile = null;
2✔
166
    if (settingsPath != null) {
2!
167
      customToolsJsonFile = settingsPath.resolve(IdeContext.FILE_CUSTOM_TOOLS);
4✔
168
    }
169
    List<CustomToolMetadata> tools;
170
    if ((customToolsJsonFile != null) && Files.exists(customToolsJsonFile)) {
7!
171
      CustomToolsJson customToolsJson = CustomToolsJsonMapper.loadJson(customToolsJsonFile);
×
172
      tools = CustomToolsJsonMapper.convert(customToolsJson, context);
×
173
    } else {
×
174
      tools = new ArrayList<>();
4✔
175
    }
176
    return new CustomToolRepositoryImpl(context, tools);
6✔
177
  }
178

179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc