diff options
Diffstat (limited to 'buildSrc/src/main/kotlin/androidx/build/docs/ConcatenateFilesTask.kt')
-rw-r--r-- | buildSrc/src/main/kotlin/androidx/build/docs/ConcatenateFilesTask.kt | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/buildSrc/src/main/kotlin/androidx/build/docs/ConcatenateFilesTask.kt b/buildSrc/src/main/kotlin/androidx/build/docs/ConcatenateFilesTask.kt new file mode 100644 index 00000000000..d97c7019df8 --- /dev/null +++ b/buildSrc/src/main/kotlin/androidx/build/docs/ConcatenateFilesTask.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.build.docs + +import org.gradle.api.Action +import org.gradle.api.DefaultTask +import org.gradle.api.tasks.TaskAction +import org.gradle.api.tasks.OutputFile +import java.io.File +import java.util.SortedMap + +open class ConcatenateFilesTask : DefaultTask() { + private var keyedInputs: MutableMap<String, File> = mutableMapOf() + + @get:OutputFile + lateinit var Output: File + + // Adds the given input file + // The order that files are concatenated in is based on sorting the corresponding keys + fun addInput(key: String, inputFile: File) { + if (this.keyedInputs.containsKey(key)) { + throw IllegalArgumentException("Key $key already exists") + } + this.inputs.file(inputFile) + this.keyedInputs[key] = inputFile + } + + @TaskAction + fun aggregate() { + val destFile = this.Output + + // sort the input files to make sure this task always concatenates them in the same order + val sortedInputs = this.keyedInputs.toSortedMap() + + val inputFiles = sortedInputs.values + if (inputFiles.contains(destFile)) { + throw IllegalArgumentException("Output file $destFile is also an input file") + } + + val text = inputFiles.joinToString(separator = "") { file -> file.readText() } + this.project.logger.info("Joining ${inputFiles.count()} files, and storing the result in ${destFile.path}") + destFile.writeText(text) + } +} |