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

devonfw / IDEasy / 26654875090

29 May 2026 06:27PM UTC coverage: 71.09% (-0.01%) from 71.101%
26654875090

Pull #1962

github

web-flow
Merge 81dd1b1f7 into dc1835d82
Pull Request #1962: #1255: Enhance snapshot version recognition in IDEasy

4506 of 7022 branches covered (64.17%)

Branch coverage included in aggregate %.

11684 of 15752 relevant lines covered (74.17%)

3.14 hits per line

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

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

3
import java.io.Reader;
4
import java.io.Writer;
5
import java.nio.file.Files;
6
import java.nio.file.Path;
7
import java.util.ArrayList;
8
import java.util.Iterator;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Set;
12
import java.util.regex.Matcher;
13
import java.util.regex.Pattern;
14

15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17

18
import com.devonfw.tools.ide.cli.CliException;
19
import com.devonfw.tools.ide.commandlet.UpgradeMode;
20
import com.devonfw.tools.ide.common.SimpleSystemPath;
21
import com.devonfw.tools.ide.common.Tag;
22
import com.devonfw.tools.ide.context.IdeContext;
23
import com.devonfw.tools.ide.io.FileAccess;
24
import com.devonfw.tools.ide.io.ini.IniFile;
25
import com.devonfw.tools.ide.io.ini.IniSection;
26
import com.devonfw.tools.ide.log.IdeLogLevel;
27
import com.devonfw.tools.ide.os.WindowsHelper;
28
import com.devonfw.tools.ide.os.WindowsPathSyntax;
29
import com.devonfw.tools.ide.process.ProcessMode;
30
import com.devonfw.tools.ide.process.ProcessResult;
31
import com.devonfw.tools.ide.tool.mvn.MvnArtifact;
32
import com.devonfw.tools.ide.tool.mvn.MvnBasedLocalToolCommandlet;
33
import com.devonfw.tools.ide.tool.mvn.MvnRepository;
34
import com.devonfw.tools.ide.variable.IdeVariables;
35
import com.devonfw.tools.ide.version.IdeVersion;
36
import com.devonfw.tools.ide.version.VersionIdentifier;
37
import com.fasterxml.jackson.databind.JsonNode;
38
import com.fasterxml.jackson.databind.ObjectMapper;
39
import com.fasterxml.jackson.databind.node.ArrayNode;
40
import com.fasterxml.jackson.databind.node.ObjectNode;
41

42
/**
43
 * {@link MvnBasedLocalToolCommandlet} for IDEasy (ide-cli).
44
 */
