Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package com.cosium.code.format;

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

import com.cosium.code.format.formatter.CodeFormatter;
import com.cosium.code.format.formatter.CompositeCodeFormatter;
import com.cosium.code.format.formatter.JavaFormatter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* Created on 01/11/17.
*
Expand Down Expand Up @@ -78,4 +73,8 @@ protected final Path getOrCreateHooksDirectory() {
}
return hooksDirectory;
}

protected final Path gitBaseDir() {
return gitRepository().getDirectory().getParentFile().toPath();
}
}
16 changes: 8 additions & 8 deletions src/main/java/com/cosium/code/format/InstallHooksMojo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.cosium.code.format;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Collectors;

import com.cosium.code.format.executable.Executable;
import com.cosium.code.format.executable.ExecutableManager;
import com.cosium.code.format.utils.MavenUtils;
Expand All @@ -8,12 +14,6 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Collectors;

import static java.util.Optional.ofNullable;

/**
Expand Down Expand Up @@ -128,14 +128,14 @@ private Path prepareHooksDirectory() {

private String preCommitHookBaseScriptCall() {
return "./"
+ baseDir().relativize(getOrCreateHooksDirectory())
+ gitBaseDir().relativize(getOrCreateHooksDirectory())
+ "/"
+ pluginPreCommitHookFileName();
}

private String postCommitHookBaseScriptCall() {
return "./"
+ baseDir().relativize(getOrCreateHooksDirectory())
+ gitBaseDir().relativize(getOrCreateHooksDirectory())
+ "/"
+ pluginPostCommitHookFileName();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.cosium.code.format.formatter;

import com.google.googlejavaformat.java.Formatter;
import com.google.googlejavaformat.java.FormatterException;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.logging.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -13,6 +8,11 @@
import java.nio.file.StandardOpenOption;
import java.util.function.Supplier;

import com.google.googlejavaformat.java.Formatter;
import com.google.googlejavaformat.java.FormatterException;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.logging.Log;

/**
* Created on 07/11/17.
*
Expand Down Expand Up @@ -47,7 +47,7 @@ public void format(Path file) {
log.get().info("Formatting '" + file + "'");
final String formattedContent;
try (InputStream inputStream = Files.newInputStream(file)) {
formattedContent = new Formatter().formatSource(IOUtils.toString(inputStream, "UTF-8"));
formattedContent = new Formatter().formatSourceAndFixImports(IOUtils.toString(inputStream, "UTF-8"));
} catch (IOException | FormatterException e) {
throw new RuntimeException(e);
}
Expand All @@ -70,7 +70,7 @@ public boolean validate(Path file) {
log.get().info("Validating '" + file + "'");
try (InputStream inputStream = Files.newInputStream(file)) {
String unformatterContent = IOUtils.toString(inputStream);
String formattedContent = new Formatter().formatSource(unformatterContent);
String formattedContent = new Formatter().formatSourceAndFixImports(unformatterContent);
return unformatterContent.equals(formattedContent);
} catch (IOException | FormatterException e) {
throw new RuntimeException(e);
Expand Down