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

devonfw / IDEasy / 14649671809

24 Apr 2025 07:06PM UTC coverage: 67.448% (+0.01%) from 67.434%
14649671809

push

github

web-flow
#1208: #1231: write version file on installation (#1265)

3079 of 4970 branches covered (61.95%)

Branch coverage included in aggregate %.

7911 of 11324 relevant lines covered (69.86%)

3.06 hits per line

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

79.66
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.io.FileAccess;
16
import com.devonfw.tools.ide.os.WindowsHelper;
17
import com.devonfw.tools.ide.os.WindowsPathSyntax;
18
import com.devonfw.tools.ide.process.ProcessMode;
19
import com.devonfw.tools.ide.tool.mvn.MvnArtifact;
20
import com.devonfw.tools.ide.tool.mvn.MvnBasedLocalToolCommandlet;
21
import com.devonfw.tools.ide.tool.repository.MavenRepository;
22
import com.devonfw.tools.ide.variable.IdeVariables;
23
import com.devonfw.tools.ide.version.IdeVersion;
24
import com.devonfw.tools.ide.version.VersionIdentifier;
25

26
/**
27
 * {@link MvnBasedLocalToolCommandlet} for IDEasy (ide-cli).
28
 */
29
public class IdeasyCommandlet extends MvnBasedLocalToolCommandlet {
30

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

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

36
  /** The {@link #getName() tool name}. */
37
  public static final String TOOL_NAME = "ideasy";
38
  public static final String BASHRC = ".bashrc";
39
  public static final String ZSHRC = ".zshrc";
40
  public static final String IDE_BIN = "\\_ide\\bin";
41
  public static final String IDE_INSTALLATION_BIN = "\\_ide\\installation\\bin";
42

43
  private final UpgradeMode mode;
44

45
  /**
46
   * The constructor.
47
   *
48
   * @param context the {@link IdeContext}.
49
   */
50
  public IdeasyCommandlet(IdeContext context) {
51
    this(context, UpgradeMode.STABLE);
4✔
52
  }
1✔
53

54
  /**
55
   * The constructor.
56
   *
57
   * @param context the {@link IdeContext}.
58
   * @param mode the {@link UpgradeMode}.
59
   */
60
  public IdeasyCommandlet(IdeContext context, UpgradeMode mode) {
61

62
    super(context, TOOL_NAME, ARTIFACT, Set.of(Tag.PRODUCTIVITY, Tag.IDE));
8✔
63
    this.mode = mode;
3✔
64
  }
1✔
65

66
  @Override
67
  public VersionIdentifier getInstalledVersion() {
68

69
    return IdeVersion.getVersionIdentifier();
2✔
70
  }
71

72
  @Override
73
  public String getConfiguredEdition() {
74

75
    return this.tool;
×
76
  }
77

78
  @Override
79
  public VersionIdentifier getConfiguredVersion() {
80

81
    UpgradeMode upgradeMode = this.mode;
×
82
    if (upgradeMode == null) {
×
83
      if (IdeVersion.getVersionString().contains("SNAPSHOT")) {
×
84
        upgradeMode = UpgradeMode.SNAPSHOT;
×
85
      } else {
86
        if (IdeVersion.getVersionIdentifier().getDevelopmentPhase().isStable()) {
×
87
          upgradeMode = UpgradeMode.STABLE;
×
88
        } else {
89
          upgradeMode = UpgradeMode.UNSTABLE;
×
90
        }
91
      }
92
    }
93
    return upgradeMode.getVersion();
×
94
  }
95

96
  @Override
97
  public Path getToolPath() {
98

99
    return this.context.getIdeInstallationPath();
×
100
  }
101

102
  @Override
103
  public boolean install(boolean silent) {
104

105
    if (IdeVersion.isUndefined()) {
2!
106
      this.context.warning("You are using IDEasy version {} which indicates local development - skipping upgrade.", IdeVersion.getVersionString());
10✔
107
      return false;
2✔
108
    }
109
    return super.install(silent);
×
110
  }
111

112
  /**
113
   * @return the latest released {@link VersionIdentifier version} of IDEasy.
114
   */
115
  public VersionIdentifier getLatestVersion() {
116

117
    VersionIdentifier currentVersion = IdeVersion.getVersionIdentifier();
2✔
118
    if (IdeVersion.isUndefined()) {
2!
119
      return currentVersion;
2✔
120
    }
121
    VersionIdentifier configuredVersion = getConfiguredVersion();
×
122
    return getToolRepository().resolveVersion(this.tool, getConfiguredEdition(), configuredVersion, this);
×
123
  }
124

125
  /**
126
   * Checks if an update is available and logs according information.
127
   *
128
   * @return {@code true} if an update is available, {@code false} otherwise.
129
   */
130
  public boolean checkIfUpdateIsAvailable() {
131
    VersionIdentifier installedVersion = getInstalledVersion();
3✔
132
    VersionIdentifier latestVersion = getLatestVersion();
3✔
133
    if (installedVersion.equals(latestVersion)) {
4!
134
      this.context.success("Your version of IDEasy is {} which is the latest released version.", installedVersion);
10✔
135
      return false;
2✔
136
    } else {
137
      this.context.interaction("Your version of IDEasy is {} but version {} is available. Please run the following command to upgrade to the latest version:\n"
×
138
          + "ide upgrade", installedVersion, latestVersion);
139
      return true;
×
140
    }
141
  }
142

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

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

216
  static String removeObsoleteEntryFromWindowsPath(String userPath) {
217
    return removeEntryFromWindowsPath(userPath, IDE_BIN);
4✔
218
  }
219

220
  static String removeEntryFromWindowsPath(String userPath, String suffix) {
221
    int len = userPath.length();
3✔
222
    int start = 0;
2✔
223
    while ((start >= 0) && (start < len)) {
5!
224
      int end = userPath.indexOf(';', start);
5✔
225
      if (end < 0) {
2✔
226
        end = len;
2✔
227
      }
228
      String entry = userPath.substring(start, end);
5✔
229
      if (entry.endsWith(suffix)) {
4✔
230
        String prefix = "";
2✔
231
        int offset = 1;
2✔
232
        if (start > 0) {
2✔
233
          prefix = userPath.substring(0, start - 1);
7✔
234
          offset = 0;
2✔
235
        }
236
        if (end == len) {
3✔
237
          return prefix;
2✔
238
        } else {
239
          return removeEntryFromWindowsPath(prefix + userPath.substring(end + offset), suffix);
10✔
240
        }
241
      }
242
      start = end + 1;
4✔
243
    }
1✔
244
    return userPath;
2✔
245
  }
246

247
  /**
248
   * Adds ourselves to the shell RC (run-commands) configuration file.
249
   *
250
   * @param filename the name of the RC file.
251
   * @param ideRoot the IDE_ROOT {@link Path}.
252
   */
253
  private void addToShellRc(String filename, Path ideRoot, String extraLine) {
254

255
    modifyShellRc(filename, ideRoot, true, extraLine);
6✔
256
  }
1✔
257

258
  private void removeFromShellRc(String filename, Path ideRoot) {
259

260
    modifyShellRc(filename, ideRoot, false, null);
6✔
261
  }
1✔
262

263
  /**
264
   * Adds ourselves to the shell RC (run-commands) configuration file.
265
   *
266
   * @param filename the name of the RC file.
267
   * @param ideRoot the IDE_ROOT {@link Path}.
268
   */
269
  private void modifyShellRc(String filename, Path ideRoot, boolean add, String extraLine) {
270

271
    if (add) {
2✔
272
      this.context.info("Configuring IDEasy in {}", filename);
11✔
273
    } else {
274
      this.context.info("Removing IDEasy from {}", filename);
10✔
275
    }
276
    Path rcFile = this.context.getUserHome().resolve(filename);
6✔
277
    FileAccess fileAccess = this.context.getFileAccess();
4✔
278
    List<String> lines = fileAccess.readFileLines(rcFile);
4✔
279
    if (lines == null) {
2✔
280
      if (!add) {
2!
281
        return;
×
282
      }
283
      lines = new ArrayList<>();
5✔
284
    } else {
285
      // since it is unspecified if the returned List may be immutable we want to get sure
286
      lines = new ArrayList<>(lines);
5✔
287
    }
288
    Iterator<String> iterator = lines.iterator();
3✔
289
    int removeCount = 0;
2✔
290
    while (iterator.hasNext()) {
3✔
291
      String line = iterator.next();
4✔
292
      line = line.trim();
3✔
293
      if (isObsoleteRcLine(line)) {
3✔
294
        this.context.info("Removing obsolete line from {}: {}", filename, line);
14✔
295
        iterator.remove();
2✔
296
        removeCount++;
2✔
297
      } else if (line.equals(extraLine)) {
4✔
298
        extraLine = null;
2✔
299
      }
300
    }
1✔
301
    if (add) {
2✔
302
      if (extraLine != null) {
2!
303
        lines.add(extraLine);
×
304
      }
305
      if (!this.context.getSystemInfo().isWindows()) {
5✔
306
        lines.add("export IDE_ROOT=\"" + WindowsPathSyntax.MSYS.format(ideRoot) + "\"");
7✔
307
      }
308
      lines.add(BASH_CODE_SOURCE_FUNCTIONS);
4✔
309
    }
310
    fileAccess.writeFileLines(lines, rcFile);
4✔
311
    this.context.debug("Successfully updated {}", filename);
10✔
312
  }
1✔
313

314
  private static boolean isObsoleteRcLine(String line) {
315
    if (line.startsWith("alias ide=")) {
4!
316
      return true;
×
317
    } else if (line.startsWith("export IDE_ROOT=")) {
4!
318
      return true;
×
319
    } else if (line.equals("ide")) {
4✔
320
      return true;
2✔
321
    } else if (line.equals("ide init")) {
4✔
322
      return true;
2✔
323
    } else if (line.startsWith("source \"$IDE_ROOT/_ide/")) {
4✔
324
      return true;
2✔
325
    }
326
    return false;
2✔
327
  }
328

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

331
    Path artifactPath = cwd.resolve(artifactName);
4✔
332
    if (Files.exists(artifactPath)) {
5!
333
      installationArtifacts.add(artifactPath);
5✔
334
    } else if (required) {
×
335
      this.context.error("Missing required file {}", artifactName);
×
336
      return false;
×
337
    }
338
    return true;
2✔
339
  }
