-
Notifications
You must be signed in to change notification settings - Fork 36
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
base: main
Are you sure you want to change the base?
#1008: improve upgrade settings #1146
Conversation
…ttps://github.com/leonrohne27/IDEasy into fix/1130-improve-behaviour-on-ambigous-xpath-match
Pull Request Test Coverage Report for Build 13944758060Details
💛 - Coveralls |
In case you wonder, I messed up with the branches, but it should be fine now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@leonrohne27 thanks for your PR.
The actual updating of the properties file looks already good to me. 👍
However, the way you try to find the properties is not correct.
It is required that you first read and understand the documentation of the related feature:
https://github.com/devonfw/IDEasy/blob/main/documentation/repository.adoc
p.s.: I took a shortcut even though this PR was in team review state to speed up our progress.
private void cleanupLegacyProperties() { | ||
this.context.info("Cleaning up legacy properties..."); | ||
|
||
Path rootDirectory = Paths.get(System.getProperty("user.dir")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dont hardcode user.dir
this will cause JUnits to apply the migration in your home directory but tests should only work in test directories (./target/
) and should never cause such side-effects.
Also the variable name could be named more intuitive:
Path rootDirectory = Paths.get(System.getProperty("user.dir")); | |
Path homeDirectory = this.config.getgetUserHome(); |
However, why are you searching recursively for IDEasy.properties
files in the users home directory?
This does not make sense.
What you IMHO actually need is something like this:
Path settingsPath = context.getSettingsPath();
Path repositoriesPath = settingsPath.resolve(IdeContext.FOLDER_REPOSITORIES);
if (!Files.isDirectory(repositoriesPath)) {
return;
}
try (Stream<Path> childStream = Files.list(repositoriesPath)) {
Iterator<Path> iterator = childStream.iterator();
while (iterator.hasNext()) {
Path child = iterator.next();
if (child.getFileName().toString().endsWith(IdeContext.EXT_PROPERTIES);
updateProperties(child);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW: Instead of using Files.list
directly what also requires a catch statement that I omitted, it seems smarter that you use fileAccess
like already done in the same class here:
IDEasy/cli/src/main/java/com/devonfw/tools/ide/commandlet/UpgradeSettingsCommandlet.java
Lines 83 to 89 in 8a9936e
fileAccess.listChildrenMapped(settingsDir, child -> { | |
Path childWorkspaceDir = child.resolve(IdeContext.FOLDER_WORKSPACE); | |
if (fileAccess.isExpectedFolder(childWorkspaceDir)) { | |
merger.upgrade(childWorkspaceDir); | |
} | |
return null; | |
}); |
This allows to supply a lambda function that is called for each file found in the directory (you can configure if only flat or recursively). That lambda function is used to map and filter the found child Path
objects. When it returns null
the child is filtered and not returned. You could even map if from Path
to some other Java type like String
in this function. By directly applying the modification as "side-effect" inside the lambda function the result of the listChildrenMapped
method can be ignored. In the example code the side effect is merger.upgrade
and in your case it would be updateProperties
instead.
Path rootDirectory = Paths.get(System.getProperty("user.dir")); | ||
try { | ||
Files.walk(rootDirectory) | ||
.filter(path -> path.getFileName().toString().equals("IDEasy.properties")) |
There was a problem hiding this comment.
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.
} | ||
} | ||
|
||
private void updateProperties(Path filePath) throws IOException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename to make more clear what this is doing:
private void updateProperties(Path filePath) throws IOException { | |
private void updateRepositoryPropertiesFile(Path filePath) throws IOException { |
|
||
private void updateProperties(Path filePath) throws IOException { | ||
List<String> lines = Files.readAllLines(filePath); | ||
List<String> updatedLines = new ArrayList<>(); |
There was a problem hiding this comment.
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.
} | ||
updatedLines.add(line); | ||
} | ||
Files.write(filePath, updatedLines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); |
There was a problem hiding this comment.
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.
updatedLines.add(line); | ||
} | ||
Files.write(filePath, updatedLines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); | ||
this.context.success("Successfully updated IDEasy.properties at " + filePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, you misunderstood the IDEasy.properties
filename. See and compare the different examples:
- https://github.com/m-m-m/settings/tree/master/repositories
- https://github.com/devonfw/ide-settings/tree/main/repositories
- https://github.com/devonfw-training/java-quarkus-settings/tree/master/projects (devonfw-ide legacy config)
Also see here: https://github.com/devonfw/IDEasy/blob/main/documentation/coding-conventions.adoc#logging
this.context.success("Successfully updated IDEasy.properties at " + filePath); | |
this.context.success("Successfully updated repository configuration file {}", filePath); |
Fixes: #1008
Implemets: