Skip to content

Instantly share code, notes, and snippets.

@mworzala
Created January 7, 2020 01:59
Show Gist options
  • Save mworzala/43c24ff574e15d94c1186167af5cc622 to your computer and use it in GitHub Desktop.
Save mworzala/43c24ff574e15d94c1186167af5cc622 to your computer and use it in GitHub Desktop.
Gradle File Preprocessor
ext {
skipFunc = true
skipPrint = true
}
task preprocessor(type: Copy) {
boolean inTag = false
from 'src/main/kotlin'
into "$buildDir/processed-src"
filter { line ->
String updated = line
try {
if (line.trim().startsWith("//PP.IF") && project.ext[line.replace("//PP.IF", "").trim()] as boolean)
inTag = true
} catch (MissingPropertyException ignored) { }
if (inTag) updated = null
if (line.trim().startsWith("//PP.ENDIF"))
inTag = false
return updated
}
}
preprocessor.dependsOn(clean)
['compileKotlin'].each {
tasks[it].doFirst {sourceSets.main.kotlin.srcDirs = ["$buildDir/processed-src"]}
tasks[it].dependsOn(preprocessor)
tasks[it].doLast {sourceSets.main.kotlin.srcDirs = ["src/main/kotlin"]}
}
package example
fun main() {
//PP.IF yes
println("This should print")
//PP.ENDIF
//PP.IF skipPrint
println("This should not.")
//PP.ENDIF
//PP.IF skipFunc
func()
//PP.ENDIF
}
//PP.IF skipFunc
fun func() {
println("I am a print from a function!")
}
//PP.ENDIF
//PP.ENDIF
@mworzala
Copy link
Author

mworzala commented Jan 7, 2020

This allows configuration in the Gradle file to skip parts of the code. Note: The preprocessor task relies on clean which results in increased build time for larger projects. A better solution is something to look into in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment