Added command to wipe the workspace.

This commit is contained in:
Oliver Gierke
2016-02-10 14:20:47 +01:00
parent 84d68f7125
commit 37372d0f3c
3 changed files with 81 additions and 4 deletions

View File

@@ -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()

View File

@@ -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<Path>() {
@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);
}
/**

View File

@@ -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();
}
}