Skip to content

Commit a068380

Browse files
authored
Download metalava jar before running the command (#714)
* update ci * fix issues * api information task * fix class path
1 parent 24ebb95 commit a068380

File tree

5 files changed

+92
-29
lines changed

5 files changed

+92
-29
lines changed

buildSrc/src/main/groovy/com/google/firebase/gradle/plugins/FirebaseLibraryPlugin.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import com.google.common.collect.ImmutableMap;
2222
import com.google.firebase.gradle.plugins.apiinfo.GenerateApiTxtFileTask;
2323
import com.google.firebase.gradle.plugins.apiinfo.ApiInformationTask;
24+
import com.google.firebase.gradle.plugins.apiinfo.GetMetalavaJarTask;
2425
import com.google.firebase.gradle.plugins.ci.device.FirebaseTestServer;
2526

27+
import java.io.IOException;
28+
import java.nio.file.Files;
2629
import java.util.Collection;
2730
import org.codehaus.groovy.util.ReleaseInfo;
2831
import org.gradle.api.Plugin;
@@ -69,10 +72,8 @@ public void apply(Project project) {
6972
}
7073
});
7174
}
72-
if (System.getenv().containsKey("METALAVA_BINARY_PATH")) {
73-
setupApiInfomrationAnalysis(project, android);
74-
}
7575

76+
setupApiInformationAnalysis(project, android);
7677

7778
android.testServer(new FirebaseTestServer(project, firebaseLibrary.testLab));
7879

@@ -90,9 +91,8 @@ public void apply(Project project) {
9091
ImmutableList.of("-module-name", kotlinModuleName(project))));
9192
}
9293

