Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1008: improve upgrade settings #1146

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.devonfw.tools.ide.commandlet;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import com.devonfw.tools.ide.context.IdeContext;
Expand Down Expand Up @@ -139,6 +144,7 @@ private void updateProperties(EnvironmentVariablesPropertiesFile environmentVari
}
updatePropertiesLegacyEdition(environmentVariables, "INTELLIJ_EDITION_TYPE", "INTELLIJ_EDITION", this::mapLegacyIntellijEdition);
updatePropertiesLegacyEdition(environmentVariables, "ECLIPSE_EDITION_TYPE", "ECLIPSE_EDITION", this::mapLegacyEclipseEdition);
cleanupLegacyProperties();
environmentVariables.save();
this.context.getFileAccess().backup(environmentVariables.getLegacyPropertiesFilePath());
}
Expand Down Expand Up @@ -181,4 +187,43 @@ private static void updatePropertiesLegacyEdition(EnvironmentVariablesProperties
environmentVariables.remove(legacyEditionVariable);
}
}

private void cleanupLegacyProperties() {
this.context.info("Cleaning up legacy properties...");

Path homeDirectory = this.config.getgetUserHome();
try {
Files.walk(rootDirectory)
.filter(path -> path.getFileName().toString().equals("IDEasy.properties"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete but for the record. This filename IDEasy.properties is just an example and cannot be hardcoded.
There can be any number of *.properites files in the repositories folder and you cannot make any assumptions about their name.

.forEach(filePath -> {
try {
updateProperties(filePath);
} catch (IOException e) {
this.context.warning("Error processing IDEasy.properties at " + filePath);
}
});
} catch (IOException e) {
this.context.warning("Error walking the file tree to find IDEasy.properties.");
}
}

private void updateRepositoryPropertiesFile(Path filePath) throws IOException {
List<String> lines = Files.readAllLines(filePath);
List<String> updatedLines = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: You can do the modifications directly in the original list (lines) and do not need a second list as a copy.


for (String line : lines) {
if (line.startsWith("git.url=") || line.startsWith("git-url")) {
String gitUrl = line.substring("git.url=".length());
updatedLines.add("git_url=" + gitUrl);
continue;
}
if (line.startsWith("eclipse=import")) {
updatedLines.add("import=eclipse");
continue;
}
updatedLines.add(line);
}
Files.write(filePath, updatedLines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should add a boolean flag boolean updated=false; before the loop and set it to true when you have modified something.
Then you can only write the file changes back if something changed and additionally log the success message only in that case so if nothing changed then nothing if logged.

this.context.success("Successfully updated repository configuration file {}", filePath);
}
}
Loading