45
public class IdeasyCommandlet extends MvnBasedLocalToolCommandlet {
46

47
  private static final Logger LOG = LoggerFactory.getLogger(IdeasyCommandlet.class);
3✔
48

49
  /** The {@link MvnArtifact} for IDEasy. */
50
  public static final MvnArtifact ARTIFACT = MvnArtifact.ofIdeasyCli("*!", "tar.gz", "${os}-${arch}");
5✔
51

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

54
  /** The {@link #getName() tool name}. */
55
  public static final String TOOL_NAME = "ideasy";
56
  public static final String BASHRC = ".bashrc";
57
  public static final String ZSHRC = ".zshrc";
58
  public static final String IDE_BIN = "\\_ide\\bin";
59
  public static final String IDE_INSTALLATION_BIN = "\\_ide\\installation\\bin";
60

61

62
  private static final Map<String, Boolean> REQUIRED_INSTALLATION_ARTIFACTS = Map.of(
4✔
63
      //artifactName: String, required: boolean
64
      "bin", true,
3✔
65
      "functions", true,
3✔
66
      "internal", true,
3✔
67
      "gui", true,
3✔
68
      "system", true,
3✔
69
      "IDEasy.pdf", true,
3✔
70
      "setup", true,
3✔
71
      "setup.bat", false
1✔
72
  );
73

74
  private final UpgradeMode mode;
75

76
  /** Pattern for IDEasy SNAPSHOT versions built locally. */
77
  // ..............................................................................1..........................2........3........4
78
  private static final Pattern PATTERN_IDEASY_SNAPSHOT_VERSION = Pattern.compile("^(\\d{4}\\.\\d{2}\\.\\d{3})-(\\d{2})_(\\d{2})_(\\d{2}).*-SNAPSHOT$");
3✔
79

80
  /** Pattern for Maven/Nexus SNAPSHOT versions from downloads . */
81
  // .............................................................................1..........................2.......3.......4..........5
82
  private static final Pattern PATTERN_MAVEN_SNAPSHOT_VERSION = Pattern.compile("^(\\d{4}\\.\\d{2}\\.\\d{3})-(\\d{4})(\\d{2})(\\d{2})\\.(\\d{2})\\d{4}.*$");
4✔
83

84
  // Group numbers for PATTERN_IDEASY_SNAPSHOT_VERSION
85
  private static final int GROUP_IDEASY_BASE = 1;
86
  private static final int GROUP_IDEASY_MONTH = 2;
87
  private static final int GROUP_IDEASY_DAY = 3;
88
  private static final int GROUP_IDEASY_HOUR = 4;
89

90
  // Group numbers for PATTERN_MAVEN_SNAPSHOT_VERSION
91
  private static final int GROUP_MAVEN_BASE = 1;
92
  private static final int GROUP_MAVEN_YEAR = 2;
93
  private static final int GROUP_MAVEN_MONTH = 3;
94
  private static final int GROUP_MAVEN_DAY = 4;
95
  private static final int GROUP_MAVEN_HOUR = 5;
96

97
  /**
98
   * The constructor.
99
   *
100
   * @param context the {@link IdeContext}.
101
   */
102
  public IdeasyCommandlet(IdeContext context) {
103
    this(context, UpgradeMode.STABLE);
4✔
104
  }
1✔
105

106
  /**
107
   * The constructor.
108
   *
109
   * @param context the {@link IdeContext}.
110
   * @param mode the {@link UpgradeMode}.
111
   */
112
  public IdeasyCommandlet(IdeContext context, UpgradeMode mode) {
113

114
    super(context, TOOL_NAME, ARTIFACT, Set.of(Tag.PRODUCTIVITY, Tag.IDE));
8✔
115
    this.mode = mode;
3✔
116
  }
1✔
117

118
  @Override
119
  public VersionIdentifier getInstalledVersion() {
120

121
    return IdeVersion.getVersionIdentifier();
2✔
122
  }
123

124
  @Override
125
  public String getInstalledEdition() {
126

127
    return this.tool;
3✔
128
  }
129

130
  @Override
131
  public String getConfiguredEdition() {
132

133
    return this.tool;
3✔
134
  }
135

136
  @Override
137
  public VersionIdentifier getConfiguredVersion() {
138

139
    UpgradeMode upgradeMode = this.mode;
3✔
140
    if (upgradeMode == null) {
2!
141
      if (IdeVersion.isSnapshot()) {
2!
142
        upgradeMode = UpgradeMode.SNAPSHOT;
3✔
143
      } else {
144
        if (IdeVersion.getVersionIdentifier().getDevelopmentPhase().isStable()) {
×
145
          upgradeMode = UpgradeMode.STABLE;
×
146
        } else {
147
          upgradeMode = UpgradeMode.UNSTABLE;
×
148
        }
149
      }
150
    }
151
    return upgradeMode.getVersion();
3✔
152
  }
153

154
  @Override
155
  public Path getToolPath() {
156

157
    return this.context.getIdeInstallationPath();
4✔
158
  }
159

160
  @Override
161
  protected ToolInstallation doInstall(ToolInstallRequest request) {
162

163
    this.context.requireOnline("upgrade of IDEasy", true);
5✔
164

165
    if (IdeVersion.isUndefined() && !this.context.isForceMode()) {
6!
166
      VersionIdentifier version = IdeVersion.getVersionIdentifier();
2✔
167
      LOG.warn("You are using IDEasy version {} which indicates local development - skipping upgrade.", version);
4✔
168
      return toolAlreadyInstalled(request);
4✔
169
    }
170
    return super.doInstall(request);
×
171
  }
172

173
  /**
174
   * @return the latest released {@link VersionIdentifier version} of IDEasy.
175
   */
176
  public VersionIdentifier getLatestVersion() {
177

178
    if (!this.context.isForceMode()) {
4!
179
      VersionIdentifier currentVersion = IdeVersion.getVersionIdentifier();
2✔
180
      if (IdeVersion.isUndefined()) {
2!
181
        return currentVersion;
2✔
182
      }
183
    }
184
    VersionIdentifier configuredVersion = getConfiguredVersion();
×
185
    return getToolRepository().resolveVersion(this.tool, getConfiguredEdition(), configuredVersion, this);
×
186
  }
187

188
  /**
189
   * Checks if an update is available and logs according information.
190
   *
191
   * @return {@code true} if an update is available, {@code false} otherwise.
192
   */
193
  public boolean checkIfUpdateIsAvailable() {
194
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
195
    IdeLogLevel.SUCCESS.log(LOG, "Your version of IDEasy is {}.", installedVersion);
10✔
196
    if (IdeVersion.isSnapshot()) {
2!
197
      LOG.warn("You are using a SNAPSHOT version of IDEasy. For stability consider switching to a stable release via 'ide upgrade --mode=stable'");
3✔
198
    }
199
    if (this.context.isOffline()) {
4✔
200
      LOG.warn("Skipping check for newer version of IDEasy because you are offline.");
3✔
201
      return false;
2✔
202
    }
203
    VersionIdentifier latestVersion = getLatestVersion();
3✔
204
    if (IdeVersion.isSnapshot()) {
2!
205
      if (isSameSnapshotVersion(installedVersion.toString(), latestVersion.toString())) {
7✔
206
        IdeLogLevel.SUCCESS.log(LOG, "Your are using the latest snapshot version of IDEasy and no update is available.");
4✔
207
        return false;
2✔
208
      }
209
    } else if (installedVersion.equals(latestVersion)) {
×
210
      IdeLogLevel.SUCCESS.log(LOG, "Your are using the latest stable version of IDEasy and no update is available.");
×
211
      return false;
×
212
    }
213
    IdeLogLevel.INTERACTION.log(LOG,
14✔
214
        "Your version of IDEasy is {} but version {} is available. Please run the following command to upgrade to the latest version:\n"
215
            + "ide upgrade", installedVersion, latestVersion);
216
    return true;
2✔
217
  }
218

219
  /**
220
   * Checks if two snapshot versions represent the version
221
   *
222
   * @param installed the installed version string
223
   * @param latest the latest available version string
224
   * @return {@code true} if both versions represent the same version, {@code false} otherwise.
225
   */
226
  private boolean isSameSnapshotVersion(String installed, String latest) {
227
    if (installed == null || latest == null) {
4!
228
      return false;
×
229
    }
230

231
    Matcher installedMatcher = PATTERN_IDEASY_SNAPSHOT_VERSION.matcher(installed);
4✔
232
    Matcher latestMatcher = PATTERN_MAVEN_SNAPSHOT_VERSION.matcher(latest);
4✔
233

234
    if (!installedMatcher.matches() || !latestMatcher.matches()) {
6!
235
      return false;
2✔
236
    }
237

238
    // Compare base versions
239
    String baseInstalled = installedMatcher.group(GROUP_IDEASY_BASE);
4✔
240
    String baseLatest = latestMatcher.group(GROUP_MAVEN_BASE);
4✔
241
    if (!baseInstalled.equals(baseLatest)) {
4✔
242
      return false;
2✔
243
    }
244

245
    // Compare year
246
    String yearLatest = latestMatcher.group(GROUP_MAVEN_YEAR);
4✔
247
    String baseYear = baseInstalled.split("\\.")[0];
6✔
248
    if (!baseYear.equals(yearLatest)) {
4!
249
      return false;
×
250
    }
251

252
    // Compare MMDD.HH for both versions
253
    String keyInstalled =
2✔
254
        installedMatcher.group(GROUP_IDEASY_MONTH) + installedMatcher.group(GROUP_IDEASY_DAY) + "." + installedMatcher.group(GROUP_IDEASY_HOUR);
9✔
255
    String keyLatest = latestMatcher.group(GROUP_MAVEN_MONTH) + latestMatcher.group(GROUP_MAVEN_DAY) + "." + latestMatcher.group(GROUP_MAVEN_HOUR);
11✔
256

257
    return keyInstalled.equals(keyLatest);
4✔
258
  }
259

260
  /**
261
   * Initial installation of IDEasy.
262
   *
263
   * @param cwd the {@link Path} to the current working directory.
264
   * @see com.devonfw.tools.ide.commandlet.InstallCommandlet
265
   */
266
  public void installIdeasy(Path cwd) {
267
    Path ideRoot = determineIdeRoot(cwd);
4✔
268
    Path idePath = ideRoot.resolve(IdeContext.FOLDER_UNDERSCORE_IDE);
4✔
269
    Path installationPath = idePath.resolve(IdeContext.FOLDER_INSTALLATION);
4✔
270
    Path ideasySoftwarePath = idePath.resolve(IdeContext.FOLDER_SOFTWARE).resolve(MvnRepository.ID).resolve(IdeasyCommandlet.TOOL_NAME)
8✔
271
        .resolve(IdeasyCommandlet.TOOL_NAME);
2✔
272
    Path ideasyVersionPath = ideasySoftwarePath.resolve(IdeVersion.getVersionString());
4✔
273
    FileAccess fileAccess = this.context.getFileAccess();
4✔
274
    if (Files.isDirectory(ideasyVersionPath)) {
5✔
275
      LOG.error("IDEasy is already installed at {} - if your installation is broken, delete it manually and rerun setup!", ideasyVersionPath);
5✔
276
    } else {
277
      List<Path> installationArtifacts = new ArrayList<>();
4✔
278
      for (Map.Entry<String, Boolean> artifactEntry : REQUIRED_INSTALLATION_ARTIFACTS.entrySet()) {
11✔
279
        String artifactName = artifactEntry.getKey();
4✔
280
        boolean required = artifactEntry.getValue();
5✔
281
        boolean success = addInstallationArtifact(cwd, artifactName, required, installationArtifacts);
7✔
282
        if (!success) {
2!
283
          throw new CliException("IDEasy release is inconsistent at %s [artifact=%s]".formatted(cwd, artifactName));
×
284
        }
285
      }
1✔
286

287
      fileAccess.mkdirs(ideasyVersionPath);
3✔
288
      for (Path installationArtifact : installationArtifacts) {
10✔
289
        fileAccess.copy(installationArtifact, ideasyVersionPath);
4✔
290
      }
1✔
291
      this.context.writeVersionFile(IdeVersion.getVersionIdentifier(), ideasyVersionPath);
5✔
292
    }
293
    fileAccess.symlink(ideasyVersionPath, installationPath);
4✔
294
    addToShellRc(BASHRC, ideRoot, null);
5✔
295
    addToShellRc(ZSHRC, ideRoot, "autoload -U +X bashcompinit && bashcompinit");
5✔
296
    installIdeasyWindowsEnv(ideRoot, installationPath);
4✔
297
    IdeLogLevel.SUCCESS.log(LOG, "IDEasy has been installed successfully on your system.");
4✔
298
    LOG.warn("IDEasy has been setup for new shells but it cannot work in your current shell(s).\n"
3✔
299
        + "To use it here, run 'source ~/.bashrc' (or your shell config). Otherwise, open a new terminal or reboot.");
300
  }
1✔
301

302
  private void installIdeasyWindowsEnv(Path ideRoot, Path installationPath) {
303
    if (!this.context.getSystemInfo().isWindows()) {
5✔
304
      return;
1✔
305
    }
306
    WindowsHelper helper = WindowsHelper.get(this.context);
4✔
307
    helper.setUserEnvironmentValue(IdeVariables.IDE_ROOT.getName(), ideRoot.toString());
6✔
308
    String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
5✔
309
    if (userPath == null) {
2!
310
      LOG.error("Could not read user PATH from registry!");
×
311
    } else {
312
      LOG.info("Found user PATH={}", userPath);
4✔
313
      Path ideasyBinPath = installationPath.resolve("bin");
4✔
314
      SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
4✔
315
      if (path.getEntries().isEmpty()) {
4!
316
        LOG.warn("ATTENTION:\n"
×
317
            + "Your user specific PATH variable seems to be empty.\n"
318
            + "You can double check this by pressing [Windows][r] and launch the program SystemPropertiesAdvanced.\n"
319
            + "Then click on 'Environment variables' and check if 'PATH' is set in the 'user variables' from the upper list.\n"
320
            + "In case 'PATH' is defined there non-empty and you get this message, please abort and give us feedback:\n"
321
            + "https://github.com/devonfw/IDEasy/issues\n"
322
            + "Otherwise all is correct and you can continue.");
323
        this.context.askToContinue("Are you sure you want to override your PATH?");
×
324
      } else {
325
        path.removeEntries(s -> s.endsWith(IDE_INSTALLATION_BIN));
7✔
326
      }
327
      path.getEntries().add(ideasyBinPath.toString());
6✔
328
      helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), path.toString());
6✔
329
      setGitLongpaths();
2✔
330
    }
