Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ plugins {
id 'java'
id 'java-gradle-plugin'
id 'maven-publish'
id 'com.gradle.plugin-publish' version '0.20.0'
id "com.github.hierynomus.license" version "0.15.0"
id "com.gradle.plugin-publish" version "2.0.0"
id "com.github.hierynomus.license" version "0.16.1"
}

group = 'com.github.hexomod'
Expand All @@ -13,13 +13,15 @@ version = '0.9'
// Minimum java version
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

// define minimum java version and encoding
tasks.withType(JavaCompile) {
// Minimum java version
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
// Make sure to use UTF-8
options.release.set(17)
options.encoding = 'UTF-8'
}

Expand Down Expand Up @@ -55,20 +57,16 @@ license {
mapping { java = 'SLASHSTAR_STYLE' }
}

// plugin definition
pluginBundle {
website = 'https://github.com/HexoMod-tools/gradle-macro-preprocessor-plugin'
vcsUrl = 'https://github.com/HexoMod-tools/gradle-macro-preprocessor-plugin.git'
description = 'A simple java macro preprocessor plugin'
tags = ['macro', 'preprocessor']
}

gradlePlugin {
website.set("https://github.com/HexoMod-tools/gradle-macro-preprocessor-plugin")
vcsUrl.set("https://github.com/HexoMod-tools/gradle-macro-preprocessor-plugin.git")
plugins {
gradleMacroPreprocessorPlugin {
id = 'com.github.hexomod.macro.preprocessor'
displayName = rootProject.name
implementationClass = 'com.github.hexomod.macro.PreprocessorPlugin'
description = "A simple java macro preprocessor plugin"
tags.set(["macro", "preprocessor"])
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Empty file modified gradlew
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
import groovy.lang.Closure;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.internal.Actions;
import org.gradle.util.ConfigureUtil;
import org.gradle.util.internal.ConfigureUtil;

import javax.inject.Inject;
import java.io.File;
Expand Down Expand Up @@ -83,7 +82,7 @@ public class PreprocessorExtension extends SourceType {
* @param project the project
*/
@Inject
public PreprocessorExtension(ProjectInternal project) {
public PreprocessorExtension(Project project) {
this.project = project;
this.vars = new LinkedHashMap<>();
this.processDir = new File(project.getBuildDir(), "preprocessor/macro");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
Expand Down Expand Up @@ -54,7 +54,7 @@ public PreprocessorInPlaceTask() {
public void process() throws IOException {
extension.log("Processing files ...");
// Loop through all SourceSets
for (SourceSet sourceSet : project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets()) {
for (SourceSet sourceSet : project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets()) {
processSourceSet(sourceSet);
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/github/hexomod/macro/PreprocessorPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@

package com.github.hexomod.macro;

import org.gradle.api.*;
import com.github.hexomod.macro.util.StringUtils;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.ProjectConfigurationException;
import org.gradle.api.Task;
import org.gradle.api.UnknownTaskException;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.language.jvm.tasks.ProcessResources;
import org.gradle.util.GUtil;

import java.io.File;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -94,7 +98,7 @@ private PreprocessorInPlaceTask RegisterInPlaceTask(final Project project, final

private void configureInPlacePreprocessor(final Project project, final PreprocessorExtension extension, final PreprocessorInPlaceTask inPlaceTask) {
// Get all sourceSet to create one preprocessor per sourceSet
final SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
final SourceSetContainer sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();

// Get compile task from SourceSet
for (SourceSet sourceSet : sourceSets) {
Expand All @@ -108,7 +112,7 @@ private void configureInPlacePreprocessor(final Project project, final Preproces

private void RegisterPreprocessors(final Project project, final PreprocessorExtension extension, final PreprocessorInPlaceTask inPlaceTask) {
// Get all sourceSet to create one preprocessor per sourceSet
final SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
final SourceSetContainer sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();

// Register each preprocessor
for (SourceSet sourceSet : sourceSets) {
Expand All @@ -117,15 +121,15 @@ private void RegisterPreprocessors(final Project project, final PreprocessorExte
final JavaCompile compileTask = (JavaCompile) project.getTasks().findByName(sourceSet.getCompileJavaTaskName());
PreprocessorTask preprocessor = RegisterJavaPreprocessor(project, extension, sourceSet, compileTask).get();
makeDependsOn(preprocessor, inPlaceTask);
makeDependsOn(project, preprocessor, "replacePreprocessor" + (sourceSet.getName() == "main" ? "" : GUtil.toCamelCase(sourceSet.getName())) + "Java");
makeDependsOn(project, preprocessor, "replacePreprocessor" + (sourceSet.getName() == "main" ? "" : StringUtils.toCamelCase(sourceSet.getName())) + "Java");
makeDependsOn(compileTask, preprocessor);
}
// Resources files
if (extension.getEnable() && extension.getResources().getEnable()) {
final ProcessResources resourceTask = (ProcessResources) project.getTasks().findByName(sourceSet.getProcessResourcesTaskName());
PreprocessorTask preprocessor = RegisterResourcesPreprocessor(project, extension, sourceSet, resourceTask).get();
makeDependsOn(preprocessor, inPlaceTask);
makeDependsOn(project, preprocessor, "replacePreprocessor" + (sourceSet.getName() == "main" ? "" : GUtil.toCamelCase(sourceSet.getName())) + "Resource");
makeDependsOn(project, preprocessor, "replacePreprocessor" + (sourceSet.getName() == "main" ? "" : StringUtils.toCamelCase(sourceSet.getName())) + "Resource");
makeDependsOn(resourceTask, preprocessor);
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/github/hexomod/macro/PreprocessorTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
package com.github.hexomod.macro;


import com.github.hexomod.macro.util.StringUtils;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.SourceDirectorySet;
Expand All @@ -32,7 +32,6 @@
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
import org.gradle.language.jvm.tasks.ProcessResources;
import org.gradle.util.GUtil;

import javax.inject.Inject;
import java.io.File;
Expand All @@ -42,18 +41,18 @@

@SuppressWarnings({"WeakerAccess", "unused"})
@CacheableTask
public class PreprocessorTask extends ProcessResources {
public abstract class PreprocessorTask extends ProcessResources {

public static final String TASK_ID = "macroPreprocessor";
public static final String TASK_RESOURCE_SUFFIX = "Resource";
public static final String TASK_JAVA_SUFFIX = "Java";

public static String getResourceTaskName(SourceSet sourceSet) {
return TASK_ID + (sourceSet.getName() == "main" ? "" : GUtil.toCamelCase(sourceSet.getName())) + TASK_RESOURCE_SUFFIX;
return TASK_ID + (sourceSet.getName().equals("main") ? "" : StringUtils.toCamelCase(sourceSet.getName())) + TASK_RESOURCE_SUFFIX;
}

public static String getJavaTaskName(SourceSet sourceSet) {
return TASK_ID + (sourceSet.getName() == "main" ? "" : GUtil.toCamelCase(sourceSet.getName())) + TASK_JAVA_SUFFIX;
return TASK_ID + (sourceSet.getName().equals("main") ? "" : StringUtils.toCamelCase(sourceSet.getName())) + TASK_JAVA_SUFFIX;
}

private final Project project;
Expand All @@ -68,8 +67,8 @@ public PreprocessorTask() {
this.getOutputs().upToDateWhen(new Spec<Task>() {
@Override
public boolean isSatisfiedBy(Task element) {
boolean java =sourceSet.getJava().getSrcDirs().contains(getDestinationDir());
boolean resources =sourceSet.getResources().getSrcDirs().contains(getDestinationDir());
boolean java = sourceSet.getJava().getSrcDirs().contains(getDestinationDir());
boolean resources = sourceSet.getResources().getSrcDirs().contains(getDestinationDir());
return java && resources;
}
});
Expand Down
109 changes: 109 additions & 0 deletions src/main/java/com/github/hexomod/macro/util/StringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.github.hexomod.macro.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

private static final Pattern WORD_SEPARATOR = Pattern.compile("\\W+");
private static final Pattern UPPER_LOWER = Pattern.compile("(?m)([A-Z]*)([a-z0-9]*)");

public static String toCamelCase(CharSequence string) {
return toCamelCase(string, false);
}

public static String toLowerCamelCase(CharSequence string) {
return toCamelCase(string, true);
}

private static String toCamelCase(CharSequence string, boolean lower) {
if (string == null) {
return null;
} else {
StringBuilder builder = new StringBuilder();
Matcher matcher = WORD_SEPARATOR.matcher(string);
int pos = 0;
boolean first = true;

while(matcher.find()) {
String chunk = string.subSequence(pos, matcher.start()).toString();
pos = matcher.end();
if (!chunk.isEmpty()) {
if (lower && first) {
chunk = uncapitalize(chunk);
first = false;
} else {
chunk = capitalize(chunk);
}

builder.append(chunk);
}
}

String rest = string.subSequence(pos, string.length()).toString();
if (lower && first) {
rest = uncapitalize(rest);
} else {
rest = capitalize(rest);
}

builder.append(rest);
return builder.toString();
}
}

public static String capitalize(String str) {
int strLen = length(str);
if (strLen == 0) {
return str;
} else {
int firstCodepoint = str.codePointAt(0);
int newCodePoint = Character.toTitleCase(firstCodepoint);
if (firstCodepoint == newCodePoint) {
return str;
} else {
int[] newCodePoints = new int[strLen];
int outOffset = 0;
newCodePoints[outOffset++] = newCodePoint;

int codePoint;
for(int inOffset = Character.charCount(firstCodepoint); inOffset < strLen; inOffset += Character.charCount(codePoint)) {
codePoint = str.codePointAt(inOffset);
newCodePoints[outOffset++] = codePoint;
}

return new String(newCodePoints, 0, outOffset);
}
}
}

public static String uncapitalize(String str) {
int strLen = length(str);
if (strLen == 0) {
return str;
} else {
int firstCodePoint = str.codePointAt(0);
int newCodePoint = Character.toLowerCase(firstCodePoint);
if (firstCodePoint == newCodePoint) {
return str;
} else {
int[] newCodePoints = new int[strLen];
int outOffset = 0;
newCodePoints[outOffset++] = newCodePoint;

int codePoint;
for(int inOffset = Character.charCount(firstCodePoint); inOffset < strLen; inOffset += Character.charCount(codePoint)) {
codePoint = str.codePointAt(inOffset);
newCodePoints[outOffset++] = codePoint;
}

return new String(newCodePoints, 0, outOffset);
}
}
}

public static int length(CharSequence cs) {
return cs == null ? 0 : cs.length();
}
}