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

devonfw / IDEasy / 15820521557

23 Jun 2025 09:29AM UTC coverage: 67.793% (+0.05%) from 67.745%
15820521557

push

github

web-flow
#901: Add mvn wrapper detection (#1373)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

3177 of 5088 branches covered (62.44%)

Branch coverage included in aggregate %.

8137 of 11601 relevant lines covered (70.14%)

3.08 hits per line

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

82.07
cli/src/main/java/com/devonfw/tools/ide/tool/IdeasyCommandlet.java
1
package com.devonfw.tools.ide.tool;
2

3
import java.io.IOException;
4
import java.nio.file.Files;
5
import java.nio.file.Path;
6
import java.util.ArrayList;
7
import java.util.Iterator;
8
import java.util.LinkedHashMap;
9
import java.util.List;
10
import java.util.Set;
11

12
import com.devonfw.tools.ide.cli.CliException;
13
import com.devonfw.tools.ide.commandlet.UpgradeMode;
14
import com.devonfw.tools.ide.common.SimpleSystemPath;
15
import com.devonfw.tools.ide.common.Tag;
16
import com.devonfw.tools.ide.context.IdeContext;
17
import com.devonfw.tools.ide.git.GitContext;
18
import com.devonfw.tools.ide.git.GitContextImpl;
19
import com.devonfw.tools.ide.io.FileAccess;
20
import com.devonfw.tools.ide.os.WindowsHelper;
21
import com.devonfw.tools.ide.os.WindowsPathSyntax;
22
import com.devonfw.tools.ide.process.ProcessMode;
23
import com.devonfw.tools.ide.tool.mvn.MvnArtifact;
24
import com.devonfw.tools.ide.tool.mvn.MvnBasedLocalToolCommandlet;
25
import com.devonfw.tools.ide.tool.repository.MavenRepository;
26
import com.devonfw.tools.ide.variable.IdeVariables;
27
import com.devonfw.tools.ide.version.IdeVersion;
28
import com.devonfw.tools.ide.version.VersionIdentifier;
29

30
/**
31
 * {@link MvnBasedLocalToolCommandlet} for IDEasy (ide-cli).
32
 */