331
  }
1✔
332

333
  private void setGitLongpaths() {
334
    this.context.getGitContext().findGitRequired();
5✔
335
    Path configPath = this.context.getUserHome().resolve(".gitconfig");
6✔
336
    FileAccess fileAccess = this.context.getFileAccess();
4✔
337
    IniFile iniFile = fileAccess.readIniFile(configPath);
4✔
338
    IniSection coreSection = iniFile.getOrCreateSection("core");
4✔
339
    coreSection.setProperty("longpaths", "true");
4✔
340
    fileAccess.writeIniFile(iniFile, configPath);
4✔
341
  }
1✔
342

343
  /**
344
   * Sets up Windows Terminal with Git Bash integration.
345
   */
346
  public void setupWindowsTerminal() {
347
    if (!this.context.getSystemInfo().isWindows()) {
×
348
      return;
×
349
    }
350
    if (!isWindowsTerminalInstalled()) {
×
351
      try {
352
        installWindowsTerminal();
×
353
      } catch (Exception e) {
×
354
        LOG.error("Failed to install Windows Terminal!", e);
×
355
      }
×
356
    }
357
    configureWindowsTerminalGitBash();
×
358
  }
×
359

360
  /**
361
   * Checks if Windows Terminal is installed.
362
   *
363
   * @return {@code true} if Windows Terminal is installed, {@code false} otherwise.
364
   */
