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

devonfw / IDEasy / 12356873353

16 Dec 2024 04:22PM UTC coverage: 67.215% (-0.2%) from 67.459%
12356873353

Pull #850

github

web-flow
Merge 532436561 into 52afdd7c7
Pull Request #850: #757: Settings in code repository

2574 of 4176 branches covered (61.64%)

Branch coverage included in aggregate %.

6668 of 9574 relevant lines covered (69.65%)

3.06 hits per line

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

84.13
cli/src/main/java/com/devonfw/tools/ide/commandlet/AbstractUpdateCommandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.nio.file.Files;
4
import java.nio.file.Path;
5
import java.util.HashSet;
6
import java.util.List;
7
import java.util.Set;
8

9
import com.devonfw.tools.ide.context.AbstractIdeContext;
10
import com.devonfw.tools.ide.context.IdeContext;
11
import com.devonfw.tools.ide.git.GitContext;
12
import com.devonfw.tools.ide.git.GitUrl;
13
import com.devonfw.tools.ide.property.FlagProperty;
14
import com.devonfw.tools.ide.property.StringProperty;
15
import com.devonfw.tools.ide.repo.CustomTool;
16
import com.devonfw.tools.ide.step.Step;
17
import com.devonfw.tools.ide.tool.CustomToolCommandlet;
18
import com.devonfw.tools.ide.tool.ToolCommandlet;
19
import com.devonfw.tools.ide.variable.IdeVariables;
20

21
/**
22
 * Abstract {@link Commandlet} base-class for both {@link UpdateCommandlet} and {@link CreateCommandlet}.
23
 */
24
public abstract class AbstractUpdateCommandlet extends Commandlet {
25

26
  /** {@link StringProperty} for the settings repository URL. */
27
  protected final StringProperty settingsRepo;
28

29
  /** {@link FlagProperty} for skipping installation/updating of tools */
30
  protected final FlagProperty skipTools;
31

32
  /**
33
   * The constructor.
34
   *
35
   * @param context the {@link IdeContext}.
36
   */
37
  public AbstractUpdateCommandlet(IdeContext context) {
38

39
    super(context);
3✔
40
    addKeyword(getName());
4✔
41
    this.skipTools = add(new FlagProperty("--skip-tools", false, null));
11✔
42
    this.settingsRepo = new StringProperty("", false, "settingsRepository");
8✔
43
  }
1✔
44

45
  @Override
46
  public void run() {
47

48
    updateSettings();
2✔
49
    updateConf();
2✔
50
    reloadContext();
2✔
51

52
    if (this.skipTools.isTrue()) {
4✔
53
      this.context.info("Skipping installation/update of tools as specified by the user.");
5✔
54
    } else {
55
      updateSoftware();
2✔
56
    }
57
  }
1✔
58

59
  private void reloadContext() {
60

61
    ((AbstractIdeContext) this.context).reload();
4✔
62
  }
1✔
63

64
  private void updateConf() {
65

66
    Path templatesFolder = this.context.getSettingsPath().resolve(IdeContext.FOLDER_TEMPLATES);
6✔
67
    if (!Files.exists(templatesFolder)) {
5✔
68
      Path legacyTemplatesFolder = this.context.getSettingsPath().resolve(IdeContext.FOLDER_LEGACY_TEMPLATES);
6✔
69
      if (Files.exists(legacyTemplatesFolder)) {
5!
70
        templatesFolder = legacyTemplatesFolder;
×
71
      } else {
72
        this.context.warning("Templates folder is missing in settings repository.");
4✔
73
        return;
1✔
74
      }
75
    }
76

77
    try (Step step = this.context.newStep("Copy configuration templates", templatesFolder)) {
11✔
78
      setupConf(templatesFolder, this.context.getIdeHome());
6✔
79
      step.success();
2✔
80
    }
81
  }
1✔
82

83
  private void setupConf(Path template, Path conf) {
84

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

88
      String basename = child.getFileName().toString();
4✔
89
      Path confPath = conf.resolve(basename);
4✔
90

91
      if (Files.isDirectory(child)) {
5✔
92
        if (!Files.isDirectory(confPath)) {
5!
93
          this.context.getFileAccess().mkdirs(confPath);
5✔
94
        }
95
        setupConf(child, confPath);
5✔
96
      } else if (Files.isRegularFile(child)) {
5!
97
        if (Files.isRegularFile(confPath)) {
5!
98
          this.context.debug("Configuration {} already exists - skipping to copy from {}", confPath, child);
×
99
        } else {
100
          if (!basename.equals("settings.xml")) {
4!
101
            this.context.info("Copying template {} to {}.", child, conf);
14✔
102
            this.context.getFileAccess().copy(child, conf);
6✔
103
          }
104
        }
105
      }
106
    }
1✔
107
  }
