Skip to content

Commit

Permalink
Do not throw IOException from OutputArtifactDestinationAndLayout
Browse files Browse the repository at this point in the history
This is to make them compatible with `map` and similar functions.

PiperOrigin-RevId: 578490892
  • Loading branch information
Googler authored and copybara-github committed Nov 1, 2023
1 parent 32783ff commit dc59ab9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,7 @@ private ImmutableMap<Path, Path> prepareFinalLayouts(
Map<OutputArtifactDestinationAndLayout, Path> destinationToArtifact) {
ImmutableMap.Builder<Path, Path> result = ImmutableMap.builder();
for (OutputArtifactDestinationAndLayout destination : destinationToArtifact.keySet()) {
try {
result.put(destination.prepareFinalLayout(), destinationToArtifact.get(destination));
} catch (IOException e) {
throw new UncheckedIOException(
"Failed to prepare " + destinationToArtifact.get(destination), e);
}
result.put(destination.prepareFinalLayout(), destinationToArtifact.get(destination));
}
return result.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public interface OutputArtifactDestinationAndLayout extends OutputArtifactDestin
* <p>Note, that this might be an no-op and in this case the method should simply return {@link
* #getCopyDestination()}.
*/
Path prepareFinalLayout() throws IOException;
Path prepareFinalLayout();
}

private final CacheLayout cacheLayout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.idea.blaze.base.qsync.cache.FileCache.OutputArtifactDestinationAndLayout;
import com.google.idea.blaze.qsync.PackageStatementParser;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -47,14 +48,25 @@ public static JavaSourceOutputArtifactDestination create(
}

@Override
public Path prepareFinalLayout() throws IOException {
String pkg = packageStatementParser.readPackage(getCopyDestination());
Path finalDest =
getDestinationJavaSourceDir()
.resolve(Path.of(pkg.replace('.', '/')))
.resolve(getOriginalFilename());
Files.createDirectories(finalDest.getParent());
Files.copy(getCopyDestination(), finalDest, StandardCopyOption.REPLACE_EXISTING);
return finalDest;
public Path prepareFinalLayout() {
Path finalDest;
try {
String pkg = packageStatementParser.readPackage(getCopyDestination());
finalDest =
getDestinationJavaSourceDir()
.resolve(Path.of(pkg.replace('.', '/')))
.resolve(getOriginalFilename());
} catch (IOException e) {
throw new UncheckedIOException(
"Failed to determine the final destination for " + getCopyDestination(), e);
}
try {
Files.createDirectories(finalDest.getParent());
Files.copy(getCopyDestination(), finalDest, StandardCopyOption.REPLACE_EXISTING);
return finalDest;
} catch (IOException e) {
throw new UncheckedIOException(
"Failed to copy " + getCopyDestination() + " to its final destination " + finalDest, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.google.idea.blaze.base.qsync.cache;

import com.google.idea.blaze.base.qsync.cache.FileCache.OutputArtifactDestinationAndLayout;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Objects;

Expand Down Expand Up @@ -51,7 +50,7 @@ public Path getCopyDestination() {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand All @@ -44,22 +45,26 @@ public ZippedOutputArtifactDestination(String key, Path finalDestination, Path c
this.copyDestination = copyDestination;
}

private static void extract(Path source, Path destination) throws IOException {
Files.createDirectories(destination);
try (ZipFile zip = new ZipFile(source.toFile())) {
for (var entries = zip.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
Files.createDirectories(destination.resolve(entry.getName()));
} else {
// Srcjars do not contain separate directory entries
Files.createDirectories(destination.resolve(entry.getName()).getParent());
Files.copy(
zip.getInputStream(entry),
destination.resolve(entry.getName()),
StandardCopyOption.REPLACE_EXISTING);
private static void extract(Path source, Path destination) {
try {
Files.createDirectories(destination);
try (ZipFile zip = new ZipFile(source.toFile())) {
for (var entries = zip.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
Files.createDirectories(destination.resolve(entry.getName()));
} else {
// Srcjars do not contain separate directory entries
Files.createDirectories(destination.resolve(entry.getName()).getParent());
Files.copy(
zip.getInputStream(entry),
destination.resolve(entry.getName()),
StandardCopyOption.REPLACE_EXISTING);
}
}
}
} catch (IOException e) {
throw new UncheckedIOException("Failed to extract " + source + " to " + destination, e);
}
}

Expand All @@ -79,7 +84,7 @@ public Path getCopyDestination() {
}

@Override
public Path prepareFinalLayout() throws IOException {
public Path prepareFinalLayout() {
if (Files.exists(finalDestination)) {
FileUtil.delete(finalDestination.toFile());
}
Expand Down

0 comments on commit dc59ab9

Please sign in to comment.