From dc59ab91a9f8a0cca2bac2f9133be63262851255 Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 1 Nov 2023 05:53:57 -0700 Subject: [PATCH] Do not throw `IOException` from `OutputArtifactDestinationAndLayout` This is to make them compatible with `map` and similar functions. PiperOrigin-RevId: 578490892 --- .../base/qsync/cache/ArtifactTrackerImpl.java | 7 +--- .../blaze/base/qsync/cache/FileCache.java | 2 +- .../JavaSourceOutputArtifactDestination.java | 30 +++++++++++----- .../PreparedOutputArtifactDestination.java | 3 +- .../ZippedOutputArtifactDestination.java | 35 +++++++++++-------- 5 files changed, 44 insertions(+), 33 deletions(-) diff --git a/base/src/com/google/idea/blaze/base/qsync/cache/ArtifactTrackerImpl.java b/base/src/com/google/idea/blaze/base/qsync/cache/ArtifactTrackerImpl.java index 64a53e71576..87c15ecdde7 100644 --- a/base/src/com/google/idea/blaze/base/qsync/cache/ArtifactTrackerImpl.java +++ b/base/src/com/google/idea/blaze/base/qsync/cache/ArtifactTrackerImpl.java @@ -367,12 +367,7 @@ private ImmutableMap prepareFinalLayouts( Map destinationToArtifact) { ImmutableMap.Builder 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(); } diff --git a/base/src/com/google/idea/blaze/base/qsync/cache/FileCache.java b/base/src/com/google/idea/blaze/base/qsync/cache/FileCache.java index 3e657e07666..1070555d143 100644 --- a/base/src/com/google/idea/blaze/base/qsync/cache/FileCache.java +++ b/base/src/com/google/idea/blaze/base/qsync/cache/FileCache.java @@ -90,7 +90,7 @@ public interface OutputArtifactDestinationAndLayout extends OutputArtifactDestin *

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; diff --git a/base/src/com/google/idea/blaze/base/qsync/cache/JavaSourceOutputArtifactDestination.java b/base/src/com/google/idea/blaze/base/qsync/cache/JavaSourceOutputArtifactDestination.java index 5bccd166d08..e00948a3a19 100644 --- a/base/src/com/google/idea/blaze/base/qsync/cache/JavaSourceOutputArtifactDestination.java +++ b/base/src/com/google/idea/blaze/base/qsync/cache/JavaSourceOutputArtifactDestination.java @@ -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; @@ -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); + } } } diff --git a/base/src/com/google/idea/blaze/base/qsync/cache/PreparedOutputArtifactDestination.java b/base/src/com/google/idea/blaze/base/qsync/cache/PreparedOutputArtifactDestination.java index 0029e1c8272..7358930307f 100644 --- a/base/src/com/google/idea/blaze/base/qsync/cache/PreparedOutputArtifactDestination.java +++ b/base/src/com/google/idea/blaze/base/qsync/cache/PreparedOutputArtifactDestination.java @@ -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; @@ -51,7 +50,7 @@ public Path getCopyDestination() { } @Override - public Path prepareFinalLayout() throws IOException { + public Path prepareFinalLayout() { return finalDestination; } diff --git a/base/src/com/google/idea/blaze/base/qsync/cache/ZippedOutputArtifactDestination.java b/base/src/com/google/idea/blaze/base/qsync/cache/ZippedOutputArtifactDestination.java index 6d8423cfa42..e7989cc2777 100644 --- a/base/src/com/google/idea/blaze/base/qsync/cache/ZippedOutputArtifactDestination.java +++ b/base/src/com/google/idea/blaze/base/qsync/cache/ZippedOutputArtifactDestination.java @@ -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; @@ -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); } } @@ -79,7 +84,7 @@ public Path getCopyDestination() { } @Override - public Path prepareFinalLayout() throws IOException { + public Path prepareFinalLayout() { if (Files.exists(finalDestination)) { FileUtil.delete(finalDestination.toFile()); }