Skip to content

Add external plugin: "formatter" - #34

Closed
sormuras wants to merge 1 commit into
forax:masterfrom
sormuras:formatter
Closed

sormuras wants to merge 1 commit into
forax:masterfrom
sormuras:formatter

Conversation

@sormuras

@sormuras sormuras commented May 3, 2017 •

Copy link
Copy Markdown
Collaborator

Overview

Based on google-java-format -- a program that reformats Java source code to comply with Google Java Style.

The current implementation uses the --module-path option to launch the program. I didn't managed to make modules via module-fixer out of neither the "-all-deps" distro nor the 3 single jars: google-java-format-1.3.jar, guava-19.0.jar and javac-9-dev-r3297-1-shaded.jar

The "-all-deps" jar is downloaded on-the-fly via Boostrap.download(...) -- a candidate for Pro itself?

The "-all-deps" jar is stored in a subfolder called plugins/formatter/libs/. All jars found in that folder are not resolved, nor module-fixed. They are copied over to the target/pro image as-is.

TODO

  • Review and discuss the plugin implementation -- maybe, it's even better to not include it.
  • add more google-java-format command line options, see details here
  • make javaFiles() configurable
  • pro-internal java provides a "fake-guava" module which collides with the one bundled in google-java-format--all-deps.jar -- is falling back to "external java" a real option? Solved via 156ec79

@sormuras sormuras changed the title Add external plugin "formatter" Add external plugin: "formatter" May 3, 2017
cmdLine.add("com.google.googlejavaformat.java.Main");
// TODO add more options, see details at https://github.com/google/google-java-format/blob/master/core/src/main/java/com/google/googlejavaformat/java/UsageException.java#L30
// cmdLine.add("--replace");
cmdLine.add("--version");

@sormuras sormuras May 3, 2017 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "--version" to actually see the reformated code on the console.
Uncomment "--replace" to replace all .java source files with the formatted output.

@sormuras
sormuras force-pushed the formatter branch 7 times, most recently from 48f248d to 47f7831 Compare May 9, 2017 19:09
Based on https://github.com/google/google-java-format -- a program that
reformats Java source code to comply with Google Java Style:

https://google.github.io/styleguide/javaguide.html

Prior to this commit, the "fake-guava" module baked into pro's internal
modules prevented classes to be loaded from the "all-deps" JAR. Using
java auto-module feature makes the format program execution possible.

Validation mode is default now; use the following to actually format all
sources:

  set("formatter.rawArguments", list("--replace"))
  run("formatter")
@sormuras

Copy link
Copy Markdown
Collaborator Author

After the perfer addition in release v0.10+44a ... I willl give formatter another chance.

@forax

forax commented Mar 15, 2018

Copy link
Copy Markdown
Owner

For the javac-9-dev-r3297-1-shaded.jar, it would be cool if google-java-format had an option to use javac through the service interface as we do in the compiler plugin instead of using its own version.

@sormuras

Copy link
Copy Markdown
Collaborator Author

Sure. That would simplify the integration work.


private List<Path> listAllJavaFiles(FormatterConf formatter) {
Predicate<Path> filter = pathFilenameEndsWith(".java");
// TODO include "module-info.java" files, blocked by https://github.com/google/google-java-format/issues/75

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that bug is fixed, are there still blockers?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't check for a long time. Will rebase and retry.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works like a charm. Removed the filter.

@sormuras

Copy link
Copy Markdown
Collaborator Author

@forax How can I help resolver with a module name?

java.lang.IllegalArgumentException: javac.shaded.9.dev.r4023: Invalid module name: '9' is not a Java identifier

[resolver] Unable to derive module descriptor for R:\dev\github\forax\pro\.\plugins\formatter\target\deps\maven-local\com\google\errorprone\javac-shaded\9-dev-r4023-3\javac-shaded-9-dev-r4023-3.jar

Using this boot strap commands:

    compileAndPackagePlugin("formatter", () -> {
      String gjfVersion = "1.5";
      String guavaVersion = "24.1";
      String javacShadedVersion = "9+181-r4173-1";
      set("resolver.dependencies", list(
          // "Google Java Format"
          "com.google.googlejavaformat=com.google.googlejavaformat:google-java-format:" + gjfVersion,
          "com.google.guava=com.google.guava:guava:" + guavaVersion,
          "com.google.errorprone=com.google.errorprone:javac-shaded:" + javacShadedVersion,
          "com.google.j2objc=com.google.j2objc:j2objc-annotations:1.1",
          "org.codehaus.animal.sniffer.annotations=org.codehaus.mojo:animal-sniffer-annotations:1.14"
      ));
    });

@forax

forax commented Mar 16, 2018

Copy link
Copy Markdown
Owner

The Manifest of the jar do not contains the property Automatic-Module-Name so the module API try to gess a name and fail. The best is to ask @cushon to add the property Automatic-Module-Name.

In the meantime, the only workaround i see is to download the jar by hand, rename it and commit it in the git repo.

@sormuras

Copy link
Copy Markdown
Collaborator Author

GJF 1.6 will include it: google/google-java-format@4d728e7

Thought there was a chance to "modulefix" it. :)

@sormuras

Copy link
Copy Markdown
Collaborator Author

I'll stick with the external java process using google-java-format-1.5-all-deps.jar on the classpath for now.

@sormuras

Copy link
Copy Markdown
Collaborator Author

Superseded by #57

@sormuras sormuras closed this Mar 17, 2018
@sormuras
sormuras deleted the formatter branch March 17, 2018 08:57
@sormuras

Copy link
Copy Markdown
Collaborator Author

@cushon said he'll fix the modular problems within error-prone. Looks like we may move from '-all-deps.jar' and spawing another process to a real module-based plugin.

See google/google-java-format#266 (comment) and a minimal running test at https://github.com/sormuras/bach/tree/master/demo/03-google-java-format-as-library (placing error-prone on the class-path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants