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

devonfw / IDEasy / 28498115208

01 Jul 2026 06:26AM UTC coverage: 71.442% (+0.08%) from 71.359%
28498115208

Pull #2073

github

web-flow
Merge 1162c0b1f into 0709e0a80
Pull Request #2073: #1676 import extra sdks automatically into ide

4722 of 7312 branches covered (64.58%)

Branch coverage included in aggregate %.

12179 of 16345 relevant lines covered (74.51%)

3.15 hits per line

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

81.87
cli/src/main/java/com/devonfw/tools/ide/commandlet/AbstractUpdateCommandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.io.IOException;
4
import java.nio.file.Files;
5
import java.nio.file.Path;
6
import java.util.HashSet;
7
import java.util.Iterator;
8
import java.util.List;
9
import java.util.Set;
10
import java.util.stream.Stream;
11

12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14

15
import com.devonfw.tools.ide.cli.CliException;
16
import com.devonfw.tools.ide.context.AbstractIdeContext;
17
import com.devonfw.tools.ide.context.IdeContext;
18
import com.devonfw.tools.ide.context.IdeStartContextImpl;
19
import com.devonfw.tools.ide.git.GitContext;
20
import com.devonfw.tools.ide.git.GitUrl;
21
import com.devonfw.tools.ide.git.repository.RepositoryCommandlet;
22
import com.devonfw.tools.ide.io.FileAccess;
23
import com.devonfw.tools.ide.property.FlagProperty;
24
import com.devonfw.tools.ide.property.StringProperty;
25
import com.devonfw.tools.ide.step.Step;
26
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
27
import com.devonfw.tools.ide.tool.ToolCommandlet;
28
import com.devonfw.tools.ide.tool.ToolEdition;
29
import com.devonfw.tools.ide.tool.ToolEditionAndVersion;
30
import com.devonfw.tools.ide.tool.ToolInstallRequest;
31
import com.devonfw.tools.ide.tool.custom.CustomToolCommandlet;
32
import com.devonfw.tools.ide.tool.custom.CustomToolMetadata;
33
import com.devonfw.tools.ide.tool.extra.ExtraToolInstallation;
34
import com.devonfw.tools.ide.tool.extra.ExtraTools;
35
import com.devonfw.tools.ide.tool.extra.ExtraToolsMapper;
36
import com.devonfw.tools.ide.tool.intellij.Intellij;
37
import com.devonfw.tools.ide.variable.IdeVariables;
38
import com.devonfw.tools.ide.version.VersionIdentifier;
39

40
/**
41
 * Abstract {@link Commandlet} base-class for both {@link UpdateCommandlet} and {@link CreateCommandlet}.
42
 */
