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

devonfw / IDEasy / 10115741272

26 Jul 2024 06:12PM UTC coverage: 61.042% (-0.1%) from 61.148%
10115741272

Pull #453

github

web-flow
Merge a6cbea603 into 5a5c2abf0
Pull Request #453: #451: mac gatekeeper

2004 of 3613 branches covered (55.47%)

Branch coverage included in aggregate %.

5304 of 8359 relevant lines covered (63.45%)

2.8 hits per line

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

80.28
cli/src/main/java/com/devonfw/tools/ide/url/model/UrlMetadata.java
1
package com.devonfw.tools.ide.url.model;
2

3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.Comparator;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9

10
import com.devonfw.tools.ide.cli.CliException;
11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.url.model.folder.UrlEdition;
13
import com.devonfw.tools.ide.url.model.folder.UrlRepository;
14
import com.devonfw.tools.ide.url.model.folder.UrlTool;
15
import com.devonfw.tools.ide.url.model.folder.UrlVersion;
16
import com.devonfw.tools.ide.version.VersionIdentifier;
17

18
/**
19
 * Service to {@link #getEdition(String, String) load} an {@link UrlEdition} to get access to its versions.
20
 */
21
public class UrlMetadata {
22

23
  private final IdeContext context;
24

25
  private final UrlRepository repository;
26

27
  private final Map<String, List<VersionIdentifier>> toolEdition2VersionMap;
28

29
  /**
30
   * The constructor.
31
   *
32
   * @param context the owning {@link IdeContext}.
33
   */
34
  public UrlMetadata(IdeContext context) {
35

36
    super();
2✔
37
    this.context = context;
3✔
38
    this.repository = new UrlRepository(this.context.getUrlsPath());
8✔
39
    this.toolEdition2VersionMap = new HashMap<>();
5✔
40
  }
1✔
41

42
  /**
43
   * @param tool the name of the {@link UrlTool}.
44
   * @param edition the name of the {@link UrlEdition}.
45
   * @return the {@link UrlEdition}. Will be lazily loaded.
46
   */
47
  public UrlEdition getEdition(String tool, String edition) {
48

49
    UrlTool urlTool = this.repository.getChild(tool);
6✔
50
    if (urlTool == null) {
2!
51
      throw new CliException("Could not find tool '" + tool + "' in ide-urls metadata!");
×
52
    }
53
    UrlEdition urlEdition = urlTool.getChild(edition);
5✔
54
    if (urlEdition == null) {
2!
55
      throw new CliException("Could not find edition '" + edition + "' for tool '" + tool + "' in ide-urls metadata!");
×
56
    }
57
    return urlEdition;
2✔
58
  }
59

60
  /**
61
   * @param tool the name of the {@link UrlTool}.
62
   * @return the sorted {@link List} of {@link String editions} .
63
   */
64
  public List<String> getSortedEditions(String tool) {
65

66
    List<String> list = new ArrayList<>();
4✔
67
    UrlTool urlTool = this.repository.getChild(tool);
6✔
68
    if (urlTool == null) {
2✔
69
      this.context.warning("Can't get sorted editions for tool {} because it does not exist in {}.", tool, this.repository.getPath());
17✔
70
    } else {
71
      for (UrlEdition urlEdition : urlTool.getChildren()) {
11✔
72
        list.add(urlEdition.getName());
5✔
73
      }
1✔
74
    }
75
    Collections.sort(list);
2✔
76
    return Collections.unmodifiableList(list);
3✔
77
  }
78

79
  /**
80
   * @param tool the name of the {@link UrlTool}.
81
   * @param edition the name of the {@link UrlEdition}.
82
   * @return the {@link List} of {@link VersionIdentifier}s sorted descending so the latest version comes first and the oldest comes last.
83
   */
84
  public List<VersionIdentifier> getSortedVersions(String tool, String edition) {
85

86
    String key = tool + "/" + edition;
4✔
87
    return this.toolEdition2VersionMap.computeIfAbsent(key, k -> computeSortedVersions(tool, edition));
15✔
88
  }
89

90
  private List<VersionIdentifier> computeSortedVersions(String tool, String edition) {
91

92
    List<VersionIdentifier> list = new ArrayList<>();
4✔
93
    UrlEdition urlEdition = getEdition(tool, edition);
5✔
94
    urlEdition.load(false);
3✔
95
    for (UrlVersion urlVersion : urlEdition.getChildren()) {
11✔
96
      VersionIdentifier versionIdentifier = urlVersion.getVersionIdentifier();
3✔
97
      list.add(versionIdentifier);
4✔
98
    }
1✔
99
    Collections.sort(list, Comparator.reverseOrder());
3✔
100
    return Collections.unmodifiableList(list);
3✔
101
  }
102

103
  /**
104
   * @param tool the name of the {@link UrlTool}.
105
   * @param edition the name of the {@link UrlEdition}.
106
   * @param version the {@link VersionIdentifier} to match. May be a {@link VersionIdentifier#isPattern() pattern}, a specific version or {@code null} for the
107
   * latest version.
108
   * @return the latest matching {@link VersionIdentifier} for the given {@code tool} and {@code edition}.
109
   */
110
  public UrlVersion getResolvedVersion(String tool, String edition, VersionIdentifier version) {
111

112
    if (version == null) {
2!
113
      version = VersionIdentifier.LATEST;
×
114
    }
115
    UrlEdition urlEdition = getEdition(tool, edition);
5✔
116
    VersionIdentifier resolvedVersion = version;
2✔
117
    if (version.isPattern()) {
3✔
118
      resolvedVersion = resolveVersion(tool, edition, version);
6✔
119
    }
120
    UrlVersion urlVersion = urlEdition.getChild(resolvedVersion.toString());
6✔
121
    if (urlVersion == null) {
2!
122
      throw new CliException("Version " + version + " for tool " + tool + " does not exist in edition " + edition + ".");
×
123
    }
124
    return urlVersion;
2✔
125
  }
126

127
  private VersionIdentifier resolveVersion(String tool, String edition, VersionIdentifier version) {
128

129
    List<VersionIdentifier> versions = getSortedVersions(tool, edition);
5✔
130
    for (VersionIdentifier vi : versions) {
10!
131
      if (version.matches(vi)) {
4!
132
        this.context.debug("Resolved version pattern {} to version {}", version, vi);
14✔
133
        return vi;
2✔
134
      }
135
    }
×
136
    // TODO properly consider edition (needs list-versions commandlet enhancement to also support edition)
137
    throw new CliException(
×
138
        "Could not find any version matching '" + version + "' for tool '" + tool + "' - potentially there are " + versions.size() + " version(s) available in "
×
139
            + getEdition(tool, edition).getPath() + " but none matched! You can get the list of available versions with the following command:\nide list-versions " + tool);
×
140
  }
141

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