Skip to content

Download metalava jar before running the command #714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import com.google.common.collect.ImmutableMap;
import com.google.firebase.gradle.plugins.apiinfo.GenerateApiTxtFileTask;
import com.google.firebase.gradle.plugins.apiinfo.ApiInformationTask;
import com.google.firebase.gradle.plugins.apiinfo.GetMetalavaJarTask;
import com.google.firebase.gradle.plugins.ci.device.FirebaseTestServer;

import java.io.IOException;
import java.nio.file.Files;
import java.util.Collection;
import org.codehaus.groovy.util.ReleaseInfo;
import org.gradle.api.Plugin;
Expand Down Expand Up @@ -69,10 +72,8 @@ public void apply(Project project) {
}
});
}
if (System.getenv().containsKey("METALAVA_BINARY_PATH")) {
setupApiInfomrationAnalysis(project, android);
}

setupApiInformationAnalysis(project, android);

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

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

private static void setupApiInfomrationAnalysis(Project project, LibraryExtension android) {

String metalavaBinaryPath = System.getenv("METALAVA_BINARY_PATH");
private static void setupApiInformationAnalysis(Project project, LibraryExtension android) {
File metalavaOutputJarFile = new File(project.getRootProject().getBuildDir(), "metalava.jar");
AndroidSourceSet mainSourceSet = android.getSourceSets().getByName("main");
File outputFile = project.getRootProject().file(Paths.get(
project.getRootProject().getBuildDir().getPath(),
Expand All @@ -105,10 +105,12 @@ private static void setupApiInfomrationAnalysis(Project project, LibraryExtensio
if(mainSourceSet.getJava().getSrcDirs().stream().noneMatch(File::exists)) {
return;
}

project.getTasks().register("getMetalavaJar", GetMetalavaJarTask.class, task -> {
task.setOutputFile(metalavaOutputJarFile);
});
project.getTasks().register("apiInformation", ApiInformationTask.class, task -> {
task.setApiTxt(project.file("api.txt"));
task.setMetalavaBinaryPath(metalavaBinaryPath);
task.setMetalavaJarPath(metalavaOutputJarFile.getAbsolutePath());
task.setSourcePath(sourcePathArgument);
task.setOutputFile(outputFile);
task.setBaselineFile(project.file("baseline.txt"));
Expand All @@ -118,18 +120,20 @@ private static void setupApiInfomrationAnalysis(Project project, LibraryExtensio
} else {
task.setUpdateBaseline(false);
}
task.dependsOn("getMetalavaJar");
});

project.getTasks().register("generateApiTxtFile", GenerateApiTxtFileTask.class, task -> {
task.setApiTxt(project.file("api.txt"));
task.setMetalavaBinaryPath(metalavaBinaryPath);
task.setMetalavaJarPath(metalavaOutputJarFile.getAbsolutePath());
task.setSourcePath(sourcePathArgument);
task.setBaselineFile(project.file("baseline.txt"));
if (project.hasProperty("updateBaseline")) {
task.setUpdateBaseline(true);
} else {
task.setUpdateBaseline(false);
}
task.dependsOn("getMetalavaJar");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public abstract class ApiInformationTask extends DefaultTask {

@Input
abstract String getMetalavaBinaryPath();
abstract String getMetalavaJarPath();

@InputFile
abstract File getApiTxt();
Expand All @@ -64,7 +64,7 @@ public abstract class ApiInformationTask extends DefaultTask {

public abstract void setUpdateBaseline(boolean value);

public abstract void setMetalavaBinaryPath(String value);
public abstract void setMetalavaJarPath(String value);

public abstract void setApiTxt(File value);

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

@TaskAction
void execute() {

File outputFileDir = getOutputFile().getParentFile();
if(!outputFileDir.exists()) {
outputFileDir.mkdirs();
}

// Generate api.txt file and store it in the build directory.
getProject().exec(spec-> {
spec.setCommandLine(Arrays.asList(
getMetalavaBinaryPath(),
getProject().javaexec(spec-> {
spec.setMain("-jar");
spec.setArgs(Arrays.asList(
getMetalavaJarPath(),
"--source-path", getSourcePath(),
"--api", getOutputApiFile().getAbsolutePath(),
"--format=v2"
));
spec.setIgnoreExitValue(true);
});
getProject().exec(spec-> {
List<String> cmd = new ArrayList<>(Arrays.asList(
getMetalavaBinaryPath(),
getProject().javaexec(spec-> {
spec.setMain("-jar");
List<String> args = new ArrayList<>(Arrays.asList(
getMetalavaJarPath(),
"--source-files", getOutputApiFile().getAbsolutePath(),
"--check-compatibility:api:current", getApiTxt().getAbsolutePath(),
"--format=v2",
"--no-color",
"--delete-empty-baselines"
));
if(getUpdateBaseline()) {
cmd.addAll(Arrays.asList("--update-baseline", getBaselineFile().getAbsolutePath()));
args.addAll(Arrays.asList("--update-baseline", getBaselineFile().getAbsolutePath()));
} else if(getBaselineFile().exists()) {
cmd.addAll(Arrays.asList("--baseline", getBaselineFile().getAbsolutePath()));
args.addAll(Arrays.asList("--baseline", getBaselineFile().getAbsolutePath()));
}
spec.setCommandLine(cmd);
spec.setArgs(args);
spec.setIgnoreExitValue(true);
try {
spec.setStandardOutput(new FileOutputStream(getOutputFile()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public abstract class GenerateApiTxtFileTask extends DefaultTask {

@Input
abstract String getMetalavaBinaryPath();
abstract String getMetalavaJarPath();

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

public abstract void setUpdateBaseline(boolean value);

public abstract void setMetalavaBinaryPath(String value);
public abstract void setMetalavaJarPath(String value);

public abstract void setApiTxt(File value);

@TaskAction
void execute() {
List<String> cmd = new ArrayList<String>(Arrays.asList(
getMetalavaBinaryPath(),
List<String> args = new ArrayList<String>(Arrays.asList(
getMetalavaJarPath(),
"--source-path", getSourcePath(),
"--api", getApiTxt().getAbsolutePath(),
"--format=v2",
"--delete-empty-baselines"
));

if(getUpdateBaseline()) {
cmd.addAll(Arrays.asList("--update-baseline", getBaselineFile().getAbsolutePath()));
args.addAll(Arrays.asList("--update-baseline", getBaselineFile().getAbsolutePath()));
} else if(getBaselineFile().exists()) {
cmd.addAll(Arrays.asList("--baseline", getBaselineFile().getAbsolutePath()));
args.addAll(Arrays.asList("--baseline", getBaselineFile().getAbsolutePath()));
}

getProject().exec(spec -> {
spec.setCommandLine(cmd);
getProject().javaexec(spec -> {
spec.setMain("-jar");
spec.setArgs(args);
spec.setIgnoreExitValue(true);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.gradle.plugins.apiinfo;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

public abstract class GetMetalavaJarTask extends DefaultTask {


@OutputFile
abstract File getOutputFile();

public abstract void setOutputFile(File outputFile);

@TaskAction
void execute() {
if (getOutputFile().exists()) {
return;
}

try (InputStream stream = new URL("https://storage.googleapis.com/android-ci/metalava-full-1.3.0-SNAPSHOT.jar").openStream()){
Files.copy(stream, getOutputFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new GradleException("Unable to read the jar file from GCS", e);
}
}

}
2 changes: 1 addition & 1 deletion ci/fireci/fireci/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def api_information(auth_token, repo_name, issue_number):
comment_string += '\n\n'
if comment_string:
comment_string += ('Please update the api.txt files for the subprojects being affected by this change '
'with the files present in the artifacts directory. Also perform a major/minor bump accordingly.\n')
'by running ./gradlew ${subproject}:generateApiTxtFile. Also perform a major/minor bump accordingly.\n')
# Comment to github.
github_client = Github(auth_token)
repo = github_client.get_repo(repo_name)
Expand Down