diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/UpgradeSettingsCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/UpgradeSettingsCommandlet.java index 0069b6a4b..1a5b366bb 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/UpgradeSettingsCommandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/UpgradeSettingsCommandlet.java @@ -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; @@ -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; @@ -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); } } @@ -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; @@ -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); diff --git a/cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java b/cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java index 0c4ec2b3d..94b8c305b 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java +++ b/cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java @@ -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; @@ -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). @@ -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 diff --git a/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java b/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java index 6c3e1e67c..16214d248 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java @@ -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; @@ -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) { @@ -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 + ".";