Skip to content

Commit

Permalink
Polish 'Add lineSeparator support to Maven Plugin'
Browse files Browse the repository at this point in the history
See gh-212
  • Loading branch information
philwebb committed Dec 3, 2020
1 parent 14fefea commit dc18d1a
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.spring.javaformat</groupId>
<artifactId>apply-line-separator</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>apply</goal>
</goals>
<configuration>
<lineSeparator>CR</lineSeparator>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package simple;

/**
* Simple.
* @author Phillip Webb
* @since 1.0.0
*/
public class Simple {

public static void main(String[] args) throws Exception {
// Main method
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new io.spring.format.maven.VerifyApply().verify(basedir, "\r")
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
public class ApplyMojo extends FormatMojo {

@Override
protected void execute(List<File> files, Charset encoding) throws MojoExecutionException, MojoFailureException {
protected void execute(List<File> files, Charset encoding, String lineSeparator)
throws MojoExecutionException, MojoFailureException {
try {
FileFormatter formatter = new FileFormatter();
formatter.formatFiles(files, encoding).filter(FileEdit::hasEdits).forEach(this::save);
formatter.formatFiles(files, encoding, lineSeparator).filter(FileEdit::hasEdits).forEach(this::save);
}
catch (FileFormatterException ex) {
throw new MojoExecutionException("Unable to format file " + ex.getFile(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -47,6 +50,15 @@ public abstract class FormatMojo extends AbstractMojo {

private static final String GENERATED_TEST_SOURCES = File.separator + "generated-test-sources" + File.separator;

private static final Map<String, String> LINE_SEPARATOR;
static {
Map<String, String> lineSeparator = new LinkedHashMap<>();
lineSeparator.put("cr", "\r");
lineSeparator.put("lf", "\n");
lineSeparator.put("crlf", "\r\n");
LINE_SEPARATOR = Collections.unmodifiableMap(lineSeparator);
}

/**
* The Maven Project Object.
*/
Expand Down Expand Up @@ -92,14 +104,10 @@ public abstract class FormatMojo extends AbstractMojo {
* Specifies the line separator to use when formatting.
*/
@Parameter(property = "spring-javaformat.lineSeparator")
private LineSeparator lineSeparator;
private String lineSeparator;

@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
if (this.lineSeparator != null) {
System.getProperties().setProperty("line.separator", this.lineSeparator.getSymbol());
}

List<File> directories = new ArrayList<>();
resolve(this.sourceDirectories).forEach(directories::add);
resolve(this.testSourceDirectories).forEach(directories::add);
Expand All @@ -108,7 +116,14 @@ public final void execute() throws MojoExecutionException, MojoFailureException
files.addAll(scan(directory));
}
Charset encoding = (this.encoding == null ? StandardCharsets.UTF_8 : Charset.forName(this.encoding));
execute(files, encoding);
String lineSeparator = null;
if (this.lineSeparator != null) {
lineSeparator = LINE_SEPARATOR.get(this.lineSeparator.toLowerCase());
if (lineSeparator == null) {
throw new MojoExecutionException("Unknown lineSeparator " + this.lineSeparator);
}
}
execute(files, encoding, this.lineSeparator != null ? lineSeparator : null);
}

private Stream<File> resolve(List<String> directories) {
Expand Down Expand Up @@ -162,41 +177,11 @@ private boolean hasLength(Object[] array) {
* Perform the formatting build-process behavior this {@code Mojo} implements.
* @param files the files to process
* @param encoding the encoding
* @param lineSeparator the line separator
* @throws MojoExecutionException on execution error
* @throws MojoFailureException on failure
*/
protected abstract void execute(List<File> files, Charset encoding)
protected abstract void execute(List<File> files, Charset encoding, String lineSeparator)
throws MojoExecutionException, MojoFailureException;


/*
* The types of line separator. {@link FormatMojo#lineSeparator}
*/
enum LineSeparator {

/**
* Carriage Return.
*/
CR("\r"),

/**
* Linefeed.
*/
LF("\n"),

/**
* Carriage Return & Linefeed.
*/
CRLF("\r\n");

LineSeparator(String symbol) {
this.symbol = symbol;
}

private final String symbol;

private String getSymbol() {
return this.symbol;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ public class ValidateMojo extends FormatMojo {
private boolean skip;

@Override
protected void execute(List<File> files, Charset encoding) throws MojoExecutionException, MojoFailureException {
protected void execute(List<File> files, Charset encoding, String lineSeparator)
throws MojoExecutionException, MojoFailureException {
if (this.skip) {
getLog().debug("skipping validation as per configuration.");
return;
}
FileFormatter formatter = new FileFormatter();
List<File> problems = formatter.formatFiles(files, encoding).filter(FileEdit::hasEdits).map(FileEdit::getFile)
.collect(Collectors.toList());
List<File> problems = formatter.formatFiles(files, encoding, lineSeparator).filter(FileEdit::hasEdits)
.map(FileEdit::getFile).collect(Collectors.toList());
if (!problems.isEmpty()) {
StringBuilder message = new StringBuilder("Formatting violations found in the following files:\n");
problems.stream().forEach((f) -> message.append(" * " + f + "\n"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ public class VerifyApply {
private static final String JAVA_FILE = "src/main/java/simple/Simple.java";

public void verify(File base) throws IOException {
verify(base, LF);
}

public void verify(File base, String lineSeparator) throws IOException {
String formated = new String(Files.readAllBytes(base.toPath().resolve(JAVA_FILE)), StandardCharsets.UTF_8);
assertThat(formated).contains("Simple." + LF + " *" + LF + " * @author").contains("public class Simple {");
assertThat(formated).contains("Simple." + lineSeparator + " *" + lineSeparator + " * @author")
.contains("public class Simple {");
}

public static void main(String[] args) throws IOException {
new VerifyApply().verify(new File(
"/Users/pwebb/projects/spring-javaformat/code/spring-javaformat-maven/spring-javaformat-maven-plugin/target/it/apply-line-separator"),
"\r");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,19 @@ public FileFormatter(Formatter formatter) {
* @return a stream of formatted files that have edits
*/
public Stream<FileEdit> formatFiles(Iterable<File> files, Charset encoding) {
return formatFiles(StreamSupport.stream(files.spliterator(), false), encoding);
return formatFiles(files, encoding, Formatter.DEFAULT_LINE_SEPARATOR);
}

/**
* Format the given source files and provide a {@link Stream} of {@link FileEdit}
* instances.
* @param files the files to format
* @param encoding the source encoding
* @param lineSeparator the line separator
* @return a stream of formatted files that have edits
*/
public Stream<FileEdit> formatFiles(Iterable<File> files, Charset encoding, String lineSeparator) {
return formatFiles(StreamSupport.stream(files.spliterator(), false), encoding, lineSeparator);
}

/**
Expand All @@ -67,7 +79,19 @@ public Stream<FileEdit> formatFiles(Iterable<File> files, Charset encoding) {
* @return a stream of formatted files that have edits
*/
public Stream<FileEdit> formatFiles(Stream<File> files, Charset encoding) {
return files.map((file) -> formatFile(file, encoding));
return formatFiles(files, encoding, Formatter.DEFAULT_LINE_SEPARATOR);
}

/**
* Format the given source files and provide a {@link Stream} of {@link FileEdit}
* instances.
* @param files the files to format
* @param encoding the source encoding
* @param lineSeparator the line separator
* @return a stream of formatted files that have edits
*/
public Stream<FileEdit> formatFiles(Stream<File> files, Charset encoding, String lineSeparator) {
return files.map((file) -> formatFile(file, encoding, lineSeparator));
}

/**
Expand All @@ -77,9 +101,20 @@ public Stream<FileEdit> formatFiles(Stream<File> files, Charset encoding) {
* @return a formatted file
*/
public FileEdit formatFile(File file, Charset encoding) {
return formatFile(file, encoding, Formatter.DEFAULT_LINE_SEPARATOR);
}

/**
* Format the the given source file and return a {@link FileEdit} instance.
* @param file the file to format
* @param encoding the source encoding
* @param lineSeparator the line separator
* @return a formatted file
*/
public FileEdit formatFile(File file, Charset encoding, String lineSeparator) {
try {
String content = new String(Files.readAllBytes(file.toPath()), encoding);
TextEdit edit = this.formatter.format(content);
TextEdit edit = this.formatter.format(content, lineSeparator);
return new FileEdit(file, encoding, content, edit);
}
catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class Formatter extends CodeFormatter {
/**
* The default line separator.
*/
private static final String DEFAULT_LINE_SEPARATOR = null;
public static final String DEFAULT_LINE_SEPARATOR = null;

private final Set<FormatterOption> options;

Expand All @@ -81,7 +81,17 @@ public Formatter(FormatterOption... options) {
* @return the text edit
*/
public TextEdit format(String source) {
return format(source, 0, source.length());
return format(source, DEFAULT_LINE_SEPARATOR);
}

/**
* Format the given source content.
* @param source the source content to format
* @param lineSeparator the line separator
* @return the text edit
*/
public TextEdit format(String source, String lineSeparator) {
return format(source, 0, source.length(), lineSeparator);
}

/**
Expand All @@ -92,7 +102,19 @@ public TextEdit format(String source) {
* @return the text edit
*/
public TextEdit format(String source, int offset, int length) {
return format(DEFAULT_COMPONENTS, source, offset, length, DEFAULT_INDENTATION_LEVEL, DEFAULT_LINE_SEPARATOR);
return format(source, offset, length, DEFAULT_LINE_SEPARATOR);
}

/**
* Format a specific subsection of the given source content.
* @param source the source content to format
* @param offset the offset to start formatting
* @param length the length to format
* @param lineSeparator the line separator
* @return the text edit
*/
public TextEdit format(String source, int offset, int length, String lineSeparator) {
return format(DEFAULT_COMPONENTS, source, offset, length, DEFAULT_INDENTATION_LEVEL, lineSeparator);
}

@Override
Expand Down

0 comments on commit dc18d1a

Please sign in to comment.