|
| 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 java.io.IOException; |
| 19 | +import java.nio.charset.Charset; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.stream.Collectors; |
| 27 | +import org.gradle.api.GradleException; |
| 28 | +import org.gradle.api.Plugin; |
| 29 | +import org.gradle.api.Project; |
| 30 | +import org.gradle.api.Task; |
| 31 | +import org.gradle.api.artifacts.DependencySet; |
| 32 | +import org.gradle.api.artifacts.ProjectDependency; |
| 33 | +import org.json.JSONArray; |
| 34 | +import org.json.JSONObject; |
| 35 | + |
| 36 | +/** Builds Firebase libraries for consumption by the smoke tests. */ |
| 37 | +public class SmokeTestsPlugin implements Plugin<Project> { |
| 38 | + @Override |
| 39 | + public void apply(Project project) { |
| 40 | + Task assembleAllTask = project.task("assembleAllForSmokeTests"); |
| 41 | + |
| 42 | + // Wait until after the projects have been evaluated or else we might skip projects. |
| 43 | + project |
| 44 | + .getGradle() |
| 45 | + .projectsEvaluated( |
| 46 | + gradle -> { |
| 47 | + Set<Project> changedProjects = getChangedProjects(project); |
| 48 | + Set<String> changedArtifacts = new HashSet<>(); |
| 49 | + Set<String> allArtifacts = new HashSet<>(); |
| 50 | + |
| 51 | + // Visit each project and add the artifacts to the appropriate sets. |
| 52 | + project.subprojects( |
| 53 | + sub -> { |
| 54 | + FirebaseLibraryExtension firebaseLibrary = |
| 55 | + sub.getExtensions().findByType(FirebaseLibraryExtension.class); |
| 56 | + if (firebaseLibrary == null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + String groupId = firebaseLibrary.groupId.get(); |
| 61 | + String artifactId = firebaseLibrary.artifactId.get(); |
| 62 | + String artifact = |
| 63 | + String.format("%s:%s:%s-SNAPSHOT", groupId, artifactId, sub.getVersion()); |
| 64 | + allArtifacts.add(artifact); |
| 65 | + |
| 66 | + if (changedProjects.contains(sub)) { |
| 67 | + changedArtifacts.add(artifact); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + // Reuse the publish task for building the libraries. |
| 72 | + Task publishAllTask = project.getTasks().getByPath("publishAllToBuildDir"); |
| 73 | + assembleAllTask.dependsOn(publishAllTask); |
| 74 | + |
| 75 | + // Generate a JSON file listing the artifacts after everything is complete. |
| 76 | + assembleAllTask.doLast( |
| 77 | + task -> { |
| 78 | + JSONArray changed = new JSONArray(); |
| 79 | + changedArtifacts.forEach(changed::put); |
| 80 | + |
| 81 | + JSONArray all = new JSONArray(); |
| 82 | + allArtifacts.forEach(all::put); |
| 83 | + |
| 84 | + JSONObject json = new JSONObject(); |
| 85 | + json.put("headGit", all); |
| 86 | + json.put("default", changed); |
| 87 | + |
| 88 | + Path path = project.getBuildDir().toPath(); |
| 89 | + Path jsonFile = |
| 90 | + path.resolve( |
| 91 | + "m2repository/changed-artifacts.json"); // .write(json.toString()) |
| 92 | + try { |
| 93 | + Files.write( |
| 94 | + jsonFile, |
| 95 | + Collections.singleton(json.toString()), |
| 96 | + Charset.defaultCharset()); |
| 97 | + } catch (IOException e) { |
| 98 | + throw new GradleException("Failed to write '" + jsonFile + "' json file.", e); |
| 99 | + } |
| 100 | + }); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + private static Set<Project> getChangedProjects(Project p) { |
| 105 | + Set<Project> roots = new AffectedProjectFinder(p, Collections.emptyList()).find(); |
| 106 | + HashSet<Project> changed = new HashSet<>(); |
| 107 | + |
| 108 | + getChangedProjectsLoop(roots, changed); |
| 109 | + return changed; |
| 110 | + } |
| 111 | + |
| 112 | + private static void getChangedProjectsLoop(Collection<Project> projects, Set<Project> changed) { |
| 113 | + for (Project p : projects) { |
| 114 | + // Skip project if it is not a Firebase library. |
| 115 | + if (p.getExtensions().findByType(FirebaseLibraryExtension.class) == null) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + // Skip processing and recursion if this project has already been added to the set. |
| 120 | + if (!changed.add(p)) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + // Find all (head) dependencies to other projects in this repository. |
| 125 | + DependencySet all = |
| 126 | + p.getConfigurations().getByName("releaseRuntimeClasspath").getAllDependencies(); |
| 127 | + Set<Project> affected = |
| 128 | + all.stream() |
| 129 | + .filter(it -> it instanceof ProjectDependency) |
| 130 | + .map(it -> (ProjectDependency) it) |
| 131 | + .map(ProjectDependency::getDependencyProject) |
| 132 | + .collect(Collectors.toSet()); |
| 133 | + |
| 134 | + // Recurse with the new dependencies. |
| 135 | + getChangedProjectsLoop(affected, changed); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments