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

devonfw / IDEasy / 17731013783

15 Sep 2025 11:09AM UTC coverage: 68.631% (-0.06%) from 68.686%
17731013783

push

github

web-flow
#1221: Improve WindowsTerminal integration (#1482)

3400 of 5421 branches covered (62.72%)

Branch coverage included in aggregate %.

8878 of 12469 relevant lines covered (71.2%)

3.12 hits per line

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

69.32
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.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.io.ini.IniFile;
21
import com.devonfw.tools.ide.io.ini.IniSection;
22
import com.devonfw.tools.ide.os.WindowsHelper;
23
import com.devonfw.tools.ide.os.WindowsPathSyntax;
24
import com.devonfw.tools.ide.process.ProcessMode;
25
import com.devonfw.tools.ide.process.ProcessResult;
26
import com.devonfw.tools.ide.tool.mvn.MvnArtifact;
27
import com.devonfw.tools.ide.tool.mvn.MvnBasedLocalToolCommandlet;
28
import com.devonfw.tools.ide.tool.repository.MavenRepository;
29
import com.devonfw.tools.ide.variable.IdeVariables;
30
import com.devonfw.tools.ide.version.IdeVersion;
31
import com.devonfw.tools.ide.version.VersionIdentifier;
32
import com.fasterxml.jackson.databind.JsonNode;
33
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.fasterxml.jackson.databind.node.ArrayNode;
35
import com.fasterxml.jackson.databind.node.ObjectNode;
36

37
/**
38
 * {@link MvnBasedLocalToolCommandlet} for IDEasy (ide-cli).
39
 */