340

341
  private Path determineIdeRoot(Path cwd) {
342
    Path ideRoot = this.context.getIdeRoot();
4✔
343
    if (ideRoot == null) {
2!
344
      Path home = this.context.getUserHome();
4✔
345
      Path installRoot = home;
2✔
346
      if (this.context.getSystemInfo().isWindows()) {
5✔
347
        if (!cwd.startsWith(home)) {
4!
348
          installRoot = cwd.getRoot();
×
349
        }
350
      }
351
      ideRoot = installRoot.resolve(IdeContext.FOLDER_PROJECTS);
4✔
352
    } else {
1✔
353
      assert (Files.isDirectory(ideRoot)) : "IDE_ROOT directory does not exist!";
×
354
    }
355
    return ideRoot;
2✔
356
  }
357

358
  /**
359
   * Uninstalls IDEasy entirely from the system.
360
   */
361
  public void uninstallIdeasy() {
362

363
    Path ideRoot = this.context.getIdeRoot();
4✔
364
    removeFromShellRc(BASHRC, ideRoot);
4✔
365
    removeFromShellRc(ZSHRC, ideRoot);
4✔
366
    Path idePath = this.context.getIdePath();
4✔
367
    uninstallIdeasyWindowsEnv(ideRoot);
3✔
368
    uninstallIdeasyIdePath(idePath);
3✔
369
    deleteDownloadCache();
2✔
370
    this.context.success("IDEasy has been uninstalled from your system.");
4✔
371
    this.context.interaction("ATTENTION:\n"
10✔
372
        + "In order to prevent data-loss, we do not delete your projects and git repositories!\n"
373
        + "To entirely get rid of IDEasy, also check your IDE_ROOT folder at:\n"
374
        + "{}", ideRoot);
375
  }