365
  private boolean isWindowsTerminalInstalled() {
366
    try {
367
      ProcessResult result = this.context.newProcess()
×
368
          .executable("powershell")
×
369
          .addArgs("-Command", "Get-AppxPackage -Name Microsoft.WindowsTerminal")
×
370
          .run(ProcessMode.DEFAULT_CAPTURE);
×
371
      return result.isSuccessful() && !result.getOut().isEmpty();
×
372
    } catch (Exception e) {
×
373
      LOG.debug("Failed to check Windows Terminal installation.", e);
×
374
      return false;
×
375
    }
376
  }
377

378
  /**
379
   * Installs Windows Terminal using winget.
380
   */
381
  private void installWindowsTerminal() {
382
    try {
383
      LOG.info("Installing Windows Terminal...");
×
384
      ProcessResult result = this.context.newProcess()
×
385
          .executable("winget")
×
386
          .addArgs("install", "Microsoft.WindowsTerminal")
×
387
          .run(ProcessMode.DEFAULT);
×
388
      if (result.isSuccessful()) {
×
389
        IdeLogLevel.SUCCESS.log(LOG, "Windows Terminal has been installed successfully.");
×
390
      } else {
391
        LOG.warn("Failed to install Windows Terminal. Please install it manually from Microsoft Store.");
×
392
      }
393
    } catch (Exception e) {
×
394
      LOG.warn("Failed to install Windows Terminal: {}. Please install it manually from Microsoft Store.", e.toString());
×
395
    }
×
396
  }
