Skip to content

Commit

Permalink
Fix snippet task cc incompatibilities (#101823)
Browse files Browse the repository at this point in the history
Addresses some Gradle configuration cache issues related to #57918
  • Loading branch information
breskeby authored Nov 7, 2023
1 parent eed3c6b commit e1184cd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.elasticsearch.gradle.Version
import org.elasticsearch.gradle.VersionProperties
import org.elasticsearch.gradle.internal.test.rest.CopyRestApiTask
import org.elasticsearch.gradle.internal.test.rest.CopyRestTestsTask
import org.gradle.api.Action
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.Directory
Expand Down Expand Up @@ -61,16 +62,24 @@ class DocsTestPlugin implements Plugin<Project> {
group 'Docs'
description 'List each snippet'
defaultSubstitutions = commonDefaultSubstitutions
perSnippet { println(it.toString()) }
perSnippet = new Action<SnippetsTask.Snippet>() {
@Override
void execute(SnippetsTask.Snippet snippet) {
println(snippet.toString())
}
}
}
project.tasks.register('listConsoleCandidates', SnippetsTask) {
group 'Docs'
description
'List snippets that probably should be marked // CONSOLE'
defaultSubstitutions = commonDefaultSubstitutions
perSnippet {
if (RestTestsFromSnippetsTask.isConsoleCandidate(it)) {
println(it.toString())
perSnippet = new Action<SnippetsTask.Snippet>() {
@Override
void execute(SnippetsTask.Snippet snippet) {
if (RestTestsFromSnippetsTask.isConsoleCandidate(it)) {
println(it.toString())
}
}
}
}
Expand All @@ -80,7 +89,7 @@ class DocsTestPlugin implements Plugin<Project> {
defaultSubstitutions = commonDefaultSubstitutions
testRoot.convention(restRootDir)
doFirst {
fileOperations.delete(restRootDir)
getFileOperations().delete(testRoot.get())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ package org.elasticsearch.gradle.internal.doc

import groovy.transform.PackageScope
import org.elasticsearch.gradle.internal.doc.SnippetsTask.Snippet
import org.gradle.api.Action
import org.gradle.api.InvalidUserDataException
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.internal.file.FileOperations
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
Expand All @@ -24,7 +26,7 @@ import java.nio.file.Path
/**
* Generates REST tests for each snippet marked // TEST.
*/
class RestTestsFromSnippetsTask extends SnippetsTask {
abstract class RestTestsFromSnippetsTask extends SnippetsTask {
/**
* These languages aren't supported by the syntax highlighter so we
* shouldn't use them.
Expand Down Expand Up @@ -64,13 +66,23 @@ class RestTestsFromSnippetsTask extends SnippetsTask {
@Internal
Set<String> names = new HashSet<>()

@Inject
abstract FileOperations getFileOperations();

@Inject
RestTestsFromSnippetsTask(ObjectFactory objectFactory) {
testRoot = objectFactory.directoryProperty()
TestBuilder builder = new TestBuilder()
perSnippet builder.&handleSnippet
doLast builder.&checkUnconverted
doLast builder.&finishLastTest
perSnippet = new Action<Snippet>() {
@Override
void execute(Snippet snippet) {
builder.handleSnippet(snippet)
}
}
doLast {
builder.checkUnconverted()
builder.finishLastTest()
}
}

/**
Expand Down Expand Up @@ -190,6 +202,7 @@ class RestTestsFromSnippetsTask extends SnippetsTask {
* Called each time a snippet is encountered. Tracks the snippets and
* calls buildTest to actually build the test.
*/

void handleSnippet(Snippet snippet) {
if (RestTestsFromSnippetsTask.isConsoleCandidate(snippet)) {
unconvertedCandidates.add(snippet.path.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ package org.elasticsearch.gradle.internal.doc
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.JsonToken

import org.gradle.api.Action;
import org.gradle.api.DefaultTask
import org.gradle.api.InvalidUserDataException
import org.gradle.api.file.ConfigurableFileTree
Expand Down Expand Up @@ -44,7 +45,7 @@ class SnippetsTask extends DefaultTask {
* instance of Snippet.
*/
@Internal
Closure perSnippet
Action<Snippet> perSnippet

/**
* The docs to scan. Defaults to every file in the directory exception the
Expand Down Expand Up @@ -134,7 +135,7 @@ class SnippetsTask extends DefaultTask {
+ "After substitutions and munging, the json looks like:\n" + quoted, e);
}
}
perSnippet(snippet)
perSnippet.execute(snippet)
snippet = null
}
file.eachLine('UTF-8') { String line, int lineNumber ->
Expand Down

0 comments on commit e1184cd

Please sign in to comment.