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

devonfw / IDEasy / 34961711466

15 Sep 2026 11:08AM UTC coverage: 74.158% (+0.03%) from 74.124%
34961711466

push

github

web-flow
#2366: support variables and expressions in repository properties (#2398)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

5681 of 8516 branches covered (66.71%)

Branch coverage included in aggregate %.

14725 of 19001 relevant lines covered (77.5%)

3.31 hits per line

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

72.79
cli/src/main/java/com/devonfw/tools/ide/git/repository/RepositoryCommandlet.java
1
package com.devonfw.tools.ide.git.repository;
2

3
import java.nio.file.Files;
4
import java.nio.file.Path;
5
import java.util.ArrayList;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10

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

14
import com.devonfw.tools.ide.cli.CliAbortException;
15
import com.devonfw.tools.ide.cli.CliException;
16
import com.devonfw.tools.ide.commandlet.Commandlet;
17
import com.devonfw.tools.ide.context.IdeContext;
18
import com.devonfw.tools.ide.git.GitContext;
19
import com.devonfw.tools.ide.git.GitUrl;
20
import com.devonfw.tools.ide.io.FileAccess;
21
import com.devonfw.tools.ide.property.RepositoryProperty;
22
import com.devonfw.tools.ide.step.Step;
23
import com.devonfw.tools.ide.tool.ToolCommandlet;
24
import com.devonfw.tools.ide.tool.ide.IdeToolCommandlet;
25

26
/**
27
 * {@link Commandlet} to setup one or multiple GIT repositories for development.
28
 */
