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

devonfw / IDEasy / 12752008871

13 Jan 2025 04:48PM UTC coverage: 68.077% (+0.5%) from 67.541%
12752008871

Pull #820

github

web-flow
Merge 3db4692df into 8e971e1a8
Pull Request #820: #759: upgrade settings commandlet

2689 of 4311 branches covered (62.38%)

Branch coverage included in aggregate %.

6946 of 9842 relevant lines covered (70.58%)

3.1 hits per line

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

20.87
cli/src/main/java/com/devonfw/tools/ide/repo/CustomToolRepositoryImpl.java
1
package com.devonfw.tools.ide.repo;
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.url.model.file.UrlDownloadFileMetadata;
16
import com.devonfw.tools.ide.url.model.file.json.ToolDependency;
17
import com.devonfw.tools.ide.version.GenericVersionRange;
18
import com.devonfw.tools.ide.version.VersionIdentifier;
19

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

25
  private final String id;
26

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

29
  private final Collection<CustomToolMetadata> tools;
30

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

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

59
  private static String computeId(String url) {
60

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

93
  @Override
94
  public String getId() {
95

96
    return this.id;
×
97
  }
98

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

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

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

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

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

132
  @Override
133
  public Collection<CustomToolMetadata> getTools() {
134

135
    return this.tools;
3✔
136
  }
137

138
  @Override
139
  public Collection<ToolDependency> findDependencies(String tool, String edition, VersionIdentifier version) {
140

141
    return Collections.emptyList();
×
142
  }
143

144
  /**
145
   * @param context the owning {@link IdeContext}.
146
   * @return the {@link CustomToolRepository}.
147
   */
148
  public static CustomToolRepository of(IdeContext context) {
149

150
    Path settingsPath = context.getSettingsPath();
3✔
151
    Path customToolsJsonFile = null;
2✔
152
    if (settingsPath != null) {
2✔
153
      customToolsJsonFile = settingsPath.resolve(IdeContext.FILE_CUSTOM_TOOLS);
4✔
154
    }
155
    List<CustomToolMetadata> tools;
156
    if ((customToolsJsonFile != null) && Files.exists(customToolsJsonFile)) {
7!
157
      CustomToolsJson customToolsJson = CustomToolsJsonMapper.loadJson(customToolsJsonFile);
×
158
      tools = CustomToolsJsonMapper.convert(customToolsJson, context);
×
159
    } else {
×
160
      tools = new ArrayList<>();
4✔
161
    }
162
    return new CustomToolRepositoryImpl(context, tools);
6✔
163
  }
164

165
}
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

© 2026 Coveralls, Inc