Skip to content

Commit

Permalink
Merge 0ea263c into 621b867
Browse files Browse the repository at this point in the history
  • Loading branch information
ysb33r committed Nov 4, 2018
2 parents 621b867 + 0ea263c commit 15aa73f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.asciidoctor.gradle

import org.apache.commons.io.FileUtils
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
Expand Down Expand Up @@ -44,7 +45,6 @@ class AsciidoctorFunctionalSpec extends Specification {
pluginClasspath = pluginClasspathResource.readLines().collect { new File(it) }
}

@SuppressWarnings('MethodName')
def "Should do nothing with an empty project"() {
given: "A minimal build file"
def buildFile = testProjectDir.newFile("build.gradle")
Expand All @@ -66,7 +66,6 @@ class AsciidoctorFunctionalSpec extends Specification {
result.task(":asciidoctor").outcome == TaskOutcome.NO_SOURCE
}

@SuppressWarnings('MethodName')
def "Should build normally for a standard project"() {
given: "A minimal build file"
def buildFile = testProjectDir.newFile("build.gradle")
Expand All @@ -81,19 +80,49 @@ class AsciidoctorFunctionalSpec extends Specification {
final buildDir = new File(testProjectDir.root, "build")

when:
final result = GradleRunner.create()
final BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments("asciidoctor")
.withPluginClasspath(pluginClasspath)
.forwardOutput()
.build()

then:
result.task(":asciidoctor").outcome == TaskOutcome.SUCCESS
new File(buildDir, "asciidoc/html5/sample.html").exists()
new File(buildDir, "asciidoc/html5/subdir/sample2.html").exists()
result.output.contains('Use --warning-mode=all to see list of potential affected files')
}

def "Should print warning message for legacy attributes"() {
given: "A minimal build file"
def buildFile = testProjectDir.newFile("build.gradle")
buildFile << """
plugins {
id "org.asciidoctor.convert"
}
"""

and: "Some source files"
FileUtils.copyDirectory(new File(TEST_PROJECTS_DIR, 'normal'), testProjectDir.root)
final buildDir = new File(testProjectDir.root, 'build')

when:
final BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments('asciidoctor', '--warning-mode=all')
.withPluginClasspath(pluginClasspath)
.forwardOutput()
.build()
final String output = result.output

then:
result.task(":asciidoctor").outcome == TaskOutcome.SUCCESS
new File(buildDir, "asciidoc/html5/sample.html").exists()
output.contains('It seems that you may be using implicit attributes ')
output.contains('sample.asciidoc')
}

@SuppressWarnings('MethodName')
def "Task should be up-to-date when executed a second time"() {
given: "A minimal build file"
def buildFile = testProjectDir.newFile("build.gradle")
Expand Down
2 changes: 2 additions & 0 deletions src/intTest/projects/normal/src/docs/asciidoc/sample.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ NOTE: This is test, only a test.
* Item 2
* Item 3

A legacy {projectdir} attribute.

44 changes: 44 additions & 0 deletions src/main/groovy/org/asciidoctor/gradle/AsciidoctorTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.gradle.util.CollectionUtils
import org.gradle.util.GradleVersion

import static org.asciidoctor.gradle.AsciidoctorUtils.getClassLocation
import static org.asciidoctor.gradle.AsciidoctorUtils.getRelativePath
import static org.asciidoctor.gradle.backported.JavaExecUtils.getJavaExecClasspath

/**
Expand All @@ -57,6 +58,8 @@ import static org.asciidoctor.gradle.backported.JavaExecUtils.getJavaExecClasspa
@SuppressWarnings(['MethodCount', 'Instanceof'])
class AsciidoctorTask extends DefaultTask {
static final boolean GRADLE_4_OR_BETTER = GradleVersion.current() >= GradleVersion.version('4.0')
static final boolean GRADLE_4_5_OR_BETTER = GradleVersion.current() >= GradleVersion.version('4.5')

static final boolean IS_WINDOWS = System.getProperty('os.name').contains('Windows')
static final String DOUBLE_BACKLASH = '\\\\'
static final String BACKLASH = '\\'
Expand Down Expand Up @@ -598,6 +601,8 @@ class AsciidoctorTask extends DefaultTask {

File output = outputDir

scanForLegacyAttributes()

ExecutorConfigurationContainer ecc = new ExecutorConfigurationContainer(activeBackends().collect { backend ->
new ExecutorConfiguration(
sourceDir: sourceDir,
Expand Down Expand Up @@ -746,6 +751,45 @@ class AsciidoctorTask extends DefaultTask {
}
}

private void scanForLegacyAttributes() {
FileTree ft
if(this.sourceDocumentPattern) {
ft = project.fileTree(sourceDir).matching(this.sourceDocumentPattern) +
project.fileTree(sourceDir).matching(defaultSourceDocumentPattern)
} else {
ft = project.fileTree(sourceDir).matching(defaultSourceDocumentPattern)
}

Set<File> hits = ft.files.findAll { File f ->
String content = f.text
content.contains('{projectdir}') || content.contains('{rootdir}')
}

if(!hits.empty) {
logger.warn 'It seems that you may be using implicit attributes `projectdir` and/or `rootdir` in your documents. These are deprecated and will no longer be set in 2.0. Please migrate your documents to use `gradle-projectdir` and `gradle-rootdir` instead.'

if(dumpLegacyFileList) {
File base = sourceDir
String finalList = hits.collect { File f ->
getRelativePath(f,base)
}.join(', ')

logger.warn "Potentially affected source files are: ${finalList}"
} else if (GRADLE_4_5_OR_BETTER) {
logger.warn 'Use --warning-mode=all to see list of potential affected files'
}
}
}

@CompileDynamic
boolean isDumpLegacyFileList() {
if (GRADLE_4_5_OR_BETTER) {
project.gradle.startParameter.warningMode.toString().toLowerCase() == 'all'
} else {
true
}
}

@SuppressWarnings('DuplicateStringLiteral')
@SuppressWarnings('DuplicateNumberLiteral')
private static Map coerceLegacyAttributeFormats(Object attributes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class AsciidoctorJavaExec {

newAttrs['gradle-projectdir'] = getRelativePath(projectDir, file.parentFile)
newAttrs['gradle-rootdir'] = getRelativePath(rootDir, file.parentFile)
println 'Implicit attributes projectdir and rootdir are deprecated and will no longer be set in 2.0. Please migrate your documents to use gradle-projectdir and gradle-rootdir instead.'
mergedOptions[Options.ATTRIBUTES] = newAttrs
}

Expand Down

0 comments on commit 15aa73f

Please sign in to comment.