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

devonfw / IDEasy / 16069425429

04 Jul 2025 08:29AM UTC coverage: 67.899%. Remained the same
16069425429

Pull #1403

github

web-flow
Merge 8fd1450b6 into b06414502
Pull Request #1403: #1400: make ide --offline status react accordingly

3258 of 5200 branches covered (62.65%)

Branch coverage included in aggregate %.

8312 of 11840 relevant lines covered (70.2%)

3.11 hits per line

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

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

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

10
import com.devonfw.tools.ide.cli.CliException;
11
import com.devonfw.tools.ide.commandlet.UpgradeMode;
12
import com.devonfw.tools.ide.common.SimpleSystemPath;
13
import com.devonfw.tools.ide.common.Tag;
14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.git.GitContext;
16
import com.devonfw.tools.ide.git.GitContextImpl;
17
import com.devonfw.tools.ide.io.FileAccess;
18
import com.devonfw.tools.ide.io.IniFile;
19
import com.devonfw.tools.ide.io.IniSection;
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
    this.context.requireOnline("upgrade of IDEasy", true);
5✔
110

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

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

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

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

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

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

228
  private void setGitLongpaths() {
229
    GitContext gitContext = new GitContextImpl(this.context);
6✔
230
    gitContext.verifyGitInstalled();
2✔
231
    Path configPath = this.context.getUserHome().resolve(".gitconfig");
6✔
232
    FileAccess fileAccess = this.context.getFileAccess();
4✔
233
    IniFile iniFile = fileAccess.readIniFile(configPath);
4✔
234
    IniSection coreSection = iniFile.getOrCreateSection("core");
4✔
235
    coreSection.getProperties().put("longpaths", "true");
6✔
236
    fileAccess.writeIniFile(iniFile, configPath);
4✔
237
  }
1✔
238

239
  static String removeObsoleteEntryFromWindowsPath(String userPath) {
240
    return removeEntryFromWindowsPath(userPath, IDE_BIN);
4✔
241
  }
242

243
  static String removeEntryFromWindowsPath(String userPath, String suffix) {
244
    int len = userPath.length();
3✔
245
    int start = 0;
2✔
246
    while ((start >= 0) && (start < len)) {
5!
247
      int end = userPath.indexOf(';', start);
5✔
248
      if (end < 0) {
2✔
249
        end = len;
2✔
250
      }
251
      String entry = userPath.substring(start, end);
5✔
252
      if (entry.endsWith(suffix)) {
4✔
253
        String prefix = "";
2✔
254
        int offset = 1;
2✔
255
        if (start > 0) {
2✔
256
          prefix = userPath.substring(0, start - 1);
7✔
257
          offset = 0;
2✔
258
        }
259
        if (end == len) {
3✔
260
          return prefix;
2✔
261
        } else {
262
          return removeEntryFromWindowsPath(prefix + userPath.substring(end + offset), suffix);
10✔
263
        }
264
      }
265
      start = end + 1;
4✔
266
    }
1✔
267
    return userPath;
2✔
268
  }
269

270
  /**
271
   * Adds ourselves to the shell RC (run-commands) configuration file.
272
   *
273
   * @param filename the name of the RC file.
274
   * @param ideRoot the IDE_ROOT {@link Path}.
275
   */
276
  private void addToShellRc(String filename, Path ideRoot, String extraLine) {
277

278
    modifyShellRc(filename, ideRoot, true, extraLine);
6✔
279
  }
1✔
280

281
  private void removeFromShellRc(String filename, Path ideRoot) {
282

283
    modifyShellRc(filename, ideRoot, false, null);
6✔
284
  }
1✔
285

286
  /**
287
   * Adds ourselves to the shell RC (run-commands) configuration file.
288
   *
289
   * @param filename the name of the RC file.
290
   * @param ideRoot the IDE_ROOT {@link Path}.
291
   */
292
  private void modifyShellRc(String filename, Path ideRoot, boolean add, String extraLine) {
293

294
    if (add) {
2✔
295
      this.context.info("Configuring IDEasy in {}", filename);
11✔
296
    } else {
297
      this.context.info("Removing IDEasy from {}", filename);
10✔
298
    }
299
    Path rcFile = this.context.getUserHome().resolve(filename);
6✔
300
    FileAccess fileAccess = this.context.getFileAccess();
4✔
301
    List<String> lines = fileAccess.readFileLines(rcFile);
4✔
302
    if (lines == null) {
2✔
303
      if (!add) {
2!
304
        return;
×
305
      }
306
      lines = new ArrayList<>();
5✔
307
    } else {
308
      // since it is unspecified if the returned List may be immutable we want to get sure
309
      lines = new ArrayList<>(lines);
5✔
310
    }
311
    Iterator<String> iterator = lines.iterator();
3✔
312
    int removeCount = 0;
2✔
313
    while (iterator.hasNext()) {
3✔
314
      String line = iterator.next();
4✔
315
      line = line.trim();
3✔
316
      if (isObsoleteRcLine(line)) {
3✔
317
        this.context.info("Removing obsolete line from {}: {}", filename, line);
14✔
318
        iterator.remove();
2✔
319
        removeCount++;
2✔
320
      } else if (line.equals(extraLine)) {
4✔
321
        extraLine = null;
2✔
322
      }
323
    }
1✔
324
    if (add) {
2✔
325
      if (extraLine != null) {
2!
326
        lines.add(extraLine);
×
327
      }
328
      if (!this.context.getSystemInfo().isWindows()) {
5✔
329
        lines.add("export IDE_ROOT=\"" + WindowsPathSyntax.MSYS.format(ideRoot) + "\"");
7✔
330
      }
331
      lines.add(BASH_CODE_SOURCE_FUNCTIONS);
4✔
332
    }
333
    fileAccess.writeFileLines(lines, rcFile);
4✔
334
    this.context.debug("Successfully updated {}", filename);
10✔
335
  }
