diff --git a/build.gradle b/build.gradle index d439ff2..eb667d3 100644 --- a/build.gradle +++ b/build.gradle @@ -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' @@ -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' } @@ -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"]) } } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ae45383..daee2e0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/java/com/github/hexomod/macro/PreprocessorExtension.java b/src/main/java/com/github/hexomod/macro/PreprocessorExtension.java index 5617aef..5731417 100644 --- a/src/main/java/com/github/hexomod/macro/PreprocessorExtension.java +++ b/src/main/java/com/github/hexomod/macro/PreprocessorExtension.java @@ -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; @@ -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"); diff --git a/src/main/java/com/github/hexomod/macro/PreprocessorInPlaceTask.java b/src/main/java/com/github/hexomod/macro/PreprocessorInPlaceTask.java index 852f577..6bc845a 100644 --- a/src/main/java/com/github/hexomod/macro/PreprocessorInPlaceTask.java +++ b/src/main/java/com/github/hexomod/macro/PreprocessorInPlaceTask.java @@ -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; @@ -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); } } diff --git a/src/main/java/com/github/hexomod/macro/PreprocessorPlugin.java b/src/main/java/com/github/hexomod/macro/PreprocessorPlugin.java index 48883bb..d655390 100644 --- a/src/main/java/com/github/hexomod/macro/PreprocessorPlugin.java +++ b/src/main/java/com/github/hexomod/macro/PreprocessorPlugin.java @@ -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; @@ -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) { @@ -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) { @@ -117,7 +121,7 @@ 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 @@ -125,7 +129,7 @@ private void RegisterPreprocessors(final Project project, final PreprocessorExte 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); } } diff --git a/src/main/java/com/github/hexomod/macro/PreprocessorTask.java b/src/main/java/com/github/hexomod/macro/PreprocessorTask.java index 6198c3f..d7a5901 100644 --- a/src/main/java/com/github/hexomod/macro/PreprocessorTask.java +++ b/src/main/java/com/github/hexomod/macro/PreprocessorTask.java @@ -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; @@ -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; @@ -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; @@ -68,8 +67,8 @@ public PreprocessorTask() { this.getOutputs().upToDateWhen(new Spec() { @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; } }); diff --git a/src/main/java/com/github/hexomod/macro/util/StringUtils.java b/src/main/java/com/github/hexomod/macro/util/StringUtils.java new file mode 100644 index 0000000..fb445ed --- /dev/null +++ b/src/main/java/com/github/hexomod/macro/util/StringUtils.java @@ -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(); + } +} +