Skip to content

Commit

Permalink
Replace spotless with Immaculate
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Dec 27, 2024
1 parent 72be0f5 commit 8754608
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 105 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,26 @@ publishing {
}
```

### Spotless
### Immaculate

The `net.neoforged.gradleutils.spotless` plugin provides integration (a formatter configuration) with Spotless.
The Spotless plugin must be manually applied to the buildscript, as GradleUtils does not apply it automatically.
This plugin also requires Gradle to run with at least Java 11, much like Spotless does.
The `net.neoforged.gradleutils.immaculate` plugin provides integration (a formatter configuration) with [Immaculate](https://github.com/lukebemishprojects/Immaculate).

This plugin provides a `spotlessUtils` extension which has a `configure` method that can be called with the `spotless` extension to configure Spotless and use the formatter configuration provided by GradleUtils:
```gradle
spotlessUtils {
configure(spotless)
When you apply the plugin alongside Immaculate, it automatically configures a java workflow.

Example usage in settings.gradle to apply it to all projects that contain java code:

```groovy
plugins {
id 'net.neoforged.gradleutils' version '4.0.0' apply false
id 'net.neoforged.gradleutils.immaculate' version '4.0.0' apply false
id 'dev.lukebemish.immaculate' version '0.1.6' apply false
}
gradle.lifecycle.beforeProject { project ->
project.plugins.withId("java") {
apply plugin: 'dev.lukebemish.immaculate'
apply plugin: 'net.neoforged.gradleutils.immaculate'
}
}
```

Expand Down
34 changes: 17 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ java {
}

sourceSets {
spotless
immaculate
}

group 'net.neoforged.gradleutils'
version '3.0.0'
version '4.0.0'

repositories {
mavenCentral()
gradlePluginPortal()
}

// Spotless runs on J11
tasks.named('compileSpotlessJava', JavaCompile).configure {
// Immaculate runs on J17
tasks.named('compileImmaculateJava', JavaCompile).configure {
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(11)
languageVersion = JavaLanguageVersion.of(17)
}
}
tasks.named('compileSpotlessGroovy', GroovyCompile).configure {
tasks.named('compileImmaculateGroovy', GroovyCompile).configure {
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(11)
languageVersion = JavaLanguageVersion.of(17)
}
}

Expand All @@ -45,7 +45,7 @@ if (System.getenv('GPP_KEY')) {
}

configurations {
spotlessImplementation.extendsFrom(api)
immaculateImplementation.extendsFrom(api)
}

dependencies {
Expand All @@ -58,8 +58,8 @@ dependencies {
}
}

spotlessImplementation(gradleApi())
spotlessImplementation 'com.diffplug.spotless:spotless-plugin-gradle:6.23.3'
immaculateImplementation(gradleApi())
immaculateImplementation "dev.lukebemish.immaculate:dev.lukebemish.immaculate.gradle.plugin:0.1.6"
}

sourceSets {
Expand Down Expand Up @@ -120,12 +120,12 @@ gradlePlugin {
tags.set(['versioning', 'changelog'])
implementationClass = 'net.neoforged.gradleutils.GradleUtilsPlugin'
}
gradleutilsSpotless {
id = 'net.neoforged.gradleutils.spotless'
displayName = 'GradleUtils Spotless'
description = 'A plugin that configures Spotless on projects'
tags.set(['spotless'])
implementationClass = 'net.neoforged.gradleutils.spotless.SpotlessPlugin'
gradleutilsImmaculate {
id = 'net.neoforged.gradleutils.immaculate'
displayName = 'GradleUtils Immaculate'
description = 'A plugin that configures Immaculate on projects'
tags.set(['immaculate'])
implementationClass = 'net.neoforged.gradleutils.immaculate.ImmaculatePlugin'
}
}
}
Expand All @@ -136,7 +136,7 @@ final changelogTask = tasks.register('changelog', GenerateChangelogTask) {
}

tasks.named('jar', Jar) {
from(sourceSets.spotless.output)
from(sourceSets.immaculate.output)
}

