Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Comment pre-processor for Java development

License

NotificationsYou must be signed in to change notification settings

WiIIiam278/PreProcessor

 
 

Repository files navigation


A Gradle plugin for preprocessing code. Based onToCraft's PreProcessor which is based onRePlayMod's PreProcessor.

Build File

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.

Gradle Tasks

By default, the plugin registers the following tasks per source set:

  • preProcessJava &applyPreProcessJava, if the source set contains java sources
  • preProcessKotlin &applyPreProcessKotlin, if the source set contains kotlin sources
  • preProcessResources &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.

Code Example

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    }}

Keywords

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.

Possible Conditions

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%

[8]ページ先頭

©2009-2025 Movatter.jp