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 14 commits into
base: main
Choose a base branch
from

Conversation

leonrohne27
Copy link
Contributor

Fixes: #1008

Implemets:

  • In properties, "git-url" und "git.url" are replaced with "git_url"
  • In properties, "eclipse=import" is replaced with "import=eclipse"

@coveralls
Copy link
Collaborator

coveralls commented Mar 19, 2025

Pull Request Test Coverage Report for Build 13944758060

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 21 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.02%) to 67.678%

Files with Coverage Reduction New Missed Lines %
com/devonfw/tools/ide/commandlet/UpgradeSettingsCommandlet.java 21 77.78%
Totals Coverage Status
Change from base Build 13918607876: 0.02%
Covered Lines: 7848
Relevant Lines: 11168

💛 - Coveralls

@leonrohne27
Copy link
Contributor Author

leonrohne27 commented Mar 19, 2025

In case you wonder, I messed up with the branches, but it should be fine now.
To implement this story, I used filewalkers, which I know you removed in the other code sections.
I tried it otherwise but I was not able to fix it. Now it works, but you have to decide whether you want this kind of implementation or not.

@leonrohne27 leonrohne27 self-assigned this Mar 19, 2025
Copy link
Member

@hohwille hohwille left a 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"));
Copy link
Member

@hohwille hohwille Mar 20, 2025

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:

Suggested change
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);
      }
    }

Copy link
Member

@hohwille hohwille Mar 20, 2025

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:

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"))
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.

}
}

private void updateProperties(Path filePath) throws IOException {
Copy link
Member

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:

Suggested change
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<>();
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.

}
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.

updatedLines.add(line);
}
Files.write(filePath, updatedLines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
this.context.success("Successfully updated IDEasy.properties at " + filePath);
Copy link
Member

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:

Also see here: https://github.com/devonfw/IDEasy/blob/main/documentation/coding-conventions.adoc#logging

Suggested change
this.context.success("Successfully updated IDEasy.properties at " + filePath);
this.context.success("Successfully updated repository configuration file {}", filePath);

@hohwille hohwille changed the title Fix/1008 improve upgrade settings #1008: improve upgrade settings Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Team Review
Development

Successfully merging this pull request may close these issues.

Improve upgrade-settings to cleanup legacy configs in repositories
4 participants