Ivan Gavrilovic | 635c7e5 | 2017-12-01 15:10:45 +0000 | [diff] [blame] | 1 | // Copyright (c) 2016, 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.File; |
| 7 | import java.io.FileInputStream; |
| 8 | import java.io.IOException; |
| 9 | import java.io.InputStream; |
| 10 | import java.nio.file.Files; |
| 11 | import java.nio.file.Path; |
| 12 | import java.util.ArrayList; |
| 13 | import java.util.List; |
| 14 | import java.util.Set; |
| 15 | import org.gradle.api.DefaultTask; |
| 16 | import org.gradle.api.artifacts.Configuration; |
| 17 | import org.gradle.api.file.FileTree; |
| 18 | import org.gradle.api.tasks.InputFiles; |
| 19 | import org.gradle.api.tasks.OutputDirectory; |
| 20 | import org.gradle.api.tasks.OutputFile; |
| 21 | import org.gradle.api.tasks.TaskAction; |
| 22 | |
| 23 | /** |
| 24 | * Extract all jars from the configuration. If an aar is in the configuration, classes.jar is |
| 25 | * extracted. Locations of the jars are written to a generated file. |
| 26 | */ |
| 27 | public class GetJarsFromConfiguration extends DefaultTask { |
| 28 | |
| 29 | private Configuration configuration; |
| 30 | |
| 31 | @InputFiles |
| 32 | public Configuration getInputFiles() { |
| 33 | return configuration; |
| 34 | } |
| 35 | |
| 36 | public void setConfiguration(Configuration configuration) { |
| 37 | this.configuration = configuration; |
| 38 | } |
| 39 | |
| 40 | @OutputDirectory |
| 41 | public File getOutputDir() { |
| 42 | return new File(getProject().getBuildDir(), "supportlibraries"); |
| 43 | } |
| 44 | |
| 45 | @OutputFile |
| 46 | public File getGeneratedFile() { |
| 47 | return new File(getProject().getBuildDir(), "generated/supportlibraries.txt"); |
| 48 | } |
| 49 | |
| 50 | @TaskAction |
| 51 | public void extract() throws IOException { |
| 52 | Files.createDirectories(getOutputDir().toPath()); |
| 53 | |
| 54 | Set<File> configurationFiles = configuration.getFiles(); |
| 55 | List<String> jarPaths = new ArrayList<>(configurationFiles.size()); |
| 56 | for (File file : configurationFiles) { |
| 57 | jarPaths.add(getSingleJar(file)); |
| 58 | } |
| 59 | |
| 60 | Path generatedPath = getGeneratedFile().toPath(); |
| 61 | Files.deleteIfExists(generatedPath); |
| 62 | Files.createDirectories(generatedPath.getParent()); |
| 63 | Files.write(generatedPath, jarPaths); |
| 64 | } |
| 65 | |
| 66 | private String getSingleJar(File jarOrAar) throws IOException { |
| 67 | if (jarOrAar.getName().endsWith(".aar")) { |
| 68 | FileTree aarEntries = getProject().zipTree(jarOrAar); |
| 69 | |
| 70 | for (File aarEntry : aarEntries) { |
| 71 | if (aarEntry.getName().equals("classes.jar")) { |
| 72 | try (InputStream is = new FileInputStream(aarEntry)) { |
| 73 | String jarName = jarOrAar.getName().replaceAll("\\.aar$", ".jar"); |
| 74 | Path extractedPath = getOutputDir().toPath().resolve(jarName); |
| 75 | Files.deleteIfExists(extractedPath); |
| 76 | Files.copy(is, extractedPath); |
| 77 | |
| 78 | return extractedPath.toString(); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | throw new RuntimeException("Aar does not contain classes.jar: " + jarOrAar.toString()); |
| 83 | } else { |
| 84 | return jarOrAar.toString(); |
| 85 | } |
| 86 | } |
| 87 | } |