-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Dathin/feat/main-refactor
feat: reafactored main structure
- Loading branch information
Showing
13 changed files
with
183 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mvn spring-javaformat:validate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mvn clean test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
25 changes: 13 additions & 12 deletions
25
...athin/jhusky/components/GitValidator.java → ...io/github/dathin/jhusky/ProcessUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
Oops, something went wrong.