Skip to content

Commit 8c65245

Browse files
committed
Output artifact list during local publishing.
This effort replaces #494.
1 parent 63232fc commit 8c65245

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
lines changed

buildSrc/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ dependencies {
3737
implementation 'org.jsoup:jsoup:1.11.2'
3838
implementation 'digital.wup:android-maven-publish:3.6.2'
3939
implementation 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.20'
40+
implementation 'org.json:json:20180813'
4041

4142
implementation 'io.opencensus:opencensus-api:0.18.0'
4243
implementation 'io.opencensus:opencensus-exporter-stats-stackdriver:0.18.0'
4344
runtime 'io.opencensus:opencensus-impl:0.18.0'
4445

4546
implementation 'com.android.tools.build:gradle:3.2.1'
4647
testImplementation 'junit:junit:4.12'
47-
testImplementation 'org.json:json:20180813'
4848
testImplementation('org.spockframework:spock-core:1.1-groovy-2.4') {
4949
exclude group: 'org.codehaus.groovy'
5050
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class AffectedProjectFinder {
2525
Set<String> changedPaths;
2626

2727
@Builder
28+
AffectedProjectFinder(Project project, List<Pattern> ignorePaths) {
29+
this(project, changedPaths(project.rootDir), ignorePaths)
30+
}
31+
2832
AffectedProjectFinder(Project project,
2933
Set<String> changedPaths,
3034
List<Pattern> ignorePaths) {
@@ -49,6 +53,13 @@ class AffectedProjectFinder {
4953
return project.subprojects
5054
}
5155

56+
private static Set<String> changedPaths(File workDir) {
57+
return 'git diff --name-only --submodule=diff HEAD@{0} HEAD@{1}'
58+
.execute([], workDir)
59+
.text
60+
.readLines()
61+
}
62+
5263
/**
5364
* Performs a post-order project tree traversal and returns a set of projects that own the
5465
* 'changedPaths'.

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class ContinuousIntegrationPlugin implements Plugin<Project> {
9797

9898
def affectedProjects = AffectedProjectFinder.builder()
9999
.project(project)
100-
.changedPaths(changedPaths(project.rootDir))
101100
.ignorePaths(extension.ignorePaths)
102101
.build()
103102
.find()
@@ -143,13 +142,6 @@ class ContinuousIntegrationPlugin implements Plugin<Project> {
143142
}
144143
}
145144

146-
private static Set<String> changedPaths(File workDir) {
147-
return 'git diff --name-only --submodule=diff HEAD@{0} HEAD@{1}'
148-
.execute([], workDir)
149-
.text
150-
.readLines()
151-
}
152-
153145
private static final ANDROID_PLUGINS = ["com.android.application", "com.android.library",
154146
"com.android.test"]
155147

buildSrc/src/main/groovy/com/google/firebase/gradle/plugins/publish/PublishingPlugin.groovy

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515
package com.google.firebase.gradle.plugins.publish
1616

17+
import com.google.firebase.gradle.plugins.ci.AffectedProjectFinder
1718
import com.google.firebase.gradle.plugins.FirebaseLibraryExtension
1819
import digital.wup.android_maven_publish.AndroidMavenPublishPlugin
1920
import org.gradle.api.Plugin
2021
import org.gradle.api.Project
22+
import org.gradle.api.artifacts.ProjectDependency
2123
import org.gradle.api.publish.maven.MavenPublication
2224
import org.gradle.api.tasks.bundling.Jar
2325
import org.gradle.api.tasks.bundling.Zip
26+
import org.json.JSONArray
27+
import org.json.JSONObject
2428

2529
/**
2630
* Enables releasing of the SDKs.
@@ -86,6 +90,12 @@ class PublishingPlugin implements Plugin<Project> {
8690
def firebasePublish = project.task('firebasePublish')
8791

8892
project.getGradle().projectsEvaluated {
93+
// These three variables are used for generating the JSON file that lists the artifacts
94+
// affected by the latest commit. This is generally useful only on CI builds.
95+
def changedProjects = getChangedProjects(project)
96+
def changedArtifacts = new HashSet<String>()
97+
def allArtifacts = new HashSet<String>()
98+
8999
project.subprojects { Project sub ->
90100
if (!sub.plugins.hasPlugin('firebase-library')) {
91101
return
@@ -121,11 +131,40 @@ class PublishingPlugin implements Plugin<Project> {
121131
publisher.decorate(sub, it)
122132
}
123133
}
134+
124135
publishAllToLocal.dependsOn "$sub.path:publishMavenAarPublicationToMavenLocal"
125136
publishAllToBuildDir.dependsOn "$sub.path:publishMavenAarPublicationToBuildDirRepository"
126137

138+
// Update the changed and all sets each time an artifact is published. This is
139+
// used to build the JSON file of affected targets.
140+
def groupId = firebaseLibrary.groupId.get()
141+
def artifactId = firebaseLibrary.artifactId.get()
142+
def artifact = "$groupId:$artifactId:$sub.version-SNAPSHOT"
143+
allArtifacts.add(artifact)
144+
145+
if (changedProjects.contains(sub)) {
146+
changedArtifacts.add(artifact)
147+
}
127148
}
149+
128150
}
151+
152+
// Build the JSON file of affected targets after all artifacts have been published.
153+
publishAllToBuildDir.doLast {
154+
def changed = new JSONArray()
155+
changedArtifacts.each { changed.put(it) }
156+
157+
def all = new JSONArray()
158+
allArtifacts.each { all.put(it) }
159+
160+
def json = new JSONObject()
161+
json.put("all", all)
162+
json.put("changed", changed)
163+
164+
def path = project.buildDir.toPath()
165+
path.resolve("m2repository/changed-artifacts.json").write(json.toString())
166+
}
167+
129168
project.task('publishProjectsToMavenLocal') {
130169
projectsToPublish.each { projectToPublish ->
131170
dependsOn getPublishTask(projectToPublish, 'MavenLocal')
@@ -155,6 +194,36 @@ class PublishingPlugin implements Plugin<Project> {
155194
}
156195
}
157196

197+
private static Set<Project> getChangedProjects(Project p) {
198+
Set<Project> roots = new AffectedProjectFinder(p, []).find()
199+
HashSet<Project> changed = new HashSet<>()
200+
201+
getChangedProjectsLoop(roots, changed)
202+
return changed
203+
}
204+
205+
private static void getChangedProjectsLoop(Collection<Project> projects, Set<Project> changed) {
206+
for (Project p : projects) {
207+
// Skip project if it is not a Firebase library.
208+
if (p.extensions.findByType(FirebaseLibraryExtension) == null) {
209+
continue;
210+
}
211+
212+
// Skip processing and recursion if this project has already been added to the set.
213+
if (!changed.add(p)) {
214+
continue;
215+
}
216+
217+
// Find all (head) dependencies to other projects in this respository.
218+
def all = p.configurations.releaseRuntimeClasspath.allDependencies
219+
def affected =
220+
all.findAll { it instanceof ProjectDependency }.collect { it.getDependencyProject() }
221+
222+
// Recurse with the new dependencies.
223+
getChangedProjectsLoop(affected, changed)
224+
}
225+
}
226+
158227
private static String getPublishTask(Project p, String repoName) {
159228
return "${p.path}:publishMavenAarPublicationTo$repoName"
160229
}

0 commit comments

Comments
 (0)