43
public abstract class AbstractUpdateCommandlet extends Commandlet {
44

45
  private static final Logger LOG = LoggerFactory.getLogger(AbstractUpdateCommandlet.class);
4✔
46

47
  private static final String MESSAGE_CODE_REPO_URL = """
48
      No code repository was given after '--code'.
49
      Further details can be found here: https://github.com/devonfw/IDEasy/blob/main/documentation/settings.adoc
50
      Please enter the code repository below that includes your settings folder.""";
51

52
  private static final String MESSAGE_SETTINGS_REPO_URL = """
53
      No settings found at {} and no SETTINGS_URL is defined.
54
      Further details can be found here: https://github.com/devonfw/IDEasy/blob/main/documentation/settings.adoc
55
      Please contact the technical lead of your project to get the SETTINGS_URL for your project to enter.
56
      In case you just want to test IDEasy you may simply hit return to install the default settings.""";
57

58
  /** {@link StringProperty} for the settings repository URL. */
59
  public final StringProperty settingsRepo;
60

61
  /** {@link FlagProperty} for skipping installation/updating of tools. */
62
  public final FlagProperty skipTools;
63

64
  /** {@link FlagProperty} for skipping the setup of git repositories. */
65
  public final FlagProperty skipRepositories;
66

67
  /** {@link FlagProperty} to force the update of the settings git repository. */
68
  public final FlagProperty forcePull;
69

70
  /** {@link FlagProperty} to force the installation/update of plugins. */
71
  public final FlagProperty forcePlugins;
72

73
  /** {@link FlagProperty} to force the setup of git repositories. */
74
  public final FlagProperty forceRepositories;
75

76
  /**
77
   * The constructor.
78
   *
79
   * @param context the {@link IdeContext}.
80
   */
81
  public AbstractUpdateCommandlet(IdeContext context) {
82

83
    super(context);
3✔
84
    addKeyword(getName());
4✔
85
    this.skipTools = add(new FlagProperty("--skip-tools"));
9✔
86
    this.skipRepositories = add(new FlagProperty("--skip-repositories"));
9✔
87
    this.forcePull = add(new FlagProperty("--force-pull"));
9✔
88
    this.forcePlugins = add(new FlagProperty("--force-plugins"));
9✔
89
    this.forceRepositories = add(new FlagProperty("--force-repositories"));
9✔
90
    this.settingsRepo = new StringProperty("", false, "settingsRepository");
8✔
91
  }
1✔
92

93
  @Override
94
  protected void doRun() {
95

96
    IdeStartContextImpl startContext = ((AbstractIdeContext) this.context).getStartContext();
5✔
97
    startContext.setForcePull(forcePull.isTrue());
5✔
98
    startContext.setForcePlugins(forcePlugins.isTrue());
5✔
99
    startContext.setForceRepositories(forceRepositories.isTrue());
5✔
100

101
    updateSettings();
2✔
102
    updateConf();
2✔
103
    reloadContext();
2✔
104

105
    updateSoftware();
2✔
106
    updateRepositories();
2✔
107
    createStartScripts();
2✔
108
  }
1✔
109

110
  private void reloadContext() {
111

112
    ((AbstractIdeContext) this.context).reload();
4✔
113
  }
1✔
114

115
  private void updateConf() {
116

117
    Path templatesFolder = this.context.getSettingsPath().resolve(IdeContext.FOLDER_TEMPLATES);
6✔
118
    if (!Files.exists(templatesFolder)) {
5✔
119
      Path legacyTemplatesFolder = this.context.getSettingsPath().resolve(IdeContext.FOLDER_LEGACY_TEMPLATES);
6✔
120
      if (Files.exists(legacyTemplatesFolder)) {
5!
121
        templatesFolder = legacyTemplatesFolder;
×
122
      } else {
123
        LOG.warn("Templates folder is missing in settings repository.");
3✔
124
        return;
1✔
125
      }
126
    }
127

128
    Step step = this.context.newStep("Copy configuration templates", templatesFolder);
11✔
129
    final Path finalTemplatesFolder = templatesFolder;
2✔
130
    step.run(() -> setupConf(finalTemplatesFolder, this.context.getIdeHome()));
13✔
131
  }
1✔
132

133
  private void setupConf(Path template, Path conf) {
134

135
    List<Path> children = this.context.getFileAccess().listChildren(template, f -> true);
9✔
136
    for (Path child : children) {
10✔
137

138
      String basename = child.getFileName().toString();
4✔
139
      Path confPath = conf.resolve(basename);
4✔
140

141
      if (Files.isDirectory(child)) {
5✔
142
        if (!Files.isDirectory(confPath)) {
5!
143
          this.context.getFileAccess().mkdirs(confPath);
5✔
144
        }
145
        setupConf(child, confPath);
5✔
146
      } else if (Files.isRegularFile(child)) {
5!
147
        if (Files.isRegularFile(confPath)) {
5!
148
          LOG.debug("Configuration {} already exists - skipping to copy from {}", confPath, child);
×
149
        } else {
150
          if (!basename.equals("settings.xml")) {
4!
151
            LOG.info("Copying template {} to {}.", child, conf);
5✔
152
            this.context.getFileAccess().copy(child, conf);
6✔
153
          }
154
        }
155
      }
156
    }
1✔
157
  }
1✔
158

159
  /**
160
   * Updates the settings repository in IDE_HOME/settings by either cloning if no such repository exists or pulling if the repository exists then saves the
161
   * latest current commit ID in the file ".commit.id".
162
   */
163
  protected void updateSettings() {
164

165
    boolean codeRepository = this.context.isSettingsCodeRepository();
4✔
166
    if (codeRepository && !(this.context.isForceMode() || forcePull.isTrue())) {
2!
167
      LOG.info("Skipping git pull in settings due to code repository. Use --force-pull to enforce pulling.");
×
168
      return;
×
169
    }
170
    this.context.newStep(getStepMessage()).run(() -> updateSettingsInStep(codeRepository));
14✔
171
  }
1✔
172

173
  protected String getStepMessage() {
174

175
    return "update (pull) settings repository";
2✔
176
  }
177

178
  private void updateSettingsInStep(boolean codeRepository) {
179
    Path settingsPath = this.context.getSettingsPath();
4✔
180
    if (!codeRepository) {
2!
181
      boolean settingsRepository = this.context.getGitContext().isGitRepo(settingsPath);
6✔
182
      if (!settingsRepository) {
2✔
183
        if (Files.exists(settingsPath)) {
5!
184
          if (!this.context.getFileAccess().isEmptyDir(settingsPath)) {
×
185
            this.context.askToContinue(
×
186
                "Your settings repository seems to be broken ('.git' folder not present). "
187
                    + "We can fix this by moving  your settings the backed up. You will be asked for the settings git URL and your settings will be cloned from scratch. "
188
                    + "Do you want to proceed?"
189
            );
190
          }
191
          this.context.getFileAccess().backup(settingsPath);
×
192
        }
193
        GitUrl gitUrl = getOrAskSettingsUrl();
3✔
194
        checkProjectNameConvention(gitUrl.getProjectName());
4✔
195
        initializeRepository(gitUrl);
3✔
196
        return;
1✔
197
      }
198
    }
199
    GitContext gitContext = this.context.getGitContext();
4✔
200
    if (gitContext.hasUntrackedFiles(settingsPath)) {
4!
201
      gitContext.pullSafelyWithStash(settingsPath);
×
202
    } else {
203
      gitContext.pull(settingsPath);
3✔
204
    }
205
    this.context.getGitContext().saveCurrentCommitId(settingsPath, this.context.getSettingsCommitIdPath());
8✔
206
  }
1✔
207

208
  private GitUrl getOrAskSettingsUrl() {
209

210
    String repository = this.settingsRepo.getValue();
5✔
211
    repository = handleDefaultRepository(repository);
4✔
212
    String userPromt;
213
    String defaultUrl;
214
    if (isCodeRepository()) {
3✔
215
      userPromt = "Code repository URL:";
2✔
216
      defaultUrl = null;
2✔
217
      LOG.info(MESSAGE_CODE_REPO_URL);
4✔
218
    } else {
219
      userPromt = "Settings URL [" + IdeContext.DEFAULT_SETTINGS_REPO_URL + "]:";
2✔
220
      defaultUrl = IdeContext.DEFAULT_SETTINGS_REPO_URL;
2✔
221
      LOG.info(MESSAGE_SETTINGS_REPO_URL, this.context.getSettingsPath());
6✔
222
    }
223
    GitUrl gitUrl = null;
2✔
224
    if (repository != null) {
2✔
225
      gitUrl = GitUrl.of(repository);
3✔
226
    }
227
    while ((gitUrl == null) || !gitUrl.isValid()) {
5!
228
      repository = this.context.askForInput(userPromt, defaultUrl);
6✔
229
      repository = handleDefaultRepository(repository);
4✔
230
      gitUrl = GitUrl.of(repository);
3✔
231
      if (!gitUrl.isValid()) {
3!
232
        LOG.warn("The input URL is not valid, please try again.");
×
233
      }
234
    }
235
    return gitUrl;
2✔
236
  }
237

238
  private String handleDefaultRepository(String repository) {
239
    if ("-".equals(repository)) {
4✔
240
      if (isCodeRepository()) {
3✔
241
        LOG.warn("'-' is found after '--code'. This is invalid.");
3✔
242
        repository = null;
3✔
243
      } else {
244
        LOG.info("'-' was found for settings repository, the default settings repository '{}' will be used.", IdeContext.DEFAULT_SETTINGS_REPO_URL);
4✔
245
        repository = IdeContext.DEFAULT_SETTINGS_REPO_URL;
2✔
246
      }
247
    }
248
    return repository;
2✔
249
  }
250

251
  private void checkProjectNameConvention(String projectName) {
252
    boolean isSettingsRepo = projectName.contains(IdeContext.SETTINGS_REPOSITORY_KEYWORD);
4✔
253
    boolean codeRepository = isCodeRepository();
3✔
254
    if (isSettingsRepo == codeRepository) {
3✔
255
      String warningTemplate;
256
      if (codeRepository) {
2✔
257
        warningTemplate = """
3✔
258
            Your git URL is pointing to the project name {} that contains the keyword '{}'.
259
            Therefore we assume that you did a mistake by adding the '--code' option to the ide project creation.
260
            Do you really want to create the project?""";
261
      } else {
262
        warningTemplate = """
2✔
263
            Your git URL is pointing to the project name {} that does not contain the keyword ''{}''.
264
            Therefore we assume that you forgot to add the '--code' option to the ide project creation.
265
            Do you really want to create the project?""";
266
      }
267
      this.context.askToContinue(warningTemplate, projectName, IdeContext.SETTINGS_REPOSITORY_KEYWORD);
14✔
268
    }
269
  }
1✔
270

271
  private void initializeRepository(GitUrl gitUrl) {
272

273
    GitContext gitContext = this.context.getGitContext();
4✔
274
    Path settingsPath = this.context.getSettingsPath();
4✔
275
    Path repoPath = settingsPath;
2✔
276
    boolean codeRepository = isCodeRepository();
3✔
277
    if (codeRepository) {
2✔
278
      // clone the given code repository into IDE_HOME/workspaces/main
279
      repoPath = context.getWorkspacePath().resolve(gitUrl.getProjectName());
7✔
280
    }
281
    gitContext.pullOrClone(gitUrl, repoPath);
4✔
282
    if (codeRepository) {
2✔
283
      // check for settings folder and create symlink to IDE_HOME/settings
284
      Path settingsFolder = repoPath.resolve(IdeContext.FOLDER_SETTINGS);
4✔
285
      if (Files.exists(settingsFolder)) {
5!
286
        context.getFileAccess().symlink(settingsFolder, settingsPath);
×
287
      } else {
288
        throw new CliException("Invalid code repository " + gitUrl + ": missing a settings folder at " + settingsFolder);
9✔
289
      }
290
    }
291
    this.context.getGitContext().saveCurrentCommitId(settingsPath, this.context.getSettingsCommitIdPath());
8✔
292
  }
1✔
293

294

295
  private void updateSoftware() {
296

297
    if (this.skipTools.isTrue()) {
4✔
298
      LOG.info("Skipping installation/update of tools as specified by the user.");
3✔
299
      return;
1✔
300
    }
301
    Step step = this.context.newStep("Install or update software");
5✔
302
    step.run(() -> doUpdateSoftwareStep(step));
10✔
303
  }
1✔
304

305
  private void doUpdateSoftwareStep(Step step) {
306

307
    Set<ToolCommandlet> toolCommandlets = new HashSet<>();
4✔
308
    CommandletManager commandletManager = this.context.getCommandletManager();
4✔
309
    // installed tools in IDE_HOME/software
310
    List<Path> softwarePaths = this.context.getFileAccess().listChildren(this.context.getSoftwarePath(), Files::isDirectory);
14✔
311
    for (Path softwarePath : softwarePaths) {
10✔
312
      String toolName = softwarePath.getFileName().toString();
4✔
313
      ToolCommandlet toolCommandlet = commandletManager.getToolCommandlet(toolName);
4✔
314
      if (toolCommandlet != null) {
2!
315
        toolCommandlets.add(toolCommandlet);
4✔
316
      }
317
    }
1✔
318

319
    // regular tools in $IDE_TOOLS
320
    List<String> regularTools = IdeVariables.IDE_TOOLS.get(this.context);
6✔
321
    if (regularTools != null) {
2!
322
      for (String regularTool : regularTools) {
10✔
323
        ToolCommandlet toolCommandlet = commandletManager.getToolCommandlet(regularTool);
4✔
324
        if (toolCommandlet == null) {
2!
325
          String displayName = (regularTool == null || regularTool.isBlank()) ? "<empty>" : "'" + regularTool + "'";
×
326
          LOG.error("Cannot install or update tool '{}''. No matching commandlet found. Please check your IDE_TOOLS configuration.", displayName);
×
327
        } else {
×
328
          toolCommandlets.add(toolCommandlet);
4✔
329
        }
330
      }
1✔
331
    }
332

333
    // custom tools in ide-custom-tools.json
334
    for (CustomToolMetadata customTool : this.context.getCustomToolRepository().getTools()) {
9!
335
      CustomToolCommandlet customToolCommandlet = new CustomToolCommandlet(this.context, customTool);
×
336
      toolCommandlets.add(customToolCommandlet);
×
337
    }
×
338

339
    // update/install the toolCommandlets
340
    for (ToolCommandlet toolCommandlet : toolCommandlets) {
10✔
341
      this.context.newStep("Install " + toolCommandlet.getName()).run(() -> toolCommandlet.install(false));
15✔
342
    }
1✔
343

344
    ExtraTools extraTools = ExtraToolsMapper.get().loadJsonFromFolder(this.context.getSettingsPath());
7✔
345
    if (extraTools != null) {
2✔
346
      List<String> toolNames = extraTools.getSortedToolNames();
3✔
347
      LOG.info("Found extra installation of the following tools: {}", toolNames);
4✔
348
      for (String tool : toolNames) {
10✔
349
        List<ExtraToolInstallation> installations = extraTools.getExtraInstallations(tool);
4✔
350
        this.context.newStep("Install extra version(s) of " + tool).run(() -> installExtraToolInstallations(tool, installations));
16✔
351
      }
1✔
352
    }
353

354
    synchronizeIdeExtraToolInstallations(extraTools);
3✔
355
  }
1✔
356

357
  private void installExtraToolInstallations(String tool, List<ExtraToolInstallation> extraInstallations) {
358

359
    CommandletManager commandletManager = this.context.getCommandletManager();
4✔
360
    FileAccess fileAccess = this.context.getFileAccess();
4✔
361
    Path extraPath = this.context.getSoftwareExtraPath();
4✔
362
    LocalToolCommandlet toolCommandlet = commandletManager.getRequiredLocalToolCommandlet(tool);
4✔
363
    for (ExtraToolInstallation extraInstallation : extraInstallations) {
10✔
364
      ToolInstallRequest request = new ToolInstallRequest(false);
5✔
365
      String edition = extraInstallation.edition();
3✔
366
      if (edition == null) {
2✔
367
        edition = toolCommandlet.getConfiguredEdition();
3✔
368
      }
369
      ToolEdition toolEdition = new ToolEdition(tool, edition);
6✔
370
      VersionIdentifier version = extraInstallation.version();
3✔
371
      request.setRequested(new ToolEditionAndVersion(toolEdition, version));
7✔
372
      Path extraToolPath = extraPath.resolve(tool);
4✔
373
      Path toolPath = extraToolPath.resolve(extraInstallation.name());
5✔
374
      request.setToolPathForExtraInstallation(toolPath);
3✔
375
      toolCommandlet.install(request);
4✔
376
    }
1✔
377
  }
1✔
378

379
  private void synchronizeIdeExtraToolInstallations(ExtraTools extraTools) {
380

381
    if (extraTools == null) {
2✔
382
      return;
1✔
383
    }
384

385
    Path workspacePath = this.context.getWorkspacePath();
4✔
386
    if (!Files.isDirectory(workspacePath)) {
5!
387
      LOG.debug("Skipping IDE extra SDK synchronization because workspace path does not exist: {}", workspacePath);
×
388
      return;
×
389
    }
390

391
    Intellij intellij = this.context.getCommandletManager().getCommandlet(Intellij.class);
7✔
392
    if (intellij != null) {
2!
393
      this.context.newStep("Synchronize IntelliJ extra SDKs").run(() -> intellij.synchronizeExtraToolInstallations(workspacePath));
13✔
394
    }
395

396
    // later:
397
    // Eclipse eclipse = ...
398
    // VsCode vscode = ...
399
  }
1✔
400

401
  private void updateRepositories() {
402

403
    if (this.skipRepositories.isTrue()) {
4!
404
      if (this.forceRepositories.isTrue()) {
×
405
        LOG.warn("Options to skip and force repositories are incompatible and should not be combined. Ignoring --force-repositories to proceed.");
×
406
      }
407
      LOG.info("Skipping setup of repositories as specified by the user.");
×
408
      return;
×
409
    }
410
    RepositoryCommandlet repositoryCommandlet = this.context.getCommandletManager().getCommandlet(RepositoryCommandlet.class);
7✔
411
    repositoryCommandlet.reset();
2✔
412
    repositoryCommandlet.run();
2✔
413
  }
1✔
414

415
  private void createStartScripts() {
416

417
    List<String> ides = IdeVariables.CREATE_START_SCRIPTS.get(this.context);
6✔
418
    if (ides == null) {
2✔
419
      LOG.info("Variable CREATE_START_SCRIPTS is undefined - skipping start script creation.");
3✔
420
      return;
1✔
421
    }
422
    for (String ide : ides) {
10✔
423
      ToolCommandlet tool = this.context.getCommandletManager().getToolCommandlet(ide);
6✔
424
      if (tool == null) {
2!
425
        LOG.error("Undefined IDE '{}' configured in variable CREATE_START_SCRIPTS.", ide);
×
426
      } else {
427
        createStartScript(ide);
3✔
428
      }
429
    }
1✔
430
  }
1✔
431

432
  private void createStartScript(String ide) {
433

434
    LOG.info("Creating start scripts for {}", ide);
4✔
435
    Path workspaces = this.context.getIdeHome().resolve(IdeContext.FOLDER_WORKSPACES);
6✔
436
    try (Stream<Path> childStream = Files.list(workspaces)) {
3✔
437
      Iterator<Path> iterator = childStream.iterator();
3✔
438
      while (iterator.hasNext()) {
3✔
439
        Path child = iterator.next();
4✔
440
        if (Files.isDirectory(child)) {
5!
441
          createStartScript(ide, child.getFileName().toString());
6✔
442
        }
443
      }
1✔
444
    } catch (IOException e) {
×
445
      throw new RuntimeException("Failed to list children of directory " + workspaces, e);
×
446
    }
1✔
447
  }
1✔
448

449
  private void createStartScript(String ide, String workspace) {
450

451
    Path ideHome = this.context.getIdeHome();
4✔
452
    String scriptName = ide + "-" + workspace;
4✔
453
    boolean windows = this.context.getSystemInfo().isWindows();
5✔
454
    if (windows) {
2!
455
      scriptName = scriptName + ".bat";
×
456
    } else {
457
      scriptName = scriptName + ".sh";
3✔
458
    }
459
    Path scriptPath = ideHome.resolve(scriptName);
4✔
460
    if (Files.exists(scriptPath)) {
5!
461
      return;
×
462
    }
463
    String scriptContent;
464
    if (windows) {
2!
465
      scriptContent = "@echo off\r\n" + "pushd %~dp0\r\n" + "cd workspaces/" + workspace + "\r\n" + "call ide " + ide + "\r\n" + "popd\r\n";
×
466
    } else {
467
      scriptContent = "#!/usr/bin/env bash\n" + "cd \"$(dirname \"$0\")\"\n" + "cd workspaces/" + workspace + "\n" + "ideasy " + ide + "\n";
4✔
468
    }
469
    FileAccess fileAccess = this.context.getFileAccess();
4✔
470
    fileAccess.writeFileContent(scriptContent, scriptPath);
4✔
471
    fileAccess.makeExecutable(scriptPath);
3✔
472
  }
1✔
473

474
  /**
475
   * Judge if the repository is a code repository.
476
   *
477
   * @return true when the repository is a code repository, otherwise false.
478
   */
479
  protected boolean isCodeRepository() {
480
    return false;
2✔
481
  }
482

483
}
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