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

devonfw / IDEasy / 30243693167

27 Jul 2026 06:44AM UTC coverage: 72.69% (+0.1%) from 72.561%
30243693167

Pull #2073

github

web-flow
Merge 27f1c50ad into dd45e9162
Pull Request #2073: #1676 import extra sdks automatically into ide

5009 of 7614 branches covered (65.79%)

Branch coverage included in aggregate %.

12899 of 17022 relevant lines covered (75.78%)

3.21 hits per line

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

82.62
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.ide.IdeToolCommandlet;
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
    this.context.verifyIdeMinVersion(true);
4✔
105

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

111
  private void reloadContext() {
112

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

116
  private void updateConf() {
117

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

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

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

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

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

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

160
  /**
161
   * 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
162
   * latest current commit ID in the file ".commit.id".
163
   */
164
  protected void updateSettings() {
165

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

174
  protected String getStepMessage() {
175

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

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

210
  private GitUrl getOrAskSettingsUrl() {
211

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

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

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

273
  private void initializeRepository(GitUrl gitUrl) {
274

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

296
  private void updateSoftware() {
297

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

306
  private void doUpdateSoftwareStep(Step step) {
307

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

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

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

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

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

354
      // After installing extra tools, synchronize them into all IDE workspaces
355
      List<String> ides = IdeVariables.CREATE_START_SCRIPTS.get(this.context);
6✔
356
      if (ides != null) {
2✔
357
        for (String ideName : ides) {
10✔
358
          ToolCommandlet ideCommandlet = commandletManager.getToolCommandlet(ideName);
4✔
359
          if (ideCommandlet instanceof IdeToolCommandlet) {
3!
360
            LOG.info("Synchronizing extra tools into {} workspace", ideName);
4✔
361
            ((IdeToolCommandlet) ideCommandlet).configureWorkspace();
3✔
362
          }
363
        }
1✔
364
      }
365
    }
366
  }
1✔
367

368
  private void installExtraToolInstallations(String tool, List<ExtraToolInstallation> extraInstallations) {
369

370
    CommandletManager commandletManager = this.context.getCommandletManager();
4✔
371
    FileAccess fileAccess = this.context.getFileAccess();
4✔
372
    Path extraPath = this.context.getSoftwareExtraPath();
4✔
373
    LocalToolCommandlet toolCommandlet = commandletManager.getRequiredLocalToolCommandlet(tool);
4✔
374
    for (ExtraToolInstallation extraInstallation : extraInstallations) {
10✔
375
      ToolInstallRequest request = new ToolInstallRequest(false);
5✔
376
      String edition = extraInstallation.edition();
3✔
377
      if (edition == null) {
2✔
378
        edition = toolCommandlet.getConfiguredEdition();
3✔
379
      }
380
      ToolEdition toolEdition = new ToolEdition(tool, edition);
6✔
381
      VersionIdentifier version = extraInstallation.version();
3✔
382
      request.setRequested(new ToolEditionAndVersion(toolEdition, version));
7✔
383
      Path extraToolPath = extraPath.resolve(tool);
4✔
384
      Path toolPath = extraToolPath.resolve(extraInstallation.name());
5✔
385
      request.setToolPathForExtraInstallation(toolPath);
3✔
386
      toolCommandlet.install(request);
4✔
387
    }
1✔
388
  }
1✔
389

390
  private void updateRepositories() {
391

392
    if (this.skipRepositories.isTrue()) {
4!
393
      if (this.forceRepositories.isTrue()) {
×
394
        LOG.warn("Options to skip and force repositories are incompatible and should not be combined. Ignoring --force-repositories to proceed.");
×
395
      }
396
      LOG.info("Skipping setup of repositories as specified by the user.");
×
397
      return;
×
398
    }
399
    RepositoryCommandlet repositoryCommandlet = this.context.getCommandletManager().getCommandlet(RepositoryCommandlet.class);
7✔
400
    repositoryCommandlet.reset();
2✔
401
    repositoryCommandlet.run();
2✔
402
  }
1✔
403

404
  private void createStartScripts() {
405

406
    List<String> ides = IdeVariables.CREATE_START_SCRIPTS.get(this.context);
6✔
407
    if (ides == null) {
2✔
408
      LOG.info("Variable CREATE_START_SCRIPTS is undefined - skipping start script creation.");
3✔
409
      return;
1✔
410
    }
411
    for (String ide : ides) {
10✔
412
      ToolCommandlet tool = this.context.getCommandletManager().getToolCommandlet(ide);
6✔
413
      if (tool == null) {
2!
414
        LOG.error("Undefined IDE '{}' configured in variable CREATE_START_SCRIPTS.", ide);
×
415
      } else {
416
        createStartScript(ide);
3✔
417
      }
418
    }
1✔
419
  }
1✔
420

421
  private void createStartScript(String ide) {
422

423
    LOG.info("Creating start scripts for {}", ide);
4✔
424
    Path workspaces = this.context.getIdeHome().resolve(IdeContext.FOLDER_WORKSPACES);
6✔
425
    try (Stream<Path> childStream = Files.list(workspaces)) {
3✔
426
      Iterator<Path> iterator = childStream.iterator();
3✔
427
      while (iterator.hasNext()) {
3✔
428
        Path child = iterator.next();
4✔
429
        if (Files.isDirectory(child)) {
5!
430
          createStartScript(ide, child.getFileName().toString());
6✔
431
        }
432
      }
1✔
433
    } catch (IOException e) {
×
434
      throw new RuntimeException("Failed to list children of directory " + workspaces, e);
×
435
    }
1✔
436
  }
1✔
437

438
  private void createStartScript(String ide, String workspace) {
439

440
    Path ideHome = this.context.getIdeHome();
4✔
441
    String scriptName = ide + "-" + workspace;
4✔
442
    boolean windows = this.context.getSystemInfo().isWindows();
5✔
443
    if (windows) {
2!
444
      scriptName = scriptName + ".bat";
×
445
    } else {
446
      scriptName = scriptName + ".sh";
3✔
447
    }
448
    Path scriptPath = ideHome.resolve(scriptName);
4✔
449
    if (Files.exists(scriptPath)) {
5!
450
      return;
×
451
    }
452
    String scriptContent;
453
    if (windows) {
2!
454
      scriptContent = "@echo off\r\n" + "pushd %~dp0\r\n" + "cd workspaces/" + workspace + "\r\n" + "call ide " + ide + "\r\n" + "popd\r\n";
×
455
    } else {
456
      scriptContent = "#!/usr/bin/env bash\n" + "cd \"$(dirname \"$0\")\"\n" + "cd workspaces/" + workspace + "\n" + "ideasy " + ide + "\n";
4✔
457
    }
458
    FileAccess fileAccess = this.context.getFileAccess();
4✔
459
    fileAccess.writeFileContent(scriptContent, scriptPath);
4✔
460
    fileAccess.makeExecutable(scriptPath);
3✔
461
  }
1✔
462

463
  /**
464
   * Judge if the repository is a code repository.
465
   *
466
   * @return true when the repository is a code repository, otherwise false.
467
   */
468
  protected boolean isCodeRepository() {
469
    return false;
2✔
470
  }
471

472
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc