Skip to content

Commit

Permalink
Optional support for AOSP mode. (#25)
Browse files Browse the repository at this point in the history
* Exposes the Google Java Formatter's Android Open Source Project mode,"aosp"
as a configuration option to the formatter.

- Adds new `style` option which is `google` by default, but can be set to `aosp`
- `AbstractFMT.formatter` is now lazily created and configured with style settings
- Unit tests for
  - unknown style configuration is rejected
  - `google` style is supported
  - `aosp` style is supported
  • Loading branch information
bryanschofield authored and malaporte committed Feb 20, 2018
1 parent 4082ebc commit 0a0e85f
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 10 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ If you prefer, you can only check formatting at build time using the `check` goa

`filesNamePattern` represents the pattern that filters files to format. The defaults value is set to `.*\.java`.

`style` sets the formatter style to be _google_ or _aosp_. By default this is 'google'. Projects using Android conventions may prefer `aosp`.

example:
```xml
<build>
Expand All @@ -88,6 +90,7 @@ example:
<param>some/dir</param>
<param>some/other/dir</param>
</additionalSourceDirectories>
<style>google</style>
</configuration>
<executions>
<execution>
Expand All @@ -109,6 +112,8 @@ example:

`displayLimit` default = 100. Number of files to display that are not compliant`

`style` sets the formatter style to be _google_ or _aosp_. By default this is 'google'. Projects using Android conventions may prefer `aosp`.

example to not display the non-compliant files:
```xml
<build>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.coveo</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.2.0</version>
<version>2.3.0</version>
<packaging>maven-plugin</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
38 changes: 30 additions & 8 deletions src/main/java/com/coveo/AbstractFMT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.google.common.base.Charsets;
import com.google.common.io.CharSource;
import com.google.common.io.Files;
import com.google.googlejavaformat.java.Formatter;
import com.google.googlejavaformat.java.FormatterException;
import com.google.googlejavaformat.java.ImportOrderer;
import com.google.googlejavaformat.java.RemoveUnusedImports;
import com.google.googlejavaformat.java.*;
import com.google.googlejavaformat.java.RemoveUnusedImports.JavadocOnlyImports;
import java.io.File;
import java.io.FileFilter;
Expand All @@ -22,7 +19,8 @@

public abstract class AbstractFMT extends AbstractMojo {
private Log logger = getLog();
private Formatter formatter = new Formatter();
/** Lazily initialized based on style. Use formatter() for access. */
private Formatter formatter;

@Parameter(
defaultValue = "${project.build.sourceDirectory}",
Expand Down Expand Up @@ -50,6 +48,9 @@ public abstract class AbstractFMT extends AbstractMojo {
@Parameter(defaultValue = ".*\\.java", property = "filesNamePattern")
private String filesNamePattern;

@Parameter(defaultValue = "google", property = "style")
private String style;

private List<String> filesProcessed = new ArrayList<String>();
private int nonComplyingFiles;

Expand Down Expand Up @@ -111,7 +112,7 @@ public List<String> getFilesProcessed() {
return filesProcessed;
}

private void formatSourceFilesInDirectory(File directory) {
private void formatSourceFilesInDirectory(File directory) throws MojoFailureException {
if (!directory.isDirectory()) {
logger.info("Directory '" + directory + "' is not a directory. Skipping.");
return;
Expand All @@ -127,6 +128,27 @@ private void formatSourceFilesInDirectory(File directory) {
}
}

private JavaFormatterOptions.Style style() throws MojoFailureException {
if ("aosp".equalsIgnoreCase(style)) {
logger.debug("Using AOSP style");
return JavaFormatterOptions.Style.AOSP;
}
if ("google".equalsIgnoreCase(style)) {
logger.debug("Using Google style");
return JavaFormatterOptions.Style.GOOGLE;
}
String message = "Unknown style '" + style + "'. Expected 'google' or 'aosp'.";
logger.error(message);
throw new MojoFailureException(message);
}

private synchronized Formatter formatter() throws MojoFailureException {
if (formatter == null) {
formatter = new Formatter(JavaFormatterOptions.builder().style(style()).build());
}
return formatter;
}

private FileFilter getFileFilter() {
if (verbose) {
logger.debug("Filter files on '" + filesNamePattern + "'.");
Expand All @@ -139,7 +161,7 @@ public boolean accept(File pathname) {
};
}

private void formatSourceFile(File file) {
private void formatSourceFile(File file) throws MojoFailureException {
if (file.isDirectory()) {
logger.info("File '" + file + "' is a directory. Skipping.");
return;
Expand All @@ -152,7 +174,7 @@ private void formatSourceFile(File file) {
CharSource source = Files.asCharSource(file, Charsets.UTF_8);
try {
String input = source.read();
String formatted = formatter.formatSource(input);
String formatted = formatter().formatSource(input);
formatted = RemoveUnusedImports.removeUnusedImports(formatted, JavadocOnlyImports.KEEP);
formatted = ImportOrderer.reorderImports(formatted);
if (!input.equals(formatted)) {
Expand Down
38 changes: 37 additions & 1 deletion src/test/java/com/coveo/FMTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import static com.google.common.truth.Truth.*;

import java.io.File;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.testing.MojoRule;
import org.junit.Rule;
Expand Down Expand Up @@ -47,6 +49,34 @@ public void withAllTypesOfSources() throws Exception {
assertThat(fmt.getFilesProcessed()).hasSize(3);
}

@Test
public void withAllTypesOfSourcesWithAospStyleSpecified() throws Exception {
FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("simple_aosp"), FORMAT);
fmt.execute();

assertThat(fmt.getFilesProcessed()).hasSize(3);

/* Let's make sure we formatted with AOSP using 4 spaces */
List<String> lines =
IOUtils.readLines(
getClass().getResourceAsStream("/simple_aosp/src/main/java/HelloWorld1.java"));
assertThat(lines.get(3)).startsWith(" public");
}

@Test
public void withAllTypesOfSourcesWithGoogleStyleSpecified() throws Exception {
FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("simple_google"), FORMAT);
fmt.execute();

assertThat(fmt.getFilesProcessed()).hasSize(3);

/* Let's make sure we formatted with Google using 2 spaces */
List<String> lines =
IOUtils.readLines(
getClass().getResourceAsStream("/simple_google/src/main/java/HelloWorld1.java"));
assertThat(lines.get(3)).startsWith(" public");
}

@Test
public void failOnUnknownFolderDoesNotFailWhenEverythingIsThere() throws Exception {
FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("failonerrorwithsources"), FORMAT);
Expand All @@ -61,6 +91,12 @@ public void failOnUnknownFolderFailsWhenAFolderIsMissing() throws Exception {
fmt.execute();
}

@Test(expected = MojoFailureException.class)
public void failOnUnknownStyle() throws Exception {
FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("failonunknownstyle"), FORMAT);
fmt.execute();
}

@Test
public void canAddAdditionalFolders() throws Exception {
FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("additionalfolders"), FORMAT);
Expand Down Expand Up @@ -120,7 +156,7 @@ public void checkSucceedsWhenFormatted() throws Exception {
check.execute();
}

public File loadPom(String folderName) {
private File loadPom(String folderName) {
return new File("src/test/resources/", folderName);
}
}
50 changes: 50 additions & 0 deletions src/test/resources/failonunknownstyle/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Test MyMojo</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.coveo</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<style>nope</style>
</configuration>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>









Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorldTest {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
50 changes: 50 additions & 0 deletions src/test/resources/simple_aosp/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Test MyMojo</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.coveo</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<style>aosp</style>
</configuration>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>









7 changes: 7 additions & 0 deletions src/test/resources/simple_aosp/src/main/java/HelloWorld1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
7 changes: 7 additions & 0 deletions src/test/resources/simple_aosp/src/main/java/HelloWorld2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorldTest {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
50 changes: 50 additions & 0 deletions src/test/resources/simple_google/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Test MyMojo</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.coveo</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<style>google</style>
</configuration>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>









Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Loading

0 comments on commit 0a0e85f

Please sign in to comment.