Created
January 7, 2020 01:59
-
-
Save mworzala/43c24ff574e15d94c1186167af5cc622 to your computer and use it in GitHub Desktop.
Gradle File Preprocessor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This allows configuration in the Gradle file to skip parts of the code. Note: The
preprocessor
task relies onclean
which results in increased build time for larger projects. A better solution is something to look into in the future.