Skip to content

Commit

Permalink
Fix incorrect usage of Groovy properties / Create doctreeCacheDirecto…
Browse files Browse the repository at this point in the history
…ry if necessary
  • Loading branch information
trustin committed Apr 4, 2020
1 parent 568e84d commit 24fadf3
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 30 deletions.
120 changes: 90 additions & 30 deletions src/main/groovy/kr/motd/gradle/sphinx/gradle/SphinxTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import java.util.concurrent.Callable

class SphinxTask extends DefaultTask {

private def binaryUrl = { SphinxRunner.DEFAULT_BINARY_URL }
private def binaryCacheDir = { "${project.gradle.gradleUserHomeDir}/caches/sphinx-binary" }
private def environments = { Collections.emptyMap() }
def binaryUrl = { SphinxRunner.DEFAULT_BINARY_URL }
def binaryCacheDir = { "${project.gradle.gradleUserHomeDir}/caches/sphinx-binary" }
def environments = { Collections.emptyMap() }
private final def additionalEnvironments = new HashMap<String, String>()
private def dotBinary = null
private def sourceDirectory = {
def dotBinary = null
def sourceDirectory = {
"${project.projectDir}${File.separator}src${File.separator}site${File.separator}sphinx"
}
private def outputDirectory = { "${project.buildDir}${File.separator}site" }
private def doctreeCacheDirectory = { "${project.buildDir}${File.separator}site${File.separator}.doctrees" }
private def builder = { "html" }
private def tags = { Collections.emptyList() }
def outputDirectory = { "${project.buildDir}${File.separator}site" }
def doctreeCacheDirectory = { "${project.buildDir}${File.separator}site${File.separator}.doctrees" }
def builder = { "html" }
def tags = { Collections.emptyList() }
private final def additionalTags = new ArrayList<String>()
private def verbose = { true }
private def traceback = { true }
private def force = { false }
private def warningsAsErrors = { false }
private def skip = { false }
private def useDoctreeCache = { false }
def verbose = { true }
def traceback = { true }
def force = { false }
def warningsAsErrors = { false }
def skip = { false }
def useDoctreeCache = { false }

private static def unwrap(Object o) {
if (o instanceof Callable) {
Expand All @@ -48,14 +48,22 @@ class SphinxTask extends DefaultTask {
this.binaryUrl = binaryUrl
}

void binaryUrl(Object binaryUrl) {
setBinaryUrl(binaryUrl);
}

@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
File getBinaryCacheDir() {
return project.file(binaryCacheDir).getCanonicalFile()
}

void setBinaryCacheDir(Object binaryCacheDir) {
this.binaryCacheDir = binaryCacheDir;
this.binaryCacheDir = binaryCacheDir
}

void binaryCacheDir(Object binaryCacheDir) {
setBinaryCacheDir(binaryCacheDir);
}

@Input
Expand Down Expand Up @@ -95,6 +103,10 @@ class SphinxTask extends DefaultTask {
this.dotBinary = dotBinary
}

void dotBinary(Object dotBinary) {
setDotBinary(dotBinary);
}

@SkipWhenEmpty
@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
Expand All @@ -106,6 +118,10 @@ class SphinxTask extends DefaultTask {
this.sourceDirectory = sourceDirectory
}

void sourceDirectory(Object sourceDirectory) {
setSourceDirectory(sourceDirectory);
}

@OutputDirectory
File getOutputDirectory() {
project.file(outputDirectory).getCanonicalFile()
Expand All @@ -115,16 +131,31 @@ class SphinxTask extends DefaultTask {
this.outputDirectory = outputDirectory
}

void outputDirectory(Object outputDirectory) {
setOutputDirectory(outputDirectory);
}

@Optional
@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
File getDoctreeCacheDirectory() {
project.file(doctreeCacheDirectory).getCanonicalFile()
if (getUseDoctreeCache()) {
def doctreeCacheDirectory = project.file(this.doctreeCacheDirectory)
doctreeCacheDirectory.mkdirs()
return doctreeCacheDirectory.getCanonicalFile()
} else {
return null
}
}

void setDoctreeCacheDirectory(Object doctreeCacheDirectory) {
this.doctreeCacheDirectory = doctreeCacheDirectory
}

void doctreeCacheDirectory(Object doctreeCacheDirectory) {
setDoctreeCacheDirectory(doctreeCacheDirectory);
}

@Input
String getBuilder() {
return String.valueOf(unwrap(builder))
Expand All @@ -134,6 +165,10 @@ class SphinxTask extends DefaultTask {
this.builder = builder
}

void builder(Object builder) {
setBuilder(builder);
}

@Input
List<String> getTags() {
def unwrapped = unwrap(tags)
Expand All @@ -160,62 +195,86 @@ class SphinxTask extends DefaultTask {
}

@Input
boolean isVerbose() {
boolean getVerbose() {
(verbose instanceof Boolean ? verbose : verbose()).asBoolean()
}

void setVerbose(Object verbose) {
this.verbose = verbose
}

void verbose(Object verbose) {
this.verbose = verbose;
}

@Input
boolean isTraceback() {
boolean getTraceback() {
(traceback instanceof Boolean ? traceback : verbose()).asBoolean()
}

void setTraceback(Object traceback) {
this.traceback = traceback
}

void traceback(Object traceback) {
setTraceback(traceback);
}

@Input
boolean isForce() {
boolean getForce() {
(force instanceof Boolean ? force : force()).asBoolean()
}

void setForce(Object force) {
this.force = force
}

void force(Object force) {
setForce(force);
}

@Input
boolean isWarningsAsErrors() {
boolean getWarningsAsErrors() {
(warningsAsErrors instanceof Boolean ? warningsAsErrors : warningsAsErrors()).asBoolean()
}

void setWarningsAsErrors(Object warningsAsErrors) {
this.warningsAsErrors = warningsAsErrors
}

void warningsAsErrors(Object warningsAsErrors) {
setWarningsAsErrors(warningsAsErrors);
}

@Input
boolean isUseDoctreeCache() {
boolean getUseDoctreeCache() {
(useDoctreeCache instanceof Boolean ? useDoctreeCache : useDoctreeCache()).asBoolean()
}

void setUseDoctreeCache(Object useDoctreeCache) {
this.useDoctreeCache = useDoctreeCache
}

void useDoctreeCache(Object useDoctreeCache) {
setUseDoctreeCache(useDoctreeCache)
}

@Input
boolean isSkip() {
boolean getSkip() {
(skip instanceof Boolean ? skip : skip()).asBoolean()
}

void setSkip(Object skip) {
this.skip = skip
}

void skip(Object skip) {
setSkip(skip)
}

@TaskAction
def run() {
if (isSkip()) {
if (getSkip()) {
logger.info("Skipping Sphinx execution.")
return
}
Expand All @@ -237,28 +296,29 @@ class SphinxTask extends DefaultTask {
private List<String> getSphinxRunnerCmdLine() {
List<String> args = []

if (isVerbose()) {
if (getVerbose()) {
args.add('-v')
} else {
args.add('-Q')
}

if (isTraceback()) {
if (getTraceback()) {
args.add('-T')
}

if (isWarningsAsErrors()) {
if (getWarningsAsErrors()) {
args.add('-W')
}

if (isForce()) {
if (getForce()) {
args.add('-a')
args.add('-E')
}

if (isUseDoctreeCache()) {
def doctreeCacheDirectory = getDoctreeCacheDirectory();
if (doctreeCacheDirectory != null) {
args.add('-d')
args.add(getDoctreeCacheDirectory().getPath())
args.add(doctreeCacheDirectory.getPath())
}

for (String tag : getTags()) {
Expand Down
20 changes: 20 additions & 0 deletions src/test/groovy/kr/motd/gradle/sphinx/SphinxTaskTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ class SphinxTaskTest {
assert task.builder == 'html'
assert task.tags.isEmpty()
assert task.verbose
assert task.traceback
assert !task.force
assert !task.skip
assert !task.warningsAsErrors
assert !task.useDoctreeCache
assert task.doctreeCacheDirectory == null
}

@Test
Expand All @@ -39,7 +44,11 @@ class SphinxTaskTest {
task.builder 'mo'
task.tags 'foo', 'bar'
task.verbose false
task.traceback false
task.force true
task.skip true
task.warningsAsErrors true
task.useDoctreeCache true

assert "${task.getSourceDirectory()}" ==
"${project.projectDir}${File.separator}src${File.separator}my${File.separator}sphinx"
Expand All @@ -49,7 +58,18 @@ class SphinxTaskTest {
assert task.builder == 'mo'
assert task.tags == [ "foo", "bar" ]
assert !task.verbose
assert !task.traceback
assert task.force
assert task.skip
assert task.warningsAsErrors
assert task.useDoctreeCache
assert "${task.doctreeCacheDirectory}" ==
"${project.buildDir}${File.separator}site${File.separator}.doctrees"

task.doctreeCacheDirectory "${project.buildDir}/my/doctree-cache"
assert task.doctreeCacheDirectory.isDirectory()
assert "${task.doctreeCacheDirectory}" ==
"${project.buildDir}${File.separator}my${File.separator}doctree-cache"
}

@Test
Expand Down

0 comments on commit 24fadf3

Please sign in to comment.