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

devonfw / IDEasy / 15217656699

23 May 2025 07:22PM UTC coverage: 67.562% (-0.3%) from 67.89%
15217656699

push

github

web-flow
#1332: fixed bug pattern, proper Step usage, allow running tool if plugin failed (#1334)

Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>

3151 of 5064 branches covered (62.22%)

Branch coverage included in aggregate %.

8048 of 11512 relevant lines covered (69.91%)

3.07 hits per line

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

79.52
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 com.devonfw.tools.ide.context.AbstractIdeContext;
13
import com.devonfw.tools.ide.context.IdeContext;
14
import com.devonfw.tools.ide.git.GitContext;
15
import com.devonfw.tools.ide.git.GitUrl;
16
import com.devonfw.tools.ide.git.repository.RepositoryCommandlet;
17
import com.devonfw.tools.ide.io.FileAccess;
18
import com.devonfw.tools.ide.property.FlagProperty;
19
import com.devonfw.tools.ide.property.StringProperty;
20
import com.devonfw.tools.ide.step.Step;
21
import com.devonfw.tools.ide.tool.CustomToolCommandlet;
22
import com.devonfw.tools.ide.tool.ToolCommandlet;
23
import com.devonfw.tools.ide.tool.repository.CustomToolMetadata;
24
import com.devonfw.tools.ide.variable.IdeVariables;
25

26
/**
27
 * Abstract {@link Commandlet} base-class for both {@link UpdateCommandlet} and {@link CreateCommandlet}.
28
 */
29
public abstract class AbstractUpdateCommandlet extends Commandlet {
30

31
  /** {@link StringProperty} for the settings repository URL. */
32
  public final StringProperty settingsRepo;
33

34
  /** {@link FlagProperty} for skipping installation/updating of tools. */
35
  public final FlagProperty skipTools;
36

37
  /** {@link FlagProperty} for skipping the setup of git repositories. */
38
  public final FlagProperty skipRepositories;
39

40
  /** {@link FlagProperty} to force the update of the settings git repository. */
41
  public final FlagProperty forcePull;
42

43
  /** {@link FlagProperty} to force the installation/update of plugins. */
44
  public final FlagProperty forcePlugins;
45

46
  /** {@link FlagProperty} to force the setup of git repositories. */
47
  public final FlagProperty forceRepositories;
48

49
  /**
50
   * The constructor.
51
   *
52
   * @param context the {@link IdeContext}.
53
   */
54
  public AbstractUpdateCommandlet(IdeContext context) {
55

56
    super(context);
3✔
57
    addKeyword(getName());
4✔
58
    this.skipTools = add(new FlagProperty("--skip-tools"));
9✔
59
    this.skipRepositories = add(new FlagProperty("--skip-repositories"));
9✔
60
    this.forcePull = add(new FlagProperty("--force-pull"));
9✔
61
    this.forcePlugins = add(new FlagProperty("--force-plugins"));
9✔
62
    this.forceRepositories = add(new FlagProperty("--force-repositories"));
9✔
63
    this.settingsRepo = new StringProperty("", false, "settingsRepository");
8✔
64
  }
1✔
65

66
  @Override
67
  public void run() {
68

69
    this.context.setForcePull(forcePull.isTrue());
6✔
70
    this.context.setForcePlugins(forcePlugins.isTrue());
6✔
71
    this.context.setForceRepositories(forceRepositories.isTrue());
6✔
72

73
    if (!this.context.isSettingsRepositorySymlinkOrJunction() || this.context.isForceMode() || forcePull.isTrue()) {
4!
74
      updateSettings();
2✔
75
    }
76
    updateConf();
2✔
77
    reloadContext();
2✔
78

79
    updateSoftware();
2✔
80
    updateRepositories();
2✔
81
    createStartScripts();
2✔
82
  }
1✔
83

84
  private void reloadContext() {
85

86
    ((AbstractIdeContext) this.context).reload();
4✔
87
  }
1✔
88

89
  private void updateConf() {
90

91
    Path templatesFolder = this.context.getSettingsPath().resolve(IdeContext.FOLDER_TEMPLATES);
6✔
92
    if (!Files.exists(templatesFolder)) {
5✔
93
      Path legacyTemplatesFolder = this.context.getSettingsPath().resolve(IdeContext.FOLDER_LEGACY_TEMPLATES);
6✔
94
      if (Files.exists(legacyTemplatesFolder)) {
5!
95
        templatesFolder = legacyTemplatesFolder;
×
96
      } else {
97
        this.context.warning("Templates folder is missing in settings repository.");
4✔
98
        return;
1✔
99
      }
100
    }
101

102
    Step step = this.context.newStep("Copy configuration templates", templatesFolder);
11✔
103
    final Path finalTemplatesFolder = templatesFolder;
2✔
104
    step.run(() -> setupConf(finalTemplatesFolder, this.context.getIdeHome()));
13✔
105
  }
1✔
106

107
  private void setupConf(Path template, Path conf) {
108

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

112
      String basename = child.getFileName().toString();
4✔
113
      Path confPath = conf.resolve(basename);
4✔
114

115
      if (Files.isDirectory(child)) {
5✔
116
        if (!Files.isDirectory(confPath)) {
5!
117
          this.context.getFileAccess().mkdirs(confPath);
5✔
118
        }
119
        setupConf(child, confPath);
5✔
120
      } else if (Files.isRegularFile(child)) {
5!
121
        if (Files.isRegularFile(confPath)) {
5!
122
          this.context.debug("Configuration {} already exists - skipping to copy from {}", confPath, child);
×
123
        } else {
124
          if (!basename.equals("settings.xml")) {
4!
125
            this.context.info("Copying template {} to {}.", child, conf);
14✔
126
            this.context.getFileAccess().copy(child, conf);
6✔
127
          }
128
        }
129
      }
130
    }
1✔
131
  }