×
397

398
  /**
399
   * Configures Git Bash integration in Windows Terminal.
400
   */
401
  protected void configureWindowsTerminalGitBash() {
402
    Path settingsPath = getWindowsTerminalSettingsPath();
3✔
403
    if (settingsPath == null || !Files.exists(settingsPath)) {
7!
404
      LOG.warn("Windows Terminal settings file not found. Cannot configure Git Bash integration.");
×
405
      return;
×
406
    }
407

408
    try {
409
      Path bashPath = this.context.findBash();
4✔
410
      if (bashPath == null) {
2!
411
        LOG.warn("Git Bash not found. Cannot configure Windows Terminal integration.");
×
412
        return;
×
413
      }
414

415
      configureGitBashProfile(settingsPath, bashPath.toString());
5✔
416
      IdeLogLevel.SUCCESS.log(LOG, "Git Bash has been configured in Windows Terminal.");
4✔
417
    } catch (Exception e) {
×
418
      LOG.warn("Failed to configure Git Bash in Windows Terminal: {}", e.getMessage());
×
419
    }
1✔
420
  }
1✔
421

422
  /**
423
   * Gets the Windows Terminal settings file path.
424
   *
425
   * @return the {@link Path} to the Windows Terminal settings file, or {@code null} if not found.
426
   */
427
  protected Path getWindowsTerminalSettingsPath() {
428
    Path localAppData = this.context.getUserHome().resolve("AppData").resolve("Local");
8✔
429
    Path packagesPath = localAppData.resolve("Packages");
4✔
430

431
    // Try the new Windows Terminal package first
432
    Path newTerminalPath = packagesPath.resolve("Microsoft.WindowsTerminal_8wekyb3d8bbwe")
4✔
433
        .resolve("LocalState").resolve("settings.json");
4✔
434
    if (Files.exists(newTerminalPath)) {
5!
435
      return newTerminalPath;
2✔
436
    }
437

438
    // Try the old Windows Terminal Preview package
439
    Path previewPath = packagesPath.resolve("Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe")
×
440
        .resolve("LocalState").resolve("settings.json");
×
441
    if (Files.exists(previewPath)) {
×
442
      return previewPath;
×
443
    }
444

445
    return null;
×
446
  }
