|
13 | 13 | import org.gradle.api.artifacts.transform.TransformOutputs;
|
14 | 14 | import org.gradle.api.artifacts.transform.TransformParameters;
|
15 | 15 | import org.gradle.api.file.FileSystemLocation;
|
| 16 | +import org.gradle.api.logging.Logger; |
16 | 17 | import org.gradle.api.logging.Logging;
|
17 | 18 | import org.gradle.api.provider.Property;
|
18 | 19 | import org.gradle.api.provider.Provider;
|
|
21 | 22 | import org.gradle.api.tasks.Optional;
|
22 | 23 | import org.gradle.api.tasks.PathSensitive;
|
23 | 24 | import org.gradle.api.tasks.PathSensitivity;
|
24 |
| -import org.gradle.internal.UncheckedException; |
25 | 25 |
|
26 | 26 | import java.io.File;
|
| 27 | +import java.io.FileInputStream; |
27 | 28 | import java.io.IOException;
|
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.UncheckedIOException; |
28 | 31 | import java.nio.file.Path;
|
29 | 32 | import java.nio.file.Paths;
|
| 33 | +import java.security.DigestInputStream; |
| 34 | +import java.security.MessageDigest; |
| 35 | +import java.security.NoSuchAlgorithmException; |
| 36 | +import java.util.HexFormat; |
30 | 37 | import java.util.List;
|
31 | 38 | import java.util.function.Function;
|
32 | 39 |
|
33 | 40 | public interface UnpackTransform extends TransformAction<UnpackTransform.Parameters> {
|
34 | 41 |
|
| 42 | + Logger LOGGER = Logging.getLogger(UnpackTransform.class); |
| 43 | + |
35 | 44 | interface Parameters extends TransformParameters {
|
36 | 45 | @Input
|
37 | 46 | @Optional
|
@@ -78,11 +87,16 @@ default void transform(TransformOutputs outputs) {
|
78 | 87 | }
|
79 | 88 |
|
80 | 89 | try {
|
81 |
| - Logging.getLogger(UnpackTransform.class) |
82 |
| - .info("Unpacking " + archiveFile.getName() + " using " + getClass().getSimpleName() + "."); |
| 90 | + LOGGER.info("Unpacking {} using {}.", archiveFile.getName(), getClass().getSimpleName()); |
83 | 91 | unpack(archiveFile, extractedDir, outputs, getParameters().getAsFiletreeOutput());
|
84 |
| - } catch (IOException e) { |
85 |
| - throw UncheckedException.throwAsUncheckedException(e); |
| 92 | + } catch (IOException e1) { |
| 93 | + String hash = "[unknown]"; |
| 94 | + try { |
| 95 | + hash = getSha1(archiveFile); |
| 96 | + } catch (Exception e2) { |
| 97 | + LOGGER.warn("Unable to calculate hash for file " + archiveFile.getPath(), e2); |
| 98 | + } |
| 99 | + throw new UncheckedIOException("Failed to unpack " + archiveFile.getName() + " with checksum " + hash, e1); |
86 | 100 | }
|
87 | 101 | }
|
88 | 102 |
|
@@ -137,4 +151,15 @@ static Path trimArchiveExtractPath(List<String> keepPatterns, String ignoredPatt
|
137 | 151 | // finally remove the top-level directories from the output path
|
138 | 152 | return entryName.subpath(index + 1, entryName.getNameCount());
|
139 | 153 | }
|
| 154 | + |
| 155 | + static String getSha1(File file) throws IOException, NoSuchAlgorithmException { |
| 156 | + MessageDigest digest = MessageDigest.getInstance("SHA-1"); |
| 157 | + try (InputStream is = new DigestInputStream(new FileInputStream(file), digest)) { |
| 158 | + byte[] buffer = new byte[4096]; |
| 159 | + while (is.read(buffer) > 0) { |
| 160 | + // loop |
| 161 | + } |
| 162 | + } |
| 163 | + return HexFormat.of().formatHex(digest.digest()); |
| 164 | + } |
140 | 165 | }
|
0 commit comments