From 37372d0f3c19232ce0ecdd44ee6e60410cd70da3 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 10 Feb 2016 14:20:47 +0100 Subject: [PATCH] Added command to wipe the workspace. --- .../data/release/cli/ModelCommands.java | 2 +- .../data/release/io/Workspace.java | 37 +++++++++++++-- .../data/release/io/WorkspaceCommands.java | 46 +++++++++++++++++++ 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/springframework/data/release/io/WorkspaceCommands.java diff --git a/src/main/java/org/springframework/data/release/cli/ModelCommands.java b/src/main/java/org/springframework/data/release/cli/ModelCommands.java index ebad2ad..4618e8b 100644 --- a/src/main/java/org/springframework/data/release/cli/ModelCommands.java +++ b/src/main/java/org/springframework/data/release/cli/ModelCommands.java @@ -30,7 +30,7 @@ import org.springframework.shell.core.annotation.CliOption; @CliComponent public class ModelCommands implements CommandMarker { - @CliCommand("trains") + @CliCommand(value = "trains", help = "Displays all release trains or contents of them if a name is provided") public String train(@CliOption(key = { "", "train" }) Train train) { return train != null ? train.toString() diff --git a/src/main/java/org/springframework/data/release/io/Workspace.java b/src/main/java/org/springframework/data/release/io/Workspace.java index 3f4f292..d7fba84 100644 --- a/src/main/java/org/springframework/data/release/io/Workspace.java +++ b/src/main/java/org/springframework/data/release/io/Workspace.java @@ -22,7 +22,11 @@ import lombok.RequiredArgsConstructor; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; import java.util.Scanner; import java.util.stream.Stream; @@ -36,8 +40,6 @@ import org.springframework.data.release.model.Project; import org.springframework.stereotype.Component; import org.springframework.util.Assert; -import com.google.common.io.Files; - /** * Abstraction of the workspace that is used to work with the {@link Project}'s repositories, execute builds, etc. * @@ -61,6 +63,35 @@ public class Workspace { return ioProperties.getWorkDir(); } + /** + * Cleans up the working directory by removing all files and folders in it. + * + * @throws IOException + */ + public void cleanup() throws IOException { + + Path workingDirPath = getWorkingDirectory().toPath(); + + Files.walkFileTree(workingDirPath, new SimpleFileVisitor() { + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + + if (!workingDirPath.equals(dir)) { + Files.delete(dir); + } + + return FileVisitResult.CONTINUE; + } + }); + } + /** * Returns the directory for the given {@link Project}. * @@ -151,7 +182,7 @@ public class Workspace { private void writeContentToFile(String name, Project project, String content) throws IOException { File file = getFile(name, project); - Files.write(content, file, UTF_8); + com.google.common.io.Files.write(content, file, UTF_8); } /** diff --git a/src/main/java/org/springframework/data/release/io/WorkspaceCommands.java b/src/main/java/org/springframework/data/release/io/WorkspaceCommands.java new file mode 100644 index 0000000..12425fa --- /dev/null +++ b/src/main/java/org/springframework/data/release/io/WorkspaceCommands.java @@ -0,0 +1,46 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.release.io; + +import lombok.RequiredArgsConstructor; + +import java.io.IOException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.release.CliComponent; +import org.springframework.data.release.utils.Logger; +import org.springframework.shell.core.CommandMarker; +import org.springframework.shell.core.annotation.CliCommand; + +/** + * @author Oliver Gierke + */ +@CliComponent +@RequiredArgsConstructor(onConstructor = @__(@Autowired) ) +public class WorkspaceCommands implements CommandMarker { + + private final Workspace workspace; + private final Logger logger; + + @CliCommand("workspace cleanup") + public void cleanup() throws IOException { + + logger.log("Workspace", "Cleaning up workspace directory at %s.", + workspace.getWorkingDirectory().getAbsolutePath()); + + workspace.cleanup(); + } +}