2021-04-02 03:56:47 -04:00
|
|
|
import groovy.json.JsonOutput
|
|
|
|
import groovy.json.JsonSlurper
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils
|
|
|
|
|
2021-08-27 06:47:19 -04:00
|
|
|
import java.util.function.Function
|
|
|
|
|
2021-03-23 04:25:53 -04:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Create common 'regenerate' task sub-tasks can hook into.
|
|
|
|
|
2021-04-19 07:37:47 -04:00
|
|
|
/**
|
|
|
|
* Compute all "checksummed" key-value pairs.
|
|
|
|
*/
|
|
|
|
def computeChecksummedEntries = { Task sourceTask ->
|
|
|
|
// An flat ordered map of key-value pairs.
|
|
|
|
Map<String, String> allEntries = new TreeMap<>()
|
|
|
|
|
|
|
|
// Make sure all input properties are either simple strings
|
|
|
|
// or closures returning simple strings.
|
|
|
|
//
|
|
|
|
// Don't overcomplicate things with other serializable types.
|
|
|
|
Map<String, Object> props = sourceTask.inputs.properties
|
|
|
|
props.forEach { key, val ->
|
|
|
|
// Handle closures and other lazy providers.
|
|
|
|
if (val instanceof Provider<?>) {
|
|
|
|
val = val.get()
|
|
|
|
}
|
|
|
|
if (val instanceof Closure<?>) {
|
|
|
|
val = val.call()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(val instanceof String)) {
|
|
|
|
throw new GradleException("Input properties of wrapped tasks must all be " +
|
|
|
|
"strings: ${key} in ${sourceTask.name} is not.")
|
|
|
|
}
|
|
|
|
allEntries.put("property:" + key, (String) val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect all of task inputs/ output files and compute their checksums.
|
|
|
|
FileCollection allFiles = sourceTask.inputs.files + sourceTask.outputs.files
|
|
|
|
|
|
|
|
// Compute checksums for root-project relative paths
|
|
|
|
allFiles.files.forEach { file ->
|
|
|
|
allEntries.put(
|
|
|
|
sourceTask.project.rootDir.relativePath(file),
|
|
|
|
file.exists() ? new DigestUtils(DigestUtils.sha1Digest).digestAsHex(file).trim() : "--")
|
|
|
|
}
|
|
|
|
|
|
|
|
return allEntries
|
|
|
|
}
|
|
|
|
|
2021-08-27 06:47:19 -04:00
|
|
|
configure(rootProject) {
|
|
|
|
ext {
|
|
|
|
/**
|
|
|
|
* Utility function to read a file, apply changes to its content and write it back.
|
|
|
|
*/
|
|
|
|
modifyFile = { File path, Function<String, String> modify ->
|
|
|
|
Function<String, String> normalizeEols = { text -> text.replace("\r\n", "\n") }
|
|
|
|
|
|
|
|
String original = path.getText("UTF-8")
|
2021-11-17 12:36:34 -05:00
|
|
|
String modified = normalizeEols.apply(original)
|
|
|
|
modified = modify.apply(modified)
|
|
|
|
modified = normalizeEols.apply(modified)
|
2021-08-27 06:47:19 -04:00
|
|
|
if (!original.equals(modified)) {
|
|
|
|
path.write(modified, "UTF-8")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 04:25:53 -04:00
|
|
|
configure([
|
|
|
|
project(":lucene:analysis:common"),
|
|
|
|
project(":lucene:analysis:icu"),
|
|
|
|
project(":lucene:analysis:kuromoji"),
|
2021-05-05 16:03:06 -04:00
|
|
|
project(":lucene:analysis:nori"),
|
|
|
|
project(":lucene:backward-codecs"),
|
|
|
|
project(":lucene:core"),
|
|
|
|
project(":lucene:queryparser"),
|
2021-08-27 06:47:19 -04:00
|
|
|
project(":lucene:expressions"),
|
2022-11-19 17:40:45 -05:00
|
|
|
project(":lucene:test-framework"),
|
2021-03-23 04:25:53 -04:00
|
|
|
]) {
|
|
|
|
task regenerate() {
|
|
|
|
description "Rerun any code or static data generation tasks."
|
|
|
|
group "generation"
|
|
|
|
}
|
|
|
|
|
2021-04-02 03:56:47 -04:00
|
|
|
project.ext {
|
|
|
|
// This utility method implements the logic required for "persistent" incremental
|
|
|
|
// source-generating tasks. The idea is simple, the implementation quite complex.
|
|
|
|
//
|
2021-04-16 16:35:51 -04:00
|
|
|
// The idea is that, given source-generating task "sourceTaskInternal" (note the suffix),
|
|
|
|
// we create a bunch of other tasks that perform checksum generation, validation and
|
2024-04-09 01:44:07 -04:00
|
|
|
// source task skipping. For example, let's say we have a task 'genFooInternal";
|
|
|
|
// the following tasks would be created
|
2021-04-02 03:56:47 -04:00
|
|
|
//
|
2024-04-09 01:44:07 -04:00
|
|
|
// genFooChecksumLoad
|
|
|
|
// genFooChecksumSave
|
|
|
|
// genFooChecksumCheck (fails if checksums are inconsistent)
|
|
|
|
//
|
|
|
|
// and the following set of dependencies would be created (with additional
|
|
|
|
// constraints to run them in this particular order!).
|
|
|
|
//
|
|
|
|
// genFoo.dependsOn [genFooChecksumLoad, genFooInternal, genFooChecksumSave]
|
2021-04-02 03:56:47 -04:00
|
|
|
//
|
|
|
|
// Checksums are persisted and computed from sourceTask's inputs/outputs. If the
|
2024-04-09 01:44:07 -04:00
|
|
|
// persisted checksums are identical to the now-current checksums, the "internal" task
|
|
|
|
// is skipped (using onlyIf { false }).
|
2021-04-02 03:56:47 -04:00
|
|
|
//
|
|
|
|
// Implementation-wise things get complicated because gradle doesn't have the notion
|
|
|
|
// of "ordered" task execution with respect to task AND its dependencies (we can add
|
|
|
|
// constraints to each node in the execution graph but not node-and-dependencies).
|
|
|
|
//
|
|
|
|
// sourceTask - the task to wrap
|
2021-04-02 05:46:43 -04:00
|
|
|
// extraConfig - a map with extra (optional) configuration options.
|
2024-04-09 01:44:07 -04:00
|
|
|
// andThenTasks: any other tasks that should be scheduled to run after the internal task and
|
|
|
|
// before checksum calculation/ saving. spotless is a good example of this.
|
|
|
|
// ignoreWithSource: any other tasks that should be ignored if the internal task is ignored
|
|
|
|
// (checksums are identical)
|
|
|
|
// mustRunBefore: any tasks which should be scheduled to run after the internal task.
|
2021-04-02 05:46:43 -04:00
|
|
|
wrapWithPersistentChecksums = { Task sourceTask, Map<String, Object> extraConfig = [:] ->
|
2021-04-16 16:35:51 -04:00
|
|
|
if (!sourceTask.name.endsWith("Internal")) {
|
|
|
|
throw new GradleException("Wrapped task must follow the convention name of *Internal: ${sourceTask.name}")
|
|
|
|
}
|
|
|
|
|
|
|
|
String sourceTaskName = sourceTask.name.replaceAll('Internal$', '')
|
|
|
|
|
2021-04-02 05:46:43 -04:00
|
|
|
def toList = { value ->
|
|
|
|
if (value instanceof List) {
|
|
|
|
return value
|
|
|
|
} else if (value == null) {
|
|
|
|
return []
|
|
|
|
} else {
|
|
|
|
return [ value ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Object> andThenTasks = toList(extraConfig.get("andThenTasks"))
|
|
|
|
List<Object> ignoreWithSource = toList(extraConfig.get("ignoreWithSource"))
|
|
|
|
|
2024-04-09 01:44:07 -04:00
|
|
|
// Schedule must-run-afters
|
|
|
|
List<Object> mustRunBefore = toList(extraConfig.get("mustRunBefore"))
|
|
|
|
// TODO: maybe ensure all task refs here are strings?
|
|
|
|
tasks.matching { it.name in mustRunBefore }.configureEach {
|
|
|
|
mustRunAfter sourceTask
|
|
|
|
}
|
|
|
|
|
2021-04-02 03:56:47 -04:00
|
|
|
// Create checksum-loader task.
|
2021-04-16 16:35:51 -04:00
|
|
|
Task checksumLoadTask = tasks.create("${sourceTaskName}ChecksumLoad", {
|
2021-04-02 03:56:47 -04:00
|
|
|
ext {
|
|
|
|
checksumMatch = true
|
|
|
|
}
|
|
|
|
|
|
|
|
doFirst {
|
2021-04-19 07:37:47 -04:00
|
|
|
// Current persisted task input/outputs (file checksums, properties)
|
|
|
|
ext.currentChecksums = computeChecksummedEntries(sourceTask)
|
2021-04-02 03:56:47 -04:00
|
|
|
|
|
|
|
// Load any previously written checksums
|
2021-04-19 07:37:47 -04:00
|
|
|
ext.savedChecksums = new TreeMap<>()
|
2021-04-16 16:35:51 -04:00
|
|
|
ext.checksumsFile = project.file("src/generated/checksums/${sourceTaskName}.json")
|
2021-04-02 03:56:47 -04:00
|
|
|
if (checksumsFile.exists()) {
|
2021-04-19 07:37:47 -04:00
|
|
|
savedChecksums.putAll(new JsonSlurper().parse(checksumsFile) as Map)
|
2021-04-02 03:56:47 -04:00
|
|
|
}
|
|
|
|
|
2021-04-19 07:37:47 -04:00
|
|
|
// Compare saved and current checksums for subsequent tasks.
|
|
|
|
ext.checksumMatch = (savedChecksums.equals(currentChecksums))
|
2021-04-02 03:56:47 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-16 16:35:51 -04:00
|
|
|
Task checksumCheckTask = tasks.create("${sourceTaskName}ChecksumCheck", {
|
2021-04-02 03:56:47 -04:00
|
|
|
dependsOn checksumLoadTask
|
|
|
|
|
|
|
|
doFirst {
|
|
|
|
if (!checksumLoadTask.checksumMatch) {
|
|
|
|
// This can be made prettier but leave it verbose for now:
|
2021-04-19 07:37:47 -04:00
|
|
|
Map<String, String> current = checksumLoadTask.currentChecksums
|
2021-04-02 03:56:47 -04:00
|
|
|
Map<String, String> expected = checksumLoadTask.savedChecksums
|
|
|
|
|
2021-04-19 07:37:47 -04:00
|
|
|
def same = current.intersect(expected)
|
|
|
|
current = current - same
|
2021-04-02 03:56:47 -04:00
|
|
|
expected = expected - same
|
|
|
|
|
|
|
|
throw new GradleException("Checksums mismatch for derived resources; you might have" +
|
2021-11-17 12:13:40 -05:00
|
|
|
" modified a generated resource (regenerate task: ${sourceTaskName}):\n" +
|
2021-04-19 07:37:47 -04:00
|
|
|
"Current:\n ${current.entrySet().join('\n ')}\n\n" +
|
2022-07-30 14:45:32 -04:00
|
|
|
"Expected:\n ${expected.entrySet().join('\n ')}\n\n" +
|
|
|
|
"Input files for this task are:\n " + sourceTask.inputs.files.join('\n ') + "\n\n" +
|
|
|
|
"Files generated by this task are:\n " + sourceTask.outputs.files.join('\n ')
|
2021-04-02 03:56:47 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
check.dependsOn checksumCheckTask
|
|
|
|
|
2021-04-16 16:35:51 -04:00
|
|
|
Task checksumSaveTask = tasks.create("${sourceTaskName}ChecksumSave", {
|
2021-04-02 03:56:47 -04:00
|
|
|
dependsOn checksumLoadTask
|
|
|
|
|
|
|
|
doFirst {
|
|
|
|
File checksumsFile = checksumLoadTask.ext.checksumsFile
|
|
|
|
checksumsFile.parentFile.mkdirs()
|
|
|
|
|
2021-04-19 07:37:47 -04:00
|
|
|
// Recompute checksums after the task has completed and write them.
|
|
|
|
def updatedChecksums = computeChecksummedEntries(sourceTask)
|
2021-04-02 03:56:47 -04:00
|
|
|
checksumsFile.setText(
|
2021-04-19 07:37:47 -04:00
|
|
|
JsonOutput.prettyPrint(JsonOutput.toJson(new TreeMap<String, String>(updatedChecksums))), "UTF-8")
|
2021-04-02 03:56:47 -04:00
|
|
|
|
|
|
|
logger.warn("Updated generated file checksums for task ${sourceTask.path}.")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-16 16:35:51 -04:00
|
|
|
Task conditionalTask = tasks.create("${sourceTaskName}", {
|
2021-04-02 03:56:47 -04:00
|
|
|
def deps = [
|
|
|
|
checksumLoadTask,
|
|
|
|
sourceTask,
|
2021-04-02 05:46:43 -04:00
|
|
|
*andThenTasks,
|
2021-04-02 03:56:47 -04:00
|
|
|
checksumSaveTask
|
|
|
|
].flatten()
|
|
|
|
|
|
|
|
dependsOn deps
|
|
|
|
mustRunInOrder deps
|
|
|
|
|
|
|
|
doFirst {
|
2021-04-16 16:35:51 -04:00
|
|
|
if (checksumLoadTask.checksumMatch && !sourceTask.didWork) {
|
2021-04-02 03:56:47 -04:00
|
|
|
logger.lifecycle("Checksums consistent with sources, skipping task: ${sourceTask.path}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-08 09:00:20 -04:00
|
|
|
// Load checksums before the source task executes, otherwise it's always ignored.
|
2021-11-17 12:13:40 -05:00
|
|
|
project.afterEvaluate {
|
|
|
|
resolveTaskRefs([sourceTask, *ignoreWithSource]).each { t ->
|
|
|
|
t.configure {
|
|
|
|
dependsOn checksumLoadTask
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 09:00:20 -04:00
|
|
|
|
2021-04-02 03:56:47 -04:00
|
|
|
// Copy the description and group from the source task.
|
|
|
|
project.afterEvaluate {
|
|
|
|
conditionalTask.group sourceTask.group
|
|
|
|
conditionalTask.description sourceTask.description + " (if sources changed)"
|
2021-04-11 11:28:41 -04:00
|
|
|
|
|
|
|
// Hide low-level tasks from help.
|
|
|
|
sourceTask.group = null
|
|
|
|
sourceTask.description sourceTask.description + " (low-level)"
|
2021-04-02 03:56:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set conditional execution only if checksum mismatch occurred.
|
|
|
|
if (!gradle.startParameter.isRerunTasks()) {
|
|
|
|
project.afterEvaluate {
|
2021-04-02 05:46:43 -04:00
|
|
|
resolveTaskRefs([sourceTask, *ignoreWithSource, checksumSaveTask]).each { t ->
|
2021-04-02 03:56:47 -04:00
|
|
|
t.configure {
|
|
|
|
logger.info("Making " + t.name + " run only if " + checksumLoadTask.name + " indicates changes")
|
|
|
|
onlyIf { !checksumLoadTask.checksumMatch }
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 10:38:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 03:56:47 -04:00
|
|
|
return conditionalTask
|
2021-03-23 04:25:53 -04:00
|
|
|
}
|
|
|
|
}
|
2021-04-10 16:00:55 -04:00
|
|
|
}
|