blob: 61fb95fd46c608a307f66d86f7525df719200cef [file] [log] [blame]
Ian Zernyb2d27c42019-02-20 09:09:41 +01001// 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.
4package tasks;
5
6import java.io.BufferedReader;
7import java.io.File;
8import java.io.IOException;
9import java.io.InputStreamReader;
10import java.nio.charset.StandardCharsets;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.List;
15import java.util.stream.Collectors;
16import org.gradle.api.DefaultTask;
17import org.gradle.api.tasks.InputFiles;
18import org.gradle.api.tasks.OutputDirectory;
19import org.gradle.api.tasks.TaskAction;
20import org.gradle.internal.os.OperatingSystem;
21
22public class DownloadDependency extends DefaultTask {
23
24 public enum Type {
25 GOOGLE_STORAGE,
26 X20
27 }
28
29 private Type type;
30 private String dependency;
31 private File outputDir;
32 private File tarGzFile;
33 private File sha1File;
34
35 public void setType(Type type) {
36 this.type = type;
37 }
38
39 public void setDependency(String dependency) {
40 this.dependency = dependency;
41 outputDir = new File(dependency);
42 tarGzFile = new File(dependency + ".tar.gz");
43 sha1File = new File(dependency + ".tar.gz.sha1");
44 }
45
46 @InputFiles
47 public Collection<File> getInputFiles() {
48 return Arrays.asList(sha1File, tarGzFile);
49 }
50
51 @OutputDirectory
52 public File getOutputDir() {
53 return outputDir;
54 }
55
56 public File getSha1File() {
57 return sha1File;
58 }
59
60 public File getTarGzFile() {
61 return tarGzFile;
62 }
63
64 @TaskAction
65 public void execute() throws IOException, InterruptedException {
66 // First run will write the tar.gz file, causing the second run to still be out-of-date.
67 // Check if the modification time of the tar is newer than the sha in which case we are done.
68 // Also, check the contents of the out directory because gradle appears to create it for us...
69 if (outputDir.exists()
70 && outputDir.isDirectory()
71 && outputDir.list().length > 0
72 && tarGzFile.exists()
73 && sha1File.lastModified() <= tarGzFile.lastModified()) {
74 return;
75 }
76 if (outputDir.exists() && outputDir.isDirectory()) {
77 outputDir.delete();
78 }
79 if (type == Type.GOOGLE_STORAGE) {
80 downloadFromGoogleStorage();
81 } else if (type == Type.X20) {
82 downloadFromX20();
83 } else {
84 throw new RuntimeException("Unexpected or missing dependency type: " + type);
85 }
86 }
87
88 private void downloadFromGoogleStorage() throws IOException, InterruptedException {
89 List<String> args = Arrays.asList("-n", "-b", "r8-deps", "-s", "-u", sha1File.toString());
90 if (OperatingSystem.current().isWindows()) {
91 List<String> command = new ArrayList<>();
92 command.add("download_from_google_storage.bat");
93 command.addAll(args);
94 runProcess(new ProcessBuilder().command(command));
95 } else {
96 runProcess(
97 new ProcessBuilder()
98 .command("bash", "-c", "download_from_google_storage " + String.join(" ", args)));
99 }
100 }
101
102 private void downloadFromX20() throws IOException, InterruptedException {
103 if (OperatingSystem.current().isWindows()) {
104 throw new RuntimeException("Downloading from x20 unsupported on windows");
105 }
106 runProcess(
107 new ProcessBuilder()
108 .command("bash", "-c", "tools/download_from_x20.py " + sha1File.toString()));
109 }
110
111 private static void runProcess(ProcessBuilder builder) throws IOException, InterruptedException {
112 String command = String.join(" ", builder.command());
113 Process p = builder.start();
114 int exit = p.waitFor();
115 if (exit != 0) {
116 throw new IOException(
117 "Process failed for "
118 + command
119 + "\n"
120 + new BufferedReader(
121 new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8))
122 .lines()
123 .collect(Collectors.joining("\n")));
124 }
125 }
126}