|
| 1 | +// Copyright 2018 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.firebase.gradle.plugins.FirebaseLibraryExtension |
| 18 | +import com.google.firebase.gradle.plugins.ci.AffectedProjectFinder |
| 19 | +import org.gradle.api.Plugin |
| 20 | +import org.gradle.api.Project |
| 21 | +import org.gradle.api.artifacts.ProjectDependency |
| 22 | +import org.json.JSONArray |
| 23 | +import org.json.JSONObject |
| 24 | + |
| 25 | +/** Builds Firebase libraries for consumption by the smoke tests. */ |
| 26 | +class SmokeTestsPlugin implements Plugin<Project> { |
| 27 | + @Override |
| 28 | + public void apply(Project project) { |
| 29 | + def assembleAllTask = project.task("assembleAllForSmokeTests") |
| 30 | + |
| 31 | + // Wait until after the projects have been evaluated or else we might skip projects. |
| 32 | + project.gradle.projectsEvaluated { |
| 33 | + def changedProjects = getChangedProjects(project) |
| 34 | + def changedArtifacts = new HashSet<String>() |
| 35 | + def allArtifacts = new HashSet<String>() |
| 36 | + |
| 37 | + // Visit each project and add the artifacts to the appropriate sets. |
| 38 | + project.subprojects { |
| 39 | + def firebaseLibrary = it.extensions.findByType(FirebaseLibraryExtension) |
| 40 | + if (firebaseLibrary == null) { |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + def groupId = firebaseLibrary.groupId.get() |
| 45 | + def artifactId = firebaseLibrary.artifactId.get() |
| 46 | + def artifact = "$groupId:$artifactId:$it.version-SNAPSHOT" |
| 47 | + allArtifacts.add(artifact) |
| 48 | + |
| 49 | + if (changedProjects.contains(it)) { |
| 50 | + changedArtifacts.add(artifact) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Reuse the publish task for building the libraries. |
| 55 | + def publishAllTask = project.tasks.getByPath("publishAllToBuildDir") |
| 56 | + assembleAllTask.dependsOn(publishAllTask) |
| 57 | + |
| 58 | + // Generate a JSON file listing the artifacts after everything is complete. |
| 59 | + assembleAllTask.doLast { |
| 60 | + def changed = new JSONArray() |
| 61 | + changedArtifacts.each { changed.put(it) } |
| 62 | + |
| 63 | + def all = new JSONArray() |
| 64 | + allArtifacts.each { all.put(it) } |
| 65 | + |
| 66 | + def json = new JSONObject() |
| 67 | + json.put("all", all) |
| 68 | + json.put("changed", changed) |
| 69 | + |
| 70 | + def path = project.buildDir.toPath() |
| 71 | + path.resolve("m2repository/changed-artifacts.json").write(json.toString()) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static Set<Project> getChangedProjects(Project p) { |
| 77 | + Set<Project> roots = new AffectedProjectFinder(p, []).find() |
| 78 | + HashSet<Project> changed = new HashSet<>() |
| 79 | + |
| 80 | + getChangedProjectsLoop(roots, changed) |
| 81 | + return changed |
| 82 | + } |
| 83 | + |
| 84 | + private static void getChangedProjectsLoop(Collection<Project> projects, Set<Project> changed) { |
| 85 | + for (Project p : projects) { |
| 86 | + // Skip project if it is not a Firebase library. |
| 87 | + if (p.extensions.findByType(FirebaseLibraryExtension) == null) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + // Skip processing and recursion if this project has already been added to the set. |
| 92 | + if (!changed.add(p)) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + // Find all (head) dependencies to other projects in this respository. |
| 97 | + def all = p.configurations.releaseRuntimeClasspath.allDependencies |
| 98 | + def affected = |
| 99 | + all.findAll { it instanceof ProjectDependency }.collect { it.getDependencyProject() } |
| 100 | + |
| 101 | + // Recurse with the new dependencies. |
| 102 | + getChangedProjectsLoop(affected, changed) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments