|
1 | 1 | package com.devonfw.tools.ide.commandlet;
|
2 | 2 |
|
3 |
| -import java.io.BufferedReader; |
4 | 3 | import java.io.FileWriter;
|
5 | 4 | import java.io.IOException;
|
6 |
| -import java.nio.file.AccessDeniedException; |
7 | 5 | import java.nio.file.Files;
|
8 | 6 | import java.nio.file.Path;
|
9 | 7 | import java.nio.file.StandardCopyOption;
|
10 | 8 | import java.nio.file.StandardOpenOption;
|
11 | 9 | import java.util.ArrayList;
|
12 | 10 | import java.util.List;
|
13 |
| -import java.util.Map; |
14 | 11 | import java.util.Map.Entry;
|
15 | 12 | import java.util.Properties;
|
16 |
| -import java.util.concurrent.atomic.AtomicBoolean; |
17 | 13 | import java.util.stream.Collectors;
|
18 | 14 |
|
19 | 15 | import org.json.simple.JSONArray;
|
20 | 16 | import org.json.simple.JSONObject;
|
21 | 17 |
|
22 | 18 | import com.devonfw.tools.ide.context.IdeContext;
|
23 | 19 | import com.devonfw.tools.ide.environment.EnvironmentVariables;
|
| 20 | +import com.devonfw.tools.ide.merge.DirectoryMerger; |
24 | 21 |
|
25 | 22 | /**
|
26 | 23 | * {@link Commandlet} to upgrade settings after a migration from devofw-ide to IDEasy.
|
@@ -50,8 +47,6 @@ public void run() {
|
50 | 47 | updateDevonProperties();
|
51 | 48 | replaceLegacyVariablesAndBracketsInWorkspace();
|
52 | 49 | checkIfLegacyFolderExists();
|
53 |
| - handleReplacementPatternsFiles(); |
54 |
| - checkForXmlNamespace(); |
55 | 50 | }
|
56 | 51 |
|
57 | 52 | private void checkIfLegacyFolderExists() {
|
@@ -96,158 +91,39 @@ private void checkIfLegacyFolderExists() {
|
96 | 91 | private void replaceLegacyVariablesAndBracketsInWorkspace() {
|
97 | 92 | this.context.info("Scanning for legacy variables...");
|
98 | 93 |
|
99 |
| - Map<String, String> legacyToNewMapping = Map.of( |
100 |
| - "${DEVON_IDE_HOME}", "$[IDE_HOME]", |
101 |
| - "${MAVEN_VERSION}", "$[MVN_VERSION]", |
102 |
| - "${SETTINGS_PATH}", "$[IDE_HOME]/settings" |
103 |
| - ); |
104 |
| - |
105 |
| - Path settingsDirectory = context.getIdeHome().resolve("settings"); |
106 |
| - |
107 |
| - try { |
108 |
| - Files.walk(settingsDirectory) |
109 |
| - .filter(path -> Files.isDirectory(path) && path.getFileName().toString().equals("workspace")) |
110 |
| - .forEach(workspaceDir -> { |
111 |
| - try { |
112 |
| - Files.walk(workspaceDir) |
113 |
| - .filter(Files::isRegularFile) |
114 |
| - .forEach(file -> { |
115 |
| - try { |
116 |
| - String content = Files.readString(file); |
117 |
| - String originalContent = content; |
118 |
| - |
119 |
| - for (Map.Entry<String, String> entry : legacyToNewMapping.entrySet()) { |
120 |
| - content = content.replace(entry.getKey(), entry.getValue()); |
121 |
| - } |
122 |
| - |
123 |
| - content = content.replace("{", "["); |
124 |
| - content = content.replace("}", "]"); |
125 |
| - |
126 |
| - if (!content.equals(originalContent)) { |
127 |
| - Files.writeString(file, content, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); |
128 |
| - this.context.success("Successfully updated variables/brackets " + file); |
129 |
| - } |
130 |
| - } catch (AccessDeniedException e) { |
131 |
| - this.context.error("Access denied to file: " + file + ", exception: " + e); |
132 |
| - } catch (IOException e) { |
133 |
| - this.context.error("Error processing file: " + file + ", exception: " + e); |
134 |
| - } |
135 |
| - }); |
136 |
| - } catch (IOException e) { |
137 |
| - this.context.error("Error while processing files in workspace: " + workspaceDir, e); |
138 |
| - } |
139 |
| - }); |
140 |
| - } catch (IOException e) { |
141 |
| - this.context.error("Error while scanning for workspace directories", e); |
142 |
| - } |
143 |
| - } |
144 |
| - |
145 |
| - private void handleReplacementPatternsFiles() { |
146 |
| - this.context.info("Scanning for legacy files"); |
147 |
| - |
148 |
| - Path settingsDirectory = context.getIdeHome().resolve("settings"); |
149 |
| - |
150 |
| - try { |
151 |
| - Files.walk(settingsDirectory) |
152 |
| - .filter(path -> Files.isRegularFile(path) && path.getFileName().toString().equals("replacement-patterns.properties")) |
153 |
| - .forEach(file -> { |
154 |
| - try { |
155 |
| - String content = Files.readString(file); |
156 |
| - if (!content.trim().isEmpty()) { |
157 |
| - this.context.warning("The file 'replacement-patterns.properties' is not empty: " + file); |
158 |
| - } |
159 |
| - |
160 |
| - Files.delete(file); |
161 |
| - this.context.success("Deleted 'replacement-patterns.properties' from: " + file); |
162 |
| - } catch (IOException e) { |
163 |
| - this.context.error("Error processing 'replacement-patterns.properties' file: " + file, e); |
164 |
| - } |
165 |
| - }); |
166 |
| - } catch (IOException e) { |
167 |
| - this.context.error("Error scanning for 'replacement-patterns.properties' file", e); |
| 94 | + DirectoryMerger merger = this.context.getWorkspaceMerger(); |
| 95 | + Path settingsDir = this.context.getSettingsPath(); |
| 96 | + Path workspaceDir = settingsDir.resolve(IdeContext.FOLDER_WORKSPACE); |
| 97 | + if (Files.isDirectory(workspaceDir)) { |
| 98 | + merger.upgrade(workspaceDir); |
168 | 99 | }
|
169 |
| - } |
170 |
| - |
171 |
| - private void checkForXmlNamespace() { |
172 |
| - this.context.info("Scanning XML files..."); |
173 |
| - Path settingsDirectory = context.getSettingsPath(); |
174 |
| - AtomicBoolean missingNamespaceFound = new AtomicBoolean(false); |
175 |
| - |
176 |
| - try { |
177 |
| - List<Path> workspaceDirs = findWorkspaceDirectories(settingsDirectory); |
178 |
| - |
179 |
| - for (Path workspaceDir : workspaceDirs) { |
180 |
| - missingNamespaceFound.set( |
181 |
| - checkXmlFilesForNamespace(workspaceDir, missingNamespaceFound.get())); |
| 100 | + this.context.getFileAccess().listChildrenMapped(settingsDir, child -> { |
| 101 | + Path childWorkspaceDir = child.resolve(IdeContext.FOLDER_WORKSPACE); |
| 102 | + if (Files.isDirectory(childWorkspaceDir)) { |
| 103 | + merger.upgrade(childWorkspaceDir); |
182 | 104 | }
|
183 |
| - |
184 |
| - // Output the result |
185 |
| - if (missingNamespaceFound.get()) { |
186 |
| - this.context.warning("For further information, please visit https://github.com/devonfw/IDEasy/blob/main/documentation/configurator.adoc#xml-merger"); |
187 |
| - } else { |
188 |
| - this.context.success("Your XML files are up to date"); |
189 |
| - } |
190 |
| - |
191 |
| - } catch (IOException e) { |
192 |
| - this.context.error("Error walking through the 'settings' directory", e); |
193 |
| - } |
| 105 | + return null; |
| 106 | + }); |
194 | 107 | }
|
195 | 108 |
|
196 |
| - private List<Path> findWorkspaceDirectories(Path settingsDirectory) throws IOException { |
| 109 | + private List<Path> findWorkspaceDirectories() { |
| 110 | + Path settingsDir = this.context.getSettingsPath(); |
197 | 111 | List<Path> workspaceDirs = new ArrayList<>();
|
198 |
| - Files.walk(settingsDirectory) |
199 |
| - .filter(path -> Files.isDirectory(path) && path.getFileName().toString().equals("workspace")) |
200 |
| - .forEach(workspaceDirs::add); |
201 |
| - return workspaceDirs; |
202 |
| - } |
203 |
| - |
204 |
| - private boolean checkXmlFilesForNamespace(Path workspaceDir, boolean missingNamespaceFound) { |
205 |
| - try { |
206 |
| - List<Path> xmlFiles = findXmlFilesInDirectory(workspaceDir); |
207 |
| - |
208 |
| - for (Path xmlFile : xmlFiles) { |
209 |
| - missingNamespaceFound = checkXmlNamespaceInFile(xmlFile, missingNamespaceFound); |
210 |
| - } |
211 |
| - } catch (IOException e) { |
212 |
| - this.context.error("Error processing workspace directory: " + workspaceDir, e); |
| 112 | + Path workspaceDir = settingsDir.resolve(IdeContext.FOLDER_WORKSPACE); |
| 113 | + if (Files.isDirectory(workspaceDir)) { |
| 114 | + workspaceDirs.add(workspaceDir); |
213 | 115 | }
|
214 |
| - return missingNamespaceFound; |
215 |
| - } |
216 |
| - |
217 |
| - private List<Path> findXmlFilesInDirectory(Path directory) throws IOException { |
218 |
| - List<Path> xmlFiles = new ArrayList<>(); |
219 |
| - Files.walk(directory) |
220 |
| - .filter(file -> Files.isRegularFile(file) && file.toString().endsWith(".xml")) |
221 |
| - .forEach(xmlFiles::add); |
222 |
| - return xmlFiles; |
223 |
| - } |
224 |
| - |
225 |
| - private boolean checkXmlNamespaceInFile(Path xmlFile, boolean missingNamespaceFound) { |
226 |
| - try (BufferedReader reader = Files.newBufferedReader(xmlFile)) { |
227 |
| - String line; |
228 |
| - int linesRead = 0; |
229 |
| - boolean namespaceFound = false; |
230 |
| - |
231 |
| - while ((line = reader.readLine()) != null && linesRead < 3) { |
232 |
| - linesRead++; |
233 |
| - if (line.contains("\"https://github.com/devonfw/IDEasy/merge\"")) { |
234 |
| - namespaceFound = true; |
235 |
| - break; |
236 |
| - } |
| 116 | + List<Path> childWorkspaceDirs = this.context.getFileAccess().listChildrenMapped(settingsDir, child -> { |
| 117 | + Path childWorkspaceDir = child.resolve(IdeContext.FOLDER_WORKSPACE); |
| 118 | + if (Files.isDirectory(childWorkspaceDir)) { |
| 119 | + return childWorkspaceDir; |
237 | 120 | }
|
238 |
| - |
239 |
| - if (!namespaceFound) { |
240 |
| - this.context.warning("The XML file " + xmlFile + " does not contain the required 'xmlns:merge' attribute."); |
241 |
| - missingNamespaceFound = true; |
242 |
| - } |
243 |
| - |
244 |
| - } catch (IOException e) { |
245 |
| - this.context.error("Error reading the file: " + xmlFile, e); |
246 |
| - } |
247 |
| - return missingNamespaceFound; |
| 121 | + return null; |
| 122 | + }); |
| 123 | + workspaceDirs.addAll(childWorkspaceDirs); |
| 124 | + return workspaceDirs; |
248 | 125 | }
|
249 | 126 |
|
250 |
| - |
251 | 127 | private void createCustomToolsJson(String variable) {
|
252 | 128 | try (FileWriter writer = new FileWriter(context.getIdeHome().resolve("settings").resolve(IdeContext.FILE_CUSTOM_TOOLS).toString())) {
|
253 | 129 | JSONArray tabelObject = new JSONArray();
|
|
0 commit comments