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

devonfw / IDEasy / 22860792264

09 Mar 2026 03:25PM UTC coverage: 70.268% (-0.2%) from 70.481%
22860792264

push

github

web-flow
#404: #1713: advanced logging (#1714)

Co-authored-by: Kian <adasd>
Co-authored-by: KianRolf <kian.loroff@capgemini.com>
Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>
Co-authored-by: jan-vcapgemini <jan-vincent.hoelzle@capgemini.com>

4068 of 6386 branches covered (63.7%)

Branch coverage included in aggregate %.

10604 of 14494 relevant lines covered (73.16%)

3.08 hits per line

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

83.13
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.variable.IdeVariables;
37
import com.devonfw.tools.ide.version.VersionIdentifier;
38

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

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

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

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

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

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

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

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

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

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

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

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

92
  @Override
93
  protected void doRun() {
94

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

100
    if (!this.context.isSettingsRepositorySymlinkOrJunction() || this.context.isForceMode() || forcePull.isTrue()) {
4 !
101
      updateSettings();
2 ✔
102
    }
103
    updateConf();
2 ✔
104
    reloadContext();
2 ✔
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
    this.context.newStep(getStepMessage()).run(this::updateSettingsInStep);
9 ✔
167
  }
1 ✔
168

169
  protected String getStepMessage() {
170

171
    return "update (pull) settings repository";
2 ✔
172
  }
173

174
  private void updateSettingsInStep() {
175
    Path settingsPath = this.context.getSettingsPath();
4 ✔
176
    GitContext gitContext = this.context.getGitContext();
4 ✔
177
    // here we do not use pullOrClone to prevent asking a pointless question for repository URL...
178
    if (Files.isDirectory(settingsPath) && !this.context.getFileAccess().isEmptyDir(settingsPath)) {
11 !
179
      if (this.context.isForcePull() || this.context.isForceMode() || Files.isDirectory(settingsPath.resolve(GitContext.GIT_FOLDER))) {
15 !
180
        gitContext.pull(settingsPath);
×
181
        this.context.getGitContext().saveCurrentCommitId(settingsPath, this.context.getSettingsCommitIdPath());
×
182
      } else {
183
        LOG.info("Skipping git pull in settings due to code repository. Use --force-pull to enforce pulling.");
4 ✔
184
      }
185
    } else {
186
      GitUrl gitUrl = getOrAskSettingsUrl();
3 ✔
187
      checkProjectNameConvention(gitUrl.getProjectName());
4 ✔
188
      initializeRepository(gitUrl);
3 ✔
189
    }
190
  }
1 ✔
191

192
  private GitUrl getOrAskSettingsUrl() {
193

194
    String repository = this.settingsRepo.getValue();
5 ✔
195
    repository = handleDefaultRepository(repository);
4 ✔
196
    String userPromt;
197
    String defaultUrl;
198
    if (isCodeRepository()) {
3 ✔
199
      userPromt = "Code repository URL:";
2 ✔
200
      defaultUrl = null;
2 ✔
201
      LOG.info(MESSAGE_CODE_REPO_URL);
4 ✔
202
    } else {
203
      userPromt = "Settings URL [" + IdeContext.DEFAULT_SETTINGS_REPO_URL + "]:";
2 ✔
204
      defaultUrl = IdeContext.DEFAULT_SETTINGS_REPO_URL;
2 ✔
205
      LOG.info(MESSAGE_SETTINGS_REPO_URL, this.context.getSettingsPath());
6 ✔
206
    }
207
    GitUrl gitUrl = null;
2 ✔
208
    if (repository != null) {
2 ✔
209
      gitUrl = GitUrl.of(repository);
3 ✔
210
    }
211
    while ((gitUrl == null) || !gitUrl.isValid()) {
5 !
212
      repository = this.context.askForInput(userPromt, defaultUrl);
6 ✔
213
      repository = handleDefaultRepository(repository);
4 ✔
214
      gitUrl = GitUrl.of(repository);
3 ✔
215
      if (!gitUrl.isValid()) {
3 !
216
        LOG.warn("The input URL is not valid, please try again.");
×
217
      }
218
    }
219
    return gitUrl;
2 ✔
220
  }
