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

devonfw / IDEasy / 26281905478

22 May 2026 10:13AM UTC coverage: 70.945% (-0.1%) from 71.064%
26281905478

Pull #1878

github

web-flow
Merge 900c0f291 into 2a8d4e347
Pull Request #1878: #1695: Clone settings to temporary directory, analyse, and then move

4453 of 6946 branches covered (64.11%)

Branch coverage included in aggregate %.

11521 of 15570 relevant lines covered (73.99%)

3.13 hits per line

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

79.73
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.io.FileCopyMode;
24
import com.devonfw.tools.ide.property.FlagProperty;
25
import com.devonfw.tools.ide.property.StringProperty;
26
import com.devonfw.tools.ide.step.Step;
27
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
28
import com.devonfw.tools.ide.tool.ToolCommandlet;
29
import com.devonfw.tools.ide.tool.ToolEdition;
30
import com.devonfw.tools.ide.tool.ToolEditionAndVersion;
31
import com.devonfw.tools.ide.tool.ToolInstallRequest;
32
import com.devonfw.tools.ide.tool.custom.CustomToolCommandlet;
33
import com.devonfw.tools.ide.tool.custom.CustomToolMetadata;
34
import com.devonfw.tools.ide.tool.extra.ExtraToolInstallation;
35
import com.devonfw.tools.ide.tool.extra.ExtraTools;
36
import com.devonfw.tools.ide.tool.extra.ExtraToolsMapper;
37
import com.devonfw.tools.ide.variable.IdeVariables;
38
import com.devonfw.tools.ide.version.VersionIdentifier;
39
import com.devonfw.tools.ide.environment.EnvironmentVariables;
40

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

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

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

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

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

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

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

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

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

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

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

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

94
  @Override
95
  protected void doRun() {
96

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

102
    if (!this.context.isSettingsRepositorySymlinkOrJunction() || this.context.isForceMode() || forcePull.isTrue()) {
4!
103
      updateSettings();
2✔
104
    }
105
    updateConf();
2✔
106
    reloadContext();
2✔
107

108
    updateSoftware();
2✔
109
    updateRepositories();
2✔
110
    createStartScripts();
2✔
111
  }
1✔
112

113

114

115
  private void reloadContext() {
116

117
    ((AbstractIdeContext) this.context).reload();
4✔
118
  }
1✔
119

120
  private void updateConf() {
121

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

133
    Step step = this.context.newStep("Copy configuration templates", templatesFolder);
11✔
134
    final Path finalTemplatesFolder = templatesFolder;
2✔
135
    step.run(() -> setupConf(finalTemplatesFolder, this.context.getIdeHome()));
13✔
136
  }
1✔
137