1✔
376

377
  private void deleteDownloadCache() {
378
    Path downloadPath = this.context.getDownloadPath();
4✔
379
    this.context.info("Deleting download cache from {}", downloadPath);
10✔
380
    this.context.getFileAccess().delete(downloadPath);
5✔
381
  }
1✔
382

383
  private void uninstallIdeasyIdePath(Path idePath) {
384
    if (this.context.getSystemInfo().isWindows()) {
5✔
385
      this.context.newProcess().executable("bash").addArgs("-c",
17✔
386
          "sleep 10 && rm -rf \"" + WindowsPathSyntax.MSYS.format(idePath) + "\"").run(ProcessMode.BACKGROUND);
5✔
387
      this.context.interaction("To prevent windows file locking errors, we perform an asynchronous deletion of {} in background now.\n"
11✔
388
          + "Please close all terminals and wait a minute for the deletion to complete before running other commands.", idePath);
389
    } else {
390
      this.context.info("Finally deleting {}", idePath);
10✔
391
      this.context.getFileAccess().delete(idePath);
5✔
392
    }
393
  }
1✔
394

395
  private void uninstallIdeasyWindowsEnv(Path ideRoot) {
396
    if (!this.context.getSystemInfo().isWindows()) {
5✔
397
      return;
1✔
398
    }
399
    WindowsHelper helper = WindowsHelper.get(this.context);
4✔
400
    helper.removeUserEnvironmentValue(IdeVariables.IDE_ROOT.getName());
4✔
401
    String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
5✔
402
    if (userPath == null) {
2!
403
      this.context.error("Could not read user PATH from registry!");
×
404
    } else {
405
      this.context.info("Found user PATH={}", userPath);
10✔
406
      String newUserPath = userPath;
2✔
407
      if (!userPath.isEmpty()) {
3!
408
        SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
4✔
409
        path.removeEntries(s -> s.endsWith(IDE_BIN) || s.endsWith(IDE_INSTALLATION_BIN));
15!
410
        newUserPath = path.toString();
3✔
411
      }
412
      if (newUserPath.equals(userPath)) {
4!
413
        this.context.error("Could not find IDEasy in PATH:\n{}", userPath);
×
414
      } else {
415
        helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), newUserPath);
5✔
416
      }
417
    }
418
  }
1✔
419
}
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