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

devonfw / IDEasy / 25501148981

07 May 2026 02:11PM UTC coverage: 70.77% (+0.03%) from 70.741%
25501148981

Pull #1894

github

web-flow
Merge 25cbc65e4 into fd215c395
Pull Request #1894: #1892: fixed gui-launcher pom.xml not being included in native images on nightly/release builds.

4407 of 6880 branches covered (64.06%)

Branch coverage included in aggregate %.

11362 of 15402 relevant lines covered (73.77%)

3.12 hits per line

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

73.18
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.HashMap;
9
import java.util.Iterator;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.Set;
13

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

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

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

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

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

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

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

60
  private final UpgradeMode mode;
61

62
  /**
63
   * The constructor.
64
   *
65
   * @param context the {@link IdeContext}.
66
   */
67
  public IdeasyCommandlet(IdeContext context) {
68
    this(context, UpgradeMode.STABLE);
4✔
69
  }
1✔
70

71
  /**
72
   * The constructor.
73
   *
74
   * @param context the {@link IdeContext}.
75
   * @param mode the {@link UpgradeMode}.
76
   */
77
  public IdeasyCommandlet(IdeContext context, UpgradeMode mode) {
78

79
    super(context, TOOL_NAME, ARTIFACT, Set.of(Tag.PRODUCTIVITY, Tag.IDE));
8✔
80
    this.mode = mode;
3✔
81
  }
1✔
82

83
  @Override
84
  public VersionIdentifier getInstalledVersion() {
85

86
    return IdeVersion.getVersionIdentifier();
2✔
87
  }
88

89
  @Override
90
  public String getInstalledEdition() {
91

92
    return this.tool;
3✔
93
  }
94

95
  @Override
96
  public String getConfiguredEdition() {
97

98
    return this.tool;
3✔
99
  }
100

101
  @Override
102
  public VersionIdentifier getConfiguredVersion() {
103

104
    UpgradeMode upgradeMode = this.mode;
3✔
105
    if (upgradeMode == null) {
2!
106
      if (IdeVersion.isSnapshot()) {
2!
107
        upgradeMode = UpgradeMode.SNAPSHOT;
3✔
108
      } else {
109
        if (IdeVersion.getVersionIdentifier().getDevelopmentPhase().isStable()) {
×
110
          upgradeMode = UpgradeMode.STABLE;
×
111
        } else {
112
          upgradeMode = UpgradeMode.UNSTABLE;
×
113
        }
114
      }
115
    }
116
    return upgradeMode.getVersion();
3✔
117
  }
118

119
  @Override
120
  public Path getToolPath() {
121

122
    return this.context.getIdeInstallationPath();
4✔
123
  }
124

125
  @Override
126
  protected ToolInstallation doInstall(ToolInstallRequest request) {
127

128
    this.context.requireOnline("upgrade of IDEasy", true);
5✔
129

130
    if (IdeVersion.isUndefined() && !this.context.isForceMode()) {
6!
131
      VersionIdentifier version = IdeVersion.getVersionIdentifier();
2✔
132
      LOG.warn("You are using IDEasy version {} which indicates local development - skipping upgrade.", version);
4✔
133
      return toolAlreadyInstalled(request);
4✔
134
    }
135
    return super.doInstall(request);
×
136
  }
137

138
  /**
139
   * @return the latest released {@link VersionIdentifier version} of IDEasy.
140
   */
141
  public VersionIdentifier getLatestVersion() {
142

143
    if (!this.context.isForceMode()) {
4!
144
      VersionIdentifier currentVersion = IdeVersion.getVersionIdentifier();
2✔
145
      if (IdeVersion.isUndefined()) {
2!
146
        return currentVersion;
2✔
147
      }
148
    }
149
    VersionIdentifier configuredVersion = getConfiguredVersion();
×
150
    return getToolRepository().resolveVersion(this.tool, getConfiguredEdition(), configuredVersion, this);
×
151
  }
152

153
  /**
154
   * Checks if an update is available and logs according information.
155
   *
156
   * @return {@code true} if an update is available, {@code false} otherwise.
157
   */
