From 561fac70fd3dc3ebe1cfbc50146757495fb828d5 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sat, 8 Apr 2023 22:56:44 +0200 Subject: build: convert Gradle scripts to Kotlin Improves IDE support build scripts in IntelliJ. There is no Eclipse IDE support, but Eclipse didn't have support for Groovy either, so there is no degradation of functionality. --- .../java/tools/refinery/buildsrc/EclipseUtils.java | 72 ++++++++++++++++++++++ .../refinery/buildsrc/SonarPropertiesUtils.java | 44 +++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 buildSrc/src/main/java/tools/refinery/buildsrc/EclipseUtils.java create mode 100644 buildSrc/src/main/java/tools/refinery/buildsrc/SonarPropertiesUtils.java (limited to 'buildSrc/src/main/java') diff --git a/buildSrc/src/main/java/tools/refinery/buildsrc/EclipseUtils.java b/buildSrc/src/main/java/tools/refinery/buildsrc/EclipseUtils.java new file mode 100644 index 00000000..0014a35d --- /dev/null +++ b/buildSrc/src/main/java/tools/refinery/buildsrc/EclipseUtils.java @@ -0,0 +1,72 @@ +package tools.refinery.buildsrc; + +import groovy.lang.Closure; +import org.gradle.api.Action; +import org.gradle.plugins.ide.api.XmlFileContentMerger; +import org.gradle.plugins.ide.eclipse.model.AbstractClasspathEntry; +import org.gradle.plugins.ide.eclipse.model.Classpath; +import org.gradle.plugins.ide.eclipse.model.EclipseModel; + +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Consumer; + +public final class EclipseUtils { + private static final String GRADLE_USED_BY_SCOPE_ATTRIBUTE = "gradle_used_by_scope"; + private static final String GRADLE_USED_BY_SCOPE_SEPARATOR = ","; + + private EclipseUtils() { + throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); + } + + public static void patchClasspathEntries(EclipseModel eclipseModel, Consumer consumer) { + whenClasspathFileMerged(eclipseModel.getClasspath().getFile(), + classpath -> patchClasspathEntries(classpath, consumer)); + } + + public static void patchClasspathEntries(Classpath eclipseClasspath, Consumer consumer) { + for (var entry : eclipseClasspath.getEntries()) { + if (entry instanceof AbstractClasspathEntry abstractClasspathEntry) { + consumer.accept(abstractClasspathEntry); + } + } + } + + /** + * Avoids ambiguous call to ({@link XmlFileContentMerger#whenMerged(Closure)} versus + * {@link XmlFileContentMerger#whenMerged(Action)}) in Kotlin build scripts. + *

+ * The {@code Closure} variant will use the build script itself as {@code this}, and Kotlin will consider any + * type ascription as a cast of {@code this} (instead of the argument of the {@code Action}). This results in + * a mysterious {@link ClassCastException}, since the class generated from the build script doesn't extend from + * {@link Classpath}. Using this helper method selects the correct call and applies the cast properly. + * + * @param file The Eclipse classpath file. + * @param consumer The lambda to run on when the classpath file is merged. + */ + public static void whenClasspathFileMerged(XmlFileContentMerger file, Consumer consumer) { + file.whenMerged(untypedClasspath -> { + var classpath = (Classpath) untypedClasspath; + consumer.accept(classpath); + }); + } + + public static void patchGradleUsedByScope(AbstractClasspathEntry entry, Consumer> consumer) { + var entryAttributes = entry.getEntryAttributes(); + var usedByValue = entryAttributes.get(GRADLE_USED_BY_SCOPE_ATTRIBUTE); + Set usedBySet; + if (usedByValue instanceof String usedByString) { + usedBySet = new LinkedHashSet<>(List.of(usedByString.split(GRADLE_USED_BY_SCOPE_SEPARATOR))); + } else { + usedBySet = new LinkedHashSet<>(); + } + consumer.accept(usedBySet); + if (usedBySet.isEmpty()) { + entryAttributes.remove(GRADLE_USED_BY_SCOPE_ATTRIBUTE); + } else { + var newUsedByString = String.join(GRADLE_USED_BY_SCOPE_SEPARATOR, usedBySet); + entryAttributes.put(GRADLE_USED_BY_SCOPE_ATTRIBUTE, newUsedByString); + } + } +} diff --git a/buildSrc/src/main/java/tools/refinery/buildsrc/SonarPropertiesUtils.java b/buildSrc/src/main/java/tools/refinery/buildsrc/SonarPropertiesUtils.java new file mode 100644 index 00000000..1d89841e --- /dev/null +++ b/buildSrc/src/main/java/tools/refinery/buildsrc/SonarPropertiesUtils.java @@ -0,0 +1,44 @@ +package tools.refinery.buildsrc; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Map; + +public final class SonarPropertiesUtils { + private SonarPropertiesUtils() { + throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); + } + + /** + * Adds the entries to a Sonar property of list type. + *

+ * According to the Sonar Gradle documentation for {@link org.sonarqube.gradle.SonarProperties}, property values + * are converted to Strings as follows: + *

    + *
  • {@code Iterable}s are recursively converted and joined into a comma-separated String.
  • + *
  • All other values are converted to Strings by calling their {@code toString()} method.
  • + *
+ * Therefore, we use {@link ArrayList} to retain lists entries, which will be recursively converted later. + * + * @param properties The sonar properties map returned by + * {@link org.sonarqube.gradle.SonarProperties#getProperties()}. + * @param propertyName The name of the property to append to. + * @param entries The entries to append. + */ + public static void addToList(Map properties, String propertyName, String... entries) { + ArrayList newValue; + var currentValue = properties.get(propertyName); + if (currentValue instanceof ArrayList currentList) { + @SuppressWarnings("unchecked") + var objectList = (ArrayList) currentList; + newValue = objectList; + } else if (currentValue == null) { + newValue = new ArrayList<>(entries.length); + } else { + newValue = new ArrayList<>(entries.length + 1); + newValue.add(currentValue); + } + Collections.addAll(newValue, entries); + properties.put(propertyName, newValue); + } +} -- cgit v1.2.3-54-g00ecf