Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 1 | // Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | package tasks; |
| 5 | |
| 6 | import java.io.BufferedReader; |
| 7 | import java.io.File; |
| 8 | import java.io.IOException; |
| 9 | import java.io.InputStreamReader; |
| 10 | import java.nio.charset.StandardCharsets; |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.Arrays; |
| 13 | import java.util.Collection; |
| 14 | import java.util.List; |
| 15 | import java.util.stream.Collectors; |
Jake Wharton | a138d5b | 2019-09-17 11:53:52 -0400 | [diff] [blame] | 16 | import javax.inject.Inject; |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 17 | import org.gradle.api.DefaultTask; |
| 18 | import org.gradle.api.tasks.InputFiles; |
| 19 | import org.gradle.api.tasks.OutputDirectory; |
| 20 | import org.gradle.api.tasks.TaskAction; |
| 21 | import org.gradle.internal.os.OperatingSystem; |
Jake Wharton | a138d5b | 2019-09-17 11:53:52 -0400 | [diff] [blame] | 22 | import org.gradle.workers.IsolationMode; |
| 23 | import org.gradle.workers.WorkerExecutor; |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 24 | |
| 25 | public class DownloadDependency extends DefaultTask { |
| 26 | |
| 27 | public enum Type { |
| 28 | GOOGLE_STORAGE, |
| 29 | X20 |
| 30 | } |
| 31 | |
Jake Wharton | a138d5b | 2019-09-17 11:53:52 -0400 | [diff] [blame] | 32 | private final WorkerExecutor workerExecutor; |
| 33 | |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 34 | private Type type; |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 35 | private File outputDir; |
| 36 | private File tarGzFile; |
| 37 | private File sha1File; |
| 38 | |
Jake Wharton | a138d5b | 2019-09-17 11:53:52 -0400 | [diff] [blame] | 39 | @Inject |
| 40 | public DownloadDependency(WorkerExecutor workerExecutor) { |
| 41 | this.workerExecutor = workerExecutor; |
| 42 | } |
| 43 | |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 44 | public void setType(Type type) { |
| 45 | this.type = type; |
| 46 | } |
| 47 | |
| 48 | public void setDependency(String dependency) { |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 49 | outputDir = new File(dependency); |
| 50 | tarGzFile = new File(dependency + ".tar.gz"); |
| 51 | sha1File = new File(dependency + ".tar.gz.sha1"); |
| 52 | } |
| 53 | |
| 54 | @InputFiles |
| 55 | public Collection<File> getInputFiles() { |
| 56 | return Arrays.asList(sha1File, tarGzFile); |
| 57 | } |
| 58 | |
| 59 | @OutputDirectory |
| 60 | public File getOutputDir() { |
| 61 | return outputDir; |
| 62 | } |
| 63 | |
| 64 | public File getSha1File() { |
| 65 | return sha1File; |
| 66 | } |
| 67 | |
| 68 | public File getTarGzFile() { |
| 69 | return tarGzFile; |
| 70 | } |
| 71 | |
| 72 | @TaskAction |
| 73 | public void execute() throws IOException, InterruptedException { |
| 74 | // First run will write the tar.gz file, causing the second run to still be out-of-date. |
| 75 | // Check if the modification time of the tar is newer than the sha in which case we are done. |
| 76 | // Also, check the contents of the out directory because gradle appears to create it for us... |
| 77 | if (outputDir.exists() |
| 78 | && outputDir.isDirectory() |
| 79 | && outputDir.list().length > 0 |
| 80 | && tarGzFile.exists() |
| 81 | && sha1File.lastModified() <= tarGzFile.lastModified()) { |
| 82 | return; |
| 83 | } |
| 84 | if (outputDir.exists() && outputDir.isDirectory()) { |
| 85 | outputDir.delete(); |
| 86 | } |
Jake Wharton | a138d5b | 2019-09-17 11:53:52 -0400 | [diff] [blame] | 87 | workerExecutor.submit(RunDownload.class, config -> { |
| 88 | config.setIsolationMode(IsolationMode.NONE); |
| 89 | config.params(type, sha1File); |
| 90 | }); |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Jake Wharton | a138d5b | 2019-09-17 11:53:52 -0400 | [diff] [blame] | 93 | public static class RunDownload implements Runnable { |
| 94 | private final Type type; |
| 95 | private final File sha1File; |
| 96 | |
| 97 | @Inject |
| 98 | public RunDownload(Type type, File sha1File) { |
| 99 | this.type = type; |
| 100 | this.sha1File = sha1File; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void run() { |
| 105 | try { |
| 106 | if (type == Type.GOOGLE_STORAGE) { |
| 107 | downloadFromGoogleStorage(); |
| 108 | } else if (type == Type.X20) { |
| 109 | downloadFromX20(); |
| 110 | } else { |
| 111 | throw new RuntimeException("Unexpected or missing dependency type: " + type); |
| 112 | } |
| 113 | } catch (Exception e) { |
| 114 | throw new RuntimeException(e); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private void downloadFromGoogleStorage() throws IOException, InterruptedException { |
| 119 | List<String> args = Arrays.asList("-n", "-b", "r8-deps", "-s", "-u", sha1File.toString()); |
| 120 | if (OperatingSystem.current().isWindows()) { |
| 121 | List<String> command = new ArrayList<>(); |
| 122 | command.add("download_from_google_storage.bat"); |
| 123 | command.addAll(args); |
| 124 | runProcess(new ProcessBuilder().command(command)); |
| 125 | } else { |
| 126 | runProcess( |
| 127 | new ProcessBuilder() |
| 128 | .command("bash", "-c", "download_from_google_storage " + String.join(" ", args))); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private void downloadFromX20() throws IOException, InterruptedException { |
| 133 | if (OperatingSystem.current().isWindows()) { |
| 134 | throw new RuntimeException("Downloading from x20 unsupported on windows"); |
| 135 | } |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 136 | runProcess( |
| 137 | new ProcessBuilder() |
Jake Wharton | a138d5b | 2019-09-17 11:53:52 -0400 | [diff] [blame] | 138 | .command("bash", "-c", "tools/download_from_x20.py " + sha1File.toString())); |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 139 | } |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 140 | |
Jake Wharton | a138d5b | 2019-09-17 11:53:52 -0400 | [diff] [blame] | 141 | private static void runProcess(ProcessBuilder builder) |
| 142 | throws IOException, InterruptedException { |
| 143 | String command = String.join(" ", builder.command()); |
| 144 | Process p = builder.start(); |
| 145 | int exit = p.waitFor(); |
| 146 | if (exit != 0) { |
| 147 | throw new IOException( |
| 148 | "Process failed for " |
| 149 | + command |
| 150 | + "\n" |
| 151 | + new BufferedReader( |
| 152 | new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8)) |
| 153 | .lines() |
| 154 | .collect(Collectors.joining("\n"))); |
| 155 | } |
Ian Zerny | b2d27c4 | 2019-02-20 09:09:41 +0100 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |