Skip to content

Commit

Permalink
Switched maven plugin over to Formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Jan 17, 2018
1 parent 5ff5581 commit 9956473
Showing 1 changed file with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/
package com.diffplug.gradle.spotless;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singleton;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
Expand All @@ -37,8 +40,11 @@
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;

import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.extra.java.EclipseFormatterStep;

@Mojo(name = "spotless")
Expand All @@ -62,31 +68,43 @@ public class SpotlessMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
ArtifactResolver resolver = new ArtifactResolver(repositorySystem, repositorySystemSession, repositories);

Provisioner provisioner = MavenProvisioner.create(resolver);

// create the eclipse step
Set<File> settingFiles = singleton(new File(eclipseFormatFile));
FormatterStep step = EclipseFormatterStep.create(settingFiles, provisioner);
FormatterStep step = EclipseFormatterStep.create(EclipseFormatterStep.defaultVersion(),
settingFiles, provisioner);

// collect all the files that are going to be formatted
File rootDir = project.getFile();
List<File> toFormat = new ArrayList<>();
for (String compileSourceRoot : project.getCompileSourceRoots()) {
Path root = Paths.get(compileSourceRoot);
try (Stream<Path> entries = Files.walk(root)) {
entries.filter(Files::isRegularFile)
.filter(file -> file.getFileName().toString().endsWith(".java"))
.forEach(file -> format(file, step));
.map(Path::toFile)
.forEach(toFormat::add);
} catch (Exception e) {
throw new MojoExecutionException("Unable to walk the file tree", e);
}
}
}

private void format(Path file, FormatterStep step) {
// create a formatter
Formatter formatter = Formatter.builder()
.lineEndingsPolicy(LineEnding.GIT_ATTRIBUTES.createPolicy(rootDir, () -> toFormat))
.encoding(StandardCharsets.UTF_8)
.rootDir(rootDir.toPath())
.steps(Collections.singletonList(step))
.build();

// use the formatter to format all the files
try {
getLog().info("Formatting " + file);
String contents = new String(Files.readAllBytes(file), UTF_8);
String formatted = step.format(contents, file.toFile());
Files.write(file, formatted.getBytes(UTF_8));
} catch (Exception e) {
throw new RuntimeException("Unable to format " + file, e);
for (File file : toFormat) {
formatter.applyTo(file);
}
} catch (IOException e) {
throw ThrowingEx.asRuntime(e);
}
}
}

0 comments on commit 9956473

Please sign in to comment.