Skip to content

Commit fc6c8fc

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

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-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: 70 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.
@@ -83,9 +87,16 @@ class PublishingPlugin implements Plugin<Project> {
8387

8488
def publishAllToLocal = project.task('publishAllToLocal')
8589
def publishAllToBuildDir = project.task('publishAllToBuildDir')
90+
def publishChangedToBuildDir = project.task('publishChangedToBuildDir')
8691
def firebasePublish = project.task('firebasePublish')
8792

8893
project.getGradle().projectsEvaluated {
94+
// These three variables are used for generating the JSON file that lists the artifacts
95+
// affected by the latest commit. This is generally useful only on CI builds.
96+
def changedProjects = getChangedProjects(project)
97+
def changedArtifacts = new HashSet<String>()
98+
def allArtifacts = new HashSet<String>()
99+
89100
project.subprojects { Project sub ->
90101
if (!sub.plugins.hasPlugin('firebase-library')) {
91102
return
@@ -121,11 +132,40 @@ class PublishingPlugin implements Plugin<Project> {
121132
publisher.decorate(sub, it)
122133
}
123134
}
135+
124136
publishAllToLocal.dependsOn "$sub.path:publishMavenAarPublicationToMavenLocal"
125137
publishAllToBuildDir.dependsOn "$sub.path:publishMavenAarPublicationToBuildDirRepository"
126138

139+
// Update the changed and all sets each time an artifact is published. This is
140+
// used to build the JSON file of affected targets.
141+
sub.tasks.getByName("publishMavenAarPublicationToBuildDirRepository").doLast {
142+
def artifact = "$sub.group:$sub.name:$sub.version-SNAPSHOT"
143+
allArtifacts.add(artifact)
144+
145+
if (changedProjects.contains(sub)) {
146+
changedArtifacts.add(artifact)
147+
}
148+
}
127149
}
150+
151+
}
152+
153+
// Build the JSON file of affected targets after all artifacts have been published.
154+
publishAllToBuildDir.doLast {
155+
def changed = new JSONArray()
156+
changedArtifacts.each { changed.put(it) }
157+
158+
def all = new JSONArray()
159+
allArtifacts.each { all.put(it) }
160+
161+
def json = new JSONObject()
162+
json.put("all", all)
163+
json.put("changed", changed)
164+
165+
def path = project.buildDir.toPath()
166+
path.resolve("m2repository/changed-artifacts.json").write(json.toString())
128167
}
168+
129169
project.task('publishProjectsToMavenLocal') {
130170
projectsToPublish.each { projectToPublish ->
131171
dependsOn getPublishTask(projectToPublish, 'MavenLocal')
@@ -155,6 +195,36 @@ class PublishingPlugin implements Plugin<Project> {
155195
}
156196
}
157197

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

0 commit comments

Comments
 (0)