158
  public boolean checkIfUpdateIsAvailable() {
159
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
160
    IdeLogLevel.SUCCESS.log(LOG, "Your version of IDEasy is {}.", installedVersion);
10✔
161
    if (IdeVersion.isSnapshot()) {
2!
162
      LOG.warn("You are using a SNAPSHOT version of IDEasy. For stability consider switching to a stable release via 'ide upgrade --mode=stable'");
3✔
163
    }
164
    if (this.context.isOffline()) {
4✔
165
      LOG.warn("Skipping check for newer version of IDEasy because you are offline.");
3✔
166
      return false;
2✔
167
    }
168
    VersionIdentifier latestVersion = getLatestVersion();
3✔
169
    if (installedVersion.equals(latestVersion)) {
4!
170
      IdeLogLevel.SUCCESS.log(LOG, "Your are using the latest version of IDEasy and no update is available.");
4✔
171
      return false;
2✔
172
    } else {
173
      IdeLogLevel.INTERACTION.log(LOG,
×
174
          "Your version of IDEasy is {} but version {} is available. Please run the following command to upgrade to the latest version:\n"
175
              + "ide upgrade", installedVersion, latestVersion);
176
      return true;
×
177
    }
178
  }
179

180
  /**
181
   * Initial installation of IDEasy.
182
   *
183
   * @param cwd the {@link Path} to the current working directory.
184
   * @see com.devonfw.tools.ide.commandlet.InstallCommandlet
185
   */
186
  public void installIdeasy(Path cwd) {
187
    Path ideRoot = determineIdeRoot(cwd);
4✔
188
    Path idePath = ideRoot.resolve(IdeContext.FOLDER_UNDERSCORE_IDE);
4✔
189
    Path installationPath = idePath.resolve(IdeContext.FOLDER_INSTALLATION);
4✔
190
    Path ideasySoftwarePath = idePath.resolve(IdeContext.FOLDER_SOFTWARE).resolve(MvnRepository.ID).resolve(IdeasyCommandlet.TOOL_NAME)
8✔
191
        .resolve(IdeasyCommandlet.TOOL_NAME);
2✔
192
    Path ideasyVersionPath = ideasySoftwarePath.resolve(IdeVersion.getVersionString());
4✔
193
    FileAccess fileAccess = this.context.getFileAccess();
4✔
194
    if (Files.isDirectory(ideasyVersionPath)) {
5✔
195
      LOG.error("IDEasy is already installed at {} - if your installation is broken, delete it manually and rerun setup!", ideasyVersionPath);
5✔
196
    } else {
197
      List<Path> installationArtifacts = new ArrayList<>();
4✔
198
      HashMap<String, Boolean> requiredArtifacts = new HashMap<>(Map.of(
7✔
199
          //artifactName: String, required: boolean
200
          "bin", true,
3✔
201
          "functions", true,
3✔
202
          "internal", true,
3✔
203
          "gui", true,
3✔
204
          "system", true,
3✔
205
          "IDEasy.pdf", true,
3✔
206
          "setup", true,
3✔
207
          "setup.bat", false)
1✔
208
      );
209

210
      for (Map.Entry<String, Boolean> entry : requiredArtifacts.entrySet()) {
11✔
211
        String artifactName = entry.getKey();
4✔
212
        boolean required = entry.getValue();
5✔
213
        boolean success = addInstallationArtifact(cwd, artifactName, required, installationArtifacts);
7✔
214
        if (!success) {
2!
215
          throw new CliException("IDEasy release is inconsistent at %s [artifact=%s]".formatted(cwd, artifactName));
×
216
        }
217
      }
1✔
218
      fileAccess.mkdirs(ideasyVersionPath);
3✔
219
      for (Path installationArtifact : installationArtifacts) {
10✔
220
        fileAccess.copy(installationArtifact, ideasyVersionPath);
4✔
221
      }
1✔
222
      this.context.writeVersionFile(IdeVersion.getVersionIdentifier(), ideasyVersionPath);
5✔
223
    }
224
    fileAccess.symlink(ideasyVersionPath, installationPath);
4✔
225
    addToShellRc(BASHRC, ideRoot, null);
5✔
226
    addToShellRc(ZSHRC, ideRoot, "autoload -U +X bashcompinit && bashcompinit");
5✔
227
    installIdeasyWindowsEnv(ideRoot, installationPath);
4✔
228
    IdeLogLevel.SUCCESS.log(LOG, "IDEasy has been installed successfully on your system.");
4✔
229
    LOG.warn("IDEasy has been setup for new shells but it cannot work in your current shell(s).\n"
3✔
230
        + "To use it here, run 'source ~/.bashrc' (or your shell config). Otherwise, open a new terminal or reboot.");
231
  }