1✔
336

337
  private static boolean isObsoleteRcLine(String line) {
338
    if (line.startsWith("alias ide=")) {
4!
339
      return true;
×
340
    } else if (line.startsWith("export IDE_ROOT=")) {
4!
341
      return true;
×
342
    } else if (line.equals("ide")) {
4✔
343
      return true;
2✔
344
    } else if (line.equals("ide init")) {
4✔
345
      return true;
2✔
346
    } else if (line.startsWith("source \"$IDE_ROOT/_ide/")) {
4✔
347
      return true;
2✔
348
    }
349
    return false;
2✔
350
  }
351

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

354
    Path artifactPath = cwd.resolve(artifactName);
4✔
355
    if (Files.exists(artifactPath)) {
5!
356
      installationArtifacts.add(artifactPath);
5✔
357
    } else if (required) {
×
358
      this.context.error("Missing required file {}", artifactName);
×
359
      return false;
×
360
    }
361
    return true;
2✔
362
  }
363

364
  private Path determineIdeRoot(Path cwd) {
365
    Path ideRoot = this.context.getIdeRoot();
4✔
366
    if (ideRoot == null) {
2!
367
      Path home = this.context.getUserHome();
4✔
368
      Path installRoot = home;
2✔
369
      if (this.context.getSystemInfo().isWindows()) {
5✔
370
        if (!cwd.startsWith(home)) {
4!
371
          installRoot = cwd.getRoot();
×
372
        }
373
      }
374
      ideRoot = installRoot.resolve(IdeContext.FOLDER_PROJECTS);
4✔
375
    } else {
1✔
376
      assert (Files.isDirectory(ideRoot)) : "IDE_ROOT directory does not exist!";
×
377
    }
378
    return ideRoot;
2✔
379
  }
380

381
  /**
382
   * Uninstalls IDEasy entirely from the system.
383
   */
384
  public void uninstallIdeasy() {
385

386
    Path ideRoot = this.context.getIdeRoot();
4✔
387
    removeFromShellRc(BASHRC, ideRoot);
4✔
388
    removeFromShellRc(ZSHRC, ideRoot);
4✔
389
    Path idePath = this.context.getIdePath();
4✔
390
    uninstallIdeasyWindowsEnv(ideRoot);
3✔
391
    uninstallIdeasyIdePath(idePath);
3✔
392
    deleteDownloadCache();
2✔
393
    this.context.success("IDEasy has been uninstalled from your system.");
4✔
394
    this.context.interaction("ATTENTION:\n"
10✔
395
        + "In order to prevent data-loss, we do not delete your projects and git repositories!\n"
396
        + "To entirely get rid of IDEasy, also check your IDE_ROOT folder at:\n"
397
        + "{}", ideRoot);
398
  }
1✔
399

400
  private void deleteDownloadCache() {
401
    Path downloadPath = this.context.getDownloadPath();
4✔
402
    this.context.info("Deleting download cache from {}", downloadPath);
10✔
403
    this.context.getFileAccess().delete(downloadPath);
5✔
404
  }
1✔
405

406
  private void uninstallIdeasyIdePath(Path idePath) {
407
    if (this.context.getSystemInfo().isWindows()) {
5✔
408
      this.context.newProcess().executable("bash").addArgs("-c",
17✔
409
          "sleep 10 && rm -rf \"" + WindowsPathSyntax.MSYS.format(idePath) + "\"").run(ProcessMode.BACKGROUND);
5✔
410
      this.context.interaction("To prevent windows file locking errors, we perform an asynchronous deletion of {} in background now.\n"
11✔
411
          + "Please close all terminals and wait a minute for the deletion to complete before running other commands.", idePath);
412
    } else {
413
      this.context.info("Finally deleting {}", idePath);
10✔
414
      this.context.getFileAccess().delete(idePath);
5✔
415
    }
416
  }
1✔
417

418
  private void uninstallIdeasyWindowsEnv(Path ideRoot) {
419
    if (!this.context.getSystemInfo().isWindows()) {
5✔
420
      return;
1✔
421
    }
422
    WindowsHelper helper = WindowsHelper.get(this.context);
4✔
423
    helper.removeUserEnvironmentValue(IdeVariables.IDE_ROOT.getName());
4✔
424
    String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
5✔
425
    if (userPath == null) {
2!
426
      this.context.error("Could not read user PATH from registry!");
×
427
    } else {
428
      this.context.info("Found user PATH={}", userPath);
10✔
429
      String newUserPath = userPath;
2✔
430
      if (!userPath.isEmpty()) {
3!
431
        SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
4✔
432
        path.removeEntries(s -> s.endsWith(IDE_BIN) || s.endsWith(IDE_INSTALLATION_BIN));
15!
433
        newUserPath = path.toString();
3✔
434
      }
435
      if (newUserPath.equals(userPath)) {
4!
436
        this.context.error("Could not find IDEasy in PATH:\n{}", userPath);
×
437
      } else {
438
        helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), newUserPath);
5✔
439
      }
440
    }
441
  }
1✔
442
}
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