- Notifications
You must be signed in to change notification settings - Fork0
Comment pre-processor for Java development
License
WiIIiam278/PreProcessor
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A Gradle plugin for preprocessing code. Based onToCraft's PreProcessor which is based onRePlayMod's PreProcessor.
Here's how you can apply the plugin in abuild.gradle
file:
plugins { id'java' id'net.william278.preprocessor' version'1.0'}
The Kotlin implementation for abuild.gradle.kts
file looks similar:
plugins { id("org.jetbrains.kotlin.jvm") version"2.0.0" id("net.william278.preprocessor") version"1.0"}
Now, you'll need to define variables so the preprocessor can evaluate the if-statements.
preprocess { vars.put("a","1")}
This sets the value of the variablea
to1
. You can define any value object you want as long as the key is aString
.
By default, the plugin registers the following tasks per source set:
preProcessJava
&applyPreProcessJava
, if the source set contains java sourcespreProcessKotlin
&applyPreProcessKotlin
, if the source set contains kotlin sourcespreProcessResources
&applyPreProcessResources
, if the source set contains resources
It automatically adapts the taskscompileJava
,compileKotlin
andprocessResources
to use the outputs of the above tasks.TheapplyPreProcess*
-tasks cause the plugin to update the sources and comment lines with//$$
that won't run since their if-condition isfalse
.This isnot required for the plugin to preprocess, but for better code readability.{:.note}
The plugin also registers one task simply calledapplyPreProcess
, which automatically applies everyapplyPreProcess*
task for every source set in this build file.
An example Java test class looks like this;
packagetest;classTest {publicstaticvoidmain(String...args) {//#if a//$$ System.out.println("Test succeeded.");//#elseSystem.out.println("Test failed.");//#endif }}
This will be preprocessed to the following, ifa
exists:
packagetest;classTest {publicstaticvoidmain(String...args) {//#if aSystem.out.println("Test succeeded.");//#else//$$ System.out.println("Test failed.");//#endif }}
You'll notice the//#if
,//#$$
,//#else
and//#endif
. There is also a//#elseif
keyword.These will work as if-statements. Every if-statementmust start with//#if
andmust end with//#endif
.
You can change these keywords with the followingbuild.gradle
structure:
preprocess { keywords.put("json",newKeywords("//#if","//#elseif","//#endif","//\$\$"))}
This will add custom keywords for every file ending withjson
.
Now, this condition istrue
, ifa
exists and is not0
ornull
:
//#if a
You can also chain conditions:
//#if a && b || c
Of course you can also compare integer values with==
,!=
,>=
,<=
,>
and<
.
//#if a == 1
About
Comment pre-processor for Java development
Topics
Resources
License
Stars
Watchers
Forks
Languages
- Java100.0%