Skip to content

Commit

Permalink
Handle AsciidoctoJ closure extensions in face of Gradle instrumentation
Browse files Browse the repository at this point in the history
- On Gradle 7.6 - 8.3, leak some Gradle libraries onto the classpath
  in order to allow closure extensions to still work.
- On Gradle 8.4+, throw an exception and tell the build script author
  to convert the closure into a string or place it in a file.
- Add support for providers to strings or files.
- Allow for external dependencies to be added.

Closes #697
  • Loading branch information
ysb33r committed Jan 8, 2024
1 parent d59ca0d commit 52fcf4a
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 253 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 4.0.0
version = 4.0.1
group = org.asciidoctor
sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand All @@ -12,7 +12,7 @@ project_vcs = https://github.com/asciidoctor/asciidoctor-gradle-plugin.g
cglibVersion = 3.3.0
jsoupVersion = 1.13.1
spockVersion = 2.3-groovy-3.0
grolifantVersion = 2.2.1
grolifantVersion = 2.2.3
jacocoVersion = 0.8.6
codenarcVersion = 3.3.0
nodejsGradleVersion = 2.2.0
Expand Down
8 changes: 4 additions & 4 deletions gradle/publishing.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tasks.named('jar', Jar) {
ext {
pomConfig = {
name project.name
description project.project_description
// description project.project_description
url project.project_website
inceptionYear '2013'
licenses {
Expand Down Expand Up @@ -115,9 +115,9 @@ ext {
}

publishing.publications.withType(MavenPublication).configureEach {
pom.withXml {
asNode().appendNode('description', project.project_description)
}
// pom.withXml {
// asNode().appendNode('description', project.project_description)
// }
}

pluginBundle {
Expand Down
2 changes: 1 addition & 1 deletion jvm/src/gradleTest/complex-jvm-setup/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repositories {

asciidoctorj {
modules {
diagram.version '1.5.16'
diagram.version '1.5.16'
}
logLevel 'INFO'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
id 'groovy'
}


dependencies {
compileOnly "org.codehaus.groovy:groovy:${System.getProperty('GROOVY_VERSION')}"
compileOnly "org.asciidoctor:asciidoctorj:${System.getProperty('ASCIIDOCTORJ_VERSION')}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ExtensionsFunctionalSpec extends FunctionalSpecification {
void 'Extension can be applied via closure (#model)'() {
given:
getBuildFile(
model.processMode, model.version, """
model.processMode, model.version, """
asciidoctor {
asciidoctorj {
docExtensions {
Expand Down Expand Up @@ -84,12 +84,95 @@ class ExtensionsFunctionalSpec extends FunctionalSpecification {
model << AsciidoctorjVersionProcessModeGenerator.get()
}
@SuppressWarnings('GStringExpressionWithinString')
void 'Extension can be applied via closure (Gradle #gradle)'() {
given:
final model = AsciidoctorjVersionProcessModeGenerator.get().first()
getBuildFile(
model.processMode, model.version, """
asciidoctor {
asciidoctorj {
docExtensions {
block(name: "BIG", contexts: [":paragraph"]) {
parent, reader, attributes ->
def upperLines = reader.readLines()*.toUpperCase()
.inject("") {a, b -> a + '\\\\n' + b}
createBlock(parent, "paragraph", [upperLines], attributes, [:])
}
block("small") {
parent, reader, attributes ->
def lowerLines = reader.readLines()*.toLowerCase()
.inject("") {a, b -> a + '\\\\n' + b}
createBlock(parent, "paragraph", [lowerLines], attributes, [:])
}
}
}
}
""".stripIndent())
GradleRunner runner = getGradleRunner(DEFAULT_ARGS).withGradleVersion(gradle)
when:
runner.build()
File resultFile = new File(buildDir, "docs/asciidoc/${ASCIIDOC_INLINE_EXTENSIONS_FILE.replaceFirst('asciidoc', 'html')}")
then: 'content is generated as HTML and XML'
resultFile.exists()
resultFile.text.contains('WRITE THIS IN UPPERCASE')
resultFile.text.contains('and write this in lowercase')
where:
gradle << ['8.3', '8.1.1', '7.6.1', '7.0.2']
}
@SuppressWarnings('GStringExpressionWithinString')
void 'Extension cannot be applied via closure if Gradle #gradle'() {
given:
final model = AsciidoctorjVersionProcessModeGenerator.get().first()
getBuildFile(
model.processMode, model.version, """
asciidoctor {
asciidoctorj {
docExtensions {
block(name: "BIG", contexts: [":paragraph"]) {
parent, reader, attributes ->
def upperLines = reader.readLines()*.toUpperCase()
.inject("") {a, b -> a + '\\\\n' + b}
createBlock(parent, "paragraph", [upperLines], attributes, [:])
}
block("small") {
parent, reader, attributes ->
def lowerLines = reader.readLines()*.toLowerCase()
.inject("") {a, b -> a + '\\\\n' + b}
createBlock(parent, "paragraph", [lowerLines], attributes, [:])
}
}
}
}
""".stripIndent())
GradleRunner runner = getGradleRunner(DEFAULT_ARGS).withGradleVersion(gradle)
when:
final result = runner.buildAndFail()
then:
result.output.contains('Closures are not supported on Gradle 8.4+ due to Gradle instrumentation issues. Place the content in a string or load from it from a file instead.')
where:
gradle << ['8.4', '8.5']
}
@Unroll
@Timeout(value = 90)
void 'Extension can be applied from a string (#model)'() {
given:
getBuildFile(
model.processMode, model.version, """
model.processMode, model.version, """
asciidoctor {
asciidoctorj {
Expand Down Expand Up @@ -132,12 +215,61 @@ block('small') {
model << AsciidoctorjVersionProcessModeGenerator.get()
}
@Unroll
@Timeout(value = 90)
void 'Extension can be applied from a string (Gradle #gradle)'() {
given:
final model = AsciidoctorjVersionProcessModeGenerator.get().first()
getBuildFile(
model.processMode, model.version, """
asciidoctor {
asciidoctorj {
docExtensions '''
block(name: 'BIG', contexts: [':paragraph']) {
parent, reader, attributes ->
def upperLines = reader.readLines()
.collect {it.toUpperCase()}
.inject('') {a, b -> "\${a}\\\n\${b}"}

createBlock(parent, "paragraph", [upperLines], attributes, [:])
}
block('small') {
parent, reader, attributes ->
def lowerLines = reader.readLines()
.collect {it.toLowerCase()}
.inject('') {a, b -> "\${a}\\\n\${b}"}

createBlock(parent, 'paragraph', [lowerLines], attributes, [:])
}
'''
}
}
""")
GradleRunner runner = getGradleRunner(DEFAULT_ARGS)
if (model.processMode != 'JAVA_EXEC') {
runner.withDebug(false)
}
when:
runner.build()
File resultFile = new File(buildDir, "docs/asciidoc/${ASCIIDOC_INLINE_EXTENSIONS_FILE.replaceFirst('asciidoc', 'html')}")
then: 'content is generated as HTML and XML'
resultFile.exists()
resultFile.text.contains('WRITE THIS IN UPPERCASE')
resultFile.text.contains('and write this in lowercase')
where:
gradle << ['8.4', '8.3', '8.1.1', '7.6.1', '7.0.2']
}
@Timeout(value = 90)
@Unroll
void 'Extension can be applied from file (#model)'() {
given:
getBuildFile(
model.processMode, model.version, """
model.processMode, model.version, """
asciidoctor {
asciidoctorj {
docExtensions file('src/docs/asciidoc/blockMacro.groovy')
Expand Down Expand Up @@ -170,7 +302,7 @@ asciidoctor {
given: 'A build file that declares extensions'
getBuildFile(
model.processMode, model.version, '''
model.processMode, model.version, '''
asciidoctorj {
docExtensions {
postprocessor { document, output ->
Expand Down Expand Up @@ -208,7 +340,7 @@ asciidoctor {
void 'Fail build if extension fails to compile (#model)'() {
given:
getBuildFile(
model.processMode, model.version, """
model.processMode, model.version, """
asciidoctor {
asciidoctorj {
docExtensions '''
Expand Down Expand Up @@ -241,22 +373,22 @@ asciidoctor {
given:
String extDSL = '''asciidoctorj.docExtensions file('src/docs/asciidoc/blockMacro.groovy')'''
getBuildFile(
processMode, version, """
processMode, version, """
${extScope == GLOBAL ? extDSL : ''}
asciidoctor {
${extScope == LOCAL ? extDSL : ''}
}
""",
verScope == GLOBAL
verScope == GLOBAL
)
GradleRunner runner = getGradleRunner(DEFAULT_ARGS)
when:
runner.build()
File resultFile = new File(
buildDir,
'docs/asciidoc/' + ASCIIDOC_INLINE_EXTENSIONS_FILE.replaceFirst('asciidoc', 'html')
buildDir,
'docs/asciidoc/' + ASCIIDOC_INLINE_EXTENSIONS_FILE.replaceFirst('asciidoc', 'html')
)
then: 'content is generated as HTML and XML'
Expand All @@ -270,14 +402,14 @@ asciidoctor {
and:
extScope | verScope
LOCAL | LOCAL
LOCAL | GLOBAL
GLOBAL | LOCAL
GLOBAL | GLOBAL
LOCAL | LOCAL
LOCAL | GLOBAL
GLOBAL | LOCAL
GLOBAL | GLOBAL
}
File getBuildFile(
final String processMode, final String version, final String extraContent, boolean configureGlobally = false) {
final String processMode, final String version, final String extraContent, boolean configureGlobally = false) {
String versionConfig = """
asciidoctorj {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package org.asciidoctor.gradle.internal
import org.ysb33r.grolifant.api.remote.worker.SerializableWorkerAppParameters

/**
* Parameters for serializing ASciidoctor jobs to workers.
* Parameters for serializing Asciidoctor jobs to workers.
*
* @author Schalk W> Cronjé
*
* @since 4.0
*/
class AsciidoctorWorkerParameters implements SerializableWorkerAppParameters {
private static final long serialVersionUID = 1251694026305095019

/**
* Whether to attempt conversions in parallel inside the worker.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import java.util.regex.Pattern
@SuppressWarnings(['CloneableWithoutClone'])
@TupleConstructor
class ExecutorConfiguration implements Serializable, Cloneable {
private static final long serialVersionUID = -2024L
File sourceDir
File outputDir
File projectDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ package org.asciidoctor.gradle.internal

import groovy.transform.CompileStatic

/** Contains a number of executor configurations.
/**
* Contains a number of executor configurations.
*
* @since 2.0.0
*
* @author Schalk W. Cronjé
*/
@CompileStatic
class ExecutorConfigurationContainer implements Serializable {
private static final long serialVersionUID = -2024L
final List<ExecutorConfiguration> configurations

ExecutorConfigurationContainer(Iterable<ExecutorConfiguration> list) {
Expand Down
Loading

0 comments on commit 52fcf4a

Please sign in to comment.