|
14 | 14 |
|
15 | 15 | package com.google.firebase.gradle.plugins.apiinfo;
|
16 | 16 |
|
| 17 | +import com.android.build.gradle.api.AndroidSourceSet; |
| 18 | +import java.io.File; |
| 19 | +import java.io.FileNotFoundException; |
| 20 | +import java.io.FileOutputStream; |
17 | 21 | import java.util.ArrayList;
|
| 22 | +import java.util.Arrays; |
18 | 23 | import java.util.List;
|
19 |
| -import java.util.stream.Collector; |
20 | 24 | import java.util.stream.Collectors;
|
21 |
| -import java.util.stream.Stream; |
22 | 25 | import org.gradle.api.DefaultTask;
|
23 | 26 | import org.gradle.api.GradleException;
|
24 | 27 | import org.gradle.api.tasks.Input;
|
25 |
| -import org.gradle.api.tasks.InputDirectory; |
26 | 28 | import org.gradle.api.tasks.InputFile;
|
27 |
| -import org.gradle.api.tasks.InputFiles; |
28 | 29 | import org.gradle.api.tasks.OutputFile;
|
29 | 30 | import org.gradle.api.tasks.TaskAction;
|
30 | 31 |
|
31 |
| -import java.io.File; |
32 |
| -import java.io.FileNotFoundException; |
33 |
| -import java.io.FileOutputStream; |
34 |
| -import java.util.Arrays; |
35 |
| - |
36 | 32 | /**
|
37 |
| - Task generates the api diff of the current source code against the api.txt file stored |
38 |
| - alongside the project's src directory. |
| 33 | + * Task generates the api diff of the current source code against the api.txt file stored alongside |
| 34 | + * the project's src directory. |
39 | 35 | */
|
40 | 36 | public abstract class ApiInformationTask extends DefaultTask {
|
41 | 37 |
|
42 |
| - @Input |
43 |
| - abstract String getMetalavaJarPath(); |
| 38 | + @Input |
| 39 | + abstract String getMetalavaJarPath(); |
44 | 40 |
|
45 |
| - @InputFile |
46 |
| - abstract File getApiTxt(); |
| 41 | + @InputFile |
| 42 | + abstract File getApiTxt(); |
47 | 43 |
|
48 |
| - @InputFiles |
49 |
| - abstract List<File> getSourcePath(); |
| 44 | + abstract AndroidSourceSet getSourceSet(); |
50 | 45 |
|
51 |
| - @OutputFile |
52 |
| - abstract File getBaselineFile(); |
| 46 | + @OutputFile |
| 47 | + abstract File getBaselineFile(); |
53 | 48 |
|
54 |
| - @OutputFile |
55 |
| - abstract File getOutputApiFile(); |
| 49 | + @OutputFile |
| 50 | + abstract File getOutputApiFile(); |
56 | 51 |
|
57 |
| - @Input |
58 |
| - abstract boolean getUpdateBaseline(); |
| 52 | + @Input |
| 53 | + abstract boolean getUpdateBaseline(); |
59 | 54 |
|
60 |
| - @OutputFile |
61 |
| - abstract File getOutputFile(); |
| 55 | + @OutputFile |
| 56 | + abstract File getOutputFile(); |
62 | 57 |
|
63 |
| - public abstract void setSourcePath(List<File> value); |
| 58 | + public abstract void setSourceSet(AndroidSourceSet value); |
64 | 59 |
|
65 |
| - public abstract void setBaselineFile(File value); |
| 60 | + public abstract void setBaselineFile(File value); |
66 | 61 |
|
67 |
| - public abstract void setUpdateBaseline(boolean value); |
| 62 | + public abstract void setUpdateBaseline(boolean value); |
68 | 63 |
|
69 |
| - public abstract void setMetalavaJarPath(String value); |
| 64 | + public abstract void setMetalavaJarPath(String value); |
70 | 65 |
|
71 |
| - public abstract void setApiTxt(File value); |
| 66 | + public abstract void setApiTxt(File value); |
72 | 67 |
|
73 |
| - public abstract void setOutputApiFile(File value); |
| 68 | + public abstract void setOutputApiFile(File value); |
74 | 69 |
|
75 |
| - public abstract void setOutputFile(File value); |
| 70 | + public abstract void setOutputFile(File value); |
76 | 71 |
|
| 72 | + @TaskAction |
| 73 | + void execute() { |
| 74 | + String sourcePath = |
| 75 | + getSourceSet().getJava().getSrcDirs().stream() |
| 76 | + .filter(File::exists) |
| 77 | + .map(File::getAbsolutePath) |
| 78 | + .collect(Collectors.joining(":")); |
| 79 | + File outputFileDir = getOutputFile().getParentFile(); |
| 80 | + if (!outputFileDir.exists()) { |
| 81 | + outputFileDir.mkdirs(); |
| 82 | + } |
77 | 83 |
|
78 |
| - @TaskAction |
79 |
| - void execute() { |
80 |
| - String sourcePath = getSourcePath().stream().map(File::getAbsolutePath).collect(Collectors.joining(":")); |
81 |
| - File outputFileDir = getOutputFile().getParentFile(); |
82 |
| - if(!outputFileDir.exists()) { |
83 |
| - outputFileDir.mkdirs(); |
84 |
| - } |
85 |
| - |
86 |
| - // Generate api.txt file and store it in the build directory. |
87 |
| - getProject().javaexec(spec-> { |
88 |
| - spec.setMain("-jar"); |
89 |
| - spec.setArgs(Arrays.asList( |
90 |
| - getMetalavaJarPath(), |
91 |
| - "--source-path", sourcePath, |
92 |
| - "--api", getOutputApiFile().getAbsolutePath(), |
93 |
| - "--format=v2" |
94 |
| - )); |
95 |
| - spec.setIgnoreExitValue(true); |
96 |
| - }); |
97 |
| - getProject().javaexec(spec-> { |
98 |
| - spec.setMain("-jar"); |
99 |
| - List<String> args = new ArrayList<>(Arrays.asList( |
100 |
| - getMetalavaJarPath(), |
101 |
| - "--source-files", getOutputApiFile().getAbsolutePath(), |
102 |
| - "--check-compatibility:api:current", getApiTxt().getAbsolutePath(), |
103 |
| - "--format=v2", |
104 |
| - "--no-color", |
105 |
| - "--delete-empty-baselines" |
106 |
| - )); |
107 |
| - if(getUpdateBaseline()) { |
108 |
| - args.addAll(Arrays.asList("--update-baseline", getBaselineFile().getAbsolutePath())); |
109 |
| - } else if(getBaselineFile().exists()) { |
| 84 | + // Generate api.txt file and store it in the build directory. |
| 85 | + getProject() |
| 86 | + .javaexec( |
| 87 | + spec -> { |
| 88 | + spec.setMain("-jar"); |
| 89 | + spec.setArgs( |
| 90 | + Arrays.asList( |
| 91 | + getMetalavaJarPath(), |
| 92 | + "--source-path", |
| 93 | + sourcePath, |
| 94 | + "--api", |
| 95 | + getOutputApiFile().getAbsolutePath(), |
| 96 | + "--format=v2")); |
| 97 | + spec.setIgnoreExitValue(true); |
| 98 | + }); |
| 99 | + getProject() |
| 100 | + .javaexec( |
| 101 | + spec -> { |
| 102 | + spec.setMain("-jar"); |
| 103 | + List<String> args = |
| 104 | + new ArrayList<>( |
| 105 | + Arrays.asList( |
| 106 | + getMetalavaJarPath(), |
| 107 | + "--source-files", |
| 108 | + getOutputApiFile().getAbsolutePath(), |
| 109 | + "--check-compatibility:api:current", |
| 110 | + getApiTxt().getAbsolutePath(), |
| 111 | + "--format=v2", |
| 112 | + "--no-color", |
| 113 | + "--delete-empty-baselines")); |
| 114 | + if (getUpdateBaseline()) { |
| 115 | + args.addAll( |
| 116 | + Arrays.asList("--update-baseline", getBaselineFile().getAbsolutePath())); |
| 117 | + } else if (getBaselineFile().exists()) { |
110 | 118 | args.addAll(Arrays.asList("--baseline", getBaselineFile().getAbsolutePath()));
|
111 |
| - } |
112 |
| - spec.setArgs(args); |
113 |
| - spec.setIgnoreExitValue(true); |
114 |
| - try { |
| 119 | + } |
| 120 | + spec.setArgs(args); |
| 121 | + spec.setIgnoreExitValue(true); |
| 122 | + try { |
115 | 123 | spec.setStandardOutput(new FileOutputStream(getOutputFile()));
|
116 |
| - } catch (FileNotFoundException e) { |
| 124 | + } catch (FileNotFoundException e) { |
117 | 125 | throw new GradleException("Unable to run the command", e);
|
118 |
| - } |
119 |
| - }); |
120 |
| - |
121 |
| - } |
122 |
| - |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
123 | 129 | }
|
0 commit comments