diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/SpringJavaFormatPlugin.java b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/SpringJavaFormatPlugin.java index 9c14a75f..e5f27015 100644 --- a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/SpringJavaFormatPlugin.java +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/SpringJavaFormatPlugin.java @@ -27,6 +27,7 @@ import org.gradle.api.tasks.TaskContainer; import org.gradle.api.tasks.TaskProvider; +import io.spring.javaformat.config.JavaFormatConfig; import io.spring.javaformat.gradle.tasks.CheckFormat; import io.spring.javaformat.gradle.tasks.Format; import io.spring.javaformat.gradle.tasks.FormatterTask; @@ -78,6 +79,9 @@ private TaskProvider addFormatterTask(SourceSet sou provider.configure((task) -> { task.setDescription(desc + " for " + sourceSet.getName()); task.setSource(sourceSet.getAllJava()); + JavaFormatConfig config = JavaFormatConfig.findFrom(this.project.getProjectDir()); + task.getIndentationStyle().convention(config.getIndentationStyle()); + task.getJavaBaseline().convention(config.getJavaBaseline()); }); return provider; } diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/tasks/FormatterTask.java b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/tasks/FormatterTask.java index 64486ee2..88089e3a 100644 --- a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/tasks/FormatterTask.java +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/tasks/FormatterTask.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2021 the original author or authors. + * Copyright 2017-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,10 +19,13 @@ import java.nio.charset.Charset; import java.util.stream.Stream; +import org.gradle.api.provider.Property; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.SourceTask; +import io.spring.javaformat.config.IndentationStyle; +import io.spring.javaformat.config.JavaBaseline; import io.spring.javaformat.config.JavaFormatConfig; import io.spring.javaformat.formatter.FileEdit; import io.spring.javaformat.formatter.FileFormatter; @@ -36,7 +39,13 @@ public abstract class FormatterTask extends SourceTask { private String encoding; + private final Property indentationStyle; + + private final Property javaBaseline; + FormatterTask() { + this.indentationStyle = getProject().getObjects().property(IndentationStyle.class); + this.javaBaseline = getProject().getObjects().property(JavaBaseline.class); } /** @@ -57,12 +66,30 @@ public void setEncoding(String encoding) { this.encoding = encoding; } + /** + * The indentation style used for formatting. + * @return the indentation style + */ + @Input + public Property getIndentationStyle() { + return this.indentationStyle; + } + + /** + * The Java baseline used for formatting. + * @return the Java baseline + */ + @Input + public Property getJavaBaseline() { + return this.javaBaseline; + } + /** * Format the source files and provide a {@link Stream} of {@link FileEdit} instances. * @return the file edits */ protected final Stream formatFiles() { - JavaFormatConfig javaFormatConfig = JavaFormatConfig.findFrom(getProject().getProjectDir()); + JavaFormatConfig javaFormatConfig = JavaFormatConfig.of(this.javaBaseline.get(), this.indentationStyle.get()); FileFormatter formatter = new FileFormatter(javaFormatConfig); Charset encoding = (getEncoding() != null ? Charset.forName(getEncoding()) : Charset.defaultCharset()); return formatter.formatFiles(getSource().getFiles(), encoding); diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java index 6b0a5ba0..6e38db7b 100644 --- a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java @@ -22,6 +22,7 @@ import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; +import java.util.Arrays; import java.util.Collections; import java.util.stream.Stream; @@ -77,6 +78,41 @@ public void whenFirstInvocationSucceedsAndSourceIsModifiedThenSecondInvocationSu assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); } + @Test + public void whenFirstInvocationSucceedsAndIndentationStyleIsChangedThenSecondInvocationFails() throws IOException { + GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-ok"); + BuildResult result = gradleBuild.build("check"); + assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); + Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(), + Arrays.asList("indentation-style=spaces")); + result = gradleBuild.buildAndFail("check"); + assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED); + } + + @Test + public void whenFirstInvocationFailsAndIndentationStyleIsChangedThenSecondInvocationSucceeds() throws IOException { + GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-spaces"); + BuildResult result = gradleBuild.buildAndFail("check"); + assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED); + Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(), + Arrays.asList("indentation-style=spaces")); + result = gradleBuild.build("check"); + assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); + } + + @Test + public void whenFirstInvocationSucceedsAndJavaBaselineIsChangedThenSecondInvocationSucceedsAndThirdIsUpToDate() throws IOException { + GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-ok"); + BuildResult result = gradleBuild.build("check"); + assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); + Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(), + Arrays.asList("java-baseline=8")); + result = gradleBuild.build("check"); + assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); + result = gradleBuild.build("check"); + assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE); + } + @Test public void checkBad() throws IOException { BuildResult result = this.gradleBuild.source("src/test/resources/check-bad").buildAndFail("check"); diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-spaces/build.gradle b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-spaces/build.gradle new file mode 100644 index 00000000..d6ca2f32 --- /dev/null +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-spaces/build.gradle @@ -0,0 +1,10 @@ +buildscript { + dependencies { + classpath files(pluginClasspath.split(',')) + } +} + +apply plugin: 'java' +apply plugin: 'io.spring.javaformat' + +sourceCompatibility = 1.8 diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-spaces/src/main/java/simple/Simple.java b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-spaces/src/main/java/simple/Simple.java new file mode 100644 index 00000000..cd5523a6 --- /dev/null +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-spaces/src/main/java/simple/Simple.java @@ -0,0 +1,15 @@ +package simple; + +/** + * Simple indented with spaces. + * + * @author Andy Wilkinson + * @since 1.0.0 + */ +public class Simple { + + public static void main(String[] args) throws Exception { + // Main method + } + +}