1✔
232

233
  private void installIdeasyWindowsEnv(Path ideRoot, Path installationPath) {
234
    if (!this.context.getSystemInfo().isWindows()) {
5✔
235
      return;
1✔
236
    }
237
    WindowsHelper helper = WindowsHelper.get(this.context);
4✔
238
    helper.setUserEnvironmentValue(IdeVariables.IDE_ROOT.getName(), ideRoot.toString());
6✔
239
    String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
5✔
240
    if (userPath == null) {
2!
241
      LOG.error("Could not read user PATH from registry!");
×
242
    } else {
243
      LOG.info("Found user PATH={}", userPath);
4✔
244
      Path ideasyBinPath = installationPath.resolve("bin");
4✔
245
      SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
4✔
246
      if (path.getEntries().isEmpty()) {
4!
247
        LOG.warn("ATTENTION:\n"
×
248
            + "Your user specific PATH variable seems to be empty.\n"
249
            + "You can double check this by pressing [Windows][r] and launch the program SystemPropertiesAdvanced.\n"
250
            + "Then click on 'Environment variables' and check if 'PATH' is set in the 'user variables' from the upper list.\n"
251
            + "In case 'PATH' is defined there non-empty and you get this message, please abort and give us feedback:\n"
252
            + "https://github.com/devonfw/IDEasy/issues\n"
253
            + "Otherwise all is correct and you can continue.");
254
        this.context.askToContinue("Are you sure you want to override your PATH?");
×
255
      } else {
256
        path.removeEntries(s -> s.endsWith(IDE_INSTALLATION_BIN));
7✔
257
      }
258
      path.getEntries().add(ideasyBinPath.toString());
6✔
259
      helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), path.toString());
6✔
260
      setGitLongpaths();
2✔
261
    }
262
  }
1✔
263

264
  private void setGitLongpaths() {
265
    this.context.getGitContext().findGitRequired();
5✔
266
    Path configPath = this.context.getUserHome().resolve(".gitconfig");
6✔
267
    FileAccess fileAccess = this.context.getFileAccess();
4✔
268
    IniFile iniFile = fileAccess.readIniFile(configPath);
4✔
269
    IniSection coreSection = iniFile.getOrCreateSection("core");
4✔
270
    coreSection.setProperty("longpaths", "true");
4✔
271
    fileAccess.writeIniFile(iniFile, configPath);
4✔
272
  }
1✔
273

274
  /**
275
   * Sets up Windows Terminal with Git Bash integration.
276
   */
277
  public void setupWindowsTerminal() {
278
    if (!this.context.getSystemInfo().isWindows()) {
×
279
      return;
×
280
    }
281
    if (!isWindowsTerminalInstalled()) {
×
282
      try {
283
        installWindowsTerminal();
×
284
      } catch (Exception e) {
×
285
        LOG.error("Failed to install Windows Terminal!", e);
×
286
      }
×
287
    }
288
    configureWindowsTerminalGitBash();
×
289
  }
×
290

291
  /**
292
   * Checks if Windows Terminal is installed.
293
   *
294
   * @return {@code true} if Windows Terminal is installed, {@code false} otherwise.
295
   */
296
  private boolean isWindowsTerminalInstalled() {
297
    try {
298
      ProcessResult result = this.context.newProcess()
×
299
          .executable("powershell")
×
300
          .addArgs("-Command", "Get-AppxPackage -Name Microsoft.WindowsTerminal")
×
301
          .run(ProcessMode.DEFAULT_CAPTURE);
×
302
      return result.isSuccessful() && !result.getOut().isEmpty();
×
303
    } catch (Exception e) {
×
304
      LOG.debug("Failed to check Windows Terminal installation.", e);
×
305
      return false;
×
306
    }
307
  }