29
public class RepositoryCommandlet extends Commandlet {
30

31
  private static final Logger LOG = LoggerFactory.getLogger(RepositoryCommandlet.class);
4 ✔
32
  private static final String REPOSITORY = "repository";
33

34
  /** the repository to setup. */
35
  public final RepositoryProperty repository;
36

37
  /**
38
   * The constructor.
39
   *
40
   * @param context the {@link IdeContext}.
41
   */
42
  public RepositoryCommandlet(IdeContext context) {
43

44
    super(context);
3 ✔
45
    addKeyword(getName());
4 ✔
46
    addKeyword("setup");
3 ✔
47
    this.repository = add(new RepositoryProperty("", false, REPOSITORY));
11 ✔
48
  }
1 ✔
49

50
  @Override
51
  public String getName() {
52

53
    return REPOSITORY;
2 ✔
54
  }
55

56
  @Override
57
  protected void doRun() {
58

59
    Path repositoryFile = this.repository.getValue();
5 ✔
60

61
    if (repositoryFile != null) {
2 ✔
62
      // Handle the case when a specific repository is provided
63
      RepositoryConfig config = prepareActiveRepository(repositoryFile, true);
5 ✔
64
      if (config != null) {
2 ✔
65
        importRepository(config);
3 ✔
66
      }
67
    } else {
1 ✔
68
      // If no specific repository is provided, check for repositories folder
69
      Path repositoriesPath = this.context.getRepositoriesPath();
4 ✔
70
      if (repositoriesPath == null) {
2 ✔
71
        LOG.warn("Cannot find folder 'repositories' nor 'projects' in your settings.");
3 ✔
72
        return;
1 ✔
73
      }
74
      List<Path> propertiesFiles = this.context.getFileAccess()
5 ✔
75
          .listChildren(repositoriesPath, path -> path.getFileName().toString().endsWith(".properties"));
8 ✔
76
      boolean forceMode = this.context.isForceMode() || this.context.isForceRepositories();
12 !
77
      Map<Path, RepositoryConfig> repositoryConfigMap = new HashMap<>(propertiesFiles.size());
6 ✔
78
      for (Path propertiesFile : propertiesFiles) {
10 ✔
79
        RepositoryConfig config = prepareActiveRepositoryIgnoringInvalid(propertiesFile, forceMode);
5 ✔
80
        if (config != null) {
2 ✔
81
          repositoryConfigMap.put(propertiesFile, config);
5 ✔
82
        }
83
      }
1 ✔
84
      for (Path propertiesFile : propertiesFiles) {
10 ✔
85
        RepositoryConfig config = repositoryConfigMap.get(propertiesFile);
5 ✔
86
        if (config != null) {
2 ✔
87
          importRepository(config);
3 ✔
88
        }
89
      }
1 ✔
90
    }
91
  }
1 ✔
92

93
  /**
94
   * Like {@link #prepareActiveRepository(Path, boolean)} but ignores a single invalid repository instead of aborting the setup of all others. Resolving a
95
   * property value may fail for a malformed expression, and one broken file in the repositories folder must not prevent every other repository from being
96
   * set up. Only used when iterating all repositories - if the user explicitly requested a single repository, the failure is reported instead.
97
   *
98
   * @param repositoryFile the {@link Path} to the repository properties file.
99
   * @param forceMode - {@code true} to setup the repository even if it is not active, {@code false} otherwise.
100
   * @return the {@link RepositoryConfig} or {@code null} if the repository shall be skipped.
101
   */
102
  private RepositoryConfig prepareActiveRepositoryIgnoringInvalid(Path repositoryFile, boolean forceMode) {
103

104
    try {
105
      return prepareActiveRepository(repositoryFile, forceMode);
5 ✔
106
    } catch (CliAbortException e) {
×
107
      throw e; // the user deliberately aborted - never swallow this
×
108
    } catch (CliException e) {
1 ✔
109
      LOG.error("Ignoring repository {} because its configuration is invalid: {}", repositoryFile, e.getMessage());
6 ✔
110
      return null;
2 ✔
111
    }
112
  }
113

114
  private RepositoryConfig prepareActiveRepository(Path repositoryFile, boolean forceMode) {
115

116
    // the active flag is evaluated before the remaining properties are read: reading a property resolves variables and expressions in its value, so an
117
    // expression like @ask-variable would otherwise ask the user for a repository that is skipped anyway
118
    RepositoryProperties properties = new RepositoryProperties(repositoryFile, this.context);
7 ✔
119
    if (!properties.isActive()) {
3 ✔
120
      if (forceMode) {
2 ✔
121
        LOG.info("Setup of repository {} is forced, hence proceeding ...", properties.getId());
6 ✔
122
      } else {
123
        LOG.info("Skipping repository {} because it is not active, use --force-repositories to setup all repositories ...", properties.getId());
5 ✔
124
        return null;
2 ✔
125
      }
126
    }
127
    RepositoryConfig config = RepositoryConfig.loadProperties(properties);
3 ✔
128
    if (config == null) {
2 ✔
129
      return null;
2 ✔
130
    }
131
    // prepare workspace creation for correct resolution of *
132
    List<String> workspaces = config.workspaces();
3 ✔
133
    for (String workspace : workspaces) {
10 ✔
134
      if (!RepositoryConfig.WORKSPACE_NAME_ALL.equals(workspace)) {
4 ✔
135
        Path workspacePath = this.context.getWorkspacePath(workspace);
5 ✔
136
        this.context.getFileAccess().mkdirs(workspacePath);
5 ✔
137
      }
138
    }
1 ✔
139
    return config;
2 ✔
140
  }
141

142
  private void importRepository(RepositoryConfig config) {
143

144
    this.context.newStep("Setup of repository " + config.id()).run(() -> {
11 ✔
145
      doImportRepository(config);
3 ✔
146
    });
1 ✔
147
  }
1 ✔
148

149
  private void doImportRepository(RepositoryConfig config) {
150
    LOG.debug("Repository configuration: {}", config);
4 ✔
151
    String repositoryRelativePath = config.path();
3 ✔
152
    if (repositoryRelativePath == null) {
2 ✔
153
      repositoryRelativePath = config.id();
3 ✔
154
    }
155
    Path ideStatusDir = this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE);
6 ✔
156
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
157
    fileAccess.mkdirs(ideStatusDir);
3 ✔
158

159
    List<String> workspaces = config.workspaces();
3 ✔
160
    if ((workspaces.size() == 1) && (RepositoryConfig.WORKSPACE_NAME_ALL.equals(workspaces.getFirst()))) {
9 ✔
161
      // if workspaces=* replace with all existing workspaces
162
      workspaces = fileAccess.listChildren(this.context.getWorkspacesBasePath(), Files::isDirectory).stream().map(Path::getFileName).map(Path::toString)
16 ✔
163
          .toList();
2 ✔
164
    }
165
    // if main is contained in workspaces, it should come first (to ensure physical cloning to main and linking to others)
166
    if (!workspaces.getFirst().equals(IdeContext.WORKSPACE_MAIN) && workspaces.contains(IdeContext.WORKSPACE_MAIN)) {
10 ✔
167
      workspaces = new ArrayList<>(workspaces); // mutable copy
5 ✔
168
      workspaces.remove(IdeContext.WORKSPACE_MAIN);
4 ✔
169
      workspaces.addFirst(IdeContext.WORKSPACE_MAIN);
3 ✔
170
    }
171

172
    Path firstRepository = null;
2 ✔
173

174
    if (config.isVirtualSettingsRepository()) {
3 ✔
175
      firstRepository = this.context.getSettingsPath();
4 ✔
176
    }
177

178
    for (String workspaceName : workspaces) {
10 ✔
179
      Path workspacePath = this.context.getWorkspacePath(workspaceName);
5 ✔
180
      Path repositoryPath = workspacePath.resolve(repositoryRelativePath);
4 ✔
181
      Path repositoryCreatedStatusFile = ideStatusDir.resolve("repository." + config.id() + "." + workspaceName);
7 ✔
182
      boolean createRepository = true;
2 ✔
183
      if (!config.isVirtualSettingsRepository() && Files.isDirectory(repositoryPath.resolve(GitContext.GIT_FOLDER))) {
10 !
184
        if (firstRepository == null) {
×
185
          firstRepository = repositoryPath;
×
186
        }
187
        LOG.info("Repository {} already exists in workspace {} at {}", config.id(), workspaceName, repositoryPath);
×
188
        if (!(this.context.isForceMode() || this.context.isForceRepositories())) {
×
189
          LOG.info("Ignoring repository {} in workspace {}, use --force-repositories to rerun setup.", config.id(), workspaceName);
×
190
          createRepository = false;
×
191
        }
192
      }
193
      if (Files.exists(repositoryCreatedStatusFile)) {
5 !
194
        if (!(this.context.isForceMode() || this.context.isForceRepositories())) {
×
195
          LOG.info("Ignoring repository {} in workspace {} because it was already setup before, use --force-repositories for recreation.",
×
196
              config.id(), workspaceName);
×
197
          createRepository = false;
×
198
        }
199
      }
200
      if (createRepository) {
2 !
201
        if (firstRepository == null) {
2 ✔
202
          GitUrl gitUrl = config.asGitUrl();
3 ✔
203
          boolean success = cloneOrPullRepository(repositoryPath, gitUrl, repositoryCreatedStatusFile);
6 ✔
204
          if (success) {
2 !
205
            firstRepository = repositoryPath;
2 ✔
206
            buildRepository(config, repositoryPath);
5 ✔
207
            importRepository(config, repositoryPath, config.id());
6 ✔
208
          }
209
        } else if (!config.isVirtualSettingsRepository()) {
4 ✔
210
          fileAccess.mkdirs(repositoryPath.getParent());
4 ✔
211
          fileAccess.symlink(firstRepository, repositoryPath);
4 ✔
212
        }
213
      }
214
      Path linkRepositoryPath = config.isVirtualSettingsRepository() ? firstRepository : repositoryPath;
7 ✔
215
      if (Files.exists(linkRepositoryPath)) {
5 !
216
        for (RepositoryRemote remote : config.remotes()) {
7 !
217
          this.context.getGitContext().addRemote(linkRepositoryPath, remote.name(), remote.url());
×
218
        }
×
219
        for (RepositoryLink link : config.links()) {
11 ✔
220
          createRepositoryLink(link, linkRepositoryPath, workspacePath);
5 ✔
221
        }
1 ✔
222
      }
223
    }
1 ✔
224
  }