33
public class IdeasyCommandlet extends MvnBasedLocalToolCommandlet {
34

35
  /** The {@link MvnArtifact} for IDEasy. */
36
  public static final MvnArtifact ARTIFACT = MvnArtifact.ofIdeasyCli("*!", "tar.gz", "${os}-${arch}");
6✔
37

38
  private static final String BASH_CODE_SOURCE_FUNCTIONS = "source \"$IDE_ROOT/_ide/installation/functions\"";
39

40
  /** The {@link #getName() tool name}. */
41
  public static final String TOOL_NAME = "ideasy";
42
  public static final String BASHRC = ".bashrc";
43
  public static final String ZSHRC = ".zshrc";
44
  public static final String IDE_BIN = "\\_ide\\bin";
45
  public static final String IDE_INSTALLATION_BIN = "\\_ide\\installation\\bin";
46

47
  private final UpgradeMode mode;
48

49
  /**
50
   * The constructor.
51
   *
52
   * @param context the {@link IdeContext}.
53
   */
54
  public IdeasyCommandlet(IdeContext context) {
55
    this(context, UpgradeMode.STABLE);
4✔
56
  }
1✔
57

58
  /**
59
   * The constructor.
60
   *
61
   * @param context the {@link IdeContext}.
62
   * @param mode the {@link UpgradeMode}.
63
   */
64
  public IdeasyCommandlet(IdeContext context, UpgradeMode mode) {
65

66
    super(context, TOOL_NAME, ARTIFACT, Set.of(Tag.PRODUCTIVITY, Tag.IDE));
8✔
67
    this.mode = mode;
3✔
68
  }
1✔
69

70
  @Override
71
  public VersionIdentifier getInstalledVersion() {
72

73
    return IdeVersion.getVersionIdentifier();
2✔
74
  }
75

76
  @Override
77
  public String getConfiguredEdition() {
78

79
    return this.tool;
×
80
  }
81

82
  @Override
83
  public VersionIdentifier getConfiguredVersion() {
84

85
    UpgradeMode upgradeMode = this.mode;
×
86
    if (upgradeMode == null) {
×
87
      if (IdeVersion.getVersionString().contains("SNAPSHOT")) {
×
88
        upgradeMode = UpgradeMode.SNAPSHOT;
×
89
      } else {
90
        if (IdeVersion.getVersionIdentifier().getDevelopmentPhase().isStable()) {
×
91
          upgradeMode = UpgradeMode.STABLE;
×
92
        } else {
93
          upgradeMode = UpgradeMode.UNSTABLE;
×
94
        }
95
      }
96
    }
97
    return upgradeMode.getVersion();
×
98
  }
99

100
  @Override
101
  public Path getToolPath() {
102

103
    return this.context.getIdeInstallationPath();
×
104
  }
105

106
  @Override
107
  public boolean install(boolean silent) {
108

109
    if (IdeVersion.isUndefined()) {
2!
110
      this.context.warning("You are using IDEasy version {} which indicates local development - skipping upgrade.", IdeVersion.getVersionString());
10✔
111
      return false;
2✔
112
    }
113
    return super.install(silent);
×
114
  }
115

116
  /**
117
   * @return the latest released {@link VersionIdentifier version} of IDEasy.
118
   */
119
  public VersionIdentifier getLatestVersion() {
120

121
    VersionIdentifier currentVersion = IdeVersion.getVersionIdentifier();
2✔
122
    if (IdeVersion.isUndefined()) {
2!
123
      return currentVersion;
2✔
124
    }
125
    VersionIdentifier configuredVersion = getConfiguredVersion();
×
126
    return getToolRepository().resolveVersion(this.tool, getConfiguredEdition(), configuredVersion, this);
×
127
  }
128

129
  /**
130
   * Checks if an update is available and logs according information.
131
   *
132
   * @return {@code true} if an update is available, {@code false} otherwise.
133
   */
134
  public boolean checkIfUpdateIsAvailable() {
135
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
136
    if (!this.context.isOnline()) {
4✔
137
      this.context.success("Your version of IDEasy is {}.", installedVersion);
10✔
138
      this.context.warning("Skipping check for newer version of IDEasy due to lack of network connectivity.");
4✔
139
      return false;
2✔
140
    }
141
    VersionIdentifier latestVersion = getLatestVersion();
3✔
142
    if (installedVersion.equals(latestVersion)) {
4!
143
      this.context.success("Your version of IDEasy is {} which is the latest released version.", installedVersion);
10✔
144
      return false;
2✔
145
    } else {
146
      this.context.interaction("Your version of IDEasy is {} but version {} is available. Please run the following command to upgrade to the latest version:\n"
×
147
          + "ide upgrade", installedVersion, latestVersion);
148
      return true;
×
149
    }
150
  }
151

152
  /**
153
   * Initial installation of IDEasy.
154
   *
155
   * @param cwd the {@link Path} to the current working directory.
156
   * @see com.devonfw.tools.ide.commandlet.InstallCommandlet
157
   */
158
  public void installIdeasy(Path cwd) {
159
    Path ideRoot = determineIdeRoot(cwd);
4✔
160
    Path idePath = ideRoot.resolve(IdeContext.FOLDER_UNDERSCORE_IDE);
4✔
161
    Path installationPath = idePath.resolve(IdeContext.FOLDER_INSTALLATION);
4✔
162
    Path ideasySoftwarePath = idePath.resolve(IdeContext.FOLDER_SOFTWARE).resolve(MavenRepository.ID).resolve(IdeasyCommandlet.TOOL_NAME)
8✔
163
        .resolve(IdeasyCommandlet.TOOL_NAME);
2✔
164
    Path ideasyVersionPath = ideasySoftwarePath.resolve(IdeVersion.getVersionString());
4✔
165
    if (Files.isDirectory(ideasyVersionPath)) {
5!
166
      throw new CliException("IDEasy is already installed at " + ideasyVersionPath + " - if your installation is broken, delete it manually and rerun setup!");
×
167
    }
168
    FileAccess fileAccess = this.context.getFileAccess();
4✔
169
    List<Path> installationArtifacts = new ArrayList<>();
4✔
170
    boolean success = true;
2✔
171
    success &= addInstallationArtifact(cwd, "bin", true, installationArtifacts);
9✔
172
    success &= addInstallationArtifact(cwd, "functions", true, installationArtifacts);
9✔
173
    success &= addInstallationArtifact(cwd, "internal", true, installationArtifacts);
9✔
174
    success &= addInstallationArtifact(cwd, "system", true, installationArtifacts);
9✔
175
    success &= addInstallationArtifact(cwd, "IDEasy.pdf", true, installationArtifacts);
9✔
176
    success &= addInstallationArtifact(cwd, "setup", true, installationArtifacts);
9✔
177
    success &= addInstallationArtifact(cwd, "setup.bat", false, installationArtifacts);
9✔
178
    if (!success) {
2!
179
      throw new CliException("IDEasy release is inconsistent at " + cwd);
×
180
    }
181
    fileAccess.mkdirs(ideasyVersionPath);
3✔
182
    for (Path installationArtifact : installationArtifacts) {
10✔
183
      fileAccess.copy(installationArtifact, ideasyVersionPath);
4✔
184
    }
1✔
185
    this.context.writeVersionFile(IdeVersion.getVersionIdentifier(), ideasyVersionPath);
5✔
186
    fileAccess.symlink(ideasyVersionPath, installationPath);
4✔
187
    addToShellRc(BASHRC, ideRoot, null);
5✔
188
    addToShellRc(ZSHRC, ideRoot, "autoload -U +X bashcompinit && bashcompinit");
5✔
189
    installIdeasyWindowsEnv(ideRoot, installationPath);
4✔
190
    this.context.success("IDEasy has been installed successfully on your system.");
4✔
191
    this.context.warning("IDEasy has been setup for new shells but it cannot work in your current shell(s).\n"
4✔
192
        + "Reboot or open a new terminal to make it work.");
193
  }
1✔
194

195
  private void installIdeasyWindowsEnv(Path ideRoot, Path installationPath) {
196
    if (!this.context.getSystemInfo().isWindows()) {
5✔
197
      return;
1✔
198
    }
199
    WindowsHelper helper = WindowsHelper.get(this.context);
4✔
200
    helper.setUserEnvironmentValue(IdeVariables.IDE_ROOT.getName(), ideRoot.toString());
6✔
201
    String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
5✔
202
    if (userPath == null) {
2!
203
      this.context.error("Could not read user PATH from registry!");
×
204
    } else {
205
      this.context.info("Found user PATH={}", userPath);
10✔
206
      Path ideasyBinPath = installationPath.resolve("bin");
4✔
207
      SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
4✔
208
      if (path.getEntries().isEmpty()) {
4!
209
        this.context.warning("ATTENTION:\n"
×
210
            + "Your user specific PATH variable seems to be empty.\n"
211
            + "You can double check this by pressing [Windows][r] and launch the program SystemPropertiesAdvanced.\n"
212
            + "Then click on 'Environment variables' and check if 'PATH' is set in the 'user variables' from the upper list.\n"
213
            + "In case 'PATH' is defined there non-empty and you get this message, please abort and give us feedback:\n"
214
            + "https://github.com/devonfw/IDEasy/issues\n"
215
            + "Otherwise all is correct and you can continue.");
216
        this.context.askToContinue("Are you sure you want to override your PATH?");
×
217
      } else {
218
        path.removeEntries(s -> s.endsWith(IDE_BIN));
7✔
219
      }
220
      path.getEntries().add(ideasyBinPath.toString());
6✔
221
      helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), path.toString());
6✔
222
      setGitLongpaths();
2✔
223
    }