308

309
  /**
310
   * Installs Windows Terminal using winget.
311
   */
312
  private void installWindowsTerminal() {
313
    try {
314
      LOG.info("Installing Windows Terminal...");
×
315
      ProcessResult result = this.context.newProcess()
×
316
          .executable("winget")
×
317
          .addArgs("install", "Microsoft.WindowsTerminal")
×
318
          .run(ProcessMode.DEFAULT);
×
319
      if (result.isSuccessful()) {
×
320
        IdeLogLevel.SUCCESS.log(LOG, "Windows Terminal has been installed successfully.");
×
321
      } else {
322
        LOG.warn("Failed to install Windows Terminal. Please install it manually from Microsoft Store.");
×
323
      }
324
    } catch (Exception e) {
×
325
      LOG.warn("Failed to install Windows Terminal: {}. Please install it manually from Microsoft Store.", e.toString());
×
326
    }
×
327
  }
×
328

329
  /**
330
   * Configures Git Bash integration in Windows Terminal.
331
   */
332
  protected void configureWindowsTerminalGitBash() {
333
    Path settingsPath = getWindowsTerminalSettingsPath();
3✔
334
    if (settingsPath == null || !Files.exists(settingsPath)) {
7!
335
      LOG.warn("Windows Terminal settings file not found. Cannot configure Git Bash integration.");
×
336
      return;
×
337
    }
338

339
    try {
340
      Path bashPath = this.context.findBash();
4✔
341
      if (bashPath == null) {
2!
342
        LOG.warn("Git Bash not found. Cannot configure Windows Terminal integration.");
×
343
        return;
×
344
      }
345

346
      configureGitBashProfile(settingsPath, bashPath.toString());
5✔
347
      IdeLogLevel.SUCCESS.log(LOG, "Git Bash has been configured in Windows Terminal.");
4✔
348
    } catch (Exception e) {
×
349
      LOG.warn("Failed to configure Git Bash in Windows Terminal: {}", e.getMessage());
×
350
    }
1✔
351
  }
1✔
352

353
  /**
354
   * Gets the Windows Terminal settings file path.
355
   *
356
   * @return the {@link Path} to the Windows Terminal settings file, or {@code null} if not found.
357
   */
358
  protected Path getWindowsTerminalSettingsPath() {
359
    Path localAppData = this.context.getUserHome().resolve("AppData").resolve("Local");
8✔
360
    Path packagesPath = localAppData.resolve("Packages");
4✔
361

362
    // Try the new Windows Terminal package first
363
    Path newTerminalPath = packagesPath.resolve("Microsoft.WindowsTerminal_8wekyb3d8bbwe")
4✔
364
        .resolve("LocalState").resolve("settings.json");
4✔
365
    if (Files.exists(newTerminalPath)) {
5!
366
      return newTerminalPath;
2✔
367
    }
368

369
    // Try the old Windows Terminal Preview package
370
    Path previewPath = packagesPath.resolve("Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe")
×
371
        .resolve("LocalState").resolve("settings.json");
×
372
    if (Files.exists(previewPath)) {
×
373
      return previewPath;
×
374
    }
375

376
    return null;
×
377
  }
378

379
  /**
380
   * Configures Git Bash profile in Windows Terminal settings.
381
   *
382
   * @param settingsPath the {@link Path} to the Windows Terminal settings file.
383
   * @param bashPath the path to the Git Bash executable.
384
   */