1 ✔
225

226
  private boolean linkTargetExists(RepositoryLink link, Path linkTargetPath) {
227

228
    if (!Files.exists(linkTargetPath)) {
5 !
229
      LOG.error("Skipping link from '{}' to '{}' because target does not exist: {}", link.link(), link.target(), linkTargetPath);
×
230
      return false;
×
231
    }
232
    return true;
2 ✔
233
  }
234

235
  private void createRepositoryLink(RepositoryLink link, Path repositoryPath, Path workspacePath) {
236
    Path linkPath = workspacePath.resolve(link.link());
5 ✔
237
    String target = link.target();
3 ✔
238
    Path linkTargetPath;
239
    if ((target != null) && !target.isBlank()) {
5 !
240
      linkTargetPath = repositoryPath.resolve(target);
4 ✔
241
      if (!linkTargetExists(link, linkTargetPath)) {
5 !
242
        return;
×
243
      }
244
    } else {
245
      linkTargetPath = repositoryPath;
2 ✔
246
    }
247
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
248
    fileAccess.mkdirs(linkPath.getParent());
4 ✔
249
    fileAccess.symlink(linkTargetPath, linkPath);
4 ✔
250
  }
1 ✔
251

252
  private boolean cloneOrPullRepository(Path repositoryPath, GitUrl gitUrl, Path repositoryCreatedStatusFile) {
253

254
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
255
    return this.context.newStep("Clone or pull repository").run(() -> {
12 ✔
256
      fileAccess.mkdirs(repositoryPath);
3 ✔
257
      this.context.getGitContext().pullOrClone(gitUrl, repositoryPath);
6 ✔
258
      fileAccess.touch(repositoryCreatedStatusFile);
3 ✔
259
    });
1 ✔
260
  }
