|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.firebase.dataconnect.gradle.sharedtest |
| 17 | + |
| 18 | +import org.gradle.api.DefaultTask |
| 19 | +import org.gradle.api.file.DirectoryProperty |
| 20 | +import org.gradle.api.logging.Logger |
| 21 | +import org.gradle.api.tasks.InputDirectory |
| 22 | +import org.gradle.api.tasks.Internal |
| 23 | +import org.gradle.api.tasks.Optional |
| 24 | +import org.gradle.api.tasks.OutputDirectory |
| 25 | +import org.gradle.api.tasks.TaskAction |
| 26 | +import java.io.File |
| 27 | + |
| 28 | +/** |
| 29 | + * A Gradle task that copies Kotlin source files annotated with `@file:SharedWithAndroidTest` from |
| 30 | + * an input directory to an output directory. |
| 31 | + * |
| 32 | + * This driving motivation behind creating this task is to share test utilities or fixtures between |
| 33 | + * unit tests and Android instrumentation tests by copying the annotated files into the appropriate |
| 34 | + * source set before compilation. |
| 35 | + */ |
| 36 | +abstract class CopySharedWithAndroidTestFiles : DefaultTask() { |
| 37 | + |
| 38 | + @get:Internal abstract val inputBaseDirectory: DirectoryProperty |
| 39 | + |
| 40 | + @get:InputDirectory abstract val inputDirectory: DirectoryProperty |
| 41 | + |
| 42 | + @get:OutputDirectory abstract val outputDirectory: DirectoryProperty |
| 43 | + |
| 44 | + @TaskAction |
| 45 | + fun run() { |
| 46 | + val inputBaseDirectory: File = inputBaseDirectory.get().asFile |
| 47 | + val inputDirectory: File = inputDirectory.get().asFile |
| 48 | + val outputDirectory: File = outputDirectory.get().asFile |
| 49 | + |
| 50 | + logger.info("inputBaseDirectory={}", inputBaseDirectory.absolutePath) |
| 51 | + logger.info("inputDirectory={}", inputDirectory.absolutePath) |
| 52 | + logger.info("outputDirectory={}", outputDirectory.absolutePath) |
| 53 | + |
| 54 | + copyAnnotatedFiles( |
| 55 | + srcBaseDir = inputBaseDirectory, |
| 56 | + srcDir = inputDirectory, |
| 57 | + destDir = outputDirectory, |
| 58 | + taskPath = path, |
| 59 | + logger = logger, |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Recursively searches through [srcDir] for Kotlin source files (`.kt`) that contain the target |
| 66 | + * annotation and copies them to [destDir], maintaining their relative directory structure. |
| 67 | + * |
| 68 | + * @param srcBaseDir The root directory to use when reporting file system paths in the generated |
| 69 | + * files, rather than their absolute paths, to avoid defeating build caches; this **MUST** be a |
| 70 | + * parent directory of [srcDir]. |
| 71 | + * @param srcDir The root directory to search for annotated Kotlin files. |
| 72 | + * @param destDir The root directory where annotated files should be copied. |
| 73 | + * @param taskPath The full Gradle path of the task performing the copy. |
| 74 | + * @param logger The Gradle logger to use for logging the copy operations. |
| 75 | + */ |
| 76 | +private fun copyAnnotatedFiles(srcBaseDir: File, srcDir: File, destDir: File, taskPath: String, logger: Logger) { |
| 77 | + if (!srcDir.exists()) { |
| 78 | + logger.info("source directory was not found; no files copied") |
| 79 | + return |
| 80 | + } |
| 81 | + if (!srcDir.isDirectory) { |
| 82 | + logger.info("source directory is not a directory; no files copied") |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + val srcFilesToCopy = srcDir |
| 87 | + .walk() |
| 88 | + .filter { it.isFile && it.extension == "kt" && it.hasALineEqualToOneOf(sharedWithAndroidTestAnnotationLines) } |
| 89 | + .toList() |
| 90 | + |
| 91 | + logger.info( |
| 92 | + "Found {} files in {} that have a line equal to one of: {}", |
| 93 | + srcFilesToCopy.size, |
| 94 | + srcDir, |
| 95 | + sharedWithAndroidTestAnnotationLines.joinToString { "\"$it\"" } |
| 96 | + ) |
| 97 | + |
| 98 | + if (srcFilesToCopy.isEmpty()) { |
| 99 | + logger.info("No files found to copy; no files copied") |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + srcFilesToCopy.forEachIndexed { fileIndex, srcFile -> |
| 104 | + val relativePath = srcFile.relativeTo(srcDir) |
| 105 | + val destFile = File(destDir, relativePath.path) |
| 106 | + logger.info("Copying file {}/{}: {} to {}", (fileIndex+1), srcFilesToCopy.size, srcFile, destFile) |
| 107 | + |
| 108 | + destFile.parentFile?.mkdirs() |
| 109 | + |
| 110 | + destFile.printWriter().use { |
| 111 | + val srcFileRelative = srcFile.absoluteFile.relativeTo(srcBaseDir.absoluteFile) |
| 112 | + it.println("// Generated from: $srcFileRelative") |
| 113 | + it.println("// Generated by Gradle task: $taskPath") |
| 114 | + it.println("//") |
| 115 | + it.println("// WARNING: This file is generated!") |
| 116 | + it.println("// Any changes made to this file will eventually be overwritten by the build.") |
| 117 | + it.println("// If changes are desired, make them in the original file instead.") |
| 118 | + it.println() |
| 119 | + |
| 120 | + srcFile.useLines { lines -> |
| 121 | + lines.forEach { line -> |
| 122 | + it.println(line) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +private val sharedWithAndroidTestAnnotationLines = listOf( |
| 130 | + "@file:SharedWithAndroidTest", |
| 131 | + "@file:com.google.firebase.dataconnect.testutil.SharedWithAndroidTest", |
| 132 | +) |
| 133 | + |
| 134 | +private fun File.hasALineEqualToOneOf(desiredLines: Collection<String>): Boolean = |
| 135 | + useLines { lines -> |
| 136 | + lines.any { |
| 137 | + it.trim() in desiredLines |
| 138 | + } |
| 139 | + } |
0 commit comments