Skip to content

Commit

Permalink
Merge branch 'master' into prevent-resource-leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
reda-alaoui authored Oct 13, 2023
2 parents ce61058 + 3d9acc5 commit 7d67e93
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
A maven plugin that automatically deploys code formatters as `pre-commit` git hook.
On commit, the hook will automatically format staged files.

# Breaking changes between 4.x and 5.x

* If the plugin runs without any formatter dependency, it will fail. This is done to prevent silent misconfiguration from happening.

# Breaking changes between 3.x and 4.x

* `Google Java Format` is not enabled by default anymore. `com.cosium.code:google-java-format` must be added as a dependency to the plugin to keep using it.
Expand Down
4 changes: 2 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.cosium.code</groupId>
<artifactId>git-code-format-maven-plugin-parent</artifactId>
<version>4.3-SNAPSHOT</version>
<version>5.2-SNAPSHOT</version>
</parent>

<artifactId>git-code-format-maven-plugin</artifactId>
Expand Down Expand Up @@ -115,7 +115,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.8.2</version>
<version>3.9.0</version>
<configuration>
<goalPrefix>git-code-format</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ private List<CodeFormatter> createCodeFormatters() {

List<CodeFormatterFactory> formatterFactories = new ArrayList<>();
ServiceLoader.load(CodeFormatterFactory.class).forEach(formatterFactories::add);
if (formatterFactories.isEmpty()) {
throw new IllegalStateException(
"No "
+ CodeFormatter.class
+ " instance found. You probably forgot to declare a code formatter as a plugin's dependency. You can use https://github.com/Cosium/git-code-format-maven-plugin#automatic-code-format-and-validation-activation as an example.");
}

CodeFormatterConfigurationFactory formatterConfigurationFactory =
new CodeFormatterConfigurationFactory(
Expand Down
8 changes: 5 additions & 3 deletions core/src/main/java/com/cosium/code/format/FormatCodeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ protected void process(CodeFormatters codeFormatters, Path path) {
}

private void format(Path path, CodeFormatter formatter) {
getLog().debug("Formatting '" + gitBaseDir().relativize(path) + "'");
Path relativePath = gitBaseDir().relativize(path);
getLog().debug("Formatting '" + relativePath + "'");

try (TemporaryFile temporaryFormattedFile =
TemporaryFile.create(getLog(), path + ".formatted")) {
Expand All @@ -40,8 +41,9 @@ private void format(Path path, CodeFormatter formatter) {
OutputStream unformattedContent = Files.newOutputStream(path)) {
IOUtils.copy(formattedContent, unformattedContent);
}
} catch (IOException e) {
throw new MavenGitCodeFormatException(e);
} catch (IOException | RuntimeException e) {
throw new MavenGitCodeFormatException(
String.format("Failed to format '%s': %s", relativePath, e.getMessage()), e);
}

getLog().debug("Formatted '" + gitBaseDir().relativize(path) + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ private boolean validate(CodeFormatters codeFormatters, Path path) {
}

private boolean doValidate(Path path, CodeFormatter formatter) {
getLog().debug("Validating '" + gitBaseDir().relativize(path) + "'");
Path relativePath = gitBaseDir().relativize(path);
getLog().debug("Validating '" + relativePath + "'");
try (InputStream content = Files.newInputStream(path)) {
return formatter.validate(content);
} catch (IOException e) {
throw new MavenGitCodeFormatException(e);
} catch (IOException | RuntimeException e) {
throw new MavenGitCodeFormatException(
String.format("Failed to validate '%s': %s", relativePath, e.getMessage()), e);
}
}
}
2 changes: 1 addition & 1 deletion google-java-format/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.cosium.code</groupId>
<artifactId>git-code-format-maven-plugin-parent</artifactId>
<version>4.3-SNAPSHOT</version>
<version>5.2-SNAPSHOT</version>
</parent>

<artifactId>google-java-format</artifactId>
Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.cosium.code</groupId>
<artifactId>git-code-format-maven-plugin-parent</artifactId>
<version>4.3-SNAPSHOT</version>
<version>5.2-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Git Code Format Maven Plugin Parent</name>
Expand All @@ -15,13 +15,13 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<source.level>1.8</source.level>
<org.apache.commons.io.version>2.13.0</org.apache.commons.io.version>
<org.apache.commons.io.version>2.14.0</org.apache.commons.io.version>
<org.apache.commons.lang.version>3.13.0</org.apache.commons.lang.version>
<org.apache.commons.exec.version>1.3</org.apache.commons.exec.version>
<jgit.version>5.13.1.202206130422-r</jgit.version>
<jacoco.version>0.8.10</jacoco.version>
<takari-plugin-testing.version>3.0.1</takari-plugin-testing.version>
<google-java-format.version>1.17.0</google-java-format.version>
<google-java-format.version>1.18.1</google-java-format.version>
</properties>

<modules>
Expand Down Expand Up @@ -148,7 +148,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<version>3.6.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -165,7 +165,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.cosium.code</groupId>
<artifactId>git-code-format-maven-plugin-parent</artifactId>
<version>4.3-SNAPSHOT</version>
<version>5.2-SNAPSHOT</version>
</parent>

<artifactId>git-code-format-maven-plugin-spi</artifactId>
Expand Down

0 comments on commit 7d67e93

Please sign in to comment.