40
public class IdeasyCommandlet extends MvnBasedLocalToolCommandlet {
41

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

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

47
  /** The {@link #getName() tool name}. */
48
  public static final String TOOL_NAME = "ideasy";
49
  public static final String BASHRC = ".bashrc";
50
  public static final String ZSHRC = ".zshrc";
51
  public static final String IDE_BIN = "\\_ide\\bin";
52
  public static final String IDE_INSTALLATION_BIN = "\\_ide\\installation\\bin";
53

54
  private final UpgradeMode mode;
55

56
  /**
57
   * The constructor.
58
   *
59
   * @param context the {@link IdeContext}.
60
   */
61
  public IdeasyCommandlet(IdeContext context) {
62
    this(context, UpgradeMode.STABLE);
4✔
63
  }
1✔
64

65
  /**
66
   * The constructor.
67
   *
68
   * @param context the {@link IdeContext}.
69
   * @param mode the {@link UpgradeMode}.
70
   */
71
  public IdeasyCommandlet(IdeContext context, UpgradeMode mode) {
72

73
    super(context, TOOL_NAME, ARTIFACT, Set.of(Tag.PRODUCTIVITY, Tag.IDE));
8✔
74
    this.mode = mode;
3✔
75
  }
1✔
76

77
  @Override
78
  public VersionIdentifier getInstalledVersion() {
79

80
    return IdeVersion.getVersionIdentifier();
2✔
81
  }
82

83
  @Override
84
  public String getConfiguredEdition() {
85

86
    return this.tool;
×
87
  }
88

89
  @Override
90
  public VersionIdentifier getConfiguredVersion() {
91

92
    UpgradeMode upgradeMode = this.mode;
×
93
    if (upgradeMode == null) {
×
94
      if (IdeVersion.isSnapshot()) {
×
95
        upgradeMode = UpgradeMode.SNAPSHOT;
×
96
      } else {
97
        if (IdeVersion.getVersionIdentifier().getDevelopmentPhase().isStable()) {
×
98
          upgradeMode = UpgradeMode.STABLE;
×
99
        } else {
100
          upgradeMode = UpgradeMode.UNSTABLE;
×
101
        }
102
      }
103
    }
104
    return upgradeMode.getVersion();
×
105
  }
106

107
  @Override
108
  public Path getToolPath() {
109

110
    return this.context.getIdeInstallationPath();
×
111
  }
112

113
  @Override
114
  public boolean install(boolean silent) {
115

116
    this.context.requireOnline("upgrade of IDEasy", true);
5✔
117

118
    if (IdeVersion.isUndefined() && !this.context.isForceMode()) {
6!
119
      this.context.warning("You are using IDEasy version {} which indicates local development - skipping upgrade.", IdeVersion.getVersionString());
10✔
120
      return false;
2✔
121
    }
122
    return super.install(silent);
×
123
  }
124

125
  /**
126
   * @return the latest released {@link VersionIdentifier version} of IDEasy.
127
   */
128
  public VersionIdentifier getLatestVersion() {
129

130
    VersionIdentifier currentVersion = IdeVersion.getVersionIdentifier();
2✔
131
    if (IdeVersion.isUndefined()) {
2!
132
      return currentVersion;
2✔
133
    }
134
    VersionIdentifier configuredVersion = getConfiguredVersion();
×
135
    return getToolRepository().resolveVersion(this.tool, getConfiguredEdition(), configuredVersion, this);
×
136
  }
137

138
  /**
139
   * Checks if an update is available and logs according information.
140
   *
141
   * @return {@code true} if an update is available, {@code false} otherwise.
142
   */
143
  public boolean checkIfUpdateIsAvailable() {
144
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
145
    this.context.success("Your version of IDEasy is {}.", installedVersion);
10✔
146
    if (IdeVersion.isSnapshot()) {
2!
147
      this.context.warning("You are using a SNAPSHOT version of IDEasy. For stability consider switching to a stable release via 'ide upgrade --mode=stable'");
4✔
148
    }
149
    if (this.context.isOffline()) {
4✔
150
      this.context.warning("Skipping check for newer version of IDEasy because you are offline.");
4✔
151
      return false;
2✔
152
    }
153
    VersionIdentifier latestVersion = getLatestVersion();
3✔
154
    if (installedVersion.equals(latestVersion)) {
4!
155
      this.context.success("Your are using the latest version of IDEasy and no update is available.");
4✔
156
      return false;
2✔
157
    } else {
158
      this.context.interaction("Your version of IDEasy is {} but version {} is available. Please run the following command to upgrade to the latest version:\n"
×
159
          + "ide upgrade", installedVersion, latestVersion);
160
      return true;
×
161
    }
162
  }
163

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

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

238
  private void setGitLongpaths() {
239
    GitContext gitContext = new GitContextImpl(this.context);
6✔
240
    gitContext.verifyGitInstalled();
2✔
241
    Path configPath = this.context.getUserHome().resolve(".gitconfig");
6✔
242
    FileAccess fileAccess = this.context.getFileAccess();
4✔
243
    IniFile iniFile = fileAccess.readIniFile(configPath);
4✔
244
    IniSection coreSection = iniFile.getOrCreateSection("core");
4✔
245
    coreSection.setProperty("longpaths", "true");
4✔
246
    fileAccess.writeIniFile(iniFile, configPath);
4✔
247
  }
1✔
248

249
  /**
250
   * Sets up Windows Terminal with Git Bash integration.
251
   */
252
  public void setupWindowsTerminal() {
253
    if (!this.context.getSystemInfo().isWindows()) {
×
254
      return;
×
255
    }
256
    if (!isWindowsTerminalInstalled()) {
×
257
      try {
258
        installWindowsTerminal();
×
259
      } catch (Exception e) {
×
260
        this.context.error(e, "Failed to install Windows Terminal!");
×
261
      }
×
262
    }
263
    configureWindowsTerminalGitBash();
×
264
  }
×
265

266
  /**
267
   * Checks if Windows Terminal is installed.
268
   *
269
   * @return {@code true} if Windows Terminal is installed, {@code false} otherwise.
270
   */
271
  private boolean isWindowsTerminalInstalled() {
272
    try {
273
      ProcessResult result = this.context.newProcess()
×
274
          .executable("powershell")
×
275
          .addArgs("-Command", "Get-AppxPackage -Name Microsoft.WindowsTerminal")
×
276
          .run(ProcessMode.DEFAULT_CAPTURE);
×
277
      return result.isSuccessful() && !result.getOut().isEmpty();
×
278
    } catch (Exception e) {
×
279
      this.context.debug("Failed to check Windows Terminal installation: {}", e.getMessage());
×
280
      return false;
×
281
    }
282
  }
283

284
  /**
285
   * Installs Windows Terminal using winget.
286
   */
287
  private void installWindowsTerminal() {
288
    try {
289
      this.context.info("Installing Windows Terminal...");
×
290
      ProcessResult result = this.context.newProcess()
×
291
          .executable("winget")
×
292
          .addArgs("install", "Microsoft.WindowsTerminal")
×
293
          .run(ProcessMode.DEFAULT);
×
294
      if (result.isSuccessful()) {
×
295
        this.context.success("Windows Terminal has been installed successfully.");
×
296
      } else {
297
        this.context.warning("Failed to install Windows Terminal. Please install it manually from Microsoft Store.");
×
298
      }
299
    } catch (Exception e) {
×
300
      this.context.warning("Failed to install Windows Terminal: {}. Please install it manually from Microsoft Store.", e.getMessage());
×
301
    }
×
302
  }
×
303

304
  /**
305
   * Configures Git Bash integration in Windows Terminal.
306
   */
307
  protected void configureWindowsTerminalGitBash() {
308
    Path settingsPath = getWindowsTerminalSettingsPath();
3✔
309
    if (settingsPath == null || !Files.exists(settingsPath)) {
7!
310
      this.context.warning("Windows Terminal settings file not found. Cannot configure Git Bash integration.");
×
311
      return;
×
312
    }
313

314
    try {
315
      String bashPath = this.context.findBash();
4✔
316
      if (bashPath == null) {
2!
317
        this.context.warning("Git Bash not found. Cannot configure Windows Terminal integration.");
×
318
        return;
×
319
      }
320

321
      configureGitBashProfile(settingsPath, bashPath);
4✔
322
      this.context.success("Git Bash has been configured in Windows Terminal.");
4✔
323
    } catch (Exception e) {
×
324
      this.context.warning("Failed to configure Git Bash in Windows Terminal: {}", e.getMessage());
×
325
    }
1✔
326
  }
1✔
327

328
  /**
329
   * Gets the Windows Terminal settings file path.
330
   *
331
   * @return the {@link Path} to the Windows Terminal settings file, or {@code null} if not found.
332
   */
333
  protected Path getWindowsTerminalSettingsPath() {
334
    Path localAppData = this.context.getUserHome().resolve("AppData").resolve("Local");
8✔
335
    Path packagesPath = localAppData.resolve("Packages");
4✔
336

337
    // Try the new Windows Terminal package first
338
    Path newTerminalPath = packagesPath.resolve("Microsoft.WindowsTerminal_8wekyb3d8bbwe")
4✔
339
        .resolve("LocalState").resolve("settings.json");
4✔
340
    if (Files.exists(newTerminalPath)) {
5!
341
      return newTerminalPath;
2✔
342
    }
343

344
    // Try the old Windows Terminal Preview package
345
    Path previewPath = packagesPath.resolve("Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe")
×
346
        .resolve("LocalState").resolve("settings.json");
×
347
    if (Files.exists(previewPath)) {
×
348
      return previewPath;
×
349
    }
350

351
    return null;
×
352
  }
353

354
  /**
355
   * Configures Git Bash profile in Windows Terminal settings.
356
   *
357
   * @param settingsPath the {@link Path} to the Windows Terminal settings file.
358
   * @param bashPath the path to the Git Bash executable.
359
   */
360
  private void configureGitBashProfile(Path settingsPath, String bashPath) throws Exception {
361

362
    ObjectMapper mapper = new ObjectMapper();
4✔
363
    ObjectNode root;
364
    try (Reader reader = Files.newBufferedReader(settingsPath)) {
3✔
365
      JsonNode rootNode = mapper.readTree(reader);
4✔
366
      root = (ObjectNode) rootNode;
3✔
367
    }
368

369
    // Get or create profiles object
370
    ObjectNode profiles = (ObjectNode) root.get("profiles");
5✔
371
    if (profiles == null) {
2!
372
      profiles = mapper.createObjectNode();
×
373
      root.set("profiles", profiles);
×
374
    }
375

376
    // Get or create list array of profiles object
377
    JsonNode profilesList = profiles.get("list");
4✔
378
    if (profilesList == null || !profilesList.isArray()) {
5!
379
      profilesList = mapper.createArrayNode();
×
380
      profiles.set("list", profilesList);
×
381
    }
382

383
    // Check if Git Bash profile already exists
384
    boolean gitBashProfileExists = false;
2✔
385
    for (JsonNode profile : profilesList) {
10✔
386
      if (profile.has("name") && profile.get("name").asText().equals("Git Bash")) {
11!
387
        gitBashProfileExists = true;
×
388
        break;
×
389
      }
390
    }
1✔
391

392
    // Add Git Bash profile if it doesn't exist
393
    if (gitBashProfileExists) {
2!
394
      this.context.info("Git Bash profile already exists in {}.", settingsPath);
×
395
    } else {
396
      ObjectNode gitBashProfile = mapper.createObjectNode();
3✔
397
      String newGuid = "{2ece5bfe-50ed-5f3a-ab87-5cd4baafed2b}";
2✔
398
      String iconPath = getGitBashIconPath(bashPath);
4✔
399
      String startingDirectory = this.context.getIdeRoot().toString();
5✔
400

401
      gitBashProfile.put("guid", newGuid);
5✔
402
      gitBashProfile.put("name", "Git Bash");
5✔
403
      gitBashProfile.put("commandline", bashPath);
5✔
404
      gitBashProfile.put("icon", iconPath);
5✔
405
      gitBashProfile.put("startingDirectory", startingDirectory);
5✔
406

407
      ((ArrayNode) profilesList).add(gitBashProfile);
5✔
408

409
      // Set Git Bash as default profile
410
      root.put("defaultProfile", newGuid);
5✔
411

412
      // Write back to file
413
      try (Writer writer = Files.newBufferedWriter(settingsPath)) {
5✔
414
        mapper.writerWithDefaultPrettyPrinter().writeValue(writer, root);
5✔
415
      }
416
    }
417
  }
1✔
418

419
  private String getGitBashIconPath(String bashPathString) {
420
    Path bashPath = Path.of(bashPathString);
5✔
421
    // "C:\\Program Files\\Git\\bin\\bash.exe"
422
    // "C:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico"
423
    Path parent = bashPath.getParent();
3✔
424
    if (parent != null) {
2!
425
      Path iconPath = parent.resolve("mingw64/share/git/git-for-windows.ico");
×
426
      if (Files.exists(iconPath)) {
×
427
        this.context.debug("Found git-bash icon at {}", iconPath);
×
428
        return iconPath.toString();
×
429
      }
430
      this.context.debug("Git Bash icon not found at {}. Using default icon.", iconPath);
×
431
    }
432
    return "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png";
2✔
433
  }
434

435
  static String removeObsoleteEntryFromWindowsPath(String userPath) {
436
    return removeEntryFromWindowsPath(userPath, IDE_BIN);
4✔
437
  }
438

439
  static String removeEntryFromWindowsPath(String userPath, String suffix) {
440
    int len = userPath.length();
3✔
441
    int start = 0;
2✔
442
    while ((start >= 0) && (start < len)) {
5!
443
      int end = userPath.indexOf(';', start);
5✔
444
      if (end < 0) {
2✔
445
        end = len;
2✔
446
      }
447
      String entry = userPath.substring(start, end);
5✔
448
      if (entry.endsWith(suffix)) {
4✔
449
        String prefix = "";
2✔
450
        int offset = 1;
2✔
451
        if (start > 0) {
2✔
452
          prefix = userPath.substring(0, start - 1);
7✔
453
          offset = 0;
2✔
454
        }
455
        if (end == len) {
3✔
456
          return prefix;
2✔
457
        } else {
458
          return removeEntryFromWindowsPath(prefix + userPath.substring(end + offset), suffix);
10✔
459
        }
460
      }
461
      start = end + 1;
4✔
462
    }
1✔
463
    return userPath;
2✔
464
  }
465

466
  /**
467
   * Adds ourselves to the shell RC (run-commands) configuration file.
468
   *
469
   * @param filename the name of the RC file.
470
   * @param ideRoot the IDE_ROOT {@link Path}.
471
   */
472
  private void addToShellRc(String filename, Path ideRoot, String extraLine) {
473

474
    modifyShellRc(filename, ideRoot, true, extraLine);
6✔
475
  }
1✔
476

477
  private void removeFromShellRc(String filename, Path ideRoot) {
478

479
    modifyShellRc(filename, ideRoot, false, null);
6✔
480
  }
1✔
481

482
  /**
483
   * Adds ourselves to the shell RC (run-commands) configuration file.
484
   *
485
   * @param filename the name of the RC file.
486
   * @param ideRoot the IDE_ROOT {@link Path}.
487
   */
488
  private void modifyShellRc(String filename, Path ideRoot, boolean add, String extraLine) {
489

490
    if (add) {
2✔
491
      this.context.info("Configuring IDEasy in {}", filename);
11✔
492
    } else {
493
      this.context.info("Removing IDEasy from {}", filename);
10✔
494
    }
495
    Path rcFile = this.context.getUserHome().resolve(filename);
6✔
496
    FileAccess fileAccess = this.context.getFileAccess();
4✔
497
    List<String> lines = fileAccess.readFileLines(rcFile);
4✔
498
    if (lines == null) {
2✔
499
      if (!add) {
2!
500
        return;
×
501
      }
502
      lines = new ArrayList<>();
5✔
503
    } else {
504
      // since it is unspecified if the returned List may be immutable we want to get sure
505
      lines = new ArrayList<>(lines);
5✔
506
    }
507
    Iterator<String> iterator = lines.iterator();
3✔
508
    int removeCount = 0;
2✔
509
    while (iterator.hasNext()) {
3✔
510
      String line = iterator.next();
4✔
511
      line = line.trim();
3✔
512
      if (isObsoleteRcLine(line)) {
3✔
513
        this.context.info("Removing obsolete line from {}: {}", filename, line);
14✔
514
        iterator.remove();
2✔
515
        removeCount++;
2✔
516
      } else if (line.equals(extraLine)) {
4✔
517
        extraLine = null;
2✔
518
      }
519
    }
1✔
520
    if (add) {
2✔
521
      if (extraLine != null) {
2!
522
        lines.add(extraLine);
×
523
      }
524
      if (!this.context.getSystemInfo().isWindows()) {
5✔
525
        lines.add("export IDE_ROOT=\"" + WindowsPathSyntax.MSYS.format(ideRoot) + "\"");
7✔
526
      }
527
      lines.add(BASH_CODE_SOURCE_FUNCTIONS);
4✔
528
    }
529
    fileAccess.writeFileLines(lines, rcFile);
4✔
530
    this.context.debug("Successfully updated {}", filename);
10✔
531
  }
1✔
532

533
  private static boolean isObsoleteRcLine(String line) {
534
    if (line.startsWith("alias ide=")) {
4!
535
      return true;
×
536
    } else if (line.startsWith("export IDE_ROOT=")) {
4!
537
      return true;
×
538
    } else if (line.equals("ide")) {
4✔
539
      return true;
2✔
540
    } else if (line.equals("ide init")) {
4✔
541
      return true;
2✔
542
    } else if (line.startsWith("source \"$IDE_ROOT/_ide/")) {
4✔
543
      return true;
2✔
544
    }
545
    return false;
2✔
546
  }
547

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

550
    Path artifactPath = cwd.resolve(artifactName);
4✔
551
    if (Files.exists(artifactPath)) {
5!
552
      installationArtifacts.add(artifactPath);
5✔
553
    } else if (required) {
×
554
      this.context.error("Missing required file {}", artifactName);
×
555
      return false;
×
556
    }
557
    return true;
2✔
558
  }
