Skip to content

Commit

Permalink
Convert subclasses of OutputArtifactDestinationAndLayout to `@AutoV…
Browse files Browse the repository at this point in the history
…alue`

This reduces the amount of code and gives us better `toString()` implementation, which can aid debugging.

PiperOrigin-RevId: 589775954
  • Loading branch information
Googler authored and copybara-github committed Dec 11, 2023
1 parent f486224 commit 9127446
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ArtifactPathCacheLayout implements CacheLayout {
@Override
public OutputArtifactDestinationAndLayout getOutputArtifactDestinationAndLayout(
OutputArtifactInfo outputArtifact) {
return new PreparedOutputArtifactDestination(
return PreparedOutputArtifactDestination.create(
CacheDirectoryManager.cacheKeyForArtifact(outputArtifact),
cacheDirectory.resolve(outputArtifact.getRelativePath()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public OutputArtifactDestinationAndLayout getOutputArtifactDestinationAndLayout(
String key = CacheDirectoryManager.cacheKeyForArtifact(outputArtifact);
final Path finalDestination = cacheDirectory.resolve(key);
if (shouldExtractFile(Path.of(outputArtifact.getRelativePath()))) {
return new ZippedOutputArtifactDestination(
return ZippedOutputArtifactDestination.create(
key, finalDestination, cacheDotDirectory.resolve(PACKED_FILES_DIR).resolve(key));
} else {
return new PreparedOutputArtifactDestination(key, finalDestination);
return PreparedOutputArtifactDestination.create(key, finalDestination);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public OutputArtifactDestinationAndLayout getOutputArtifactDestinationAndLayout(
return null;
}
String key = CacheDirectoryManager.cacheKeyForArtifact(outputArtifact);
return new PreparedOutputArtifactDestination(
return PreparedOutputArtifactDestination.create(
key, cacheDirectory.resolve(ROOT_DIRECTORY_NAME).resolve(artifactPath));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,23 @@
*/
package com.google.idea.blaze.base.qsync.cache;

import com.google.auto.value.AutoValue;
import com.google.idea.blaze.base.qsync.cache.FileCache.OutputArtifactDestinationAndLayout;
import java.nio.file.Path;
import java.util.Objects;

/** A record that describes the location of an output artifact in cache directories. */
public class PreparedOutputArtifactDestination implements OutputArtifactDestinationAndLayout {
@AutoValue
public abstract class PreparedOutputArtifactDestination
implements OutputArtifactDestinationAndLayout {

private final String key;
/**
* The location where in the cache directory the representation of the artifact for the IDE should
* be placed.
*/
public final Path finalDestination;

public PreparedOutputArtifactDestination(String key, Path finalDestination) {
this.key = key;
this.finalDestination = finalDestination;
public static PreparedOutputArtifactDestination create(String key, Path finalDestination) {
return new AutoValue_PreparedOutputArtifactDestination(key, finalDestination);
}

@Override
public String getKey() {
return key;
}
public abstract String getKey();

public abstract Path getFinalDestination();

/**
* The location where in the cache directory the artifact should be placed.
Expand All @@ -46,28 +40,11 @@ public String getKey() {
*/
@Override
public Path getCopyDestination() {
return finalDestination;
return getFinalDestination();
}

@Override
public Path prepareFinalLayout() {
return finalDestination;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof PreparedOutputArtifactDestination)) {
return false;
}
PreparedOutputArtifactDestination that = (PreparedOutputArtifactDestination) o;
return key.equals(that.key) && finalDestination.equals(that.finalDestination);
}

@Override
public int hashCode() {
return Objects.hash(key, finalDestination);
return getFinalDestination();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,25 @@
*/
package com.google.idea.blaze.base.qsync.cache;

import com.google.auto.value.AutoValue;
import com.google.idea.blaze.base.qsync.cache.FileCache.OutputArtifactDestinationAndLayout;
import com.intellij.openapi.util.io.FileUtil;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/** A record that describes the location of an output artifact in cache directories. */
public class ZippedOutputArtifactDestination implements OutputArtifactDestinationAndLayout {
@AutoValue
public abstract class ZippedOutputArtifactDestination
implements OutputArtifactDestinationAndLayout {

private final String key;

/**
* The location where in the cache directory the representation of the artifact for the IDE should
* be placed.
*/
public final Path finalDestination;

private final Path copyDestination;

public ZippedOutputArtifactDestination(String key, Path finalDestination, Path copyDestination) {
this.key = key;
this.finalDestination = finalDestination;
this.copyDestination = copyDestination;
public static ZippedOutputArtifactDestination create(
String key, Path finalDestination, Path copyDestination) {
return new AutoValue_ZippedOutputArtifactDestination(key, finalDestination, copyDestination);
}

private static void extract(Path source, Path destination) {
Expand All @@ -69,45 +60,29 @@ private static void extract(Path source, Path destination) {
}

@Override
public String getKey() {
return key;
}
public abstract String getKey();

/**
* The location where in the cache directory the representation of the artifact for the IDE should
* be placed.
*/
protected abstract Path getFinalDestination();

/**
* The location where in the cache directory the artifact file itself should be placed.
*
* <p>The final and copy destinations are the same if the artifact file needs not to be extracted.
*/
@Override
public Path getCopyDestination() {
return copyDestination;
}
public abstract Path getCopyDestination();

@Override
public Path prepareFinalLayout() {
Path finalDestination = getFinalDestination();
if (Files.exists(finalDestination)) {
FileUtil.delete(finalDestination.toFile());
}
extract(copyDestination, finalDestination);
extract(getCopyDestination(), finalDestination);
return finalDestination;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ZippedOutputArtifactDestination)) {
return false;
}
ZippedOutputArtifactDestination that = (ZippedOutputArtifactDestination) o;
return key.equals(that.key)
&& finalDestination.equals(that.finalDestination)
&& copyDestination.equals(that.copyDestination);
}

@Override
public int hashCode() {
return Objects.hash(key, finalDestination, copyDestination);
}
}

0 comments on commit 9127446

Please sign in to comment.