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

devonfw / IDEasy / 13541800051

26 Feb 2025 10:29AM UTC coverage: 68.073% (+0.03%) from 68.046%
13541800051

Pull #1078

github

web-flow
Merge f5a942958 into 76ae28441
Pull Request #1078: #929 optimize upgrade settings using file access

2998 of 4849 branches covered (61.83%)

Branch coverage included in aggregate %.

7782 of 10987 relevant lines covered (70.83%)

3.08 hits per line

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

78.36
cli/src/main/java/com/devonfw/tools/ide/commandlet/UpgradeSettingsCommandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.nio.file.Path;
4
import java.util.function.Function;
5

6
import com.devonfw.tools.ide.context.IdeContext;
7
import com.devonfw.tools.ide.environment.EnvironmentVariables;
8
import com.devonfw.tools.ide.environment.EnvironmentVariablesPropertiesFile;
9
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
10
import com.devonfw.tools.ide.io.FileAccess;
11
import com.devonfw.tools.ide.merge.DirectoryMerger;
12
import com.devonfw.tools.ide.tool.mvn.Mvn;
13
import com.devonfw.tools.ide.tool.repository.CustomToolsJson;
14
import com.devonfw.tools.ide.tool.repository.CustomToolsJsonMapper;
15
import com.devonfw.tools.ide.variable.IdeVariables;
16
import com.devonfw.tools.ide.variable.VariableDefinition;
17

18
/**
19
 * {@link Commandlet} to upgrade settings after a migration from devonfw-ide to IDEasy.
20
 */
21
public class UpgradeSettingsCommandlet extends Commandlet {
22

23
  /**
24
   * The constructor.
25
   *
26
   * @param context the {@link IdeContext}.
27
   */
28
  public UpgradeSettingsCommandlet(IdeContext context) {
29

30
    super(context);
3✔
31
    addKeyword(getName());
4✔
32
  }
1✔
33

34
  @Override
35
  public String getName() {
36

37
    return "upgrade-settings";
2✔
38
  }
39

40
  @Override
41
  public void run() {
42
    updateLegacyFolders();
2✔
43
    updateProperties();
2✔
44
    updateWorkspaceTemplates();
2✔
45
  }
1✔
46

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

55
  private void updateLegacyFolder(Path folder, String legacyName, String newName) {
56
    FileAccess fileAccess = this.context.getFileAccess();
4✔
57
    Path legacyFolder = folder.resolve(legacyName);
4✔
58
    Path newFolder = folder.resolve(newName);
4✔
59
    if (fileAccess.isExpectedFolder(legacyFolder)) {
4✔
60
      try {
61
        if (!fileAccess.exists(newFolder)) {
4!
62
          fileAccess.move(legacyFolder, newFolder);
4✔
63
          this.context.success("Successfully renamed folder '{}' to '{}' in {}.", legacyName, newName, folder);
18✔
64
        }
65
      } catch (IllegalStateException e) {
×
66
        this.context.error(e, "Error renaming folder {} to {} in {}", legacyName, newName, folder);
×
67
      }
1✔
68
    }
69
  }
1✔
70

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

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

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

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

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

146
  private String mapLegacyIntellijEdition(String legacyEdition) {
147

148
    return switch (legacyEdition) {
9!
149
      case "U" -> "ultimate";
2✔
150
      case "C" -> "intellij";
×
151
      default -> {
152
        this.context.warning("Undefined legacy edition {}", legacyEdition);
×
153
        yield "intellij";
×
154
      }
155
    };
156
  }
157

158
  private String mapLegacyEclipseEdition(String legacyEdition) {
159

160
    return switch (legacyEdition) {
×
161
      case "java" -> "eclipse";
×
162
      case "jee" -> "jee";
×
163
      case "cpp" -> "cpp";
×
164
      default -> {
165
        this.context.warning("Undefined legacy edition {}", legacyEdition);
×
166
        yield "eclipse";
×
167
      }
168
    };
169
  }
170

171
  private static void updatePropertiesLegacyEdition(EnvironmentVariablesPropertiesFile environmentVariables, String legacyEditionVariable,
172
      String newEditionVariable, Function<String, String> editionMapper) {
173

174
    String legacyEdition = environmentVariables.get(legacyEditionVariable);
4✔
175
    if (legacyEdition != null) {
2✔
176
      String newEdition = environmentVariables.get(newEditionVariable);
4✔
177
      if (newEdition == null) {
2✔
178
        environmentVariables.set(newEditionVariable, editionMapper.apply(legacyEdition), false);
9✔
179
      }
180
      environmentVariables.remove(legacyEditionVariable);
3✔
181
    }
182
  }
1✔
183
}
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