224
  }
1✔
225

226
  private void setGitLongpaths() {
227
    GitContext gitContext = new GitContextImpl(this.context);
6✔
228
    gitContext.verifyGitInstalled();
2✔
229
    setGitConfigProperty("core", "longpaths", "true", this.context.getUserHome().resolve(".gitconfig"));
10✔
230
  }
1✔
231

232
  /**
233
   * @param section the section to modify
234
   * @param property the property to set
235
   * @param value the value to set
236
   * @param configPath the path of the config file
237
   */
238
  public void setGitConfigProperty(String section, String property, String value, Path configPath) {
239
    String gitconfig;
240
    // read files
241
    FileAccess fileAccess = this.context.getFileAccess();
4✔
242
    gitconfig = fileAccess.readFileContent(configPath);
4✔
243
    if (gitconfig == null) {
2✔
244
      gitconfig = "";
2✔
245
    }
246

247
    LinkedHashMap<String, LinkedHashMap<String, String>> iniMap = parseIniFile(gitconfig);
4✔
248

249
    // check if section exists
250
    if (!iniMap.containsKey(section)) {
4✔
251
      iniMap.put(section, new LinkedHashMap<>());
7✔
252
    }
253

254
    // set property
255
    iniMap.get(section).put(property, value);
8✔
256

257
    // write out new file
258
    StringBuilder newConfig = new StringBuilder();
4✔
259
    for (String configSection : iniMap.keySet()) {
11✔
260
      newConfig.append(String.format("[%s]\n", configSection));
11✔
261
      LinkedHashMap<String, String> properties = iniMap.get(configSection);
5✔
262
      for (String sectionProperty : properties.keySet()) {
11✔
263
        String propertyValue = properties.get(sectionProperty);
5✔
264
        newConfig.append(String.format("\t%s = %s\n", sectionProperty, propertyValue));
15✔
265
      }
1✔
266
    }
1✔
267

268
    try {
269
      Files.writeString(configPath, newConfig.toString());
7✔
270
    } catch (IOException e) {
×
271
      this.context.error("could not write git config file at %s", configPath);
×
272
    }
1✔
273
  }