559

560
  private Path determineIdeRoot(Path cwd) {
561
    Path ideRoot = this.context.getIdeRoot();
4✔
562
    if (ideRoot == null) {
2!
563
      Path home = this.context.getUserHome();
4✔
564
      Path installRoot = home;
2✔
565
      if (this.context.getSystemInfo().isWindows()) {
5✔
566
        if (!cwd.startsWith(home)) {
4!
567
          installRoot = cwd.getRoot();
×
568
        }
569
      }
570
      ideRoot = installRoot.resolve(IdeContext.FOLDER_PROJECTS);
4✔
571
    } else {
1✔
572
      assert (Files.isDirectory(ideRoot)) : "IDE_ROOT directory does not exist!";
×
573
    }
574
    return ideRoot;
2✔
575
  }
576

577
  /**
578
   * Uninstalls IDEasy entirely from the system.
579
   */
580
  public void uninstallIdeasy() {
581

582
    Path ideRoot = this.context.getIdeRoot();
4✔
583
    removeFromShellRc(BASHRC, ideRoot);
4✔
584
    removeFromShellRc(ZSHRC, ideRoot);
4✔
585
    Path idePath = this.context.getIdePath();
4✔
586
    uninstallIdeasyWindowsEnv(ideRoot);
3✔
587
    uninstallIdeasyIdePath(idePath);
3✔
588
    deleteDownloadCache();
2✔
589
    this.context.success("IDEasy has been uninstalled from your system.");
4✔
590
    this.context.interaction("ATTENTION:\n"
10✔
591
        + "In order to prevent data-loss, we do not delete your projects and git repositories!\n"
592
        + "To entirely get rid of IDEasy, also check your IDE_ROOT folder at:\n"
593
        + "{}", ideRoot);
594
  }
