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

devonfw / IDEasy / 14191090483

01 Apr 2025 08:38AM UTC coverage: 67.77% (+0.02%) from 67.755%
14191090483

Pull #1146

github

web-flow
Merge 02f8c1714 into 175374c2c
Pull Request #1146: #1008: improve upgrade settings

3064 of 4950 branches covered (61.9%)

Branch coverage included in aggregate %.

7906 of 11237 relevant lines covered (70.36%)

3.07 hits per line

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

76.97
cli/src/main/java/com/devonfw/tools/ide/commandlet/UpgradeSettingsCommandlet.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.nio.file.StandardCopyOption;
7
import java.nio.file.StandardOpenOption;
8
import java.util.List;
9
import java.util.function.Function;
10

11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.environment.EnvironmentVariables;
13
import com.devonfw.tools.ide.environment.EnvironmentVariablesPropertiesFile;
14
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
15
import com.devonfw.tools.ide.io.FileAccess;
16
import com.devonfw.tools.ide.merge.DirectoryMerger;
17
import com.devonfw.tools.ide.tool.mvn.Mvn;
18
import com.devonfw.tools.ide.tool.repository.CustomToolsJson;
19
import com.devonfw.tools.ide.tool.repository.CustomToolsJsonMapper;
20
import com.devonfw.tools.ide.variable.IdeVariables;
21
import com.devonfw.tools.ide.variable.VariableDefinition;
22

23
/**
24
 * {@link Commandlet} to upgrade settings after a migration from devonfw-ide to IDEasy.
25
 */