385
  private void configureGitBashProfile(Path settingsPath, String bashPath) throws Exception {
386

387
    ObjectMapper mapper = new ObjectMapper();
4✔
388
    ObjectNode root;
389
    try (Reader reader = Files.newBufferedReader(settingsPath)) {
3✔
390
      JsonNode rootNode = mapper.readTree(reader);
4✔
391
      root = (ObjectNode) rootNode;
3✔
392
    }
393

394
    // Get or create profiles object
395
    ObjectNode profiles = (ObjectNode) root.get("profiles");
5✔
396
    if (profiles == null) {
2!
397
      profiles = mapper.createObjectNode();
×
398
      root.set("profiles", profiles);
×
399
    }
400

401
    // Get or create list array of profiles object
402
    JsonNode profilesList = profiles.get("list");
4✔
403
    if (profilesList == null || !profilesList.isArray()) {
5!
404
      profilesList = mapper.createArrayNode();
×
405
      profiles.set("list", profilesList);
×
406
    }
407

408
    // Check if Git Bash profile already exists
409
    boolean gitBashProfileExists = false;
2✔
410
    for (JsonNode profile : profilesList) {
10✔
411
      if (profile.has("name") && profile.get("name").asText().equals("Git Bash")) {
11!
412
        gitBashProfileExists = true;
×
413
        break;
×
414
      }
415
    }
1✔
416

417
    // Add Git Bash profile if it doesn't exist
418
    if (gitBashProfileExists) {
2!
419
      LOG.info("Git Bash profile already exists in {}.", settingsPath);
×
420
    } else {
421
      ObjectNode gitBashProfile = mapper.createObjectNode();
3✔
422
      String newGuid = "{2ece5bfe-50ed-5f3a-ab87-5cd4baafed2b}";
2✔
423
      String iconPath = getGitBashIconPath(bashPath);
4✔
424
      String startingDirectory = this.context.getIdeRoot().toString();
5✔
425

426
      gitBashProfile.put("guid", newGuid);
5✔
427
      gitBashProfile.put("name", "Git Bash");
5✔
428
      gitBashProfile.put("commandline", bashPath);
5✔
429
      gitBashProfile.put("icon", iconPath);
5✔
430
      gitBashProfile.put("startingDirectory", startingDirectory);
5✔
431

432
      ((ArrayNode) profilesList).add(gitBashProfile);
5✔
433

434
      // Set Git Bash as default profile
435
      root.put("defaultProfile", newGuid);
5✔
436

437
      // Write back to file
438
      try (Writer writer = Files.newBufferedWriter(settingsPath)) {
5✔
439
        mapper.writerWithDefaultPrettyPrinter().writeValue(writer, root);
5✔
440
      }
441
    }
442
  }
1✔
443

444
  private String getGitBashIconPath(String bashPathString) {
445
    Path bashPath = Path.of(bashPathString);
5✔
446
    // "C:\\Program Files\\Git\\bin\\bash.exe"
447
    // "C:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico"
448
    Path parent = bashPath.getParent();
3✔
449
    if (parent != null) {
2!
450
      Path iconPath = parent.resolve("mingw64/share/git/git-for-windows.ico");
4✔
451
      if (Files.exists(iconPath)) {
5!
452
        LOG.debug("Found git-bash icon at {}", iconPath);
×
453
        return iconPath.toString();
×
454
      }
455
      LOG.debug("Git Bash icon not found at {}. Using default icon.", iconPath);
4✔
456
    }
457
    return "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png";
2✔
458
  }
459

460
  static String removeObsoleteEntryFromWindowsPath(String userPath) {
461
    return removeEntryFromWindowsPath(userPath, IDE_BIN);
4✔
462
  }
463

464
  static String removeEntryFromWindowsPath(String userPath, String suffix) {
465
    int len = userPath.length();
3✔
466
    int start = 0;
2✔
467
    while ((start >= 0) && (start < len)) {
5!
468
      int end = userPath.indexOf(';', start);
5✔
469
      if (end < 0) {
2✔
470
        end = len;
2✔
471
      }
472
      String entry = userPath.substring(start, end);
5✔
473
      if (entry.endsWith(suffix)) {
4✔
474
        String prefix = "";
2✔
475
        int offset = 1;
2✔
476
        if (start > 0) {
2✔
477
          prefix = userPath.substring(0, start - 1);
7✔
478
          offset = 0;
2✔
479
        }
480
        if (end == len) {
3✔
481
          return prefix;
2✔
482
        } else {
483
          return removeEntryFromWindowsPath(prefix + userPath.substring(end + offset), suffix);
10✔
484
        }
485
      }
486
      start = end + 1;
4✔
487
    }
1✔
488
    return userPath;
2✔
489
  }