221

222
  private String handleDefaultRepository(String repository) {
223
    if ("-".equals(repository)) {
4 ✔
224
      if (isCodeRepository()) {
3 ✔
225
        LOG.warn("'-' is found after '--code'. This is invalid.");
3 ✔
226
        repository = null;
3 ✔
227
      } else {
228
        LOG.info("'-' was found for settings repository, the default settings repository '{}' will be used.", IdeContext.DEFAULT_SETTINGS_REPO_URL);
4 ✔
229
        repository = IdeContext.DEFAULT_SETTINGS_REPO_URL;
2 ✔
230
      }
231
    }
232
    return repository;
2 ✔
233
  }
234

235
  private void checkProjectNameConvention(String projectName) {
236
    boolean isSettingsRepo = projectName.contains(IdeContext.SETTINGS_REPOSITORY_KEYWORD);
4 ✔
237
    boolean codeRepository = isCodeRepository();
3 ✔
238
    if (isSettingsRepo == codeRepository) {
3 ✔
239
      String warningTemplate;
240
      if (codeRepository) {
2 ✔
241
        warningTemplate = """
3 ✔
242
            Your git URL is pointing to the project name {} that contains the keyword '{}'.
243
            Therefore we assume that you did a mistake by adding the '--code' option to the ide project creation.
244
            Do you really want to create the project?""";
245
      } else {
246
        warningTemplate = """
2 ✔
247
            Your git URL is pointing to the project name {} that does not contain the keyword ''{}''.
248
            Therefore we assume that you forgot to add the '--code' option to the ide project creation.
249
            Do you really want to create the project?""";
250
      }
251
      this.context.askToContinue(warningTemplate, projectName,
14 ✔
252
          IdeContext.SETTINGS_REPOSITORY_KEYWORD);
253
    }
254
  }
1 ✔
255

256
  private void initializeRepository(GitUrl gitUrl) {
257

258
    GitContext gitContext = this.context.getGitContext();
4 ✔
259
    Path settingsPath = this.context.getSettingsPath();
4 ✔
260
    Path repoPath = settingsPath;
2 ✔
261
    boolean codeRepository = isCodeRepository();
3 ✔
262
    if (codeRepository) {
2 ✔
263
      // clone the given code repository into IDE_HOME/workspaces/main
264
      repoPath = context.getWorkspacePath().resolve(gitUrl.getProjectName());
7 ✔
265
    }
266
    gitContext.pullOrClone(gitUrl, repoPath);
4 ✔
267
    if (codeRepository) {
2 ✔
268
      // check for settings folder and create symlink to IDE_HOME/settings
269
      Path settingsFolder = repoPath.resolve(IdeContext.FOLDER_SETTINGS);
4 ✔
270
      if (Files.exists(settingsFolder)) {
5 !
271
        context.getFileAccess().symlink(settingsFolder, settingsPath);
×
272
      } else {
273
        throw new CliException("Invalid code repository " + gitUrl + ": missing a settings folder at " + settingsFolder);
9 ✔
274
      }
275
    }
276
    this.context.getGitContext().saveCurrentCommitId(settingsPath, this.context.getSettingsCommitIdPath());
8 ✔
277
  }
1 ✔
278

279

280
  private void updateSoftware() {
281

282
    if (this.skipTools.isTrue()) {
4 ✔
283
      LOG.info("Skipping installation/update of tools as specified by the user.");
3 ✔
284
      return;
1 ✔
285
    }
286
    Step step = this.context.newStep("Install or update software");
5 ✔
287
    step.run(() -> doUpdateSoftwareStep(step));
10 ✔
288
  }
1 ✔
289