447

448
  /**
449
   * Configures Git Bash profile in Windows Terminal settings.
450
   *
451
   * @param settingsPath the {@link Path} to the Windows Terminal settings file.
452
   * @param bashPath the path to the Git Bash executable.
453
   */
454
  private void configureGitBashProfile(Path settingsPath, String bashPath) throws Exception {
455

456
    ObjectMapper mapper = new ObjectMapper();
4✔
457
    ObjectNode root;
458
    try (Reader reader = Files.newBufferedReader(settingsPath)) {
3✔
459
      JsonNode rootNode = mapper.readTree(reader);
4✔
460
      root = (ObjectNode) rootNode;
3✔
461
    }
462

463
    // Get or create profiles object
464
    ObjectNode profiles = (ObjectNode) root.get("profiles");
5✔
465
    if (profiles == null) {
2!
466
      profiles = mapper.createObjectNode();
×
467
      root.set("profiles", profiles);
×
468
    }
469

470
    // Get or create list array of profiles object
471
    JsonNode profilesList = profiles.get("list");
4✔
472
    if (profilesList == null || !profilesList.isArray()) {
5!
473
      profilesList = mapper.createArrayNode();
×
474
      profiles.set("list", profilesList);
×
475
    }
476

477
    // Check if Git Bash profile already exists
478
    boolean gitBashProfileExists = false;
2✔
479
    for (JsonNode profile : profilesList) {
10✔
480
      if (profile.has("name") && profile.get("name").asText().equals("Git Bash")) {
11!
481
        gitBashProfileExists = true;
×
482
        break;
×
483
      }
484
    }
1✔
485

486
    // Add Git Bash profile if it doesn't exist
487
    if (gitBashProfileExists) {
2!
488
      LOG.info("Git Bash profile already exists in {}.", settingsPath);
×
489
    } else {
490
      ObjectNode gitBashProfile = mapper.createObjectNode();
3✔
491
      String newGuid = "{2ece5bfe-50ed-5f3a-ab87-5cd4baafed2b}";
2✔
492
      String iconPath = getGitBashIconPath(bashPath);
4✔
493
      String startingDirectory = this.context.getIdeRoot().toString();
5✔
494

495
      gitBashProfile.put("guid", newGuid);
5✔
496
      gitBashProfile.put("name", "Git Bash");
5✔
497
      gitBashProfile.put("commandline", bashPath);
5✔
498
      gitBashProfile.put("icon", iconPath);
5✔
499
      gitBashProfile.put("startingDirectory", startingDirectory);
5✔
500

501
      ((ArrayNode) profilesList).add(gitBashProfile);
5✔
502

503
      // Set Git Bash as default profile
504
      root.put("defaultProfile", newGuid);
5✔
505

506
      // Write back to file
507
      try (Writer writer = Files.newBufferedWriter(settingsPath)) {
5✔
508
        mapper.writerWithDefaultPrettyPrinter().writeValue(writer, root);
5✔
509
      }
510
    }
511
  }
1✔
512

513
  private String getGitBashIconPath(String bashPathString) {
514
    Path bashPath = Path.of(bashPathString);
5✔
515
    // "C:\\Program Files\\Git\\bin\\bash.exe"
516
    // "C:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico"
517
    Path parent = bashPath.getParent();
3✔
518
    if (parent != null) {
2!
519
      Path iconPath = parent.resolve("mingw64/share/git/git-for-windows.ico");
4✔
520
      if (Files.exists(iconPath)) {
5!
521
        LOG.debug("Found git-bash icon at {}", iconPath);
×
522
        return iconPath.toString();
×
523
      }
524
      LOG.debug("Git Bash icon not found at {}. Using default icon.", iconPath);
4✔
525
    }
526
    return "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png";
2✔
527
  }
528

529
  static String removeObsoleteEntryFromWindowsPath(String userPath) {
530
    return removeEntryFromWindowsPath(userPath, IDE_BIN);
4✔
531
  }
532

