Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Delete buck-out when changing versions.
Browse files Browse the repository at this point in the history
Summary:
The structure of the output directory can change between Buck
versions. Since none of the outputs from a different version of
Buck are going to be useful as they will always have different
rule keys, we can safely delete all directories in it when we
detect the version has changed.

Test Plan:
CI
change Buck version, see buck-out/bin and buck-out/gen are deleted
  • Loading branch information
Uri Baghin authored and Gray0Ed committed Oct 12, 2015
1 parent bade1dd commit 7e153d4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/com/facebook/buck/cli/Main.java
Expand Up @@ -45,6 +45,7 @@
import com.facebook.buck.log.CommandThreadAssociation;
import com.facebook.buck.log.LogConfig;
import com.facebook.buck.log.Logger;
import com.facebook.buck.model.BuckVersion;
import com.facebook.buck.model.BuildId;
import com.facebook.buck.parser.Parser;
import com.facebook.buck.parser.ParserConfig;
Expand Down Expand Up @@ -567,6 +568,15 @@ public int runMainWithExitCode(
if (!commandSemaphoreAcquired) {
return BUSY_EXIT_CODE;
}
Optional<String> currentVersion =
filesystem.readFileIfItExists(BuckConstant.CURRENT_VERSION_FILE);
if (!currentVersion.isPresent() || !currentVersion.get().equals(BuckVersion.getVersion())) {
filesystem.deleteRecursivelyIfExists(BuckConstant.ANNOTATION_PATH);
filesystem.deleteRecursivelyIfExists(BuckConstant.GEN_PATH);
filesystem.deleteRecursivelyIfExists(BuckConstant.SCRATCH_PATH);
filesystem.mkdirs(BuckConstant.CURRENT_VERSION_FILE.getParent());
filesystem.writeContentsToPath(BuckVersion.getVersion(), BuckConstant.CURRENT_VERSION_FILE);
}
}

PropertyFinder propertyFinder = new DefaultPropertyFinder(
Expand Down
5 changes: 5 additions & 0 deletions src/com/facebook/buck/util/BuckConstant.java
Expand Up @@ -30,6 +30,11 @@ public class BuckConstant {
*/
public static final String BUCK_OUTPUT_DIRECTORY = "buck-out";
public static final Path BUCK_OUTPUT_PATH = Paths.get("buck-out");
/**
* The version the buck output directory was created for
*/
public static final Path CURRENT_VERSION_FILE =
BUCK_OUTPUT_PATH.resolve(".currentversion");

// TODO(mbolin): The constants GEN_DIR, BIN_DIR, and ANNOTATION_DIR should be
// package-private to the com.facebook.buck.rules directory. Currently, they are also used in the
Expand Down
26 changes: 26 additions & 0 deletions test/com/facebook/buck/testutil/integration/ProjectWorkspace.java
Expand Up @@ -31,8 +31,10 @@
import com.facebook.buck.io.ExecutableFinder;
import com.facebook.buck.io.MoreFiles;
import com.facebook.buck.io.MorePaths;
import com.facebook.buck.model.BuckVersion;
import com.facebook.buck.model.BuildId;
import com.facebook.buck.testutil.TestConsole;
import com.facebook.buck.util.BuckConstant;
import com.facebook.buck.util.CapturingPrintStream;
import com.facebook.buck.util.MoreStrings;
import com.facebook.buck.util.ProcessExecutor;
Expand All @@ -43,21 +45,29 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.eventbus.Subscribe;
import com.martiansoftware.nailgun.NGContext;

import org.junit.rules.TemporaryFolder;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

Expand Down Expand Up @@ -135,10 +145,26 @@ public ProjectWorkspace(Path templateDir, TemporaryPaths temporaryFolder) {
* This will copy the template directory, renaming files named {@code foo.fixture} to {@code foo}
* in the process. Files whose names end in {@code .expected} will not be copied.
*/
@SuppressWarnings("PMD.EmptyCatchBlock")
public ProjectWorkspace setUp() throws IOException {

MoreFiles.copyRecursively(templatePath, destPath, BUILD_FILE_RENAME);

// Stamp the buck-out directory if it exists and isn't stamped already
try (OutputStream outputStream =
new BufferedOutputStream(
Channels.newOutputStream(
Files.newByteChannel(
destPath.resolve(BuckConstant.CURRENT_VERSION_FILE),
ImmutableSet.<OpenOption>of(
StandardOpenOption.CREATE_NEW,
StandardOpenOption.WRITE))))) {
outputStream.write(BuckVersion.getVersion().getBytes(Charsets.UTF_8));
} catch (FileAlreadyExistsException | NoSuchFileException e) {
// If the current version file already exists we don't need to create it
// If buck-out doesn't exist we don't need to stamp it
}

// If there's a local.properties in the host project but not in the destination, make a copy.
Path localProperties = FileSystems.getDefault().getPath("local.properties");
Path destLocalProperties = destPath.resolve(localProperties.getFileName());
Expand Down

0 comments on commit 7e153d4

Please sign in to comment.