1✔
132

133
  /**
134
   * 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
135
   * latest current commit ID in the file ".commit.id".
136
   */
137
  protected void updateSettings() {
138

139
    Path settingsPath = this.context.getSettingsPath();
4✔
140
    GitContext gitContext = this.context.getGitContext();
4✔
141
    Step step = null;
2✔
142
    try {
143
      // here we do not use pullOrClone to prevent asking a pointless question for repository URL...
144
      if (Files.isDirectory(settingsPath) && !this.context.getFileAccess().isEmptyDir(settingsPath)) {
11!
145
        step = this.context.newStep("Pull settings repository");
5✔
146
        gitContext.pull(settingsPath);
4✔
147
      } else {
148
        step = this.context.newStep("Clone settings repository");
5✔
149
        // check if a settings repository is given, otherwise prompt user for a repository.
150
        String repository = this.settingsRepo.getValue();
5✔
151
        if (repository == null) {
2!
152
          String message = "Missing your settings at " + settingsPath + " and no SETTINGS_URL is defined.\n"
×
153
              + "Further details can be found here: https://github.com/devonfw/IDEasy/blob/main/documentation/settings.adoc\n"
154
              + "Please contact the technical lead of your project to get the SETTINGS_URL for your project.\n"
155
              + "In case you just want to test IDEasy you may simply hit return to install the default settings.\n" + "Settings URL ["
156
              + IdeContext.DEFAULT_SETTINGS_REPO_URL + "]:";
157
          repository = this.context.askForInput(message, IdeContext.DEFAULT_SETTINGS_REPO_URL);
×
158
        } else if ("-".equals(repository)) {
4!
159
          repository = IdeContext.DEFAULT_SETTINGS_REPO_URL;
×
160
        }
161
        gitContext.pullOrClone(GitUrl.of(repository), settingsPath);
5✔
162
      }
163
      this.context.getGitContext().saveCurrentCommitId(settingsPath, this.context.getSettingsCommitIdPath());
8✔
164
      step.success("Successfully updated settings repository.");
3✔
165
    } finally {
166
      if (step != null) {
2!
167
        step.close();
2✔
168
      }
169
    }
170
  }
1✔
171

172
  private void updateSoftware() {
173

174
    if (this.skipTools.isTrue()) {
4✔
175
      this.context.info("Skipping installation/update of tools as specified by the user.");
4✔
176
      return;
1✔
177
    }
178
    Step step = this.context.newStep("Install or update software");
5✔
179
    step.run(() -> doUpdateSoftwareStep(step));
10✔
180
  }
1✔
181

