|
| 1 | +// Copyright 2021 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 | +package com.google.firebase.gradle.plugins; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import org.gradle.api.Plugin; |
| 18 | +import org.gradle.api.Project; |
| 19 | +import org.gradle.api.Task; |
| 20 | +import org.gradle.api.publish.PublishingExtension; |
| 21 | +import org.gradle.api.publish.maven.MavenPublication; |
| 22 | +import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; |
| 23 | +import org.gradle.api.tasks.bundling.Jar; |
| 24 | +import org.gradle.api.tasks.bundling.Zip; |
| 25 | + |
| 26 | +/** |
| 27 | + * Injects release related artifact into the maven publication. |
| 28 | + * |
| 29 | + * <p>The artifact contains the following files in it: |
| 30 | + * |
| 31 | + * <ul> |
| 32 | + * <li>api-diff.txt |
| 33 | + * <li>api.txt |
| 34 | + * <li>mapping.txt(optional,only present if library is proguarded) |
| 35 | + * </ul> |
| 36 | + */ |
| 37 | +public class FireEscapeArtifactPlugin implements Plugin<Project> { |
| 38 | + private Project project; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void apply(Project project) { |
| 42 | + this.project = project; |
| 43 | + project.afterEvaluate( |
| 44 | + p -> { |
| 45 | + FirebaseLibraryExtension firebaseLibrary = |
| 46 | + p.getExtensions().findByType(FirebaseLibraryExtension.class); |
| 47 | + if (firebaseLibrary == null) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + Zip fireEscapeTask = |
| 52 | + p.getTasks() |
| 53 | + .create( |
| 54 | + "mavenAarFireEscapeArtifact", Zip.class, t -> t.setClassifier("fireescape")); |
| 55 | + |
| 56 | + p.getPlugins() |
| 57 | + .withType( |
| 58 | + MavenPublishPlugin.class, |
| 59 | + plugin -> |
| 60 | + p.getExtensions() |
| 61 | + .configure( |
| 62 | + PublishingExtension.class, |
| 63 | + pub -> |
| 64 | + pub.getPublications() |
| 65 | + .withType( |
| 66 | + MavenPublication.class, |
| 67 | + publication -> { |
| 68 | + if ("mavenAar".equals(publication.getName())) { |
| 69 | + configurePublication( |
| 70 | + publication, |
| 71 | + fireEscapeTask, |
| 72 | + firebaseLibrary.type); |
| 73 | + } |
| 74 | + }))); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + private void configurePublication( |
| 79 | + MavenPublication publication, Zip artifactTask, LibraryType libraryType) { |
| 80 | + publication.artifact(artifactTask); |
| 81 | + artifactTask.from(apiTxtFileTask()); |
| 82 | + if (libraryType.equals(LibraryType.ANDROID)) { |
| 83 | + artifactTask.from(proguardMappingFileTask()); |
| 84 | + } |
| 85 | + publication.artifact(javadocTask()); |
| 86 | + } |
| 87 | + |
| 88 | + private Task proguardMappingFileTask() { |
| 89 | + return project |
| 90 | + .getTasks() |
| 91 | + .create( |
| 92 | + "fireEscapeProguardMapping", |
| 93 | + task -> { |
| 94 | + project |
| 95 | + .getTasks() |
| 96 | + .all( |
| 97 | + it -> { |
| 98 | + if (it.getName().equals("assembleRelease")) { |
| 99 | + task.dependsOn(it); |
| 100 | + } |
| 101 | + }); |
| 102 | + task.getOutputs() |
| 103 | + .file(new File(project.getBuildDir(), "outputs/mapping/release/mapping.txt")); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + private Task apiTxtFileTask() { |
| 108 | + return project |
| 109 | + .getTasks() |
| 110 | + .create( |
| 111 | + "fireEscapeApiTxt", |
| 112 | + task -> { |
| 113 | + task.dependsOn(TasksKt.JAVADOC_TASK_NAME); |
| 114 | + task.getOutputs().file(new File(project.getBuildDir(), "tmp/javadoc/api.txt")); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + private Task javadocTask() { |
| 119 | + return project |
| 120 | + .getTasks() |
| 121 | + .create( |
| 122 | + "fireescapeJavadocJar", |
| 123 | + Jar.class, |
| 124 | + javadoc -> { |
| 125 | + javadoc.dependsOn(TasksKt.JAVADOC_TASK_NAME); |
| 126 | + javadoc.from(new File(project.getBuildDir(), "/docs/javadoc/reference")); |
| 127 | + javadoc.include("**/*"); |
| 128 | + javadoc.setArchiveName("fireescape-javadoc.jar"); |
| 129 | + javadoc.setClassifier("javadoc"); |
| 130 | + }); |
| 131 | + } |
| 132 | +} |
0 commit comments