1✔
274

275
  private LinkedHashMap<String, LinkedHashMap<String, String>> parseIniFile(String iniFile) {
276
    List<String> iniLines = iniFile.lines().toList();
4✔
277
    LinkedHashMap<String, LinkedHashMap<String, String>> iniMap = new LinkedHashMap<>();
4✔
278
    String currentSection = "";
2✔
279
    for (String line : iniLines) {
10✔
280
      if (line.isEmpty()) {
3!
281
        continue;
×
282
      }
283
      if (line.startsWith("[")) {
4✔
284
        currentSection = line.replace("[", "").replace("]", "");
8✔
285
        iniMap.put(currentSection, new LinkedHashMap<>());
8✔
286
      } else {
287
        String[] parts = line.split("=");
4✔
288
        String propertyName = parts[0].trim();
5✔
289
        String propertyValue = parts[1].trim();
5✔
290
        iniMap.get(currentSection).put(propertyName, propertyValue);
8✔
291
      }
292
    }
1✔
293
    return iniMap;
2✔
294
  }
295

296
  static String removeObsoleteEntryFromWindowsPath(String userPath) {
297
    return removeEntryFromWindowsPath(userPath, IDE_BIN);
4✔
298
  }
299

300
  static String removeEntryFromWindowsPath(String userPath, String suffix) {
301
    int len = userPath.length();
3✔
302
    int start = 0;
2✔
303
    while ((start >= 0) && (start < len)) {
5!
304
      int end = userPath.indexOf(';', start);
5✔
305
      if (end < 0) {
2✔
306
        end = len;
2✔
307
      }
308
      String entry = userPath.substring(start, end);
5✔
309
      if (entry.endsWith(suffix)) {
4✔
310
        String prefix = "";
2✔
311
        int offset = 1;
2✔
312
        if (start > 0) {
2✔
313
          prefix = userPath.substring(0, start - 1);
7✔
314
          offset = 0;
2✔
315
        }
316
        if (end == len) {
3✔
317
          return prefix;
2✔
318
        } else {
319
          return removeEntryFromWindowsPath(prefix + userPath.substring(end + offset), suffix);
10✔
320
        }
321
      }
322
      start = end + 1;
4✔
323
    }
1✔
324
    return userPath;
2✔
325
  }