26
public class UpgradeSettingsCommandlet extends Commandlet {
27

28
  /**
29
   * The constructor.
30
   *
31
   * @param context the {@link IdeContext}.
32
   */
33
  public UpgradeSettingsCommandlet(IdeContext context) {
34

35
    super(context);
3✔
36
    addKeyword(getName());
4✔
37
  }
1✔
38

39
  @Override
40
  public String getName() {
41

42
    return "upgrade-settings";
2✔
43
  }
44

45
  @Override
46
  public void run() {
47
    updateLegacyFolders();
2✔
48
    updateProperties();
2✔
49
    updateWorkspaceTemplates();
2✔
50
  }
1✔
51

52
  private void updateLegacyFolders() {
53
    this.context.info("Updating legacy folders if present...");
4✔
54
    Path settingsPath = context.getSettingsPath();
4✔
55
    updateLegacyFolder(settingsPath, IdeContext.FOLDER_LEGACY_REPOSITORIES, IdeContext.FOLDER_REPOSITORIES);
5✔
56
    updateLegacyFolder(settingsPath, IdeContext.FOLDER_LEGACY_TEMPLATES, IdeContext.FOLDER_TEMPLATES);
5✔
57
    updateLegacyFolder(settingsPath.resolve(IdeContext.FOLDER_TEMPLATES).resolve(IdeContext.FOLDER_CONF), Mvn.MVN_CONFIG_LEGACY_FOLDER, Mvn.MVN_CONFIG_FOLDER);
9✔
58
  }
1✔
59

60
  private void updateLegacyFolder(Path folder, String legacyName, String newName) {
61
    FileAccess fileAccess = this.context.getFileAccess();
4✔
62
    Path legacyFolder = folder.resolve(legacyName);
4✔
63
    Path newFolder = folder.resolve(newName);
4✔
64
    if (fileAccess.isExpectedFolder(legacyFolder)) {
4✔
65
      try {
66
        if (!Files.exists(newFolder)) {
5!
67
          fileAccess.move(legacyFolder, newFolder, StandardCopyOption.REPLACE_EXISTING);
10✔
68
          this.context.success("Successfully renamed folder '{}' to '{}' in {}.", legacyName, newName, folder);
18✔
69
        }
70
      } catch (IllegalStateException e) {
×
71
        this.context.error(e, "Error renaming folder {} to {} in {}", legacyName, newName, folder);
×
72
      }
1✔
73
    }
74
  }
1✔
75

76
  private void updateWorkspaceTemplates() {
77
    this.context.info("Updating workspace templates (replace legacy variables and change variable syntax)...");
4✔
78

79
    FileAccess fileAccess = this.context.getFileAccess();
4✔
80
    DirectoryMerger merger = this.context.getWorkspaceMerger();
4✔
81
    Path settingsDir = this.context.getSettingsPath();
4✔
82
    Path workspaceDir = settingsDir.resolve(IdeContext.FOLDER_WORKSPACE);
4✔
83
    if (fileAccess.isExpectedFolder(workspaceDir)) {
4!
84
      merger.upgrade(workspaceDir);
3✔
85
    }
86
    fileAccess.listChildrenMapped(settingsDir, child -> {
7✔
87
      Path childWorkspaceDir = child.resolve(IdeContext.FOLDER_WORKSPACE);
4✔
88
      if (fileAccess.isExpectedFolder(childWorkspaceDir)) {
4✔
89
        merger.upgrade(childWorkspaceDir);
3✔
90
      }
91
      return null;
2✔
92
    });
93
  }
1✔
94

95
  private void updateProperties() {
96
    // updates DEVON_IDE_CUSTOM_TOOLS to new ide-custom-tools.json
97
    String devonCustomTools = IdeVariables.DEVON_IDE_CUSTOM_TOOLS.get(this.context);
6✔
98
    if (devonCustomTools != null) {
2✔
99
      CustomToolsJson customToolsJson = CustomToolsJsonMapper.parseCustomToolsFromLegacyConfig(devonCustomTools, context);
5✔
100
      if (customToolsJson != null) {
2!
101
        CustomToolsJsonMapper.saveJson(customToolsJson, this.context.getSettingsPath().resolve(IdeContext.FILE_CUSTOM_TOOLS));
7✔
102
      }
103
    }
104

105
    // update properties (devon.properties -> ide.properties, convert legacy properties)
106
    EnvironmentVariables environmentVariables = context.getVariables();
4✔
107
    while (environmentVariables != null) {
2✔
108
      if (environmentVariables instanceof EnvironmentVariablesPropertiesFile environmentVariablesProperties) {
6✔
109
        updateProperties(environmentVariablesProperties);
3✔
110
      }
111
      environmentVariables = environmentVariables.getParent();
4✔
112
    }
113
    Path templatePropertiesDir = this.context.getSettingsTemplatePath().resolve(IdeContext.FOLDER_CONF);
6✔
114
    if (Files.exists(templatePropertiesDir)) {
5!
115
      EnvironmentVariablesPropertiesFile environmentVariablesProperties = new EnvironmentVariablesPropertiesFile(null, EnvironmentVariablesType.CONF,
10✔
116
          templatePropertiesDir, null, this.context);
117
      updateProperties(environmentVariablesProperties);
3✔
118
    }
119
  }
1✔
120

121
  private void updateProperties(EnvironmentVariablesPropertiesFile environmentVariables) {
122
    Path propertiesFilePath = environmentVariables.getPropertiesFilePath();
3✔
123
    if (environmentVariables.getLegacyConfiguration() != null) {
3!
124
      if (environmentVariables.getType() == EnvironmentVariablesType.SETTINGS) {
4✔
125
        // adds disabled legacySupportEnabled variable if missing in ide.properties
126
        environmentVariables.set(IdeVariables.IDE_VARIABLE_SYNTAX_LEGACY_SUPPORT_ENABLED.getName(), "false", false);
7✔
127
      }
128
      environmentVariables.remove(IdeVariables.DEVON_IDE_CUSTOM_TOOLS.getName());
4✔
129
      for (VariableDefinition<?> var : IdeVariables.VARIABLES) {
10✔
130
        String legacyName = var.getLegacyName();
3✔
131
        if (legacyName != null) {
2✔
132
          String value = environmentVariables.get(legacyName);
4✔
133
          if (value != null) {
2!
134
            String name = var.getName();
×
135
            String newValue = environmentVariables.get(name);
×
136
            if (newValue == null) {
×
137
              environmentVariables.set(name, value, environmentVariables.isExported(name));
×
138
            }
139
          }
140
          environmentVariables.remove(legacyName);
3✔
141
        }
142
      }
1✔
143
      updatePropertiesLegacyEdition(environmentVariables, "INTELLIJ_EDITION_TYPE", "INTELLIJ_EDITION", this::mapLegacyIntellijEdition);
6✔
144
      updatePropertiesLegacyEdition(environmentVariables, "ECLIPSE_EDITION_TYPE", "ECLIPSE_EDITION", this::mapLegacyEclipseEdition);
6✔
145
      cleanupLegacyProperties();
2✔
146
      environmentVariables.save();
2✔
147
      this.context.getFileAccess().backup(environmentVariables.getLegacyPropertiesFilePath());
7✔
148
    }
149
  }
1✔
150

151
  private String mapLegacyIntellijEdition(String legacyEdition) {
152

153
    return switch (legacyEdition) {
9!
154
      case "U" -> "ultimate";
2✔
155
      case "C" -> "intellij";
×
156
      default -> {
157
        this.context.warning("Undefined legacy edition {}", legacyEdition);
×
158
        yield "intellij";
×
159
      }
160
    };
161
  }
162

163
  private String mapLegacyEclipseEdition(String legacyEdition) {
164

165
    return switch (legacyEdition) {
×
166
      case "java" -> "eclipse";
×
167
      case "jee" -> "jee";
×
168
      case "cpp" -> "cpp";
×
169
      default -> {
170
        this.context.warning("Undefined legacy edition {}", legacyEdition);
×
171
        yield "eclipse";
×
172
      }
173
    };
174
  }
175

176
  private static void updatePropertiesLegacyEdition(EnvironmentVariablesPropertiesFile environmentVariables, String legacyEditionVariable,
177
      String newEditionVariable, Function<String, String> editionMapper) {
178

179
    String legacyEdition = environmentVariables.get(legacyEditionVariable);
4✔
180
    if (legacyEdition != null) {
2✔
181
      String newEdition = environmentVariables.get(newEditionVariable);
4✔
182
      if (newEdition == null) {
2✔
183
        environmentVariables.set(newEditionVariable, editionMapper.apply(legacyEdition), false);
9✔
184
      }
185
      environmentVariables.remove(legacyEditionVariable);
3✔
186
    }
187
  }
1✔
188

189
  private void cleanupLegacyProperties() {
190
    this.context.info("Cleaning up legacy properties...");
4✔
191

192
    Path settingsPath = context.getSettingsPath();
4✔
193
    Path repositoriesPath = settingsPath.resolve(IdeContext.FOLDER_REPOSITORIES);
4✔
194

195
    FileAccess fileAccess = this.context.getFileAccess();
4✔
196
    if (fileAccess.isExpectedFolder(repositoriesPath)) {
4!
197
      fileAccess.listChildrenMapped(repositoriesPath, child -> {
6✔
198
        try {
199
          updateRepositoryPropertiesFile(child);
3✔
200
        } catch (IOException e) {
×
201
          throw new RuntimeException(e);
×
202
        }
1✔
203
        return null;
2✔
204
      });
205
    }
206
  }
1✔
207

208
  private void updateRepositoryPropertiesFile(Path filePath) throws IOException {
209
    List<String> lines = Files.readAllLines(filePath);
3✔
210
    boolean updated = false;
2✔
211

212
    for (int i = 0; i < lines.size(); i++) {
8✔
213
      String line = lines.get(i).trim();
6✔
214
      if (line.startsWith("git.url=") || line.startsWith("git-url")) {
8!
215
        String gitUrl = line.substring(line.indexOf("=") + 1).trim();
×
216
        lines.set(i, "git_url=" + gitUrl);
×
217
        updated = true;
×
218
        continue;
×
219
      }
220
      if (line.startsWith("eclipse=import")) {
4✔
221
        lines.set(i, "import=eclipse");
5✔
222
        updated = true;
2✔
223
      }
224
    }
225
    if (updated) {
2✔
226
      try {
227
        Files.write(filePath, lines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
14✔
228
        this.context.success("Successfully updated repository configuration file {}", filePath);
10✔
229
      } catch (IOException e) {
×
230
        this.context.error("Failed to write updated repository configuration file {}", filePath);
×
231
        throw e;
×
232
      }
1✔
233
    }
234
  }
1✔
235
}
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