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

#929: optimize upgrade settings using file access #1078

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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,7 +1,5 @@
package com.devonfw.tools.ide.commandlet;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.function.Function;
Expand All @@ -10,6 +8,7 @@
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariablesPropertiesFile;
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.merge.DirectoryMerger;
import com.devonfw.tools.ide.tool.mvn.Mvn;
import com.devonfw.tools.ide.tool.repository.CustomToolsJson;
Expand Down Expand Up @@ -55,16 +54,16 @@ private void updateLegacyFolders() {
}

private void updateLegacyFolder(Path folder, String legacyName, String newName) {

FileAccess fileAccess = this.context.getFileAccess();
Path legacyFolder = folder.resolve(legacyName);
Path newFolder = folder.resolve(newName);
if (Files.isDirectory(legacyFolder)) {
if (fileAccess.isExpectedFolder(legacyFolder)) {
try {
if (!Files.exists(newFolder)) {
Files.move(legacyFolder, newFolder, StandardCopyOption.REPLACE_EXISTING);
if (!fileAccess.exists(newFolder)) {
fileAccess.move(legacyFolder, newFolder, StandardCopyOption.REPLACE_EXISTING);
this.context.success("Successfully renamed folder '{}' to '{}' in {}.", legacyName, newName, folder);
}
} catch (IOException e) {
} catch (IllegalStateException e) {
this.context.error(e, "Error renaming folder {} to {} in {}", legacyName, newName, folder);
}
}
Expand All @@ -73,15 +72,16 @@ private void updateLegacyFolder(Path folder, String legacyName, String newName)
private void updateWorkspaceTemplates() {
this.context.info("Updating workspace templates (replace legacy variables and change variable syntax)...");

FileAccess fileAccess = this.context.getFileAccess();
DirectoryMerger merger = this.context.getWorkspaceMerger();
Path settingsDir = this.context.getSettingsPath();
Path workspaceDir = settingsDir.resolve(IdeContext.FOLDER_WORKSPACE);
if (Files.isDirectory(workspaceDir)) {
if (fileAccess.isExpectedFolder(workspaceDir)) {
merger.upgrade(workspaceDir);
}
this.context.getFileAccess().listChildrenMapped(settingsDir, child -> {
fileAccess.listChildrenMapped(settingsDir, child -> {
Path childWorkspaceDir = child.resolve(IdeContext.FOLDER_WORKSPACE);
if (Files.isDirectory(childWorkspaceDir)) {
if (fileAccess.isExpectedFolder(childWorkspaceDir)) {
merger.upgrade(childWorkspaceDir);
}
return null;
Expand All @@ -106,8 +106,9 @@ private void updateProperties() {
}
environmentVariables = environmentVariables.getParent();
}
FileAccess fileAccess = this.context.getFileAccess();
Path templatePropertiesDir = this.context.getSettingsTemplatePath().resolve(IdeContext.FOLDER_CONF);
if (Files.exists(templatePropertiesDir)) {
if (fileAccess.exists(templatePropertiesDir)) {
EnvironmentVariablesPropertiesFile environmentVariablesProperties = new EnvironmentVariablesPropertiesFile(null, EnvironmentVariablesType.CONF,
templatePropertiesDir, null, this.context);
updateProperties(environmentVariablesProperties);
Expand Down
10 changes: 9 additions & 1 deletion cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Reader;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.PosixFilePermission;
import java.time.Duration;
import java.util.List;
Expand Down Expand Up @@ -43,6 +44,12 @@ public interface FileAccess {
*/
boolean isFile(Path file);

/**
* @param file the {@link Path} to check.
* @return {@code true} if the given {@code file} points to an existing file, {@code false} otherwise (the given {@link Path} does not exist
*/
boolean exists(Path file);

/**
* @param folder the {@link Path} to check.
* @return {@code true} if the given {@code folder} points to an existing directory, {@code false} otherwise (a warning is logged in this case).
Expand All @@ -67,8 +74,9 @@ public interface FileAccess {
/**
* @param source the source {@link Path file or folder} to move.
* @param targetDir the {@link Path} with the directory to move {@code source} into.
* @param copyOptions the {@link java.nio.file.CopyOption} which specify how the move should be done
*/
void move(Path source, Path targetDir);
void move(Path source, Path targetDir, StandardCopyOption... copyOptions);

/**
* Creates a symbolic link. If the given {@code targetLink} already exists and is a symbolic link or a Windows junction, it will be replaced. In case of
Expand Down
10 changes: 8 additions & 2 deletions cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFilePermission;
Expand Down Expand Up @@ -219,6 +220,11 @@ public boolean isFile(Path file) {
return true;
}

@Override
public boolean exists(Path file) {
return Files.exists(file);
}

@Override
public boolean isExpectedFolder(Path folder) {

Expand Down Expand Up @@ -300,11 +306,11 @@ private static Path appendParentPath(Path path, Path parent, int max) {
}

@Override
public void move(Path source, Path targetDir) {
public void move(Path source, Path targetDir, StandardCopyOption... copyOptions) {

this.context.trace("Moving {} to {}", source, targetDir);
try {
Files.move(source, targetDir);
Files.move(source, targetDir, copyOptions);
} catch (IOException e) {
String fileType = Files.isSymbolicLink(source) ? "symlink" : isJunction(source) ? "junction" : Files.isDirectory(source) ? "directory" : "file";
String message = "Failed to move " + fileType + ": " + source + " to " + targetDir + ".";
Expand Down