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

devonfw / IDEasy / 15490858611

06 Jun 2025 12:45PM UTC coverage: 67.734% (-0.008%) from 67.742%
15490858611

push

github

web-flow
#1293: ensure core longpaths on windows (#1359)

Fixes issues of #1315

3160 of 5070 branches covered (62.33%)

Branch coverage included in aggregate %.

8111 of 11570 relevant lines covered (70.1%)

3.07 hits per line

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

81.77
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
    VersionIdentifier latestVersion = getLatestVersion();
3✔
137
    if (installedVersion.equals(latestVersion)) {
4!
138
      this.context.success("Your version of IDEasy is {} which is the latest released version.", installedVersion);
10✔
139
      return false;
2✔
140
    } else {
141
      this.context.interaction("Your version of IDEasy is {} but version {} is available. Please run the following command to upgrade to the latest version:\n"
×
142
          + "ide upgrade", installedVersion, latestVersion);
143
      return true;
×
144
    }
145
  }
146

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

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

221
  private void setGitLongpaths() {
222
    GitContext gitContext = new GitContextImpl(this.context);
6✔
223
    gitContext.verifyGitInstalled();
2✔
224
    setGitConfigProperty("core", "longpaths", "true", this.context.getUserHome().resolve(".gitconfig"));
10✔
225
  }
1✔
226

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

242
    LinkedHashMap<String, LinkedHashMap<String, String>> iniMap = parseIniFile(gitconfig);
4✔
243

244
    // check if section exists
245
    if (!iniMap.containsKey(section)) {
4✔
246
      iniMap.put(section, new LinkedHashMap<>());
7✔
247
    }
248

249
    // set property
250
    iniMap.get(section).put(property, value);
8✔
251

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

263
    try {
264
      Files.writeString(configPath, newConfig.toString());
7✔
265
    } catch (IOException e) {
×
266
      this.context.error("could not write git config file at %s", configPath);
×
267
    }
1✔
268
  }
1✔
269

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

291
  static String removeObsoleteEntryFromWindowsPath(String userPath) {
292
    return removeEntryFromWindowsPath(userPath, IDE_BIN);
4✔
293
  }
294

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

322
  /**
323
   * Adds ourselves to the shell RC (run-commands) configuration file.
324
   *
325
   * @param filename the name of the RC file.
326
   * @param ideRoot the IDE_ROOT {@link Path}.
327
   */
328
  private void addToShellRc(String filename, Path ideRoot, String extraLine) {
329

330
    modifyShellRc(filename, ideRoot, true, extraLine);
6✔
331
  }
1✔
332

333
  private void removeFromShellRc(String filename, Path ideRoot) {
334

335
    modifyShellRc(filename, ideRoot, false, null);
6✔
336
  }
1✔
337

338
  /**
339
   * Adds ourselves to the shell RC (run-commands) configuration file.
340
   *
341
   * @param filename the name of the RC file.
342
   * @param ideRoot the IDE_ROOT {@link Path}.
343
   */
344
  private void modifyShellRc(String filename, Path ideRoot, boolean add, String extraLine) {
345

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

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

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

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

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

433
  /**
434
   * Uninstalls IDEasy entirely from the system.
435
   */
436
  public void uninstallIdeasy() {
437

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

452
  private void deleteDownloadCache() {
453
    Path downloadPath = this.context.getDownloadPath();
4✔
454
    this.context.info("Deleting download cache from {}", downloadPath);
10✔
455
    this.context.getFileAccess().delete(downloadPath);
5✔
456
  }
1✔
457

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

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