1✔
108

109
  protected void updateSettings() {
110

111
    Path settingsPath = this.context.getSettingsPath();
4✔
112
    GitContext gitContext = this.context.getGitContext();
4✔
113
    Step step = null;
2✔
114
    try {
115
      // here we do not use pullOrClone to prevent asking a pointless question for repository URL...
116
      if (Files.isDirectory(settingsPath) && !this.context.getFileAccess().isEmptyDir(settingsPath)) {
11!
117
        step = this.context.newStep("Pull settings repository");
5✔
118
        gitContext.pull(settingsPath);
4✔
119
      } else {
120
        step = this.context.newStep("Clone settings repository");
5✔
121
        // check if a settings repository is given, otherwise prompt user for a repository.
122
        String repository = this.settingsRepo.getValue();
5✔
123
        if (repository == null) {
2!
124
          String message = "Missing your settings at " + settingsPath + " and no SETTINGS_URL is defined.\n"
×
125
              + "Further details can be found here: https://github.com/devonfw/IDEasy/blob/main/documentation/settings.asciidoc\n"
126
              + "Please contact the technical lead of your project to get the SETTINGS_URL for your project.\n"
127
              + "In case you just want to test IDEasy you may simply hit return to install the default settings.\n" + "Settings URL ["
128
              + IdeContext.DEFAULT_SETTINGS_REPO_URL + "]:";
129
          repository = this.context.askForInput(message, IdeContext.DEFAULT_SETTINGS_REPO_URL);
×
130
        } else if ("-".equals(repository)) {
4!
131
          repository = IdeContext.DEFAULT_SETTINGS_REPO_URL;
×
132
        }
133
        gitContext.pullOrClone(GitUrl.of(repository), settingsPath);
5✔
134
      }
135
      this.context.getGitContext().saveCurrentCommitId(settingsPath, this.context.getSettingsCommitIdPath());
8✔
136
      step.success("Successfully updated settings repository.");
3✔
137
    } finally {
138
      if (step != null) {
2!
139
        step.close();
2✔
140
      }
141
    }
142
  }
1✔
143

144
  private void updateSoftware() {
145

146
    try (Step step = this.context.newStep("Install or update software")) {
5✔
147
      Set<ToolCommandlet> toolCommandlets = new HashSet<>();
4✔
148

149
      // installed tools in IDE_HOME/software
150
      List<Path> softwarePaths = this.context.getFileAccess().listChildren(this.context.getSoftwarePath(), Files::isDirectory);
14✔
151
      for (Path softwarePath : softwarePaths) {
10✔
152
        String toolName = softwarePath.getFileName().toString();
4✔
153
        ToolCommandlet toolCommandlet = this.context.getCommandletManager().getToolCommandlet(toolName);
6✔
154
        if (toolCommandlet != null) {
2!
155
          toolCommandlets.add(toolCommandlet);
4✔
156
        }
157
      }
1✔
158

159
      // regular tools in $IDE_TOOLS
160
      List<String> regularTools = IdeVariables.IDE_TOOLS.get(this.context);
6✔
161
      if (regularTools != null) {
2!
162
        for (String regularTool : regularTools) {
10✔
163
          toolCommandlets.add(this.context.getCommandletManager().getRequiredToolCommandlet(regularTool));
8✔
164
        }
1✔
165
      }
166

167
      // custom tools in ide-custom-tools.json
168
      for (CustomTool customTool : this.context.getCustomToolRepository().getTools()) {
9!
169
        CustomToolCommandlet customToolCommandlet = new CustomToolCommandlet(this.context, customTool);
×
170
        toolCommandlets.add(customToolCommandlet);
×
171
      }
×
172

173
      // update/install the toolCommandlets
174
      for (ToolCommandlet toolCommandlet : toolCommandlets) {
10✔
175
        try {
176
          toolCommandlet.install(false);
4✔
177
        } catch (Exception e) {
1✔
178
          step.error(e, "Installation of {} failed!", toolCommandlet.getName());
11✔
179
        }
1✔
180

181
      }
1✔
182
      step.success();
2✔
183
    }
184
  }
1✔
185

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

© 2025 Coveralls, Inc