490

491
  /**
492
   * Adds ourselves to the shell RC (run-commands) configuration file.
493
   *
494
   * @param filename the name of the RC file.
495
   * @param ideRoot the IDE_ROOT {@link Path}.
496
   */
497
  private void addToShellRc(String filename, Path ideRoot, String extraLine) {
498

499
    modifyShellRc(filename, ideRoot, true, extraLine);
6✔
500
  }
1✔
501

502
  private void removeFromShellRc(String filename, Path ideRoot) {
503

504
    modifyShellRc(filename, ideRoot, false, null);
6✔
505
  }
1✔
506

507
  /**
508
   * Adds ourselves to the shell RC (run-commands) configuration file.
509
   *
510
   * @param filename the name of the RC file.
511
   * @param ideRoot the IDE_ROOT {@link Path}.
512
   */
513
  private void modifyShellRc(String filename, Path ideRoot, boolean add, String extraLine) {
514

515
    if (add) {
2✔
516
      LOG.info("Configuring IDEasy in {}", filename);
5✔
517
    } else {
518
      LOG.info("Removing IDEasy from {}", filename);
4✔
519
    }
520
    Path rcFile = this.context.getUserHome().resolve(filename);
6✔
521
    FileAccess fileAccess = this.context.getFileAccess();
4✔
522
    List<String> lines = fileAccess.readFileLines(rcFile);
4✔
523
    if (lines == null) {
2✔
524
      if (!add) {
2!
525
        return;
×
526
      }
527
      lines = new ArrayList<>();
5✔
528
    } else {
529
      // since it is unspecified if the returned List may be immutable we want to get sure
530
      lines = new ArrayList<>(lines);
5✔
531
    }
532
    Iterator<String> iterator = lines.iterator();
3✔
533
    int removeCount = 0;
2✔
534
    while (iterator.hasNext()) {
3✔
535
      String line = iterator.next();
4✔
536
      line = line.trim();
3✔
537
      if (isObsoleteRcLine(line)) {
3✔
538
        LOG.info("Removing obsolete line from {}: {}", filename, line);
5✔
539
        iterator.remove();
2✔
540
        removeCount++;
2✔
541
      } else if (line.equals(extraLine)) {
4✔
542
        extraLine = null;
2✔
543
      }
544
    }
1✔
545
    if (add) {
2✔
546
      if (extraLine != null) {
2!
547
        lines.add(extraLine);
×
548
      }
549
      if (!this.context.getSystemInfo().isWindows()) {
5✔
550
        lines.add("export IDE_ROOT=\"" + WindowsPathSyntax.MSYS.format(ideRoot) + "\"");
7✔
551
      }
552
      lines.add(BASH_CODE_SOURCE_FUNCTIONS);
4✔
553
    }
554
    fileAccess.writeFileLines(lines, rcFile);
4✔
555
    LOG.debug("Successfully updated {}", filename);
4✔
556
  }
1✔
557

558
  private static boolean isObsoleteRcLine(String line) {
559
    if (line.startsWith("alias ide=")) {
4!
560
      return true;
×
561
    } else if (line.startsWith("export IDE_ROOT=")) {
4!
562
      return true;
×
563
    } else if (line.equals("ide")) {
4✔
564
      return true;
2✔
565
    } else if (line.equals("ide init")) {
4✔
566
      return true;
2✔
567
    } else if (line.startsWith("source \"$IDE_ROOT/_ide/")) {
4✔
568
      return true;
2✔
569
    }
570
    return false;
2✔
571
  }
572

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

575
    Path artifactPath = cwd.resolve(artifactName);
4✔
576
    if (Files.exists(artifactPath)) {
5!
577
      installationArtifacts.add(artifactPath);
5✔
578
    } else if (required) {
×
579
      LOG.error("Missing required file {}", artifactName);
×
580
      return false;
×
581
    }
582
    return true;
2✔
583
  }
584

