Skip to content

Commit 0662a9c

Browse files
authored
WIP Migrate build logic to Java (firebase#1516)
* Migrate publishing build logic to java. * Migrate build logic to java. * Add license to files. * Add projectsToRelease property to the root project.
1 parent 3335ea2 commit 0662a9c

23 files changed

+1036
-789
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ local.properties
77
google-services.json
88
_artifacts
99
.DS_Store
10+
firebase-crashlytics-ndk/.externalNativeBuild/

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ apply plugin: com.google.firebase.gradle.plugins.publish.PublishingPlugin
5656
apply plugin: com.google.firebase.gradle.plugins.ci.ContinuousIntegrationPlugin
5757
apply plugin: com.google.firebase.gradle.plugins.ci.SmokeTestsPlugin
5858
apply plugin: com.google.firebase.gradle.plugins.ci.metrics.MetricsPlugin
59-
apply plugin: com.google.firebase.gradle.plugins.ci.CheckCoveragePlugin
6059

6160
firebaseContinuousIntegration {
6261
ignorePaths = [

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.firebase.gradle.plugins.ci.device.FirebaseTestLabExtension;
1919
import java.util.Collections;
2020
import java.util.HashSet;
21+
import java.util.Optional;
2122
import java.util.Set;
2223
import java.util.stream.Collectors;
2324
import javax.inject.Inject;
@@ -30,7 +31,9 @@
3031
import org.gradle.api.publish.maven.MavenPom;
3132

3233
public class FirebaseLibraryExtension {
33-
private final Project project;
34+
35+
public final Project project;
36+
public final LibraryType type;
3437
private final Set<FirebaseLibraryExtension> librariesToCoRelease = new HashSet<>();
3538

3639
/** Indicates whether the library has public javadoc. */
@@ -66,8 +69,9 @@ public class FirebaseLibraryExtension {
6669
};
6770

6871
@Inject
69-
public FirebaseLibraryExtension(Project project) {
72+
public FirebaseLibraryExtension(Project project, LibraryType type) {
7073
this.project = project;
74+
this.type = type;
7175
this.testLab = new FirebaseTestLabExtension(project.getObjects());
7276
this.artifactId = project.getObjects().property(String.class);
7377
this.groupId = project.getObjects().property(String.class);
@@ -134,6 +138,13 @@ public Set<Project> getProjectsToRelease() {
134138
.build();
135139
}
136140

141+
public Set<FirebaseLibraryExtension> getLibrariesToRelease() {
142+
return ImmutableSet.<FirebaseLibraryExtension>builder()
143+
.add(this)
144+
.addAll(librariesToCoRelease)
145+
.build();
146+
}
147+
137148
/** Provides a hook to customize pom generation. */
138149
public void customizePom(Action<MavenPom> action) {
139150
customizePomAction = action;
@@ -148,4 +159,31 @@ public void applyPomCustomization(MavenPom pom) {
148159
public void staticAnalysis(Action<FirebaseStaticAnalysis> action) {
149160
action.execute(staticAnalysis);
150161
}
162+
163+
public String getVersion() {
164+
return project.getVersion().toString();
165+
}
166+
167+
public Optional<String> getLatestReleasedVersion() {
168+
if (project.hasProperty("latestReleasedVersion")) {
169+
return Optional.of(
170+
project.getExtensions().getExtraProperties().get("latestReleasedVersion").toString());
171+
}
172+
return Optional.empty();
173+
}
174+
175+
public String getMavenName() {
176+
return groupId.get() + ":" + artifactId.get();
177+
}
178+
179+
public String getPath() {
180+
return project.getPath();
181+
}
182+
183+
@Override
184+
public String toString() {
185+
return String.format(
186+
"FirebaseLibraryExtension{name=\"%s:%s\", project=\"%s\", type=\"%s\"}",
187+
groupId.get(), artifactId.get(), getPath(), type);
188+
}
151189
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.firebase.gradle.plugins.apiinfo.GenerateApiTxtFileTask;
2525
import com.google.firebase.gradle.plugins.apiinfo.GenerateStubsTask;
2626
import com.google.firebase.gradle.plugins.apiinfo.GetMetalavaJarTask;
27+
import com.google.firebase.gradle.plugins.ci.Coverage;
2728
import com.google.firebase.gradle.plugins.ci.device.FirebaseTestServer;
2829
import java.io.File;
2930
import java.nio.file.Paths;
@@ -41,7 +42,10 @@ public void apply(Project project) {
4142
project.apply(ImmutableMap.of("plugin", "com.android.library"));
4243

4344
FirebaseLibraryExtension firebaseLibrary =
44-
project.getExtensions().create("firebaseLibrary", FirebaseLibraryExtension.class, project);
45+
project
46+
.getExtensions()
47+
.create(
48+
"firebaseLibrary", FirebaseLibraryExtension.class, project, LibraryType.ANDROID);
4549

4650
LibraryExtension android = project.getExtensions().getByType(LibraryExtension.class);
4751

@@ -85,7 +89,7 @@ public void apply(Project project) {
8589

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

88-
setupStaticAnalysis(project, android, firebaseLibrary);
92+
setupStaticAnalysis(project, firebaseLibrary);
8993

9094
// reduce the likelihood of kotlin module files colliding.
9195
project
@@ -200,8 +204,7 @@ private static void setupApiInformationAnalysis(Project project, LibraryExtensio
200204
});
201205
}
202206

203-
private static void setupStaticAnalysis(
204-
Project project, LibraryExtension android, FirebaseLibraryExtension library) {
207+
private static void setupStaticAnalysis(Project project, FirebaseLibraryExtension library) {
205208
project.afterEvaluate(
206209
p ->
207210
project
@@ -226,6 +229,7 @@ private static void setupStaticAnalysis(
226229
}));
227230

228231
project.getTasks().register("firebaseLint", task -> task.dependsOn("lint"));
232+
Coverage.apply(library);
229233
}
230234

231235
private static String kotlinModuleName(Project project) {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 Google LLC
1+
// Copyright 2020 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,12 +12,19 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package com.google.firebase.gradle.plugins.ci
15+
package com.google.firebase.gradle.plugins;
1616

17-
import java.util.regex.Pattern
17+
public enum LibraryType {
18+
ANDROID("aar"),
19+
JAVA("jar");
1820

19-
/** Contains plugin configuration properties. */
20-
class ContinuousIntegrationExtension {
21-
/** List of paths that the plugin should ignore when querying the Git commit. */
22-
List<Pattern> ignorePaths = []
23-
}
21+
private final String format;
22+
23+
LibraryType(String format) {
24+
this.format = format;
25+
}
26+
27+
public String getFormat() {
28+
return format;
29+
}
30+
}

buildSrc/src/main/groovy/com/google/firebase/gradle/plugins/ci/AffectedProjectFinder.groovy

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright 2020 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.ci;
16+
17+
import com.google.common.collect.ImmutableSet;
18+
19+
import com.google.common.io.CharStreams;
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.io.InputStreamReader;
23+
import java.util.HashSet;
24+
import java.util.Iterator;
25+
import java.util.List;
26+
import java.util.Set;
27+
import java.util.regex.Pattern;
28+
import java.util.stream.Collectors;
29+
import java.util.stream.Stream;
30+
import org.gradle.api.GradleException;
31+
import org.gradle.api.Project;
32+
33+
/** Determines a set of subprojects that own the 'changedPaths'. */
34+
public class AffectedProjectFinder {
35+
36+
private final Project project;
37+
private final Set<String> changedPaths;
38+
39+
public AffectedProjectFinder(Project project, List<Pattern> ignorePaths) {
40+
this(project, changedPaths(project.getRootDir()), ignorePaths);
41+
}
42+
43+
private AffectedProjectFinder(
44+
Project project, Set<String> changedPaths, List<Pattern> ignorePaths) {
45+
this.project = project;
46+
this.changedPaths =
47+
changedPaths.stream()
48+
.filter(
49+
p -> {
50+
for (Pattern path : ignorePaths) {
51+
if (path.matcher(p).matches()) {
52+
return false;
53+
}
54+
}
55+
return true;
56+
})
57+
.collect(Collectors.toSet());
58+
}
59+
60+
Set<Project> find() {
61+
Set<String> paths = new HashSet<>(changedPaths);
62+
Set<Project> projects = changedSubProjects(project, paths);
63+
64+
if (!containsRootProject(projects)) {
65+
return projects;
66+
}
67+
return project.getSubprojects();
68+
}
69+
70+
private static Set<String> changedPaths(File workDir) {
71+
try {
72+
Process process =
73+
Runtime.getRuntime().exec("git diff --name-only --submodule=diff", null, workDir);
74+
process.waitFor();
75+
return ImmutableSet.copyOf(
76+
CharStreams.readLines(new InputStreamReader(process.getInputStream())));
77+
} catch (IOException e) {
78+
throw new GradleException("Could not determine changed files", e);
79+
} catch (InterruptedException e) {
80+
Thread.currentThread().interrupt();
81+
throw new RuntimeException(e);
82+
}
83+
}
84+
85+
/**
86+
* Performs a post-order project tree traversal and returns a set of projects that own the
87+
* 'changedPaths'.
88+
*/
89+
private static Set<Project> changedSubProjects(Project project, Set<String> changedPaths) {
90+
// project.subprojects include all descendants of a given project, we only want immediate
91+
// children.
92+
Stream<Project> immediateChildProjects =
93+
project.getSubprojects().stream().filter(p -> project.equals(p.getParent()));
94+
95+
Set<Project> projects =
96+
immediateChildProjects
97+
.flatMap(p -> changedSubProjects(p, changedPaths).stream())
98+
.collect(Collectors.toSet());
99+
String relativePath =
100+
project.getRootDir().toURI().relativize(project.getProjectDir().toURI()).toString();
101+
102+
Iterator<String> itr = changedPaths.iterator();
103+
while (itr.hasNext()) {
104+
String file = itr.next();
105+
if (file.startsWith(relativePath)) {
106+
System.out.println("Claiming file " + file + " for project " + project);
107+
itr.remove();
108+
projects.add(project);
109+
}
110+
}
111+
return projects;
112+
}
113+
114+
private static boolean containsRootProject(Set<Project> projects) {
115+
return projects.stream().anyMatch(p -> p.getRootProject().equals(p));
116+
}
117+
}

0 commit comments

Comments
 (0)