138
  private void setupConf(Path template, Path conf) {
139

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

143
      String basename = child.getFileName().toString();
4✔
144
      Path confPath = conf.resolve(basename);
4✔
145

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

164
  /**
165
   * 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
166
   * latest current commit ID in the file ".commit.id".
167
   */
168
  protected void updateSettings() {
169

170
    this.context.newStep(getStepMessage()).run(this::updateSettingsInStep);
9✔
171
  }
1✔
172

173
  protected String getStepMessage() {
174

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

178
  private void updateSettingsInStep() {
179
    Path settingsPath = this.context.getSettingsPath();
4✔
180
    GitContext gitContext = this.context.getGitContext();
4✔
181
    // here we do not use pullOrClone to prevent asking a pointless question for repository URL...
182
    if (Files.isDirectory(settingsPath) && this.context.getGitContext().isGitRepo(settingsPath) || this.context.isSettingsRepositorySymlinkOrJunction()) {
15!
183
      if (this.context.isForcePull() || this.context.isForceMode()) {
8!
184
        if (gitContext.hasUntrackedFiles(settingsPath)) {
×
185
          gitContext.pullSafelyWithStash(settingsPath);
×
186
        } else {
187
          gitContext.pull(settingsPath);
×
188
        }
189
        this.context.getGitContext().saveCurrentCommitId(settingsPath, this.context.getSettingsCommitIdPath());
×
190
      } else {
191
        LOG.info("Skipping git pull in settings due to code repository. Use --force-pull to enforce pulling.");
4✔
192
      }
193
    } else {
194
      if (!this.context.getFileAccess().isEmptyDir(settingsPath)) {
6!
195
        this.context.askToContinue(
×
196
          "Your settings repository seems to be broken ('.git' folder not present). We can fix this by moving "
197
          + " your settings the backed up. You will be asked for the settings git URL and your settings will be cloned from scratch. Do you want to proceed?"
198
        );
199

200
        this.context.getFileAccess().backup(settingsPath);
×
201
      }
202

203
      GitUrl gitUrl = getOrAskSettingsUrl();
3✔
204
      initializeRepository(gitUrl);
3✔
205
    }
206
  }
1✔
207

208
  private GitUrl getOrAskSettingsUrl() {
209

210
    String repository = this.settingsRepo.getValue();
5✔
211
    repository = handleDefaultRepository(repository);
4✔
212
    String userPromt = "Repository URL [" + IdeContext.DEFAULT_SETTINGS_REPO_URL + "]:";
2✔
213
    String defaultUrl = IdeContext.DEFAULT_SETTINGS_REPO_URL;
2✔
214
    LOG.info(MESSAGE_SETTINGS_REPO_URL, this.context.getSettingsPath());
6✔
215

216
    GitUrl gitUrl = null;
2✔
217
    if (repository != null) {
2✔
218
      gitUrl = GitUrl.of(repository);
3✔
219
    }
220
    while ((gitUrl == null) || !gitUrl.isValid()) {
5!
221
      repository = this.context.askForInput(userPromt, defaultUrl);
6✔
222
      repository = handleDefaultRepository(repository);
4✔
223
      gitUrl = GitUrl.of(repository);
3✔
224
      if (!gitUrl.isValid()) {
3!
225
        LOG.warn("The input URL is not valid, please try again.");
×
226
      }
227
    }
228
    return gitUrl;
2✔
229
  }
230

231
  private String handleDefaultRepository(String repository) {
232
    if ("-".equals(repository)) {
4✔
233
      LOG.info("'-' was found for the repository, the default settings repository '{}' will be used.", IdeContext.DEFAULT_SETTINGS_REPO_URL);
4✔
234
      repository = IdeContext.DEFAULT_SETTINGS_REPO_URL;
2✔
235
    }
236
    return repository;
2✔
237
  }
238

239
  private void initializeRepository(GitUrl gitUrl) {
240

241
    GitContext gitContext = this.context.getGitContext();
4✔
242
    Path settingsPath = this.context.getSettingsPath();
4✔
243
    Path repoPath = settingsPath;
2✔
244
    gitContext.pullOrClone(gitUrl, repoPath);
4✔
245
    this.context.getGitContext().saveCurrentCommitId(settingsPath, this.context.getSettingsCommitIdPath());
8✔
246
  }
1✔
247

248
  private void updateSoftware() {
249

250
    if (this.skipTools.isTrue()) {
4✔
251
      LOG.info("Skipping installation/update of tools as specified by the user.");
3✔
252
      return;
1✔
253
    }
254
    Step step = this.context.newStep("Install or update software");
5✔
255
    step.run(() -> doUpdateSoftwareStep(step));
10✔
256
  }
1✔
257

258
  private void doUpdateSoftwareStep(Step step) {
259

260
    Set<ToolCommandlet> toolCommandlets = new HashSet<>();
4✔
261
    CommandletManager commandletManager = this.context.getCommandletManager();
4✔
262
    // installed tools in IDE_HOME/software
263
    List<Path> softwarePaths = this.context.getFileAccess().listChildren(this.context.getSoftwarePath(), Files::isDirectory);
14✔
264
    for (Path softwarePath : softwarePaths) {
10✔
265
      String toolName = softwarePath.getFileName().toString();
4✔
266
      ToolCommandlet toolCommandlet = commandletManager.getToolCommandlet(toolName);
4✔
267
      if (toolCommandlet != null) {
2!
268
        toolCommandlets.add(toolCommandlet);
4✔
269
      }
270
    }
1✔
271

272
    // regular tools in $IDE_TOOLS
273
    List<String> regularTools = IdeVariables.IDE_TOOLS.get(this.context);
6✔
274
    if (regularTools != null) {
2!
275
      for (String regularTool : regularTools) {
10✔
276
        ToolCommandlet toolCommandlet = commandletManager.getToolCommandlet(regularTool);
4✔
277
        if (toolCommandlet == null) {
2!
278
          String displayName = (regularTool == null || regularTool.isBlank()) ? "<empty>" : "'" + regularTool + "'";
×
279
          LOG.error("Cannot install or update tool '{}''. No matching commandlet found. Please check your IDE_TOOLS configuration.", displayName);
×
280
        } else {
×
281
          toolCommandlets.add(toolCommandlet);
4✔
282
        }
283
      }
1✔
284
    }
285

286
    // custom tools in ide-custom-tools.json
287
    for (CustomToolMetadata customTool : this.context.getCustomToolRepository().getTools()) {
9!
288
      CustomToolCommandlet customToolCommandlet = new CustomToolCommandlet(this.context, customTool);
×
289
      toolCommandlets.add(customToolCommandlet);
×
290
    }
×
291

292
    // update/install the toolCommandlets
293
    for (ToolCommandlet toolCommandlet : toolCommandlets) {
10✔
294
      this.context.newStep("Install " + toolCommandlet.getName()).run(() -> toolCommandlet.install(false));
15✔
295
    }
1✔
296

297
    ExtraTools extraTools = ExtraToolsMapper.get().loadJsonFromFolder(this.context.getSettingsPath());
7✔
298
    if (extraTools != null) {
2✔
299
      List<String> toolNames = extraTools.getSortedToolNames();
3✔
300
      LOG.info("Found extra installation of the following tools: {}", toolNames);
4✔
301
      for (String tool : toolNames) {
10✔
302
        List<ExtraToolInstallation> installations = extraTools.getExtraInstallations(tool);
4✔
303
        this.context.newStep("Install extra version(s) of " + tool).run(() -> installExtraToolInstallations(tool, installations));
16✔
304
      }
1✔
305
    }
306
  }
1✔
307

308
  private void installExtraToolInstallations(String tool, List<ExtraToolInstallation> extraInstallations) {
309

310
    CommandletManager commandletManager = this.context.getCommandletManager();
4✔
311
    FileAccess fileAccess = this.context.getFileAccess();
4✔
312
    Path extraPath = this.context.getSoftwareExtraPath();
4✔
313
    LocalToolCommandlet toolCommandlet = commandletManager.getRequiredLocalToolCommandlet(tool);
4✔
314
    for (ExtraToolInstallation extraInstallation : extraInstallations) {
10✔
315
      ToolInstallRequest request = new ToolInstallRequest(false);
5✔
316
      String edition = extraInstallation.edition();
3✔
317
      if (edition == null) {
2✔
318
        edition = toolCommandlet.getConfiguredEdition();
3✔
319
      }
320
      ToolEdition toolEdition = new ToolEdition(tool, edition);
6✔
321
      VersionIdentifier version = extraInstallation.version();
3✔
322
      request.setRequested(new ToolEditionAndVersion(toolEdition, version));
7✔
323
      Path extraToolPath = extraPath.resolve(tool);
4✔
324
      Path toolPath = extraToolPath.resolve(extraInstallation.name());
5✔
325
      request.setToolPathForExtraInstallation(toolPath);
3✔
326
      toolCommandlet.install(request);
4✔
327
    }
1✔
328
  }
1✔
329

330
  private void updateRepositories() {
331

332
    if (this.skipRepositories.isTrue()) {
4!
333
      if (this.forceRepositories.isTrue()) {
×
334
        LOG.warn("Options to skip and force repositories are incompatible and should not be combined. Ignoring --force-repositories to proceed.");
×
335
      }
336
      LOG.info("Skipping setup of repositories as specified by the user.");
×
337
      return;
×
338
    }
339
    RepositoryCommandlet repositoryCommandlet = this.context.getCommandletManager().getCommandlet(RepositoryCommandlet.class);
7✔
340
    repositoryCommandlet.reset();
2✔
341
    repositoryCommandlet.run();
2✔
342
  }
1✔
343

344
  private void createStartScripts() {
345

346
    List<String> ides = IdeVariables.CREATE_START_SCRIPTS.get(this.context);
6✔
347
    if (ides == null) {
2✔
348
      LOG.info("Variable CREATE_START_SCRIPTS is undefined - skipping start script creation.");
3✔
349
      return;
1✔
350
    }
351
    for (String ide : ides) {
10✔
352
      ToolCommandlet tool = this.context.getCommandletManager().getToolCommandlet(ide);
6✔
353
      if (tool == null) {
2!
354
        LOG.error("Undefined IDE '{}' configured in variable CREATE_START_SCRIPTS.", ide);
×
355
      } else {
356
        createStartScript(ide);
3✔
357
      }
358
    }
1✔
359
  }
1✔
360

361
  private void createStartScript(String ide) {
362

363
    LOG.info("Creating start scripts for {}", ide);
4✔
364
    Path workspaces = this.context.getIdeHome().resolve(IdeContext.FOLDER_WORKSPACES);
6✔
365
    try (Stream<Path> childStream = Files.list(workspaces)) {
3✔
366
      Iterator<Path> iterator = childStream.iterator();
3✔
367
      while (iterator.hasNext()) {
3✔
368
        Path child = iterator.next();
4✔
369
        if (Files.isDirectory(child)) {
5!
370
          createStartScript(ide, child.getFileName().toString());
6✔
371
        }
372
      }
1✔
373
    } catch (IOException e) {
×
374
      throw new RuntimeException("Failed to list children of directory " + workspaces, e);
×
375
    }
1✔
376
  }
1✔
377

378
  private void createStartScript(String ide, String workspace) {
379

380
    Path ideHome = this.context.getIdeHome();
4✔
381
    String scriptName = ide + "-" + workspace;
4✔
382
    boolean windows = this.context.getSystemInfo().isWindows();
5✔
383
    if (windows) {
2!
384
      scriptName = scriptName + ".bat";
×
385
    } else {
386
      scriptName = scriptName + ".sh";
3✔
387
    }
388
    Path scriptPath = ideHome.resolve(scriptName);
4✔
389
    if (Files.exists(scriptPath)) {
5!
390
      return;
×
391
    }
392
    String scriptContent;
393
    if (windows) {
2!
394
      scriptContent = "@echo off\r\n" + "pushd %~dp0\r\n" + "cd workspaces/" + workspace + "\r\n" + "call ide " + ide + "\r\n" + "popd\r\n";
×
395
    } else {
396
      scriptContent = "#!/usr/bin/env bash\n" + "cd \"$(dirname \"$0\")\"\n" + "cd workspaces/" + workspace + "\n" + "ideasy " + ide + "\n";
4✔
397
    }
398
    FileAccess fileAccess = this.context.getFileAccess();
4✔
399
    fileAccess.writeFileContent(scriptContent, scriptPath);
4✔
400
    fileAccess.makeExecutable(scriptPath);
3✔
401
  }
1✔
402
}
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