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

devonfw / IDEasy / 13518757257

25 Feb 2025 10:12AM UTC coverage: 68.203% (+0.2%) from 68.046%
13518757257

Pull #1071

github

web-flow
Merge 424a5bd57 into cbacec943
Pull Request #1071: #789: implement uninstall of IDEasy

3025 of 4885 branches covered (61.92%)

Branch coverage included in aggregate %.

7854 of 11066 relevant lines covered (70.97%)

3.08 hits per line

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

78.65
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.tool.mvn.MvnArtifact;
19
import com.devonfw.tools.ide.tool.mvn.MvnBasedLocalToolCommandlet;
20
import com.devonfw.tools.ide.tool.repository.MavenRepository;
21
import com.devonfw.tools.ide.variable.IdeVariables;
22
import com.devonfw.tools.ide.version.IdeVersion;
23
import com.devonfw.tools.ide.version.VersionIdentifier;
24

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

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

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

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

42
  private final UpgradeMode mode;
43

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

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

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

65
  @Override
66
  public VersionIdentifier getInstalledVersion() {
67

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

71
  @Override
72
  public String getConfiguredEdition() {
73

74
    return this.tool;
×
75
  }
76

77
  @Override
78
  public VersionIdentifier getConfiguredVersion() {
79

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

95
  @Override
96
  public Path getToolPath() {
97

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

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

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

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

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

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

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

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

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

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

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

253
    modifyShellRc(filename, ideRoot, true, extraLine);
6✔
254
  }
1✔
255

256
  private void removeFromShellRc(String filename, Path ideRoot) {
257

258
    modifyShellRc(filename, ideRoot, false, null);
6✔
259
  }
1✔
260

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

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

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

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

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

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

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

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

375
  private void uninstallIdeasyWindowsEnv(Path ideRoot) {
376
    if (!this.context.getSystemInfo().isWindows()) {
5✔
377
      return;
1✔
378
    }
379
    WindowsHelper helper = WindowsHelper.get(this.context);
4✔
380
    helper.removeUserEnvironmentValue(IdeVariables.IDE_ROOT.getName());
4✔
381
    String userPath = helper.getUserEnvironmentValue(IdeVariables.PATH.getName());
5✔
382
    if (userPath == null) {
2!
383
      this.context.error("Could not read user PATH from registry!");
×
384
    } else {
385
      this.context.info("Found user PATH={}", userPath);
10✔
386
      String newUserPath = userPath;
2✔
387
      if (!userPath.isEmpty()) {
3!
388
        SimpleSystemPath path = SimpleSystemPath.of(userPath, ';');
4✔
389
        path.removeEntries(s -> s.endsWith(IDE_BIN) || s.endsWith(IDE_INSTALLATION_BIN));
15!
390
        newUserPath = path.toString();
3✔
391
      }
392
      if (newUserPath.equals(userPath)) {
4!
393
        this.context.error("Could not find IDEasy in PATH:\n{}", userPath);
×
394
      } else {
395
        helper.setUserEnvironmentValue(IdeVariables.PATH.getName(), newUserPath);
5✔
396
      }
397
    }
398
  }
1✔
399
}
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

© 2025 Coveralls, Inc