93-
private static void setupApiInfomrationAnalysis(Project project, LibraryExtension android) {
94-
95-
String metalavaBinaryPath = System.getenv("METALAVA_BINARY_PATH");
94+
private static void setupApiInformationAnalysis(Project project, LibraryExtension android) {
95+
File metalavaOutputJarFile = new File(project.getRootProject().getBuildDir(), "metalava.jar");
9696
AndroidSourceSet mainSourceSet = android.getSourceSets().getByName("main");
9797
File outputFile = project.getRootProject().file(Paths.get(
9898
project.getRootProject().getBuildDir().getPath(),
@@ -105,10 +105,12 @@ private static void setupApiInfomrationAnalysis(Project project, LibraryExtensio
105105
if(mainSourceSet.getJava().getSrcDirs().stream().noneMatch(File::exists)) {
106106
return;
107107
}
108-
108+
project.getTasks().register("getMetalavaJar", GetMetalavaJarTask.class, task -> {
109+
task.setOutputFile(metalavaOutputJarFile);
110+
});
109111
project.getTasks().register("apiInformation", ApiInformationTask.class, task -> {
110112
task.setApiTxt(project.file("api.txt"));
111-
task.setMetalavaBinaryPath(metalavaBinaryPath);
113+
task.setMetalavaJarPath(metalavaOutputJarFile.getAbsolutePath());
112114
task.setSourcePath(sourcePathArgument);
113115
task.setOutputFile(outputFile);
114116
task.setBaselineFile(project.file("baseline.txt"));
@@ -118,18 +120,20 @@ private static void setupApiInfomrationAnalysis(Project project, LibraryExtensio
118120
} else {
119121
task.setUpdateBaseline(false);
120122
}
123+
task.dependsOn("getMetalavaJar");
121124
});
122125

123126
project.getTasks().register("generateApiTxtFile", GenerateApiTxtFileTask.class, task -> {
124127
task.setApiTxt(project.file("api.txt"));
125-
task.setMetalavaBinaryPath(metalavaBinaryPath);
128+
task.setMetalavaJarPath(metalavaOutputJarFile.getAbsolutePath());
126129
task.setSourcePath(sourcePathArgument);
127130
task.setBaselineFile(project.file("baseline.txt"));
128131
if (project.hasProperty("updateBaseline")) {
129132
task.setUpdateBaseline(true);
130133
} else {
131134
task.setUpdateBaseline(false);
132135
}
136+
task.dependsOn("getMetalavaJar");
133137
});
134138
}
135139

buildSrc/src/main/groovy/com/google/firebase/gradle/plugins/apiinfo/ApiInformationTask.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
public abstract class ApiInformationTask extends DefaultTask {
3939

4040
@Input
41-
abstract String getMetalavaBinaryPath();
41+
abstract String getMetalavaJarPath();
4242

4343
@InputFile
4444
abstract File getApiTxt();
@@ -64,7 +64,7 @@ public abstract class ApiInformationTask extends DefaultTask {
6464

6565
public abstract void setUpdateBaseline(boolean value);
6666

67-
public abstract void setMetalavaBinaryPath(String value);
67+
public abstract void setMetalavaJarPath(String value);
6868

6969
public abstract void setApiTxt(File value);
7070

@@ -75,36 +75,39 @@ public abstract class ApiInformationTask extends DefaultTask {
7575

7676
@TaskAction
7777
void execute() {
78+
7879
File outputFileDir = getOutputFile().getParentFile();
7980
if(!outputFileDir.exists()) {
8081
outputFileDir.mkdirs();
8182
}
8283

8384
// Generate api.txt file and store it in the build directory.
84-
getProject().exec(spec-> {
85-
spec.setCommandLine(Arrays.asList(
86-
getMetalavaBinaryPath(),
85+
getProject().javaexec(spec-> {
86+
spec.setMain("-jar");
87+
spec.setArgs(Arrays.asList(
88+
getMetalavaJarPath(),
8789
"--source-path", getSourcePath(),
8890
"--api", getOutputApiFile().getAbsolutePath(),
8991
"--format=v2"
9092
));
9193
spec.setIgnoreExitValue(true);
9294
});
93-
getProject().exec(spec-> {
94-
List<String> cmd = new ArrayList<>(Arrays.asList(
95-
getMetalavaBinaryPath(),
95+
getProject().javaexec(spec-> {
96+
spec.setMain("-jar");
97+
List<String> args = new ArrayList<>(Arrays.asList(
98+
getMetalavaJarPath(),
9699
"--source-files", getOutputApiFile().getAbsolutePath(),
97100
"--check-compatibility:api:current", getApiTxt().getAbsolutePath(),
98101
"--format=v2",
99102
"--no-color",
100103
"--delete-empty-baselines"
101104
));
102105
if(getUpdateBaseline()) {
103-
cmd.addAll(Arrays.asList("--update-baseline", getBaselineFile().getAbsolutePath()));
106+
args.addAll(Arrays.asList("--update-baseline", getBaselineFile().getAbsolutePath()));
104107
} else if(getBaselineFile().exists()) {
105-
cmd.addAll(Arrays.asList("--baseline", getBaselineFile().getAbsolutePath()));
108+
args.addAll(Arrays.asList("--baseline", getBaselineFile().getAbsolutePath()));
106109
}
107-
spec.setCommandLine(cmd);
110+
spec.setArgs(args);
108111
spec.setIgnoreExitValue(true);
109112
try {
110113
spec.setStandardOutput(new FileOutputStream(getOutputFile()));

buildSrc/src/main/groovy/com/google/firebase/gradle/plugins/apiinfo/GenerateApiTxtFileTask.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
public abstract class GenerateApiTxtFileTask extends DefaultTask {
3030

3131
@Input
32-
abstract String getMetalavaBinaryPath();
32+
abstract String getMetalavaJarPath();
3333

3434
@OutputFile
3535
abstract File getApiTxt();
@@ -51,28 +51,29 @@ public abstract class GenerateApiTxtFileTask extends DefaultTask {
5151

5252
public abstract void setUpdateBaseline(boolean value);
5353

54-
public abstract void setMetalavaBinaryPath(String value);
54+
public abstract void setMetalavaJarPath(String value);
5555

5656
public abstract void setApiTxt(File value);
5757

5858
@TaskAction
5959
void execute() {
60-
List<String> cmd = new ArrayList<String>(Arrays.asList(
61-
getMetalavaBinaryPath(),
60+
List<String> args = new ArrayList<String>(Arrays.asList(
61+
getMetalavaJarPath(),
6262
"--source-path", getSourcePath(),
6363
"--api", getApiTxt().getAbsolutePath(),
6464
"--format=v2",
6565
"--delete-empty-baselines"
6666
));
6767

6868
if(getUpdateBaseline()) {
69-
cmd.addAll(Arrays.asList("--update-baseline", getBaselineFile().getAbsolutePath()));
69+
args.addAll(Arrays.asList("--update-baseline", getBaselineFile().getAbsolutePath()));
7070
} else if(getBaselineFile().exists()) {
71-
cmd.addAll(Arrays.asList("--baseline", getBaselineFile().getAbsolutePath()));
71+
args.addAll(Arrays.asList("--baseline", getBaselineFile().getAbsolutePath()));
7272
}
7373

74-
getProject().exec(spec -> {
75-
spec.setCommandLine(cmd);
74+
getProject().javaexec(spec -> {
75+
spec.setMain("-jar");
76+
spec.setArgs(args);
7677
spec.setIgnoreExitValue(true);
7778
});
7879

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.gradle.plugins.apiinfo;
16+
17+
import java.io.BufferedInputStream;
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.net.JarURLConnection;
22+
import java.net.MalformedURLException;
23+
import java.net.URL;
24+
import java.nio.file.FileSystems;
25+
import java.nio.file.Files;
26+
import java.nio.file.StandardCopyOption;
27+
import org.gradle.api.DefaultTask;
28+
import org.gradle.api.GradleException;
29+
import org.gradle.api.invocation.Gradle;
30+
import org.gradle.api.tasks.Input;
31+
import org.gradle.api.tasks.OutputFile;
32+
import org.gradle.api.tasks.TaskAction;
33+
34+
public abstract class GetMetalavaJarTask extends DefaultTask {
35+
36+
37+
@OutputFile
38+
abstract File getOutputFile();
39+
40+
public abstract void setOutputFile(File outputFile);
41+
42+
@TaskAction
43+
void execute() {
44+
if (getOutputFile().exists()) {
45+
return;
46+
}
47+
48+
try (InputStream stream = new URL("https://storage.googleapis.com/android-ci/metalava-full-1.3.0-SNAPSHOT.jar").openStream()){
49+
Files.copy(stream, getOutputFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
50+
} catch (IOException e) {
51+
throw new GradleException("Unable to read the jar file from GCS", e);
52+
}
53+
}
54+
55+
}

ci/fireci/fireci/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def api_information(auth_token, repo_name, issue_number):
9292
comment_string += '\n\n'
9393
if comment_string:
9494
comment_string += ('Please update the api.txt files for the subprojects being affected by this change '
95-
'with the files present in the artifacts directory. Also perform a major/minor bump accordingly.\n')
95+
'by running ./gradlew ${subproject}:generateApiTxtFile. Also perform a major/minor bump accordingly.\n')
9696
# Comment to github.
9797
github_client = Github(auth_token)
9898
repo = github_client.get_repo(repo_name)

0 commit comments

Comments
 (0)