326

327
  /**
328
   * Adds ourselves to the shell RC (run-commands) configuration file.
329
   *
330
   * @param filename the name of the RC file.
331
   * @param ideRoot the IDE_ROOT {@link Path}.
332
   */
333
  private void addToShellRc(String filename, Path ideRoot, String extraLine) {
334

335
    modifyShellRc(filename, ideRoot, true, extraLine);
6✔
336
  }
1✔
337

338
  private void removeFromShellRc(String filename, Path ideRoot) {
339

340
    modifyShellRc(filename, ideRoot, false, null);
6✔
341
  }
1✔
342

343
  /**
344
   * Adds ourselves to the shell RC (run-commands) configuration file.
345
   *
346
   * @param filename the name of the RC file.
347
   * @param ideRoot the IDE_ROOT {@link Path}.
348
   */
349
  private void modifyShellRc(String filename, Path ideRoot, boolean add, String extraLine) {
350

351
    if (add) {
2✔
352
      this.context.info("Configuring IDEasy in {}", filename);
11✔
353
    } else {
354
      this.context.info("Removing IDEasy from {}", filename);
10✔
355
    }
356
    Path rcFile = this.context.getUserHome().resolve(filename);
6✔
357
    FileAccess fileAccess = this.context.getFileAccess();
4✔
358
    List<String> lines = fileAccess.readFileLines(rcFile);
4✔
359
    if (lines == null) {
2✔
360
      if (!add) {
2!
361
        return;
×
362
      }
363
      lines = new ArrayList<>();
5✔
364
    } else {
365
      // since it is unspecified if the returned List may be immutable we want to get sure
366
      lines = new ArrayList<>(lines);
5✔
367
    }
368
    Iterator<String> iterator = lines.iterator();
3✔
369
    int removeCount = 0;
2✔
370
    while (iterator.hasNext()) {
3✔
371
      String line = iterator.next();
4✔
372
      line = line.trim();
3✔
373
      if (isObsoleteRcLine(line)) {
3✔
374
        this.context.info("Removing obsolete line from {}: {}", filename, line);
14✔
375
        iterator.remove();
2✔
376
        removeCount++;
2✔
377
      } else if (line.equals(extraLine)) {
4✔
378
        extraLine = null;
2✔
379
      }
380
    }
1✔
381
    if (add) {
2✔
382
      if (extraLine != null) {
2!
383
        lines.add(extraLine);
×
384
      }
385
      if (!this.context.getSystemInfo().isWindows()) {
5✔
386
        lines.add("export IDE_ROOT=\"" + WindowsPathSyntax.MSYS.format(ideRoot) + "\"");
7✔
387
      }
388
      lines.add(BASH_CODE_SOURCE_FUNCTIONS);
4✔
389
    }
390
    fileAccess.writeFileLines(lines, rcFile);
4✔
391
    this.context.debug("Successfully updated {}", filename);
10✔
392
  }
1✔
393

394
  private static boolean isObsoleteRcLine(String line) {
395
    if (line.startsWith("alias ide=")) {
4!
396
      return true;
×
397
    } else if (line.startsWith("export IDE_ROOT=")) {
4!
398
      return true;
×
399
    } else if (line.equals("ide")) {
4✔
400
      return true;
2✔
401
    } else if (line.equals("ide init")) {
4✔
402
      return true;
2✔
403
    } else if (line.startsWith("source \"$IDE_ROOT/_ide/")) {
4✔
404
      return true;
2✔
405
    }
406
    return false;
2✔
407
  }
408

409
  private boolean addInstallationArtifact(Path cwd, String artifactName, boolean required, List<Path> installationArtifacts) {
410

411
    Path artifactPath = cwd.resolve(artifactName);
4✔
412
    if (Files.exists(artifactPath)) {
5!
413
      installationArtifacts.add(artifactPath);
5✔
414
    } else if (required) {
×
415
      this.context.error("Missing required file {}", artifactName);
×
416
      return false;
×
417
    }
418
    return true;
2✔
419
  }
420