290
  private void doUpdateSoftwareStep(Step step) {
291

292
    Set<ToolCommandlet> toolCommandlets = new HashSet<>();
4 ✔
293
    CommandletManager commandletManager = this.context.getCommandletManager();
4 ✔
294
    // installed tools in IDE_HOME/software
295
    List<Path> softwarePaths = this.context.getFileAccess().listChildren(this.context.getSoftwarePath(), Files::isDirectory);
14 ✔
296
    for (Path softwarePath : softwarePaths) {
10 ✔
297
      String toolName = softwarePath.getFileName().toString();
4 ✔
298
      ToolCommandlet toolCommandlet = commandletManager.getToolCommandlet(toolName);
4 ✔
299
      if (toolCommandlet != null) {
2 !
300
        toolCommandlets.add(toolCommandlet);
4 ✔
301
      }
302
    }
1 ✔
303

304
    // regular tools in $IDE_TOOLS
305
    List<String> regularTools = IdeVariables.IDE_TOOLS.get(this.context);
6 ✔
306
    if (regularTools != null) {
2 !
307
      for (String regularTool : regularTools) {
10 ✔
308
        ToolCommandlet toolCommandlet = commandletManager.getToolCommandlet(regularTool);
4 ✔
309
        if (toolCommandlet == null) {
2 !
310
          String displayName = (regularTool == null || regularTool.isBlank()) ? "<empty>" : "'" + regularTool + "'";
×
311
          LOG.error("Cannot install or update tool '{}''. No matching commandlet found. Please check your IDE_TOOLS configuration.", displayName);
×
312
        } else {
×
313
          toolCommandlets.add(toolCommandlet);
4 ✔
314
        }
315
      }
1 ✔
316
    }
317

318
    // custom tools in ide-custom-tools.json
319
    for (CustomToolMetadata customTool : this.context.getCustomToolRepository().getTools()) {
9 !
320
      CustomToolCommandlet customToolCommandlet = new CustomToolCommandlet(this.context, customTool);
×
321
      toolCommandlets.add(customToolCommandlet);
×
322
    }
×
323

324
    // update/install the toolCommandlets
325
    for (ToolCommandlet toolCommandlet : toolCommandlets) {
10 ✔
326
      this.context.newStep("Install " + toolCommandlet.getName()).run(() -> toolCommandlet.install(false));
15 ✔
327
    }
1 ✔
328

329
    ExtraTools extraTools = ExtraToolsMapper.get().loadJsonFromFolder(this.context.getSettingsPath());
7 ✔
330
    if (extraTools != null) {
2 ✔
331
      List<String> toolNames = extraTools.getSortedToolNames();
3 ✔
332
      LOG.info("Found extra installation of the following tools: {}", toolNames);
4 ✔
333
      for (String tool : toolNames) {
10 ✔
334
        List<ExtraToolInstallation> installations = extraTools.getExtraInstallations(tool);
4 ✔
335
        this.context.newStep("Install extra version(s) of " + tool).run(() -> installExtraToolInstallations(tool, installations));
16 ✔
336
      }
1 ✔
337
    }
338
  }
1 ✔
339

340
  private void installExtraToolInstallations(String tool, List<ExtraToolInstallation> extraInstallations) {
341

342
    CommandletManager commandletManager = this.context.getCommandletManager();
4 ✔
343
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
344
    Path extraPath = this.context.getSoftwareExtraPath();
4 ✔
345
    LocalToolCommandlet toolCommandlet = commandletManager.getRequiredLocalToolCommandlet(tool);
4 ✔
346
    for (ExtraToolInstallation extraInstallation : extraInstallations) {
10 ✔
347
      ToolInstallRequest request = new ToolInstallRequest(false);
5 ✔
348
      String edition = extraInstallation.edition();
3 ✔
349
      if (edition == null) {
2 ✔
350
        edition = toolCommandlet.getConfiguredEdition();
3 ✔
351
      }
352
      ToolEdition toolEdition = new ToolEdition(tool, edition);
6 ✔
353
      VersionIdentifier version = extraInstallation.version();
3 ✔
354
      request.setRequested(new ToolEditionAndVersion(toolEdition, version));
7 ✔
355
      Path extraToolPath = extraPath.resolve(tool);
4 ✔
356
      Path toolPath = extraToolPath.resolve(extraInstallation.name());
5 ✔
357
      request.setToolPathForExtraInstallation(toolPath);
3 ✔
358
      toolCommandlet.install(request);
4 ✔
359
    }
1 ✔
360
  }
1 ✔
361