1✔
595

596
  private void deleteDownloadCache() {
597
    Path downloadPath = this.context.getDownloadPath();
4✔
598
    this.context.info("Deleting download cache from {}", downloadPath);
10✔
599
    this.context.getFileAccess().delete(downloadPath);
5✔
600
  }
1✔
601

602
  private void uninstallIdeasyIdePath(Path idePath) {
603
    if (this.context.getSystemInfo().isWindows()) {
5✔
604
      this.context.newProcess().executable("bash").addArgs("-c",
17✔
605
          "sleep 10 && rm -rf \"" + WindowsPathSyntax.MSYS.format(idePath) + "\"").run(ProcessMode.BACKGROUND);
5✔
606
      this.context.interaction("To prevent windows file locking errors, we perform an asynchronous deletion of {} in background now.\n"
11✔
607
          + "Please close all terminals and wait a minute for the deletion to complete before running other commands.", idePath);
608
    } else {
609
      this.context.info("Finally deleting {}", idePath);
10✔
610
      this.context.getFileAccess().delete(idePath);
5✔
611
    }
612
  }
1✔
613

614
  private void uninstallIdeasyWindowsEnv(Path ideRoot) {
615
    if (!this.context.getSystemInfo().isWindows()) {
5✔
616
      return;
1✔
617
    }
618
    WindowsHelper helper = WindowsHelper.get(this.context);
4✔
619
    helper.removeUserEnvironmentValue(IdeVariables.IDE_ROOT.getName());
4✔
620
    String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
5✔
621
    if (userPath == null) {
2!
622
      this.context.error("Could not read user PATH from registry!");
×
623
    } else {
624
      this.context.info("Found user PATH={}", userPath);
10✔
625
      String newUserPath = userPath;
2✔
626
      if (!userPath.isEmpty()) {
3!
627
        SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
4✔
628
        path.removeEntries(s -> s.endsWith(IDE_BIN) || s.endsWith(IDE_INSTALLATION_BIN));
15!
629
        newUserPath = path.toString();
3✔
630
      }
631
      if (newUserPath.equals(userPath)) {
4!
632
        this.context.error("Could not find IDEasy in PATH:\n{}", userPath);
×
633
      } else {
634
        helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), newUserPath);
5✔
635
      }
636
    }
637
  }
1✔
638
}
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