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. --- buildSrc/src/main/groovy/refinery-eclipse.gradle | 28 ------- .../groovy/refinery-frontend-conventions.gradle | 14 ---- .../main/groovy/refinery-frontend-workspace.gradle | 29 ------- .../main/groovy/refinery-frontend-worktree.gradle | 95 --------------------- .../main/groovy/refinery-java-application.gradle | 11 --- .../main/groovy/refinery-java-conventions.gradle | 93 --------------------- .../src/main/groovy/refinery-java-library.gradle | 4 - .../main/groovy/refinery-java-test-fixtures.gradle | 35 -------- buildSrc/src/main/groovy/refinery-jmh.gradle | 64 --------------- buildSrc/src/main/groovy/refinery-mwe2.gradle | 16 ---- buildSrc/src/main/groovy/refinery-sonarqube.gradle | 8 -- .../main/groovy/refinery-xtext-conventions.gradle | 21 ----- .../java/tools/refinery/buildsrc/EclipseUtils.java | 72 ++++++++++++++++ .../refinery/buildsrc/SonarPropertiesUtils.java | 44 ++++++++++ .../src/main/kotlin/refinery-eclipse.gradle.kts | 30 +++++++ .../refinery-frontend-conventions.gradle.kts | 18 ++++ .../kotlin/refinery-frontend-workspace.gradle.kts | 32 ++++++++ .../kotlin/refinery-frontend-worktree.gradle.kts | 84 +++++++++++++++++++ .../kotlin/refinery-java-application.gradle.kts | 12 +++ .../kotlin/refinery-java-conventions.gradle.kts | 96 ++++++++++++++++++++++ .../main/kotlin/refinery-java-library.gradle.kts | 5 ++ .../kotlin/refinery-java-test-fixtures.gradle.kts | 31 +++++++ buildSrc/src/main/kotlin/refinery-jmh.gradle.kts | 63 ++++++++++++++ buildSrc/src/main/kotlin/refinery-mwe2.gradle.kts | 18 ++++ .../src/main/kotlin/refinery-sonarqube.gradle.kts | 3 + .../kotlin/refinery-xtext-conventions.gradle.kts | 21 +++++ 26 files changed, 529 insertions(+), 418 deletions(-) delete mode 100644 buildSrc/src/main/groovy/refinery-eclipse.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-frontend-conventions.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-frontend-workspace.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-frontend-worktree.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-java-application.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-java-conventions.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-java-library.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-java-test-fixtures.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-jmh.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-mwe2.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-sonarqube.gradle delete mode 100644 buildSrc/src/main/groovy/refinery-xtext-conventions.gradle create mode 100644 buildSrc/src/main/java/tools/refinery/buildsrc/EclipseUtils.java create mode 100644 buildSrc/src/main/java/tools/refinery/buildsrc/SonarPropertiesUtils.java create mode 100644 buildSrc/src/main/kotlin/refinery-eclipse.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-frontend-conventions.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-frontend-workspace.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-frontend-worktree.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-java-application.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-java-conventions.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-java-library.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-java-test-fixtures.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-jmh.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-mwe2.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-sonarqube.gradle.kts create mode 100644 buildSrc/src/main/kotlin/refinery-xtext-conventions.gradle.kts (limited to 'buildSrc/src/main') diff --git a/buildSrc/src/main/groovy/refinery-eclipse.gradle b/buildSrc/src/main/groovy/refinery-eclipse.gradle deleted file mode 100644 index 15dcb5ce..00000000 --- a/buildSrc/src/main/groovy/refinery-eclipse.gradle +++ /dev/null @@ -1,28 +0,0 @@ -plugins { - id 'eclipse' -} - -// Workaround from https://github.com/gradle/gradle/issues/898#issuecomment-885765821 -def eclipseResourceEncoding = tasks.register('eclipseResourceEncoding') { - ext.outputFile = file('.settings/org.eclipse.core.resources.prefs') - def compileTask = tasks.findByName('compileJava') - ext.encoding = provider({ compileTask?.options?.encoding }).orElse(providers.systemProperty('file.encoding')) - - inputs.property('file.encoding', encoding) - outputs.file(outputFile).withPropertyName('outputFile') - - doLast { - Properties eclipseEncodingProperties = - new Properties(Collections.singletonMap('eclipse.preferences.version', '1')) - eclipseEncodingProperties.put('encoding/', encoding.get()) - outputFile.withOutputStream { - eclipseEncodingProperties.store(it, 'generated by ' + name) - } - } -} - -tasks.named('eclipse') { - dependsOn(eclipseResourceEncoding) -} - -eclipse.synchronizationTasks(eclipseResourceEncoding) diff --git a/buildSrc/src/main/groovy/refinery-frontend-conventions.gradle b/buildSrc/src/main/groovy/refinery-frontend-conventions.gradle deleted file mode 100644 index da9370fe..00000000 --- a/buildSrc/src/main/groovy/refinery-frontend-conventions.gradle +++ /dev/null @@ -1,14 +0,0 @@ -plugins { - id 'org.siouan.frontend-jdk11' -} - -frontend { - nodeVersion = project.ext['frontend.nodeVersion'] - nodeInstallDirectory = file("${rootDir}/.node") - yarnEnabled = true - yarnVersion = project.ext['frontend.yarnVersion'] -} - -tasks.named('enableYarnBerry') { - enabled = false -} diff --git a/buildSrc/src/main/groovy/refinery-frontend-workspace.gradle b/buildSrc/src/main/groovy/refinery-frontend-workspace.gradle deleted file mode 100644 index 9c6d7b13..00000000 --- a/buildSrc/src/main/groovy/refinery-frontend-workspace.gradle +++ /dev/null @@ -1,29 +0,0 @@ -plugins { - id 'refinery-eclipse' - id 'refinery-frontend-conventions' -} - -tasks.named('installNode') { - dependsOn rootProject.tasks.named('installNode') - enabled = false -} - -tasks.named('installYarnGlobally') { - dependsOn rootProject.tasks.named('installYarnGlobally') - enabled = false -} - -tasks.named('installYarn') { - dependsOn rootProject.tasks.named('installYarn') - enabled = false -} - -def rootInstallFrontend = rootProject.tasks.named('installFrontend') -rootInstallFrontend.configure { - inputs.file "${projectDir}/package.json" -} - -tasks.named('installFrontend') { - dependsOn rootInstallFrontend - enabled = false -} diff --git a/buildSrc/src/main/groovy/refinery-frontend-worktree.gradle b/buildSrc/src/main/groovy/refinery-frontend-worktree.gradle deleted file mode 100644 index 1d239fd5..00000000 --- a/buildSrc/src/main/groovy/refinery-frontend-worktree.gradle +++ /dev/null @@ -1,95 +0,0 @@ -plugins { - id 'refinery-frontend-conventions' -} - -frontend { - yarnGlobalInstallScript = "install -g yarn@${project.ext['frontend.yarn1Version']}" - yarnInstallScript = "set version ${frontend.yarnVersion.get()} --only-if-needed" - if (project.hasProperty('ci')) { - installScript = 'install --immutable --inline-builds' - } else { - installScript = 'install' - } -} - -ext.frontedPropertiesFile = "${frontend.nodeInstallDirectory.get()}/frontend.properties" - -String getFrontendProperty(String propertyName) { - FileInputStream inputStream = null - Properties props = new Properties() - try { - inputStream = new FileInputStream(frontedPropertiesFile) - props.load(inputStream) - } catch (IOException ignored) { - return null - } finally { - if (inputStream != null) { - inputStream.close() - } - } - return props.get(propertyName) -} - -void putFrontedProperty(String propertyName, String propertyValue) { - FileInputStream inputStream = null - Properties props = new Properties() - try { - inputStream = new FileInputStream(frontedPropertiesFile) - props.load(inputStream) - } catch (FileNotFoundException ignored) { - // Use an empty Properties object instead - } finally { - if (inputStream != null) { - inputStream.close() - } - } - props.put(propertyName, propertyValue) - FileOutputStream outputStream = null - try { - outputStream = new FileOutputStream(frontedPropertiesFile) - props.store(outputStream, null) - } catch (IOException ignored) { - } finally { - if (outputStream != null) { - outputStream.close() - } - } -} - -tasks.named('installNode') { - onlyIf { - getFrontendProperty('installedNodeVersion') != frontend.nodeVersion.get() - } - doLast { - putFrontedProperty('installedNodeVersion', frontend.nodeVersion.get()) - } -} - -tasks.named('installYarnGlobally') { - onlyIf { - getFrontendProperty('installedYarn1Version') != project.ext['frontend.yarn1Version'] - } - doLast { - putFrontedProperty('installedYarn1Version', project.ext['frontend.yarn1Version']) - } - outputs.dir "${frontend.nodeInstallDirectory.get()}/lib/node_modules/yarn" -} - -tasks.named('installYarn') { - outputs.file ".yarn/releases/yarn-${frontend.yarnVersion.get()}.cjs" -} - -tasks.named('installFrontend') { - inputs.files('package.json', 'yarn.lock') - outputs.files('.pnp.cjs', '.pnp.loader.mjs') -} - -tasks.register('clobberFrontend', Delete) { - delete frontend.nodeInstallDirectory.get() - delete '.yarn/cache' - delete '.yarn/install-state.gz' - delete '.yarn/sdks' - delete '.yarn/unplugged' - delete '.pnp.cjs' - delete '.pnp.loader.mjs' -} diff --git a/buildSrc/src/main/groovy/refinery-java-application.gradle b/buildSrc/src/main/groovy/refinery-java-application.gradle deleted file mode 100644 index c38ccdb3..00000000 --- a/buildSrc/src/main/groovy/refinery-java-application.gradle +++ /dev/null @@ -1,11 +0,0 @@ -plugins { - id 'application' - id 'com.github.johnrengelman.shadow' - id 'refinery-java-conventions' -} - -for (taskName in ['distTar', 'distZip', 'shadowDistTar', 'shadowDistZip']) { - tasks.named(taskName) { - enabled = false - } -} diff --git a/buildSrc/src/main/groovy/refinery-java-conventions.gradle b/buildSrc/src/main/groovy/refinery-java-conventions.gradle deleted file mode 100644 index fdf5818a..00000000 --- a/buildSrc/src/main/groovy/refinery-java-conventions.gradle +++ /dev/null @@ -1,93 +0,0 @@ -plugins { - id 'jacoco' - id 'java' - id 'refinery-eclipse' -} - -repositories { - mavenCentral() - maven { - url 'https://repo.eclipse.org/content/groups/releases/' - } -} - -dependencies { - compileOnly libs.jetbrainsAnnotations - testCompileOnly libs.jetbrainsAnnotations - testImplementation libs.hamcrest - testImplementation libs.junit.api - testRuntimeOnly libs.junit.engine - testImplementation libs.junit.params - testImplementation libs.mockito.core - testImplementation libs.mockito.junit -} - -java.toolchain { - languageVersion = JavaLanguageVersion.of(19) -} - -tasks.withType(JavaCompile) { - options.release.set(17) -} - -def jacocoTestReport = tasks.named('jacocoTestReport') -jacocoTestReport.configure { - dependsOn test - reports { - xml.required = true - } -} - -tasks.named('test') { - useJUnitPlatform { - excludeTags 'slow' - } - finalizedBy jacocoTestReport -} - -tasks.register('slowTest', Test) { - useJUnitPlatform() - finalizedBy jacocoTestReport -} - -tasks.named('jar') { - manifest { - attributes( - 'Bundle-SymbolicName': "${project.group}.${project.name}", - 'Bundle-Version': project.version - ) - } -} - -def generateEclipseSourceFolders = tasks.register('generateEclipseSourceFolders') - -tasks.register('prepareEclipse') { - dependsOn generateEclipseSourceFolders - dependsOn tasks.named('eclipseJdt') -} - -tasks.named('eclipseClasspath') { - dependsOn generateEclipseSourceFolders -} - -eclipse { - classpath.file.whenMerged { - for (entry in entries) { - if (entry.path.endsWith('-gen')) { - entry.entryAttributes['ignore_optional_problems'] = true - } - // If a project has a main dependency on a project and an test dependency on the testFixtures of a project, - // it will be erroneously added as a test-only dependency to Eclipse. - // As a workaround, we add all project dependencies as main dependencies - // (we do not deliberately use test-only project dependencies). - if (entry in org.gradle.plugins.ide.eclipse.model.ProjectDependency) { - entry.entryAttributes.remove('test') - } - } - } - - jdt.file.withProperties { properties -> - // Allow @SuppressWarnings to suppress SonarLint warnings - properties['org.eclipse.jdt.core.compiler.problem.unhandledWarningToken'] = 'ignore' - } -} diff --git a/buildSrc/src/main/groovy/refinery-java-library.gradle b/buildSrc/src/main/groovy/refinery-java-library.gradle deleted file mode 100644 index daa80f17..00000000 --- a/buildSrc/src/main/groovy/refinery-java-library.gradle +++ /dev/null @@ -1,4 +0,0 @@ -plugins { - id 'java-library' - id 'refinery-java-conventions' -} diff --git a/buildSrc/src/main/groovy/refinery-java-test-fixtures.gradle b/buildSrc/src/main/groovy/refinery-java-test-fixtures.gradle deleted file mode 100644 index 02568abd..00000000 --- a/buildSrc/src/main/groovy/refinery-java-test-fixtures.gradle +++ /dev/null @@ -1,35 +0,0 @@ -import org.gradle.plugins.ide.eclipse.model.AbstractClasspathEntry - -plugins { - id 'java-test-fixtures' - id 'refinery-java-conventions' -} - -eclipse.classpath { - containsTestFixtures = true - - file.whenMerged { classpath -> - def hasTest = classpath.entries.any { entry -> - entry in AbstractClasspathEntry && - entry.entryAttributes['gradle_scope'] == 'test' - } - for (entry in classpath.entries) { - // Workaround https://github.com/gradle/gradle/issues/11845 based on - // https://discuss.gradle.org/t/gradle-used-by-scope-not-correctly-generated-when-the-java-test-fixtures-plugin-is-used/39935/2 - if (entry in AbstractClasspathEntry) { - def usedBy = new LinkedHashSet( - Arrays.asList((entry.entryAttributes['gradle_used_by_scope'] ?: '').split(',')) - ) - if (usedBy.contains('main')) { - usedBy += 'testFixtures' - } - if (hasTest && usedBy.contains('testFixtures')) { - usedBy += 'test' - } - if (!usedBy.empty) { - entry.entryAttributes['gradle_used_by_scope'] = usedBy.join(',') - } - } - } - } -} diff --git a/buildSrc/src/main/groovy/refinery-jmh.gradle b/buildSrc/src/main/groovy/refinery-jmh.gradle deleted file mode 100644 index 1ab9edc3..00000000 --- a/buildSrc/src/main/groovy/refinery-jmh.gradle +++ /dev/null @@ -1,64 +0,0 @@ -import org.gradle.plugins.ide.eclipse.model.AbstractClasspathEntry - -plugins { - id 'refinery-java-conventions' - id 'refinery-sonarqube' -} - -configurations { - jmh { - extendsFrom implementation - } -} - -sourceSets { - jmh { - java.srcDirs = ['src/jmh/java'] - resources.srcDirs = ['src/jmh/resources'] - compileClasspath += sourceSets.main.runtimeClasspath - compileClasspath += sourceSets.test.runtimeClasspath - } -} - -dependencies { - jmhImplementation libs.jmh.core - jmhAnnotationProcessor libs.jmh.annprocess -} - -tasks.register('jmh', JavaExec) { - dependsOn tasks.named('jmhClasses') - mainClass = 'org.openjdk.jmh.Main' - classpath = sourceSets.jmh.compileClasspath + sourceSets.jmh.runtimeClasspath -} - -eclipse.classpath.file.whenMerged { classpath -> - for (entry in classpath.entries) { - if (entry in AbstractClasspathEntry) { - // Workaround from https://github.com/gradle/gradle/issues/4802#issuecomment-407902081 - if (entry.entryAttributes['gradle_scope'] == 'jmh') { - // Allow test helper classes to be used in benchmarks from Eclipse - // and do not expose JMH dependencies to the main source code. - entry.entryAttributes['test'] = true - } else { - // Workaround based on - // https://discuss.gradle.org/t/gradle-used-by-scope-not-correctly-generated-when-the-java-test-fixtures-plugin-is-used/39935/2 - def usedBy = new LinkedHashSet( - Arrays.asList((entry.entryAttributes['gradle_used_by_scope'] ?: '').split(',')) - ) - if (['main', 'test', 'testFixtures'].any { e -> usedBy.contains(e) }) { - // main and test sources are also used by jmh sources. - usedBy += 'jmh' - } - if (!usedBy.empty) { - entry.entryAttributes['gradle_used_by_scope'] = usedBy.join(',') - } - } - } - } -} - -sonarqube.properties { - properties['sonar.tests'] += [ - 'src/jmh/java', - ] -} diff --git a/buildSrc/src/main/groovy/refinery-mwe2.gradle b/buildSrc/src/main/groovy/refinery-mwe2.gradle deleted file mode 100644 index c7f15e82..00000000 --- a/buildSrc/src/main/groovy/refinery-mwe2.gradle +++ /dev/null @@ -1,16 +0,0 @@ -plugins { - id 'eclipse' - id 'refinery-java-conventions' -} - -configurations { - mwe2 { - extendsFrom implementation - } -} - -dependencies { - mwe2 libs.mwe2.launch -} - -eclipse.classpath.plusConfigurations += [configurations.mwe2] diff --git a/buildSrc/src/main/groovy/refinery-sonarqube.gradle b/buildSrc/src/main/groovy/refinery-sonarqube.gradle deleted file mode 100644 index d84f4ada..00000000 --- a/buildSrc/src/main/groovy/refinery-sonarqube.gradle +++ /dev/null @@ -1,8 +0,0 @@ -plugins { - id 'org.sonarqube' -} - -sonarqube.properties { - // Make sure `exclusions` is a List in every subproject - property 'sonar.exclusions', [] -} diff --git a/buildSrc/src/main/groovy/refinery-xtext-conventions.gradle b/buildSrc/src/main/groovy/refinery-xtext-conventions.gradle deleted file mode 100644 index 0c7c82f0..00000000 --- a/buildSrc/src/main/groovy/refinery-xtext-conventions.gradle +++ /dev/null @@ -1,21 +0,0 @@ -plugins { - id 'refinery-java-conventions' - id 'refinery-sonarqube' -} - -sourceSets { - main { - java.srcDirs += ['src/main/xtext-gen'] - resources.srcDirs += ['src/main/xtext-gen'] - } -} - -tasks.named('clean') { - delete 'src/main/xtext-gen' -} - -sonarqube.properties { - properties['sonar.exclusions'] += [ - 'src/main/xtext-gen/**', - ] -} 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); + } +} diff --git a/buildSrc/src/main/kotlin/refinery-eclipse.gradle.kts b/buildSrc/src/main/kotlin/refinery-eclipse.gradle.kts new file mode 100644 index 00000000..ed7c5250 --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-eclipse.gradle.kts @@ -0,0 +1,30 @@ +import org.gradle.plugins.ide.eclipse.model.EclipseModel +import java.util.* + +plugins { + eclipse +} + +// Workaround from https://github.com/gradle/gradle/issues/898#issuecomment-885765821 +val eclipseResourceEncoding by tasks.registering { + val outputFile = file(".settings/org.eclipse.core.resources.prefs") + val encoding = providers.systemProperty("file.encoding") + + inputs.property("file.encoding", encoding) + outputs.file(outputFile) + + doLast { + val eclipseEncodingProperties = Properties(2) + eclipseEncodingProperties["eclipse.preferences.version"] = "1" + eclipseEncodingProperties["encoding/"] = encoding.get() + outputFile.outputStream().use { outputStream -> + eclipseEncodingProperties.store(outputStream, "generated by $name") + } + } +} + +tasks.named("eclipse") { + dependsOn(eclipseResourceEncoding) +} + +the().synchronizationTasks(eclipseResourceEncoding) diff --git a/buildSrc/src/main/kotlin/refinery-frontend-conventions.gradle.kts b/buildSrc/src/main/kotlin/refinery-frontend-conventions.gradle.kts new file mode 100644 index 00000000..c4658948 --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-frontend-conventions.gradle.kts @@ -0,0 +1,18 @@ +import org.siouan.frontendgradleplugin.infrastructure.gradle.EnableYarnBerryTask +import org.siouan.frontendgradleplugin.infrastructure.gradle.FrontendExtension + +plugins { + id("org.siouan.frontend-jdk11") +} + +configure { + nodeVersion.set(providers.gradleProperty("frontend.nodeVersion")) + nodeInstallDirectory.set(file("$rootDir/.node")) + yarnEnabled.set(true) + yarnVersion.set(providers.gradleProperty("frontend.yarnVersion")) +} + +tasks.named("enableYarnBerry") { + // There is no need to enable berry manually, because berry files are already committed to the repo. + enabled = false +} diff --git a/buildSrc/src/main/kotlin/refinery-frontend-workspace.gradle.kts b/buildSrc/src/main/kotlin/refinery-frontend-workspace.gradle.kts new file mode 100644 index 00000000..198f73f3 --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-frontend-workspace.gradle.kts @@ -0,0 +1,32 @@ +import org.siouan.frontendgradleplugin.infrastructure.gradle.* + +plugins { + id("refinery-eclipse") + id("refinery-frontend-conventions") +} + +tasks.named("installNode") { + dependsOn(rootProject.tasks.named("installNode")) + enabled = false +} + +tasks.named("installYarnGlobally") { + dependsOn(rootProject.tasks.named("installYarnGlobally")) + enabled = false +} + +tasks.named("installYarn") { + dependsOn(rootProject.tasks.named("installYarn")) + enabled = false +} + +val rootInstallFrontend = rootProject.tasks.named("installFrontend") + +rootInstallFrontend.configure { + inputs.file("$projectDir/package.json") +} + +tasks.named("installFrontend") { + dependsOn(rootInstallFrontend) + enabled = false +} diff --git a/buildSrc/src/main/kotlin/refinery-frontend-worktree.gradle.kts b/buildSrc/src/main/kotlin/refinery-frontend-worktree.gradle.kts new file mode 100644 index 00000000..d8c3d51f --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-frontend-worktree.gradle.kts @@ -0,0 +1,84 @@ +import org.siouan.frontendgradleplugin.infrastructure.gradle.* +import java.io.FileInputStream +import java.io.FileNotFoundException +import java.io.FileOutputStream +import java.util.* + +plugins { + id("refinery-frontend-conventions") +} + +val frontend = the() + +val yarn1Version = providers.gradleProperty("frontend.yarn1Version") + +frontend.yarnGlobalInstallScript.set(yarn1Version.map { version -> "install -g yarn@$version" }) +frontend.yarnInstallScript.set(frontend.yarnVersion.map { version -> "set version $version --only-if-needed" }) +frontend.installScript.set(provider { + if (project.hasProperty("ci")) "install --immutable --inline-builds" else "install" +}) + +val frontendPropertiesFile = frontend.nodeInstallDirectory.map { dir -> "$dir/frontend.properties" } + +fun readFrontendProperties(): Properties { + val props = Properties() + try { + FileInputStream(frontendPropertiesFile.get()).use { inputStream -> + props.load(inputStream) + } + } catch (ignored: FileNotFoundException) { + // Ignore missing file. + } + return props +} + +fun getFrontendProperty(propertyName: String): String? { + val props = readFrontendProperties() + return props[propertyName]?.toString() +} + +fun putFrontedProperty(propertyName: String, propertyValue: String) { + val props = readFrontendProperties() + props[propertyName] = propertyValue + FileOutputStream(frontendPropertiesFile.get()).use { outputStream -> + props.store(outputStream, "generated by refinery-frontend-worktree") + } +} + +tasks.named("installNode") { + onlyIf { + getFrontendProperty("installedNodeVersion") != frontend.nodeVersion.get() + } + doLast { + putFrontedProperty("installedNodeVersion", frontend.nodeVersion.get()) + } +} + +tasks.named("installYarnGlobally") { + onlyIf { + getFrontendProperty("installedYarn1Version") != yarn1Version.get() + } + doLast { + putFrontedProperty("installedYarn1Version", yarn1Version.get()) + } + outputs.dir(frontend.nodeInstallDirectory.map { dir -> "$dir/lib/node_modules/yarn" }) +} + +tasks.named("installYarn") { + outputs.file(frontend.yarnVersion.map { version -> ".yarn/releases/yarn-$version.cjs" }) +} + +tasks.named("installFrontend") { + inputs.files("package.json", "yarn.lock") + outputs.files(".pnp.cjs", ".pnp.loader.mjs") +} + +tasks.register("clobberFrontend", Delete::class) { + delete(frontend.nodeInstallDirectory) + delete(".yarn/cache") + delete(".yarn/install-state.gz") + delete(".yarn/sdks") + delete(".yarn/unplugged") + delete(".pnp.cjs") + delete(".pnp.loader.mjs") +} diff --git a/buildSrc/src/main/kotlin/refinery-java-application.gradle.kts b/buildSrc/src/main/kotlin/refinery-java-application.gradle.kts new file mode 100644 index 00000000..b3fab1fa --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-java-application.gradle.kts @@ -0,0 +1,12 @@ +plugins { + application + id("com.github.johnrengelman.shadow") +} + +apply(plugin = "refinery-java-conventions") + +for (taskName in listOf("distTar", "distZip", "shadowDistTar", "shadowDistZip")) { + tasks.named(taskName) { + enabled = false + } +} diff --git a/buildSrc/src/main/kotlin/refinery-java-conventions.gradle.kts b/buildSrc/src/main/kotlin/refinery-java-conventions.gradle.kts new file mode 100644 index 00000000..2d5ce8b5 --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-java-conventions.gradle.kts @@ -0,0 +1,96 @@ +import org.gradle.accessors.dm.LibrariesForLibs +import org.gradle.plugins.ide.eclipse.model.EclipseModel +import org.gradle.plugins.ide.eclipse.model.ProjectDependency +import tools.refinery.buildsrc.EclipseUtils + +plugins { + jacoco + java +} + +apply(plugin = "refinery-eclipse") + +repositories { + mavenCentral() + maven { + url = uri("https://repo.eclipse.org/content/groups/releases/") + } +} + +val libs = the() + +dependencies { + compileOnly(libs.jetbrainsAnnotations) + testCompileOnly(libs.jetbrainsAnnotations) + testImplementation(libs.hamcrest) + testImplementation(libs.junit.api) + testRuntimeOnly(libs.junit.engine) + testImplementation(libs.junit.params) + testImplementation(libs.mockito.core) + testImplementation(libs.mockito.junit) +} + +java.toolchain { + languageVersion.set(JavaLanguageVersion.of(19)) +} + +tasks.withType(JavaCompile::class) { + options.release.set(17) +} + +val test = tasks.named("test") + +val jacocoTestReport = tasks.named("jacocoTestReport") + +test.configure { + useJUnitPlatform { + excludeTags("slow") + } + finalizedBy(jacocoTestReport) +} + +jacocoTestReport.configure { + dependsOn(test) + reports { + xml.required.set(true) + } +} + +tasks.named("jar") { + manifest { + attributes( + "Bundle-SymbolicName" to "${project.group}.${project.name}", + "Bundle-Version" to project.version + ) + } +} + +val generateEclipseSourceFolders by tasks.registering + +tasks.register("prepareEclipse") { + dependsOn(generateEclipseSourceFolders) + dependsOn(tasks.named("eclipseJdt")) +} + +tasks.named("eclipseClasspath") { + dependsOn(generateEclipseSourceFolders) +} + +configure { + EclipseUtils.patchClasspathEntries(this) { entry -> + if (entry.path.endsWith("-gen")) { + entry.entryAttributes["ignore_optional_problems"] = true + } + // If a project has a main dependency on a project and a test dependency on the testFixtures of a project, + // it will be erroneously added as a test-only dependency to Eclipse. As a workaround, we add all project + // dependencies as main dependencies (we do not deliberately use test-only project dependencies). + if (entry is ProjectDependency) { + entry.entryAttributes.remove("test") + } + } + + jdt.file.withProperties { + // Allow @SuppressWarnings to suppress SonarLint warnings + this["org.eclipse.jdt.core.compiler.problem.unhandledWarningToken"] = "ignore" + } +} diff --git a/buildSrc/src/main/kotlin/refinery-java-library.gradle.kts b/buildSrc/src/main/kotlin/refinery-java-library.gradle.kts new file mode 100644 index 00000000..5a6200e0 --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-java-library.gradle.kts @@ -0,0 +1,5 @@ +plugins { + `java-library` +} + +apply(plugin = "refinery-java-conventions") diff --git a/buildSrc/src/main/kotlin/refinery-java-test-fixtures.gradle.kts b/buildSrc/src/main/kotlin/refinery-java-test-fixtures.gradle.kts new file mode 100644 index 00000000..86b0a04b --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-java-test-fixtures.gradle.kts @@ -0,0 +1,31 @@ +import org.gradle.plugins.ide.eclipse.model.AbstractClasspathEntry +import org.gradle.plugins.ide.eclipse.model.EclipseModel +import tools.refinery.buildsrc.EclipseUtils + +plugins { + `java-test-fixtures` +} + +apply(plugin = "refinery-java-conventions") + +the().classpath { + containsTestFixtures.set(true) + + EclipseUtils.whenClasspathFileMerged(file) { eclipseClasspath -> + val hasTest = eclipseClasspath.entries.any { entry -> + entry is AbstractClasspathEntry && entry.entryAttributes["gradle_scope"] == "test" + } + EclipseUtils.patchClasspathEntries(eclipseClasspath) { entry -> + // Workaround https://github.com/gradle/gradle/issues/11845 based on + // https://discuss.gradle.org/t/gradle-used-by-scope-not-correctly-generated-when-the-java-test-fixtures-plugin-is-used/39935/2 + EclipseUtils.patchGradleUsedByScope(entry) { usedBy -> + if (usedBy.contains("main")) { + usedBy += "testFixtures" + } + if (hasTest && usedBy.contains("testFixtures")) { + usedBy += "test" + } + } + } + } +} diff --git a/buildSrc/src/main/kotlin/refinery-jmh.gradle.kts b/buildSrc/src/main/kotlin/refinery-jmh.gradle.kts new file mode 100644 index 00000000..11888b59 --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-jmh.gradle.kts @@ -0,0 +1,63 @@ +import org.gradle.accessors.dm.LibrariesForLibs +import org.gradle.plugins.ide.eclipse.model.EclipseModel +import org.sonarqube.gradle.SonarExtension +import tools.refinery.buildsrc.EclipseUtils +import tools.refinery.buildsrc.SonarPropertiesUtils + +apply(plugin = "refinery-java-conventions") +apply(plugin = "refinery-sonarqube") + +val sourceSets = the() + +val main: SourceSet by sourceSets.getting + +val test: SourceSet by sourceSets.getting + +val jmh: SourceSet by sourceSets.creating { + compileClasspath += main.output + runtimeClasspath += main.output + // Allow using test classes in benchmarks for now. + compileClasspath += test.output + runtimeClasspath += test.output +} + +val jmhImplementation: Configuration by configurations.getting { + extendsFrom(configurations["implementation"], configurations["testImplementation"]) +} + +val jmhAnnotationProcessor: Configuration by configurations.getting + +configurations["jmhRuntimeOnly"].extendsFrom(configurations["runtimeOnly"], configurations["testRuntimeOnly"]) + +val libs = the() + +dependencies { + jmhImplementation(libs.jmh.core) + jmhAnnotationProcessor(libs.jmh.annprocess) +} + +tasks.register("jmh", JavaExec::class) { + dependsOn(tasks.named("jmhClasses")) + mainClass.set("org.openjdk.jmh.Main") + classpath = jmh.runtimeClasspath +} + +EclipseUtils.patchClasspathEntries(the()) { entry -> + // Workaround from https://github.com/gradle/gradle/issues/4802#issuecomment-407902081 + if (entry.entryAttributes["gradle_scope"] == "jmh") { + // Allow test helper classes to be used in benchmarks from Eclipse + // and do not expose JMH dependencies to the main source code. + entry.entryAttributes["test"] = true + } else { + EclipseUtils.patchGradleUsedByScope(entry) { usedBy -> + if (listOf("main", "test", "testFixtures").any { e -> usedBy.contains(e) }) { + // main and test sources are also used by jmh sources. + usedBy += "jmh" + } + } + } +} + +the().properties { + SonarPropertiesUtils.addToList(properties, "sonar.tests", "src/jmh/java") +} diff --git a/buildSrc/src/main/kotlin/refinery-mwe2.gradle.kts b/buildSrc/src/main/kotlin/refinery-mwe2.gradle.kts new file mode 100644 index 00000000..26963837 --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-mwe2.gradle.kts @@ -0,0 +1,18 @@ +import org.gradle.accessors.dm.LibrariesForLibs +import org.gradle.plugins.ide.eclipse.model.EclipseModel + +apply(plugin = "refinery-java-conventions") + +val mwe2: Configuration by configurations.creating { + isCanBeConsumed = false + isCanBeResolved = true + extendsFrom(configurations["implementation"]) +} + +val libs = the() + +dependencies { + mwe2(libs.mwe2.launch) +} + +the().classpath.plusConfigurations += mwe2 diff --git a/buildSrc/src/main/kotlin/refinery-sonarqube.gradle.kts b/buildSrc/src/main/kotlin/refinery-sonarqube.gradle.kts new file mode 100644 index 00000000..6a1dbbf6 --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-sonarqube.gradle.kts @@ -0,0 +1,3 @@ +plugins { + id("org.sonarqube") +} diff --git a/buildSrc/src/main/kotlin/refinery-xtext-conventions.gradle.kts b/buildSrc/src/main/kotlin/refinery-xtext-conventions.gradle.kts new file mode 100644 index 00000000..34fbae99 --- /dev/null +++ b/buildSrc/src/main/kotlin/refinery-xtext-conventions.gradle.kts @@ -0,0 +1,21 @@ +import org.gradle.api.tasks.SourceSetContainer +import org.sonarqube.gradle.SonarExtension +import tools.refinery.buildsrc.SonarPropertiesUtils + +apply(plugin = "refinery-java-conventions") +apply(plugin = "refinery-sonarqube") + +val xtextGenPath = "src/main/xtext-gen" + +the().named("main") { + java.srcDir(xtextGenPath) + resources.srcDir(xtextGenPath) +} + +tasks.named("clean") { + delete(xtextGenPath) +} + +the().properties { + SonarPropertiesUtils.addToList(properties, "sonar.exclusions", "$xtextGenPath/**") +} -- cgit v1.2.3-54-g00ecf