585
  private Path determineIdeRoot(Path cwd) {
586
    Path ideRoot = this.context.getIdeRoot();
4✔
587
    if (ideRoot == null) {
2!
588
      Path home = this.context.getUserHome();
4✔
589
      Path installRoot = home;
2✔
590
      if (this.context.getSystemInfo().isWindows()) {
5✔
591
        if (!cwd.startsWith(home)) {
4!
592
          installRoot = cwd.getRoot();
×
593
        }
594
      }
595
      ideRoot = installRoot.resolve(IdeContext.FOLDER_PROJECTS);
4✔
596
    } else {
1✔
597
      assert (Files.isDirectory(ideRoot)) : "IDE_ROOT directory does not exist!";
×
598
    }
599
    return ideRoot;
2✔
600
  }
601

602
  /**
603
   * Uninstalls IDEasy entirely from the system.
604
   */
605
  public void uninstallIdeasy() {
606

607
    Path ideRoot = this.context.getIdeRoot();
4✔
608
    removeFromShellRc(BASHRC, ideRoot);
4✔
609
    removeFromShellRc(ZSHRC, ideRoot);
4✔
610
    Path idePath = this.context.getIdePath();
4✔
611
    uninstallIdeasyWindowsEnv(ideRoot);
3✔
612
    uninstallIdeasyIdePath(idePath);
3✔
613
    deleteDownloadCache();
2✔
614
    IdeLogLevel.SUCCESS.log(LOG, "IDEasy has been uninstalled from your system.");
4✔
615
    IdeLogLevel.INTERACTION.log(LOG, "ATTENTION:\n"
10✔
616
        + "In order to prevent data-loss, we do not delete your projects and git repositories!\n"
617
        + "To entirely get rid of IDEasy, also check your IDE_ROOT folder at:\n"
618
        + "{}", ideRoot);
619
  }
1✔
620

621
  private void deleteDownloadCache() {
622
    Path downloadPath = this.context.getDownloadPath();
4✔
623
    LOG.info("Deleting download cache from {}", downloadPath);
4✔
624
    this.context.getFileAccess().delete(downloadPath);
5✔
625
  }
1✔
626

627
  private void uninstallIdeasyIdePath(Path idePath) {
628
    if (this.context.getSystemInfo().isWindows()) {
5✔
629
      this.context.newProcess().executable("bash").addArgs("-c",
17✔
630
          "sleep 10 && rm -rf \"" + WindowsPathSyntax.MSYS.format(idePath) + "\"").run(ProcessMode.BACKGROUND);
5✔
631
      IdeLogLevel.INTERACTION.log(LOG,
11✔
632
          "To prevent windows file locking errors, we perform an asynchronous deletion of {} in background now.\n"
633
              + "Please close all terminals and wait a minute for the deletion to complete before running other commands.", idePath);
634
    } else {
635
      LOG.info("Finally deleting {}", idePath);
4✔
636
      this.context.getFileAccess().delete(idePath);
5✔
637
    }
638
  }
1✔
639

640
  private void uninstallIdeasyWindowsEnv(Path ideRoot) {
641
    if (!this.context.getSystemInfo().isWindows()) {
5✔
642
      return;
1✔
643
    }
644
    WindowsHelper helper = WindowsHelper.get(this.context);
4✔
645
    helper.removeUserEnvironmentValue(IdeVariables.IDE_ROOT.getName());
4✔
646
    String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
5✔
647
    if (userPath == null) {
2!
648
      LOG.error("Could not read user PATH from registry!");
×
649
    } else {
650
      LOG.info("Found user PATH={}", userPath);
4✔
651
      String newUserPath = userPath;
2✔
652
      if (!userPath.isEmpty()) {
3!
653
        SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
4✔
654
        path.removeEntries(s -> s.endsWith(IDE_BIN) || s.endsWith(IDE_INSTALLATION_BIN));
15!
655
        newUserPath = path.toString();
3✔
656
      }
657
      if (newUserPath.equals(userPath)) {
4!
658
        LOG.error("Could not find IDEasy in PATH:\n{}", userPath);
×
659
      } else {
660
        helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), newUserPath);
5✔
661
      }
662
    }
663
  }
1✔
664
}
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