533
  static String removeEntryFromWindowsPath(String userPath, String suffix) {
534
    int len = userPath.length();
3✔
535
    int start = 0;
2✔
536
    while ((start >= 0) && (start < len)) {
5!
537
      int end = userPath.indexOf(';', start);
5✔
538
      if (end < 0) {
2✔
539
        end = len;
2✔
540
      }
541
      String entry = userPath.substring(start, end);
5✔
542
      if (entry.endsWith(suffix)) {
4✔
543
        String prefix = "";
2✔
544
        int offset = 1;
2✔
545
        if (start > 0) {
2✔
546
          prefix = userPath.substring(0, start - 1);
7✔
547
          offset = 0;
2✔
548
        }
549
        if (end == len) {
3✔
550
          return prefix;
2✔
551
        } else {
552
          return removeEntryFromWindowsPath(prefix + userPath.substring(end + offset), suffix);
10✔
553
        }
554
      }
555
      start = end + 1;
4✔
556
    }
1✔
557
    return userPath;
2✔
558
  }
559

560
  /**
561
   * Adds ourselves to the shell RC (run-commands) configuration file.
562
   *
563
   * @param filename the name of the RC file.
564
   * @param ideRoot the IDE_ROOT {@link Path}.
565
   */
566
  private void addToShellRc(String filename, Path ideRoot, String extraLine) {
567

568
    modifyShellRc(filename, ideRoot, true, extraLine);
6✔
569
  }
1✔
570

571
  private void removeFromShellRc(String filename, Path ideRoot) {
572

573
    modifyShellRc(filename, ideRoot, false, null);
6✔
574
  }
1✔
575

576
  /**
577
   * Adds ourselves to the shell RC (run-commands) configuration file.
578
   *
579
   * @param filename the name of the RC file.
580
   * @param ideRoot the IDE_ROOT {@link Path}.
581
   */
582
  private void modifyShellRc(String filename, Path ideRoot, boolean add, String extraLine) {
583

584
    if (add) {
2✔
585
      LOG.info("Configuring IDEasy in {}", filename);
5✔
586
    } else {
587
      LOG.info("Removing IDEasy from {}", filename);
4✔
588
    }
589
    Path rcFile = this.context.getUserHome().resolve(filename);
6✔
590
    FileAccess fileAccess = this.context.getFileAccess();
4✔
591
    List<String> lines = fileAccess.readFileLines(rcFile);
4✔
592
    if (lines == null) {
2✔
593
      if (!add) {
2!
594
        return;
×
595
      }
596
      lines = new ArrayList<>();
5✔
597
    } else {
598
      // since it is unspecified if the returned List may be immutable we want to get sure
599
      lines = new ArrayList<>(lines);
5✔
600
    }
601
    Iterator<String> iterator = lines.iterator();
3✔
602
    int removeCount = 0;
2✔
603
    while (iterator.hasNext()) {
3✔
604
      String line = iterator.next();
4✔
605
      line = line.trim();
3✔
606
      if (isObsoleteRcLine(line)) {
3✔
607
        LOG.info("Removing obsolete line from {}: {}", filename, line);
5✔
608
        iterator.remove();
2✔
609
        removeCount++;
2✔
610
      } else if (line.equals(extraLine)) {
4✔
611
        extraLine = null;
2✔
612
      }
613
    }
1✔
614
    if (add) {
2✔
615
      if (extraLine != null) {
2!
616
        lines.add(extraLine);
×
617
      }
618
      if (!this.context.getSystemInfo().isWindows()) {
5✔
619
        lines.add("export IDE_ROOT=\"" + WindowsPathSyntax.MSYS.format(ideRoot) + "\"");
7✔
620
      }
621
      lines.add(BASH_CODE_SOURCE_FUNCTIONS);
4✔
622
    }
623
    fileAccess.writeFileLines(lines, rcFile);
4✔
624
    LOG.debug("Successfully updated {}", filename);
4✔
625
  }
1✔
626

627
  private static boolean isObsoleteRcLine(String line) {
628
    if (line.startsWith("alias ide=")) {
4!
629
      return true;
×
630
    } else if (line.startsWith("export IDE_ROOT=")) {
4!
631
      return true;
×
632
    } else if (line.equals("ide")) {
4✔
633
      return true;
2✔
634
    } else if (line.equals("ide init")) {
4✔
635
      return true;
2✔
636
    } else if (line.startsWith("source \"$IDE_ROOT/_ide/")) {
4✔
637
      return true;
2✔
638
    }
639
    return false;
2✔
640
  }
641

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

644
    Path artifactPath = cwd.resolve(artifactName);
4✔
645
    if (Files.exists(artifactPath)) {
5!
646
      installationArtifacts.add(artifactPath);
5✔
647
    } else if (required) {
×
648
      LOG.error("Missing required file {}", artifactName);
×
649
      return false;
×
650
    }
651
    return true;
2✔
652
  }
653

