Skip to content

Commit

Permalink
Merge pull request #6 from Dathin/feat/main-refactor
Browse files Browse the repository at this point in the history
feat: reafactored main structure
  • Loading branch information
Dathin committed Nov 9, 2021
2 parents 3493c4e + 0fc670e commit 773b95c
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 244 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mvn spring-javaformat:validate
1 change: 1 addition & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mvn clean test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ git commit -m "Keep calm and commit"
If you didn't like it you can just run uninstall goal and remove the plugin

```sh
mvn jhusky:uninstall
mvn jhusky:uninstall -Ddirectory=.husky
```

# Inspiration
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.dathin</groupId>
<artifactId>jhusky</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.2-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>JHusky</name>
Expand Down Expand Up @@ -62,6 +62,11 @@

<build>
<plugins>
<plugin>
<groupId>io.github.dathin</groupId>
<artifactId>jhusky</artifactId>
<version>1.0.2-SNAPSHOT</version>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
Expand Down
57 changes: 35 additions & 22 deletions src/main/java/io/github/dathin/jhusky/Add.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.dathin.jhusky;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -14,7 +13,7 @@
import java.util.Collections;

@Mojo(name = "add")
public class Add extends AbstractMojo {
public class Add extends HuskyCommand {

@Parameter(property = "hookPath")
private String hookPath;
Expand All @@ -23,28 +22,42 @@ public class Add extends AbstractMojo {
private String command;

@Override
public void execute() throws MojoExecutionException {
try {
Path huskyCommandPath = Paths.get(hookPath);
if (!Files.exists(huskyCommandPath.getParent())) {
throw new MojoExecutionException(
"Can't create hook, " + huskyCommandPath.getParent().getFileName().toString()
+ " directory doesn't exist (try running install goal)");
}
if (Files.exists(huskyCommandPath)) {
Files.write(huskyCommandPath, Arrays.asList("\n", command), StandardOpenOption.APPEND);
getLog().info("Updated");
}
else {
Path createdFile2 = Files.createFile(huskyCommandPath);
createdFile2.toFile().setExecutable(true);
Files.write(createdFile2, Collections.singletonList(command));
getLog().info("Created");
}
public void command() throws MojoExecutionException, IOException {
Path commandPath = Paths.get(hookPath);

checkParentDirectoryExists(commandPath);

if (Files.exists(commandPath)) {
updateCommand(commandPath, command);
}
else {
createCommand(commandPath, command);
}
catch (IOException ex) {
throw new MojoExecutionException("Unable to add: " + ex.getMessage());
}

private void checkParentDirectoryExists(Path huskyCommandPath) throws MojoExecutionException {
if (!Files.exists(huskyCommandPath.getParent())) {
throw new MojoExecutionException(
String.format("Can't create hook, %s directory doesn't exist (try running install goal)",
huskyCommandPath.getParent().getFileName().toString()));
}
}

private void updateCommand(Path commandPath, String command) throws IOException {
Files.write(commandPath, Arrays.asList("\n", command), StandardOpenOption.APPEND);
getLog().info("Updated");
}

private void createCommand(Path commandPath, String command) throws IOException {
Path createdCommandPath = Files.createFile(commandPath);
createdCommandPath.toFile().setExecutable(true);
Files.write(createdCommandPath, Collections.singletonList(command));
getLog().info("Created");
}

@Override
String getCommandName() {
return "Add";
}

}
24 changes: 24 additions & 0 deletions src/main/java/io/github/dathin/jhusky/HuskyCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.dathin.jhusky;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.IOException;

public abstract class HuskyCommand extends AbstractMojo {

public void execute() throws MojoExecutionException {
try {
command();
}
catch (InterruptedException | IOException | MojoExecutionException ex) {
getLog().error(ex);
throw new MojoExecutionException(String.format("Unable to run %s goal", getCommandName()));
}
}

abstract void command() throws InterruptedException, IOException, MojoExecutionException;

abstract String getCommandName();

}
80 changes: 80 additions & 0 deletions src/main/java/io/github/dathin/jhusky/Install.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.github.dathin.jhusky;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Mojo(name = "install")
public class Install extends HuskyCommand {

public static final String HUSKY_SH_DIR = "_";

@Parameter(property = "directory", defaultValue = ".husky")
private String directory;

private ProcessUtils processUtils;

public Install() {
this.processUtils = new ProcessUtils(getLog());
}

public Install(ProcessUtils processUtils) {
this.processUtils = processUtils;
}

@Override
public void command() throws MojoExecutionException, IOException, InterruptedException {
processUtils.runAndHandleProcess(directory, "git", "rev-parse");

prepareEnvironment(directory);

installHuskyFiles(directory);

processUtils.runAndHandleProcess(directory, "git", "config", "core.hooksPath", directory);

getLog().info("Git hooks installed");
}

private void prepareEnvironment(String directory) throws MojoExecutionException, IOException {
String customDirHelpUrl = "https://git.io/Jc3F9";

if (directory.contains("..")) {
throw new MojoExecutionException(String.format(".. not allowed (see %s)", customDirHelpUrl));
}

Files.createDirectories(Paths.get(directory));
Files.deleteIfExists(Paths.get(directory, HUSKY_SH_DIR, ".gitignore"));
Files.deleteIfExists(Paths.get(directory, HUSKY_SH_DIR, "husky.sh"));
Files.createDirectories(Paths.get(directory, HUSKY_SH_DIR));
}

private void installHuskyFiles(String directory) throws IOException {
Path huskySh = Files.createFile(Paths.get(directory, HUSKY_SH_DIR, "husky.sh"));
huskySh.toFile().setExecutable(true);
Files.write(huskySh,
("#!/bin/sh\n" + "if [ -z \"$husky_skip_init\" ]; then\n" + " debug () {\n"
+ " if [ \"$HUSKY_DEBUG\" = \"1\" ]; then\n" + " echo \"husky (debug) - $1\"\n"
+ " fi\n" + " }\n" + "\n" + " readonly hook_name=\"$(basename \"$0\")\"\n"
+ " debug \"starting $hook_name...\"\n" + "\n" + " if [ \"$HUSKY\" = \"0\" ]; then\n"
+ " debug \"HUSKY env variable is set to 0, skipping hook\"\n" + " exit 0\n" + " fi\n"
+ "\n" + " if [ -f ~/.huskyrc ]; then\n" + " debug \"sourcing ~/.huskyrc\"\n"
+ " . ~/.huskyrc\n" + " fi\n" + "\n" + " export readonly husky_skip_init=1\n"
+ " sh -e \"$0\" \"$@\"\n" + " exitCode=\"$?\"\n" + "\n" + " if [ $exitCode != 0 ]; then\n"
+ " echo \"husky - $hook_name hook exited with code $exitCode (error)\"\n" + " fi\n" + "\n"
+ " exit $exitCode\n" + "fi\n").getBytes());

Path gitignore = Files.createFile(Paths.get(directory, HUSKY_SH_DIR, ".gitignore"));
Files.write(gitignore, "*".getBytes());
}

@Override
String getCommandName() {
return "Install";
}

}
61 changes: 0 additions & 61 deletions src/main/java/io/github/dathin/jhusky/InstallCommand.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
package io.github.dathin.jhusky.components;
package io.github.dathin.jhusky;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;

public class GitValidator {
public class ProcessUtils {

private GitValidator() {
throw new RuntimeException("Utility class");
private Log log;

public ProcessUtils(Log log) {
this.log = log;
}

public static int isGitRepository(String directory, Log logger)
public void runAndHandleProcess(String executionDirectory, String... command)
throws IOException, InterruptedException, MojoExecutionException {
ProcessBuilder processBuilder = new ProcessBuilder("git", "rev-parse");
if (directory != null) {
processBuilder.directory(new File(directory));
}
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.directory(Files.createDirectories(Paths.get(executionDirectory)).toFile());
processBuilder.inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE);

Process process = processBuilder.start();
process.waitFor();

if (process.exitValue() != 0) {
BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = buf.readLine()) != null) {
logger.info(line);
log.info(line);
}
throw new MojoExecutionException(String.format("Process exit value: %s", process.exitValue()));
}
return process.exitValue();
}

}
32 changes: 22 additions & 10 deletions src/main/java/io/github/dathin/jhusky/Uninstall.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
package io.github.dathin.jhusky;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.IOException;

@Mojo(name = "uninstall")
public class Uninstall extends AbstractMojo {
public class Uninstall extends HuskyCommand {

@Parameter(property = "directory", defaultValue = ".husky")
private String directory;

private final ProcessUtils processUtils;

public Uninstall() {
this.processUtils = new ProcessUtils(getLog());
}

public Uninstall(ProcessUtils processUtils) {
this.processUtils = processUtils;
}

@Override
void command() throws InterruptedException, IOException, MojoExecutionException {
processUtils.runAndHandleProcess(directory, "git", "config", "--unset", "core.hooksPath");
}

@Override
public void execute() throws MojoExecutionException {
ProcessBuilder builder2 = new ProcessBuilder("git", "config", "--unset", "core.hooksPath");
try {
builder2.start();
}
catch (IOException ex) {
throw new MojoExecutionException("Unable to uninstall: " + ex.getMessage());
}
String getCommandName() {
return "Uninstall";
}

}

0 comments on commit 773b95c

Please sign in to comment.