tasks.named('assemble') {
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
20 changes: 10 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.gradleutils.spotless
package net.neoforged.gradleutils.immaculate

import groovy.transform.CompileStatic
import org.gradle.api.DefaultTask
Expand All @@ -16,11 +16,11 @@ import java.nio.file.Files

@CompileStatic
@DisableCachingByDefault(because = 'configuration contained inside the jar may change')
abstract class ExtractSpotlessConfiguration extends DefaultTask {
abstract class ExtractImmaculateConfiguration extends DefaultTask {
@OutputFile
abstract RegularFileProperty getOutput()

ExtractSpotlessConfiguration() {
ExtractImmaculateConfiguration() {
// configuration contained inside the jar may change
outputs.upToDateWhen {
false
Expand All @@ -35,8 +35,8 @@ abstract class ExtractSpotlessConfiguration extends DefaultTask {
}

try (
final input = SpotlessUtilsExtension.getResourceAsStream('/formatter-config.xml');
final out = Files.newOutputStream(output)
final input = ExtractImmaculateConfiguration.getResourceAsStream('/formatter-config.xml');
final out = Files.newOutputStream(output)
) {
input.transferTo(out)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.gradleutils.immaculate

import dev.lukebemish.immaculate.CheckTask
import dev.lukebemish.immaculate.FormattingWorkflow
import dev.lukebemish.immaculate.ImmaculateExtension
import org.gradle.api.GradleException
import org.gradle.api.Project

/**
* This is a separate class to avoid class-loading issues when immaculate is *not* applied to the project.
*/
final class ImmaculateConfiguration {
private ImmaculateConfiguration() {
}

static void apply(Project project) {
final configPath = new File(project.rootDir, '.gradle/formatter-config.xml')

final extract = project.tasks.register('extractGUImmaculateConfiguration', ExtractImmaculateConfiguration) {
it.output.set(configPath)
}

project.tasks.withType(CheckTask).configureEach {
it.dependsOn(extract)
}

var immaculate = (ImmaculateExtension) project.extensions.getByName("immaculate")

immaculate.workflows.register("java") {
configure(it, configPath)
}
}

static void configure(FormattingWorkflow workflow, File configPath) {
workflow.java()
workflow.trailingNewline()
workflow.noTabs()
workflow.googleFixImports()
workflow.toggleOff.set('spotless:off')
workflow.toggleOn.set('spotless:on')
workflow.eclipse {
it.version '3.37.0'
it.config.set(configPath)
}

// courtesy of diffplug/spotless#240
// https://github.com/diffplug/spotless/issues/240#issuecomment-385206606
workflow.custom 'noWildcardImports', { String fileContents ->
if (fileContents.contains('*;\n')) {
throw new GradleException('No wildcard imports are allowed!')
}
}

workflow.custom 'noNotNull', { String fileContents ->
if (fileContents.contains('@NotNull') || fileContents.contains('@Nonnull')) {
throw new GradleException('@NotNull and @Nonnull are disallowed.')
}
}

workflow.custom 'jetbrainsNullable', { String fileContents ->
fileContents.replace('javax.annotation.Nullable', 'org.jetbrains.annotations.Nullable')
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.gradleutils.spotless
package net.neoforged.gradleutils.immaculate


import groovy.transform.CompileStatic
import org.gradle.api.Plugin
import org.gradle.api.Project

@CompileStatic
class SpotlessPlugin implements Plugin<Project> {
class ImmaculatePlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.pluginManager.withPlugin('com.diffplug.spotless') {
project.extensions.create('spotlessUtils', SpotlessUtilsExtension, project)
project.pluginManager.withPlugin('dev.lukebemish.immaculate') {
ImmaculateConfiguration.apply(project)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.jetbrains.annotations.NotNull
*/
@CompileStatic
abstract class ReportMavenPublications implements BuildService<Params>, AutoCloseable {
private final List<Publication> artifacts = Collections.synchronizedList(new ArrayList<>())
private final List<Publication> artifacts = Collections.synchronizedList(new ArrayList<Publication>())

void record(String groupId, String artifactId, String version) {
artifacts.add(new Publication(
Expand Down

This file was deleted.

0 comments on commit 8754608

Please sign in to comment.