182
  private void doUpdateSoftwareStep(Step step) {
183

184
    Set<ToolCommandlet> toolCommandlets = new HashSet<>();
4✔
185
    // installed tools in IDE_HOME/software
186
    List<Path> softwarePaths = this.context.getFileAccess().listChildren(this.context.getSoftwarePath(), Files::isDirectory);
14✔
187
    for (Path softwarePath : softwarePaths) {
10✔
188
      String toolName = softwarePath.getFileName().toString();
4✔
189
      ToolCommandlet toolCommandlet = this.context.getCommandletManager().getToolCommandlet(toolName);
6✔
190
      if (toolCommandlet != null) {
2!
191
        toolCommandlets.add(toolCommandlet);
4✔
192
      }
193
    }
1✔
194

195
    // regular tools in $IDE_TOOLS
196
    List<String> regularTools = IdeVariables.IDE_TOOLS.get(this.context);
6✔
197
    if (regularTools != null) {
2!
198
      for (String regularTool : regularTools) {
10✔
199
        toolCommandlets.add(this.context.getCommandletManager().getRequiredToolCommandlet(regularTool));
8✔
200
      }
1✔
201
    }
202

203
    // custom tools in ide-custom-tools.json
204
    for (CustomToolMetadata customTool : this.context.getCustomToolRepository().getTools()) {
9!
205
      CustomToolCommandlet customToolCommandlet = new CustomToolCommandlet(this.context, customTool);
×
206
      toolCommandlets.add(customToolCommandlet);
×
207
    }
×
208

209
    // update/install the toolCommandlets
210
    for (ToolCommandlet toolCommandlet : toolCommandlets) {
10✔
211
      this.context.newStep("Install " + toolCommandlet.getName()).run(() -> toolCommandlet.install(false));
15✔
212
    }
1✔
213
  }
1✔
214

215
  private void updateRepositories() {
216

217
    if (this.skipRepositories.isTrue()) {
4!
218
      if (this.forceRepositories.isTrue()) {
×
219
        this.context.warning("Options to skip and force repositories are incompatible and should not be combined. Ignoring --force-repositories to proceed.");
×
220
      }
221
      this.context.info("Skipping setup of repositories as specified by the user.");
×
222
      return;
×
223
    }
224
    RepositoryCommandlet repositoryCommandlet = this.context.getCommandletManager().getCommandlet(RepositoryCommandlet.class);
7✔
225
    repositoryCommandlet.reset();
2✔
226
    repositoryCommandlet.run();
2✔
227
  }
1✔
228

229
  private void createStartScripts() {
230

231
    List<String> ides = IdeVariables.CREATE_START_SCRIPTS.get(this.context);
6✔
232
    if (ides == null) {
2✔
233
      this.context.info("Variable CREATE_START_SCRIPTS is undefined - skipping start script creation.");
4✔
234
      return;
1✔
235
    }
236
    for (String ide : ides) {
10✔
237
      ToolCommandlet tool = this.context.getCommandletManager().getToolCommandlet(ide);
6✔
238
      if (tool == null) {
2!
239
        this.context.error("Undefined IDE '{}' configured in variable CREATE_START_SCRIPTS.");
×
240
      } else {
241
        createStartScript(ide);
3✔
242
      }
243
    }
1✔
244
  }
1✔
245

246
  private void createStartScript(String ide) {
247

248
    this.context.info("Creating start scripts for {}", ide);
10✔
249
    Path workspaces = this.context.getIdeHome().resolve(IdeContext.FOLDER_WORKSPACES);
6✔
250
    try (Stream<Path> childStream = Files.list(workspaces)) {
3✔
251
      Iterator<Path> iterator = childStream.iterator();
3✔
252
      while (iterator.hasNext()) {
3✔
253
        Path child = iterator.next();
4✔
254
        if (Files.isDirectory(child)) {
5!
255
          createStartScript(ide, child.getFileName().toString());
6✔
256
        }
257
      }
1✔
258
    } catch (IOException e) {
×
259
      throw new RuntimeException("Failed to list children of directory " + workspaces, e);
×
260
    }
1✔
261
  }
1✔
262

263
  private void createStartScript(String ide, String workspace) {
264

265
    Path ideHome = this.context.getIdeHome();
4✔
266
    String scriptName = ide + "-" + workspace;
4✔
267
    boolean windows = this.context.getSystemInfo().isWindows();
5✔
268
    if (windows) {
2!
269
      scriptName = scriptName + ".bat";
×
270
    } else {
271
      scriptName = scriptName + ".sh";
3✔
272
    }
273
    Path scriptPath = ideHome.resolve(scriptName);
4✔
274
    if (Files.exists(scriptPath)) {
5!
275
      return;
×
276
    }
277
    String scriptContent;
278
    if (windows) {
2!
279
      scriptContent = "@echo off\r\n"
×
280
          + "pushd %~dp0\r\n"
281
          + "cd workspaces/" + workspace + "\r\n"
282
          + "call ide " + ide + "\r\n"
283
          + "popd\r\n";
284
    } else {
285
      scriptContent = "#!/usr/bin/env bash\n"
4✔
286
          + "cd \"$(dirname \"$0\")\"\n"
287
          + "cd workspaces/" + workspace + "\n"
288
          + "ideasy " + ide + "\n";
289
    }
290
    FileAccess fileAccess = this.context.getFileAccess();
4✔
291
    fileAccess.writeFileContent(scriptContent, scriptPath);
4✔
292
    fileAccess.makeExecutable(scriptPath);
3✔
293
  }
1✔
294

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