261

262
  private boolean buildRepository(RepositoryConfig repositoryConfig, Path repositoryPath) {
263
    String buildCmd = repositoryConfig.buildCmd();
3 ✔
264
    if (buildCmd != null && !buildCmd.isEmpty()) {
2 !
265
      return this.context.newStep("Build repository via: " + buildCmd).run(() -> {
×
266
        String[] command = buildCmd.split("\\s+");
×
267
        ToolCommandlet commandlet = this.context.getCommandletManager().getToolCommandlet(command[0]);
×
268
        if (commandlet == null) {
×
269
          String displayName = (command[0] == null || command[0].isBlank()) ? "<empty>" : "'" + command[0] + "'";
×
270
          LOG.error("Cannot build repository. Required tool '{}' not found. Please check your repository's build_cmd configuration value.",
×
271
              displayName);
272
          return;
×
273
        }
274
        commandlet.reset();
×
275
        for (int i = 1; i < command.length; i++) {
×
276
          commandlet.arguments.addValue(command[i]);
×
277
        }
278
        Path executionDirectory = repositoryPath;
×
279
        String path = repositoryConfig.buildPath();
×
280
        if (path != null) {
×
281
          executionDirectory = executionDirectory.resolve(path);
×
282
        }
283
        commandlet.setExecutionDirectory(executionDirectory);
×
284
        commandlet.run();
×
285
      });
×
286
    } else {
287
      LOG.debug("Build command not set. Skipping build for repository.");
3 ✔
288
      return true;
2 ✔
289
    }
290
  }
291

292
  private void importRepository(RepositoryConfig repositoryConfig, Path repositoryPath, String repositoryId) {
293

294
    Set<String> imports = repositoryConfig.imports();
3 ✔
295
    if ((imports == null) || imports.isEmpty()) {
5 !
296
      LOG.debug("Repository {} has no IDE configured for import.", repositoryId);
4 ✔
297
      return;
1 ✔
298
    }
299
    for (String ide : imports) {
10 ✔
300
      Step step = this.context.newStep("Importing repository " + repositoryId + " into " + ide);
7 ✔
301
      step.run(() -> {
9 ✔
302
        ToolCommandlet commandlet = this.context.getCommandletManager().getToolCommandlet(ide);
6 ✔
303
        if (commandlet == null) {
2 !
304
          String displayName = (ide == null || ide.isBlank()) ? "<empty>" : "'" + ide + "'";
×
305
          step.error("Cannot import repository '{}'. Required IDE '{}' not found. Please check your repository's imports configuration.", repositoryId,
×
306
              displayName);
307
        } else if (commandlet instanceof IdeToolCommandlet ideCommandlet) {
6 !
308
          ideCommandlet.importRepository(repositoryPath);
4 ✔
309
        } else {
310
          step.error("Repository {} has import {} configured that is not an IDE!", repositoryId, ide);
×
311
        }
312
      });
1 ✔
313
    }
1 ✔
314
  }
1 ✔
315
}
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