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

devonfw / IDEasy / 13944758060

19 Mar 2025 10:51AM UTC coverage: 67.678% (+0.02%) from 67.657%
13944758060

Pull #1146

github

web-flow
Merge 9610ea93e into 2ef884351
Pull Request #1146: Fix/1008 improve upgrade settings

3042 of 4923 branches covered (61.79%)

Branch coverage included in aggregate %.

7848 of 11168 relevant lines covered (70.27%)

3.07 hits per line

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

77.78
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.Paths;
7
import java.nio.file.StandardCopyOption;
8
import java.nio.file.StandardOpenOption;
9
import java.util.ArrayList;
10
import java.util.List;
11
import java.util.function.Function;
12

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

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

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

37
    super(context);
3✔
38
    addKeyword(getName());
4✔
39
  }
1✔
40

41
  @Override
42
  public String getName() {
43

44
    return "upgrade-settings";
2✔
45
  }
46

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

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

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

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

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

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

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

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

153
  private String mapLegacyIntellijEdition(String legacyEdition) {
154

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

165
  private String mapLegacyEclipseEdition(String legacyEdition) {
166

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

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

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

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

194
    Path rootDirectory = Paths.get(System.getProperty("user.dir"));
6✔
195
    try {
196
      Files.walk(rootDirectory)
5✔
197
          .filter(path -> path.getFileName().toString().equals("IDEasy.properties"))
9✔
198
          .forEach(filePath -> {
1✔
199
            try {
200
              updateProperties(filePath);
3✔
201
            } catch (IOException e) {
×
202
              this.context.warning("Error processing IDEasy.properties at " + filePath);
×
203
            }
1✔
204
          });
1✔
205
    } catch (IOException e) {
×
206
      this.context.warning("Error walking the file tree to find IDEasy.properties.");
×
207
    }
1✔
208
  }
1✔
209

210
  private void updateProperties(Path filePath) throws IOException {
211
    List<String> lines = Files.readAllLines(filePath);
3✔
212
    List<String> updatedLines = new ArrayList<>();
4✔
213

214
    for (String line : lines) {
10✔
215
      if (line.startsWith("git.url=") || line.startsWith("git-url")) {
8!
216
        String gitUrl = line.substring("git.url=".length());
×
217
        updatedLines.add("git_url=" + gitUrl);
×
218
        continue;
×
219
      }
220
      if (line.startsWith("eclipse=import")) {
4✔
221
        updatedLines.add("import=eclipse");
4✔
222
        continue;
1✔
223
      }
224
      updatedLines.add(line);
4✔
225
    }
1✔
226
    Files.write(filePath, updatedLines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
14✔
227
    this.context.success("Successfully updated IDEasy.properties at " + filePath);
6✔
228
  }
1✔
229
}
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