654
  private Path determineIdeRoot(Path cwd) {
655
    Path ideRoot = this.context.getIdeRoot();
4✔
656
    if (ideRoot == null) {
2!
657
      Path home = this.context.getUserHome();
4✔
658
      Path installRoot = home;
2✔
659
      if (this.context.getSystemInfo().isWindows()) {
5✔
660
        if (!cwd.startsWith(home)) {
4!
661
          installRoot = cwd.getRoot();
×
662
        }
663
      }
664
      ideRoot = installRoot.resolve(IdeContext.FOLDER_PROJECTS);
4✔
665
    } else {
1✔
666
      assert (Files.isDirectory(ideRoot)) : "IDE_ROOT directory does not exist!";
×
667
    }
668
    return ideRoot;
2✔
669
  }
670

671
  /**
672
   * Uninstalls IDEasy entirely from the system.
673
   */
674
  public void uninstallIdeasy() {
675

676
    Path ideRoot = this.context.getIdeRoot();
4✔
677
    removeFromShellRc(BASHRC, ideRoot);
4✔
678
    removeFromShellRc(ZSHRC, ideRoot);
4✔
679
    Path idePath = this.context.getIdePath();
4✔
680
    uninstallIdeasyWindowsEnv(ideRoot);
3✔
681
    uninstallIdeasyIdePath(idePath);
3✔
682
    deleteDownloadCache();
2✔
683
    IdeLogLevel.SUCCESS.log(LOG, "IDEasy has been uninstalled from your system.");
4✔
684
    IdeLogLevel.INTERACTION.log(LOG, "ATTENTION:\n"
10✔
685
        + "In order to prevent data-loss, we do not delete your projects and git repositories!\n"
686
        + "To entirely get rid of IDEasy, also check your IDE_ROOT folder at:\n"
687
        + "{}", ideRoot);
688
  }
1✔
689

690
  private void deleteDownloadCache() {
691
    Path downloadPath = this.context.getDownloadPath();
4✔
692
    LOG.info("Deleting download cache from {}", downloadPath);
4✔
693
    this.context.getFileAccess().delete(downloadPath);
5✔
694
  }
1✔
695

696
  private void uninstallIdeasyIdePath(Path idePath) {
697
    if (this.context.getSystemInfo().isWindows()) {
5✔
698
      Path bash = this.context.findBash();
4✔
699
      if (bash == null) {
2!
700
        LOG.warn("Could not find bash for asynchronous deletion of {}. Falling back to direct deletion.", idePath);
×
701
        this.context.getFileAccess().delete(idePath);
×
702
        return;
×
703
      }
704
      this.context.newProcess().executable(bash).addArgs("-c",
17✔
705
          "sleep 10 && rm -rf \"" + WindowsPathSyntax.MSYS.format(idePath) + "\"").run(ProcessMode.BACKGROUND);
5✔
706
      IdeLogLevel.INTERACTION.log(LOG,
10✔
707
          "To prevent windows file locking errors, we perform an asynchronous deletion of {} in background now.\n"
708
              + "Please close all terminals and wait a minute for the deletion to complete before running other commands.", idePath);
709
    } else {
1✔
710
      LOG.info("Finally deleting {}", idePath);
4✔
711
      this.context.getFileAccess().delete(idePath);
5✔
712
    }
713
  }
1✔
714

715
  private void uninstallIdeasyWindowsEnv(Path ideRoot) {
716
    if (!this.context.getSystemInfo().isWindows()) {
5✔
717
      return;
1✔
718
    }
719
    WindowsHelper helper = WindowsHelper.get(this.context);
4✔
720
    helper.removeUserEnvironmentValue(IdeVariables.IDE_ROOT.getName());
4✔
721
    String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
5✔
722
    if (userPath == null) {
2!
723
      LOG.error("Could not read user PATH from registry!");
×
724
    } else {
725
      LOG.info("Found user PATH={}", userPath);
4✔
726
      String newUserPath = userPath;
2✔
727
      if (!userPath.isEmpty()) {
3!
728
        SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
4✔
729
        path.removeEntries(s -> s.endsWith(IDE_BIN) || s.endsWith(IDE_INSTALLATION_BIN));
15!
730
        newUserPath = path.toString();
3✔
731
      }
732
      if (newUserPath.equals(userPath)) {
4!
733
        LOG.error("Could not find IDEasy in PATH:\n{}", userPath);
×
734
      } else {
735
        helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), newUserPath);
5✔
736
      }
737
    }
738
  }
1✔
739
}
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