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

devonfw / IDEasy / 13562865061

27 Feb 2025 09:14AM UTC coverage: 68.255% (+0.03%) from 68.229%
13562865061

push

github

web-flow
#929: optimize upgrade settings using file access (#1078)

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

3032 of 4891 branches covered (61.99%)

Branch coverage included in aggregate %.

7867 of 11077 relevant lines covered (71.02%)

3.09 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.nio.file.StandardCopyOption;
5
import java.util.function.Function;
6

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

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

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

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

35
  @Override
36
  public String getName() {
37

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

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

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

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

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

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

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

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

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

147
  private String mapLegacyIntellijEdition(String legacyEdition) {
148

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

159
  private String mapLegacyEclipseEdition(String legacyEdition) {
160

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

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

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