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

devonfw / IDEasy / 12995336297

27 Jan 2025 06:04PM UTC coverage: 68.45% (-0.05%) from 68.499%
12995336297

push

github

web-flow
#931: enhance settings in code repository (#983)

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

2793 of 4475 branches covered (62.41%)

Branch coverage included in aggregate %.

7211 of 10140 relevant lines covered (71.11%)

3.09 hits per line

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

82.44
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.CustomToolMetadata;
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
    if (!this.context.isSettingsRepositorySymlinkOrJunction() || this.context.isForceMode()) {
4!
49
      updateSettings();
2✔
50
    }
51
    updateConf();
2✔
52
    reloadContext();
2✔
53

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

61
  private void reloadContext() {
62

63
    ((AbstractIdeContext) this.context).reload();
4✔
64
  }
1✔
65

66
  private void updateConf() {
67

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

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

85
  private void setupConf(Path template, Path conf) {
86

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

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

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

111
  /**
112
   * Updates the settings repository in IDE_HOME/settings by either cloning if no such repository exists or pulling
113
   * if the repository exists then saves the latest current commit ID in the file ".commit.id".
114
   */
115
  protected void updateSettings() {
116

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

150
  private void updateSoftware() {
151

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

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

165
      // regular tools in $IDE_TOOLS
166
      List<String> regularTools = IdeVariables.IDE_TOOLS.get(this.context);
6✔
167
      if (regularTools != null) {
2!
168
        for (String regularTool : regularTools) {
10✔
169
          toolCommandlets.add(this.context.getCommandletManager().getRequiredToolCommandlet(regularTool));
8✔
170
        }
1✔
171
      }
172

173
      // custom tools in ide-custom-tools.json
174
      for (CustomToolMetadata customTool : this.context.getCustomToolRepository().getTools()) {
9!
175
        CustomToolCommandlet customToolCommandlet = new CustomToolCommandlet(this.context, customTool);
×
176
        toolCommandlets.add(customToolCommandlet);
×
177
      }
×
178

179
      // update/install the toolCommandlets
180
      for (ToolCommandlet toolCommandlet : toolCommandlets) {
10✔
181
        try {
182
          toolCommandlet.install(false);
4✔
183
        } catch (Exception e) {
1✔
184
          step.error(e, "Installation of {} failed!", toolCommandlet.getName());
11✔
185
        }
1✔
186

187
      }
1✔
188
      step.success();
2✔
189
    }
190
  }
1✔
191

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