362
  private void updateRepositories() {
363

364
    if (this.skipRepositories.isTrue()) {
4 !
365
      if (this.forceRepositories.isTrue()) {
×
366
        LOG.warn("Options to skip and force repositories are incompatible and should not be combined. Ignoring --force-repositories to proceed.");
×
367
      }
368
      LOG.info("Skipping setup of repositories as specified by the user.");
×
369
      return;
×
370
    }
371
    RepositoryCommandlet repositoryCommandlet = this.context.getCommandletManager().getCommandlet(RepositoryCommandlet.class);
7 ✔
372
    repositoryCommandlet.reset();
2 ✔
373
    repositoryCommandlet.run();
2 ✔
374
  }
1 ✔
375

376
  private void createStartScripts() {
377

378
    List<String> ides = IdeVariables.CREATE_START_SCRIPTS.get(this.context);
6 ✔
379
    if (ides == null) {
2 ✔
380
      LOG.info("Variable CREATE_START_SCRIPTS is undefined - skipping start script creation.");
3 ✔
381
      return;
1 ✔
382
    }
383
    for (String ide : ides) {
10 ✔
384
      ToolCommandlet tool = this.context.getCommandletManager().getToolCommandlet(ide);
6 ✔
385
      if (tool == null) {
2 !
386
        LOG.error("Undefined IDE '{}' configured in variable CREATE_START_SCRIPTS.", ide);
×
387
      } else {
388
        createStartScript(ide);
3 ✔
389
      }
390
    }
1 ✔
391
  }
1 ✔
392

393
  private void createStartScript(String ide) {
394

395
    LOG.info("Creating start scripts for {}", ide);
4 ✔
396
    Path workspaces = this.context.getIdeHome().resolve(IdeContext.FOLDER_WORKSPACES);
6 ✔
397
    try (Stream<Path> childStream = Files.list(workspaces)) {
3 ✔
398
      Iterator<Path> iterator = childStream.iterator();
3 ✔
399
      while (iterator.hasNext()) {
3 ✔
400
        Path child = iterator.next();
4 ✔
401
        if (Files.isDirectory(child)) {
5 !
402
          createStartScript(ide, child.getFileName().toString());
6 ✔
403
        }
404
      }
1 ✔
405
    } catch (IOException e) {
×
406
      throw new RuntimeException("Failed to list children of directory " + workspaces, e);
×
407
    }
1 ✔
408
  }
1 ✔
409

410
  private void createStartScript(String ide, String workspace) {
411

412
    Path ideHome = this.context.getIdeHome();
4 ✔
413
    String scriptName = ide + "-" + workspace;
4 ✔
414
    boolean windows = this.context.getSystemInfo().isWindows();
5 ✔
415
    if (windows) {
2 !
416
      scriptName = scriptName + ".bat";
×
417
    } else {
418
      scriptName = scriptName + ".sh";
3 ✔
419
    }
420
    Path scriptPath = ideHome.resolve(scriptName);
4 ✔
421
    if (Files.exists(scriptPath)) {
5 !
422
      return;
×
423
    }
424
    String scriptContent;
425
    if (windows) {
2 !
426
      scriptContent = "@echo off\r\n"
×
427
          + "pushd %~dp0\r\n"
428
          + "cd workspaces/" + workspace + "\r\n"
429
          + "call ide " + ide + "\r\n"
430
          + "popd\r\n";
431
    } else {
432
      scriptContent = "#!/usr/bin/env bash\n"
4 ✔
433
          + "cd \"$(dirname \"$0\")\"\n"
434
          + "cd workspaces/" + workspace + "\n"
435
          + "ideasy " + ide + "\n";
436
    }
437
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
438
    fileAccess.writeFileContent(scriptContent, scriptPath);
4 ✔
439
    fileAccess.makeExecutable(scriptPath);
3 ✔
440
  }
1 ✔
441

442
  /**
443
   * Judge if the repository is a code repository.
444
   *
445
   * @return true when the repository is a code repository, otherwise false.
446
   */
447
  protected boolean isCodeRepository() {
448
    return false;
2 ✔
449
  }
450

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