From 78cec83a2a0d938e6e2648c1ead6a59d965a261e Mon Sep 17 00:00:00 2001 From: Marco Gora Date: Mon, 11 Oct 2021 13:42:59 +0200 Subject: [PATCH 1/2] Optional hooks file name config --- .../cosium/code/format/InstallHooksMojo.java | 74 ++++++++++--------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/cosium/code/format/InstallHooksMojo.java b/src/main/java/com/cosium/code/format/InstallHooksMojo.java index 7cd5be1..4e0ae80 100644 --- a/src/main/java/com/cosium/code/format/InstallHooksMojo.java +++ b/src/main/java/com/cosium/code/format/InstallHooksMojo.java @@ -1,10 +1,6 @@ package com.cosium.code.format; import static java.util.Optional.ofNullable; - -import com.cosium.code.format.executable.Executable; -import com.cosium.code.format.executable.ExecutableManager; -import com.cosium.code.format.maven.MavenEnvironment; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -13,6 +9,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.maven.plugin.MojoExecutionException; @@ -20,6 +17,9 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import com.cosium.code.format.executable.Executable; +import com.cosium.code.format.executable.ExecutableManager; +import com.cosium.code.format.maven.MavenEnvironment; /** * Installs git hooks on each initialization. Hooks are always overriden in case of changes in: @@ -74,13 +74,17 @@ public class InstallHooksMojo extends AbstractMavenGitCodeFormatMojo { @Parameter(property = "gcf.preCommitHookPipeline", defaultValue = "") private String preCommitHookPipeline; + @Parameter(property = "gcf.hookScriptName") + private String hookScriptName; + + @Override public void execute() throws MojoExecutionException { if (!isExecutionRoot()) { getLog().debug("Not in execution root. Do not execute."); return; } - if (skip || skipInstallHooks) { - Log log = getLog(); + if (this.skip || this.skipInstallHooks) { + final Log log = getLog(); if (log.isInfoEnabled()) { log.info("skipped"); } @@ -91,42 +95,42 @@ public void execute() throws MojoExecutionException { getLog().info("Installing git hooks"); doExecute(); getLog().info("Installed git hooks"); - } catch (Exception e) { + } catch (final Exception e) { throw new MojoExecutionException(e.getMessage(), e); } } private void doExecute() throws IOException { - Path hooksDirectory = prepareHooksDirectory(); + final Path hooksDirectory = prepareHooksDirectory(); writePluginHooks(hooksDirectory); configureHookBaseScripts(hooksDirectory); } - private void writePluginHooks(Path hooksDirectory) throws IOException { + private void writePluginHooks(final Path hooksDirectory) throws IOException { getLog().debug("Removing legacy pre commit hook file"); Files.deleteIfExists(hooksDirectory.resolve(legacyPluginPreCommitHookFileName())); getLog().debug("Rmeoved legacy pre commit hook file"); getLog().debug("Writing plugin pre commit hook file"); - executableManager - .getOrCreateExecutableScript(hooksDirectory.resolve(pluginPreCommitHookFileName())) - .truncateWithTemplate( - () -> getClass().getResourceAsStream(BASE_PLUGIN_PRE_COMMIT_HOOK), - StandardCharsets.UTF_8.toString(), - mavenEnvironment.getMavenExecutable(debug).toAbsolutePath(), - pomFile().toAbsolutePath(), - mavenCliArguments()); + this.executableManager + .getOrCreateExecutableScript(hooksDirectory.resolve(pluginPreCommitHookFileName())) + .truncateWithTemplate( + () -> getClass().getResourceAsStream(BASE_PLUGIN_PRE_COMMIT_HOOK), + StandardCharsets.UTF_8.toString(), + this.mavenEnvironment.getMavenExecutable(this.debug).toAbsolutePath(), + pomFile().toAbsolutePath(), + mavenCliArguments()); getLog().debug("Written plugin pre commit hook file"); } - private void configureHookBaseScripts(Path hooksDirectory) throws IOException { - Executable basePreCommitHook = - executableManager.getOrCreateExecutableScript( + private void configureHookBaseScripts(final Path hooksDirectory) throws IOException { + final Executable basePreCommitHook = + this.executableManager.getOrCreateExecutableScript( hooksDirectory.resolve(PRE_COMMIT_HOOK_BASE_SCRIPT)); getLog().debug("Configuring '" + basePreCommitHook + "'"); - if (truncateHooksBaseScripts) { + if (this.truncateHooksBaseScripts) { basePreCommitHook.truncate(); } else { legacyPreCommitHookBaseScriptCalls().forEach(basePreCommitHook::removeCommandCall); @@ -135,15 +139,15 @@ private void configureHookBaseScripts(Path hooksDirectory) throws IOException { } private String mavenCliArguments() { - Stream propagatedProperties = - ofNullable(propertiesToPropagate).map(Arrays::asList).orElse(Collections.emptyList()) - .stream() - .filter(prop -> System.getProperty(prop) != null) - .map(prop -> "-D" + prop + "=" + System.getProperty(prop)); - - Stream properties = Stream.concat(propagatedProperties, Stream.of(propertiesToAdd)); - if (preCommitHookPipeline != null && !preCommitHookPipeline.isEmpty()) { - properties = Stream.concat(properties, Stream.of(preCommitHookPipeline)); + final Stream propagatedProperties = + ofNullable(this.propertiesToPropagate).map(Arrays::asList).orElse(Collections.emptyList()) + .stream() + .filter(prop -> System.getProperty(prop) != null) + .map(prop -> "-D" + prop + "=" + System.getProperty(prop)); + + Stream properties = Stream.concat(propagatedProperties, Stream.of(this.propertiesToAdd)); + if (this.preCommitHookPipeline != null && !this.preCommitHookPipeline.isEmpty()) { + properties = Stream.concat(properties, Stream.of(this.preCommitHookPipeline)); } return properties.collect(Collectors.joining(" ")); } @@ -161,7 +165,7 @@ private String preCommitHookBaseScriptCall() { } private List legacyPreCommitHookBaseScriptCalls() { - List calls = new ArrayList<>(); + final List calls = new ArrayList<>(); calls.add( "./" + gitBaseDir().relativize(getOrCreateHooksDirectory()) @@ -175,11 +179,15 @@ private List legacyPreCommitHookBaseScriptCalls() { return calls; } + private String getHookScriptName() { + return Optional.ofNullable(this.hookScriptName).orElseGet(this::artifactId); + } + private String pluginPreCommitHookFileName() { - return artifactId() + "." + BASE_PLUGIN_PRE_COMMIT_HOOK; + return getHookScriptName() + "." + BASE_PLUGIN_PRE_COMMIT_HOOK; } private String legacyPluginPreCommitHookFileName() { - return artifactId() + "." + LEGACY_BASE_PLUGIN_PRE_COMMIT_HOOK; + return getHookScriptName() + "." + LEGACY_BASE_PLUGIN_PRE_COMMIT_HOOK; } } From 002b296e7425207593cb5066aaed84bec179c3a5 Mon Sep 17 00:00:00 2001 From: Marco Gora Date: Mon, 11 Oct 2021 14:01:57 +0200 Subject: [PATCH 2/2] format all according to google standard --- .../AbstractModuleMavenGitCodeFormatMojo.java | 3 +- .../cosium/code/format/FormatCodeMojo.java | 7 ++-- .../cosium/code/format/InstallHooksMojo.java | 36 ++++++++++--------- .../com/cosium/code/format/TemporaryFile.java | 3 +- .../executable/DefaultCommandRunner.java | 7 ++-- .../code/format/git/AutoCRLFObjectReader.java | 11 +++--- .../code/format/git/AutoCRLFObjectStream.java | 9 +++-- .../code/format/git/AutoCRLFRepository.java | 9 +++-- .../cosium/code/format/git/GitIndexEntry.java | 8 +++-- .../code/format/git/GitStagedFiles.java | 23 ++++++------ .../com/cosium/code/format/git/Index.java | 3 +- 11 files changed, 60 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/cosium/code/format/AbstractModuleMavenGitCodeFormatMojo.java b/src/main/java/com/cosium/code/format/AbstractModuleMavenGitCodeFormatMojo.java index cb137aa..fdceeeb 100644 --- a/src/main/java/com/cosium/code/format/AbstractModuleMavenGitCodeFormatMojo.java +++ b/src/main/java/com/cosium/code/format/AbstractModuleMavenGitCodeFormatMojo.java @@ -44,7 +44,8 @@ private boolean isEnabled() { if ((!includedModules.isEmpty() || !excludedModules.isEmpty()) && isExecutionRoot()) { getLog() .info( - "Explicit included or excluded modules defined and the current module the execution root. Goal disabled."); + "Explicit included or excluded modules defined and the current module the execution" + + " root. Goal disabled."); return false; } diff --git a/src/main/java/com/cosium/code/format/FormatCodeMojo.java b/src/main/java/com/cosium/code/format/FormatCodeMojo.java index 68cbe37..1f5f309 100644 --- a/src/main/java/com/cosium/code/format/FormatCodeMojo.java +++ b/src/main/java/com/cosium/code/format/FormatCodeMojo.java @@ -2,15 +2,14 @@ import com.cosium.code.format.formatter.CodeFormatter; import com.cosium.code.format.formatter.LineRanges; -import org.apache.commons.io.IOUtils; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; +import org.apache.commons.io.IOUtils; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; /** * Created on 07/11/17. diff --git a/src/main/java/com/cosium/code/format/InstallHooksMojo.java b/src/main/java/com/cosium/code/format/InstallHooksMojo.java index 4e0ae80..2c1668e 100644 --- a/src/main/java/com/cosium/code/format/InstallHooksMojo.java +++ b/src/main/java/com/cosium/code/format/InstallHooksMojo.java @@ -1,6 +1,10 @@ package com.cosium.code.format; import static java.util.Optional.ofNullable; + +import com.cosium.code.format.executable.Executable; +import com.cosium.code.format.executable.ExecutableManager; +import com.cosium.code.format.maven.MavenEnvironment; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -17,9 +21,6 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import com.cosium.code.format.executable.Executable; -import com.cosium.code.format.executable.ExecutableManager; -import com.cosium.code.format.maven.MavenEnvironment; /** * Installs git hooks on each initialization. Hooks are always overriden in case of changes in: @@ -115,13 +116,13 @@ private void writePluginHooks(final Path hooksDirectory) throws IOException { getLog().debug("Writing plugin pre commit hook file"); this.executableManager - .getOrCreateExecutableScript(hooksDirectory.resolve(pluginPreCommitHookFileName())) - .truncateWithTemplate( - () -> getClass().getResourceAsStream(BASE_PLUGIN_PRE_COMMIT_HOOK), - StandardCharsets.UTF_8.toString(), - this.mavenEnvironment.getMavenExecutable(this.debug).toAbsolutePath(), - pomFile().toAbsolutePath(), - mavenCliArguments()); + .getOrCreateExecutableScript(hooksDirectory.resolve(pluginPreCommitHookFileName())) + .truncateWithTemplate( + () -> getClass().getResourceAsStream(BASE_PLUGIN_PRE_COMMIT_HOOK), + StandardCharsets.UTF_8.toString(), + this.mavenEnvironment.getMavenExecutable(this.debug).toAbsolutePath(), + pomFile().toAbsolutePath(), + mavenCliArguments()); getLog().debug("Written plugin pre commit hook file"); } @@ -140,12 +141,15 @@ private void configureHookBaseScripts(final Path hooksDirectory) throws IOExcept private String mavenCliArguments() { final Stream propagatedProperties = - ofNullable(this.propertiesToPropagate).map(Arrays::asList).orElse(Collections.emptyList()) - .stream() - .filter(prop -> System.getProperty(prop) != null) - .map(prop -> "-D" + prop + "=" + System.getProperty(prop)); - - Stream properties = Stream.concat(propagatedProperties, Stream.of(this.propertiesToAdd)); + ofNullable(this.propertiesToPropagate) + .map(Arrays::asList) + .orElse(Collections.emptyList()) + .stream() + .filter(prop -> System.getProperty(prop) != null) + .map(prop -> "-D" + prop + "=" + System.getProperty(prop)); + + Stream properties = + Stream.concat(propagatedProperties, Stream.of(this.propertiesToAdd)); if (this.preCommitHookPipeline != null && !this.preCommitHookPipeline.isEmpty()) { properties = Stream.concat(properties, Stream.of(this.preCommitHookPipeline)); } diff --git a/src/main/java/com/cosium/code/format/TemporaryFile.java b/src/main/java/com/cosium/code/format/TemporaryFile.java index be4a2b7..5d4c389 100644 --- a/src/main/java/com/cosium/code/format/TemporaryFile.java +++ b/src/main/java/com/cosium/code/format/TemporaryFile.java @@ -1,13 +1,12 @@ package com.cosium.code.format; -import org.apache.maven.plugin.logging.Log; - import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; +import org.apache.maven.plugin.logging.Log; /** @author Réda Housni Alaoui */ public class TemporaryFile implements Closeable { diff --git a/src/main/java/com/cosium/code/format/executable/DefaultCommandRunner.java b/src/main/java/com/cosium/code/format/executable/DefaultCommandRunner.java index 1bcaef8..1f7b279 100644 --- a/src/main/java/com/cosium/code/format/executable/DefaultCommandRunner.java +++ b/src/main/java/com/cosium/code/format/executable/DefaultCommandRunner.java @@ -1,14 +1,13 @@ package com.cosium.code.format.executable; import com.cosium.code.format.MavenGitCodeFormatException; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.maven.plugin.logging.Log; - import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.function.Supplier; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.maven.plugin.logging.Log; /** @author Réda Housni Alaoui */ public class DefaultCommandRunner implements CommandRunner { diff --git a/src/main/java/com/cosium/code/format/git/AutoCRLFObjectReader.java b/src/main/java/com/cosium/code/format/git/AutoCRLFObjectReader.java index ac792ff..67ee3da 100644 --- a/src/main/java/com/cosium/code/format/git/AutoCRLFObjectReader.java +++ b/src/main/java/com/cosium/code/format/git/AutoCRLFObjectReader.java @@ -1,5 +1,10 @@ package com.cosium.code.format.git; +import static java.util.Objects.requireNonNull; + +import java.io.IOException; +import java.util.Collection; +import java.util.Set; import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.lib.AbbreviatedObjectId; @@ -9,12 +14,6 @@ import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.ObjectReader; -import java.io.IOException; -import java.util.Collection; -import java.util.Set; - -import static java.util.Objects.requireNonNull; - /** @author Réda Housni Alaoui */ public class AutoCRLFObjectReader extends ObjectReader { diff --git a/src/main/java/com/cosium/code/format/git/AutoCRLFObjectStream.java b/src/main/java/com/cosium/code/format/git/AutoCRLFObjectStream.java index 8e70a2a..6bf3745 100644 --- a/src/main/java/com/cosium/code/format/git/AutoCRLFObjectStream.java +++ b/src/main/java/com/cosium/code/format/git/AutoCRLFObjectStream.java @@ -1,13 +1,12 @@ package com.cosium.code.format.git; -import org.eclipse.jgit.lib.CoreConfig.EolStreamType; -import org.eclipse.jgit.lib.ObjectStream; -import org.eclipse.jgit.util.io.EolStreamTypeUtil; +import static java.util.Objects.requireNonNull; import java.io.IOException; import java.io.InputStream; - -import static java.util.Objects.requireNonNull; +import org.eclipse.jgit.lib.CoreConfig.EolStreamType; +import org.eclipse.jgit.lib.ObjectStream; +import org.eclipse.jgit.util.io.EolStreamTypeUtil; /** @author Réda Housni Alaoui */ public class AutoCRLFObjectStream extends ObjectStream { diff --git a/src/main/java/com/cosium/code/format/git/AutoCRLFRepository.java b/src/main/java/com/cosium/code/format/git/AutoCRLFRepository.java index 8327c8d..2cd57f7 100644 --- a/src/main/java/com/cosium/code/format/git/AutoCRLFRepository.java +++ b/src/main/java/com/cosium/code/format/git/AutoCRLFRepository.java @@ -1,13 +1,12 @@ package com.cosium.code.format.git; -import org.eclipse.jgit.internal.storage.file.FileRepository; -import org.eclipse.jgit.lib.CoreConfig.EolStreamType; -import org.eclipse.jgit.lib.ObjectReader; +import static java.util.Objects.requireNonNull; import java.io.File; import java.io.IOException; - -import static java.util.Objects.requireNonNull; +import org.eclipse.jgit.internal.storage.file.FileRepository; +import org.eclipse.jgit.lib.CoreConfig.EolStreamType; +import org.eclipse.jgit.lib.ObjectReader; /** @author Réda Housni Alaoui */ public class AutoCRLFRepository extends FileRepository { diff --git a/src/main/java/com/cosium/code/format/git/GitIndexEntry.java b/src/main/java/com/cosium/code/format/git/GitIndexEntry.java index 40292eb..ab0fbbc 100644 --- a/src/main/java/com/cosium/code/format/git/GitIndexEntry.java +++ b/src/main/java/com/cosium/code/format/git/GitIndexEntry.java @@ -144,8 +144,12 @@ private LineRanges computeLineRanges(DirCacheEntry dirCacheEntry) { } try { - return git.diff().setPathFilter(PathFilter.create(dirCacheEntry.getPathString())) - .setCached(true).call().stream() + return git + .diff() + .setPathFilter(PathFilter.create(dirCacheEntry.getPathString())) + .setCached(true) + .call() + .stream() .map(this::computeLineRanges) .reduce(LineRanges::concat) .orElse(LineRanges.all()); diff --git a/src/main/java/com/cosium/code/format/git/GitStagedFiles.java b/src/main/java/com/cosium/code/format/git/GitStagedFiles.java index c84aa26..e640020 100644 --- a/src/main/java/com/cosium/code/format/git/GitStagedFiles.java +++ b/src/main/java/com/cosium/code/format/git/GitStagedFiles.java @@ -1,8 +1,19 @@ package com.cosium.code.format.git; +import static java.util.Objects.requireNonNull; + import com.cosium.code.format.MavenGitCodeFormatException; import com.cosium.code.format.TemporaryFile; import com.cosium.code.format.formatter.CodeFormatters; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Path; +import java.util.Collections; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.maven.plugin.logging.Log; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Status; @@ -16,18 +27,6 @@ import org.eclipse.jgit.treewalk.AbstractTreeIterator; import org.eclipse.jgit.treewalk.WorkingTreeOptions; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Path; -import java.util.Collections; -import java.util.Set; -import java.util.function.Predicate; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - /** @author Réda Housni Alaoui */ public class GitStagedFiles { diff --git a/src/main/java/com/cosium/code/format/git/Index.java b/src/main/java/com/cosium/code/format/git/Index.java index af8074e..9266c15 100644 --- a/src/main/java/com/cosium/code/format/git/Index.java +++ b/src/main/java/com/cosium/code/format/git/Index.java @@ -1,13 +1,12 @@ package com.cosium.code.format.git; +import java.io.IOException; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheEditor; import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.treewalk.AbstractTreeIterator; -import java.io.IOException; - /** @author Réda Housni Alaoui */ public class Index implements AutoCloseable {