421
  private Path determineIdeRoot(Path cwd) {
422
    Path ideRoot = this.context.getIdeRoot();
4✔
423
    if (ideRoot == null) {
2!
424
      Path home = this.context.getUserHome();
4✔
425
      Path installRoot = home;
2✔
426
      if (this.context.getSystemInfo().isWindows()) {
5✔
427
        if (!cwd.startsWith(home)) {
4!
428
          installRoot = cwd.getRoot();
×
429
        }
430
      }
431
      ideRoot = installRoot.resolve(IdeContext.FOLDER_PROJECTS);
4✔
432
    } else {
1✔
433
      assert (Files.isDirectory(ideRoot)) : "IDE_ROOT directory does not exist!";
×
434
    }
435
    return ideRoot;
2✔
436
  }
437

438
  /**
439
   * Uninstalls IDEasy entirely from the system.
440
   */
441
  public void uninstallIdeasy() {
442

443
    Path ideRoot = this.context.getIdeRoot();
4✔
444
    removeFromShellRc(BASHRC, ideRoot);
4✔
445
    removeFromShellRc(ZSHRC, ideRoot);
4✔
446
    Path idePath = this.context.getIdePath();
4✔
447
    uninstallIdeasyWindowsEnv(ideRoot);
3✔
448
    uninstallIdeasyIdePath(idePath);
3✔
449
    deleteDownloadCache();
2✔
450
    this.context.success("IDEasy has been uninstalled from your system.");
4✔
451
    this.context.interaction("ATTENTION:\n"
10✔
452
        + "In order to prevent data-loss, we do not delete your projects and git repositories!\n"
453
        + "To entirely get rid of IDEasy, also check your IDE_ROOT folder at:\n"
454
        + "{}", ideRoot);
455
  }
1✔
456

457
  private void deleteDownloadCache() {
458
    Path downloadPath = this.context.getDownloadPath();
4✔
459
    this.context.info("Deleting download cache from {}", downloadPath);
10✔
460
    this.context.getFileAccess().delete(downloadPath);
5✔
461
  }
1✔
462

463
  private void uninstallIdeasyIdePath(Path idePath) {
464
    if (this.context.getSystemInfo().isWindows()) {
5✔
465
      this.context.newProcess().executable("bash").addArgs("-c",
17✔
466
          "sleep 10 && rm -rf \"" + WindowsPathSyntax.MSYS.format(idePath) + "\"").run(ProcessMode.BACKGROUND);
5✔
467
      this.context.interaction("To prevent windows file locking errors, we perform an asynchronous deletion of {} in background now.\n"
11✔
468
          + "Please close all terminals and wait a minute for the deletion to complete before running other commands.", idePath);
469
    } else {
470
      this.context.info("Finally deleting {}", idePath);
10✔
471
      this.context.getFileAccess().delete(idePath);
5✔
472
    }
473
  }
1✔
474

475
  private void uninstallIdeasyWindowsEnv(Path ideRoot) {
476
    if (!this.context.getSystemInfo().isWindows()) {
5✔
477
      return;
1✔
478
    }
479
    WindowsHelper helper = WindowsHelper.get(this.context);
4✔
480
    helper.removeUserEnvironmentValue(IdeVariables.IDE_ROOT.getName());
4✔
481
    String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
5✔
482
    if (userPath == null) {
2!
483
      this.context.error("Could not read user PATH from registry!");
×
484
    } else {
485
      this.context.info("Found user PATH={}", userPath);
10✔
486
      String newUserPath = userPath;
2✔
487
      if (!userPath.isEmpty()) {
3!
488
        SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
4✔
489
        path.removeEntries(s -> s.endsWith(IDE_BIN) || s.endsWith(IDE_INSTALLATION_BIN));
15!
490
        newUserPath = path.toString();
3✔
491
      }
492
      if (newUserPath.equals(userPath)) {
4!
493
        this.context.error("Could not find IDEasy in PATH:\n{}", userPath);
×
494
      } else {
495
        helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), newUserPath);
5✔
496
      }
497
    }
498
  }
1✔
499
}
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