|
| 1 | +// Copyright 2023 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 |
| 16 | + |
| 17 | +import java.io.File |
| 18 | +import org.gradle.api.Plugin |
| 19 | +import org.gradle.api.Project |
| 20 | +import org.gradle.api.provider.Property |
| 21 | +import org.gradle.api.tasks.Exec |
| 22 | +import org.gradle.kotlin.dsl.create |
| 23 | +import org.gradle.kotlin.dsl.register |
| 24 | + |
| 25 | +/** |
| 26 | + * Exposes configuration for [GitSubmodulePlugin]. |
| 27 | + * |
| 28 | + * @param submodules the parent directory of the SDK's Git Submodules. Defaults to `src/third_party` |
| 29 | + */ |
| 30 | +interface GitSubmodulePluginExtension { |
| 31 | + val submodules: Property<File> |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Helper plugin for common actions regarding Git Submodules |
| 36 | + * |
| 37 | + * At the time of writing this, we only have one SDK with submodules. Although, that could grow in |
| 38 | + * the future. More importantly though, this provides a way for us to make sure the submodules are |
| 39 | + * initilized whenever we are building said SDKs- while keeping our system clean and modular. |
| 40 | + * |
| 41 | + * This plugin is automatically applied to all SDKs that utilize [FirebaseLibraryPlugin], and is |
| 42 | + * subsequently bound to the `preBuild` task that is apart of all gradle modules. |
| 43 | + * |
| 44 | + * The following tasks are registered when this plugin is applied: |
| 45 | + * - [initializeGitSubmodules][registerInitializeGitSubmodulesTask] |
| 46 | + * - [updateGitSubmodules][registerUpdateGitSubmodulesTask] |
| 47 | + * - [removeGitSubmodules][registerRemoveGitSubmodulesTask] |
| 48 | + * |
| 49 | + * __Documentation explaining each task is provided in the annotation for each task__ |
| 50 | + * |
| 51 | + * @see [GitSubmodulePluginExtension] |
| 52 | + */ |
| 53 | +abstract class GitSubmodulePlugin : Plugin<Project> { |
| 54 | + |
| 55 | + override fun apply(project: Project) { |
| 56 | + with(configureExtension(project)) { |
| 57 | + registerInitializeGitSubmodulesTask(project, submodules.get()) |
| 58 | + registerUpdateGitSubmodulesTask(project, submodules.get()) |
| 59 | + registerRemoveGitSubmodulesTask(project, submodules.get()) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private fun configureExtension(project: Project) = |
| 64 | + project.extensions.create<GitSubmodulePluginExtension>("GitSubmodule").apply { |
| 65 | + submodules.convention(project.file("src/third_party")) |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Registers the initializeGitSubmodules Task for the provided [Project]. |
| 70 | + * |
| 71 | + * Creates a local configuration for the predefined submodules. It does this by running the |
| 72 | + * command `git submodule init` from the [project]'s root directory. |
| 73 | + * |
| 74 | + * If there aren't any submodules to initialize, this task is skipped- saving resources. |
| 75 | + * |
| 76 | + * @param project the [Project] to register this task to |
| 77 | + * @param submodules the root directory of where the submodules live |
| 78 | + */ |
| 79 | + private fun registerInitializeGitSubmodulesTask(project: Project, submodules: File) = |
| 80 | + project.tasks.register<Exec>("initializeGitSubmodules") { |
| 81 | + onlyIf { hasEmptySubmodules(submodules) } |
| 82 | + |
| 83 | + workingDir = project.projectDir |
| 84 | + commandLine = "git submodule init".split(" ") |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Registers the updateGitSubmodules Task for the provided [Project]. |
| 89 | + * |
| 90 | + * Pulls the latest data for each submodule, similiar to `git pull`. It does this by running the |
| 91 | + * command `git submodule update` from the [project]'s root directory. |
| 92 | + * |
| 93 | + * If there aren't any submodules, this task is skipped- saving resources. |
| 94 | + * |
| 95 | + * @param project the [Project] to register this task to |
| 96 | + * @param submodules the root directory of where the submodules live |
| 97 | + */ |
| 98 | + private fun registerUpdateGitSubmodulesTask(project: Project, submodules: File) = |
| 99 | + project.tasks.register<Exec>("updateGitSubmodules") { |
| 100 | + onlyIf { hasEmptySubmodules(submodules) } |
| 101 | + dependsOn("initializeGitSubmodules") |
| 102 | + |
| 103 | + workingDir = project.projectDir |
| 104 | + commandLine = "git submodule update".split(" ") |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Registers the removeGitSubmodules Task for the provided [Project]. |
| 109 | + * |
| 110 | + * Removes and de initilizes all submodules for the given [project]. It does this by running the |
| 111 | + * command `git submodule deinit --all` from the [project]'s root directory. |
| 112 | + * |
| 113 | + * If there aren't any submodules to remove, this task is skipped- saving resources. |
| 114 | + * |
| 115 | + * @param project the [Project] to register this task to |
| 116 | + * @param submodules the root directory of where the submodules live |
| 117 | + */ |
| 118 | + private fun registerRemoveGitSubmodulesTask(project: Project, submodules: File) = |
| 119 | + project.tasks.register<Exec>("removeGitSubmodules") { |
| 120 | + onlyIf { submodules.exists() } |
| 121 | + |
| 122 | + workingDir = project.projectDir |
| 123 | + commandLine = "git submodule deinit --all".split(" ") |
| 124 | + } |
| 125 | + |
| 126 | + private fun hasEmptySubmodules(parentFolder: File) = |
| 127 | + parentFolder.listFilesOrEmpty().any { it.isDirectory && it.listFilesOrEmpty().isEmpty() } |
| 128 | +} |
0 commit comments