Skip to content

Commit

Permalink
SpotlessTaskImpl now generates both a formatted result and a lint res…
Browse files Browse the repository at this point in the history
…ult.
  • Loading branch information
nedtwigg committed Jan 14, 2022
1 parent aac3972 commit a25eded
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@ public abstract class SpotlessApply extends SpotlessTaskService.ClientTask {
@TaskAction
public void performAction() {
getTaskService().get().registerApplyAlreadyRan(this);
ConfigurableFileTree files = getConfigCacheWorkaround().fileTree().from(getSpotlessOutDirectory().get());
ConfigurableFileTree files = getConfigCacheWorkaround().fileTree().from(contentDir());
if (files.isEmpty()) {
getState().setDidWork(sourceDidWork());
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,7 +50,7 @@ public void performAction() throws IOException {
}

private void performAction(boolean isTest) throws IOException {
ConfigurableFileTree files = getConfigCacheWorkaround().fileTree().from(getSpotlessOutDirectory().get());
ConfigurableFileTree files = getConfigCacheWorkaround().fileTree().from(contentDir());
if (files.isEmpty()) {
getState().setDidWork(sourceDidWork());
} else if (!isTest && applyHasRun()) {
Expand Down Expand Up @@ -101,7 +101,7 @@ public void visitFile(FileVisitDetails fileVisitDetails) {
.runToFix("Run '" + calculateGradleCommand() + " " + getTaskPathPrefix() + "spotlessApply' to fix these violations.")
.formatterFolder(
getProjectDir().get().getAsFile().toPath(),
getSpotlessOutDirectory().get().toPath(),
contentDir().toPath(),
getEncoding().get())
.problemFiles(problemFiles)
.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 DiffPlug
* Copyright 2020-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -154,6 +154,17 @@ public File getOutputDirectory() {
return outputDirectory;
}

static final String CONTENT = "content";
static final String LINT = "lint";

File contentDir() {
return new File(outputDirectory, CONTENT);
}

File lintDir() {
return new File(outputDirectory, LINT);
}

protected final LiveCache<List<FormatterStep>> steps = createLive("steps");
{
steps.set(new ArrayList<FormatterStep>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Inject;
Expand All @@ -38,6 +40,7 @@
import com.diffplug.common.base.StringPrinter;
import com.diffplug.spotless.DirtyState;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.Lint;
import com.diffplug.spotless.extra.GitRatchet;

@CacheableTask
Expand All @@ -64,7 +67,8 @@ public void performAction(InputChanges inputs) throws Exception {
if (!inputs.isIncremental()) {
getLogger().info("Not incremental: removing prior outputs");
getFs().delete(d -> d.delete(outputDirectory));
Files.createDirectories(outputDirectory.toPath());
Files.createDirectories(contentDir().toPath());
Files.createDirectories(lintDir().toPath());
}

try (Formatter formatter = buildFormatter()) {
Expand All @@ -86,10 +90,14 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
File output = getOutputFile(input);
getLogger().debug("Applying format to " + input + " and writing to " + output);
DirtyState dirtyState;
List<Lint> lints;
if (ratchet != null && ratchet.isClean(getProjectDir().get().getAsFile(), getRootTreeSha(), input)) {
dirtyState = DirtyState.clean();
lints = Collections.emptyList();
} else {
dirtyState = DirtyState.of(formatter, input).calculateDirtyState();
DirtyState.Calculation calculation = DirtyState.of(formatter, input);
dirtyState = calculation.calculateDirtyState();
lints = calculation.calculateLintAgainstRaw();
}
if (dirtyState.isClean()) {
// Remove previous output if it exists
Expand All @@ -106,27 +114,46 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
Files.copy(input.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
dirtyState.writeCanonicalTo(output);
}

File lint = getLintFile(input);
if (lints.isEmpty()) {
Files.deleteIfExists(lint.toPath());
} else {
Lint.toFile(lints, lint);
}
}

private void deletePreviousResult(File input) throws IOException {
File output = getOutputFile(input);
if (output.isDirectory()) {
getFs().delete(d -> d.delete(output));
delete(getOutputFile(input));
delete(getLintFile(input));
}

private File getOutputFile(File input) {
return new File(contentDir(), relativize(input));
}

private File getLintFile(File input) {
return new File(lintDir(), relativize(input));
}

private void delete(File file) throws IOException {
if (file.isDirectory()) {
getFs().delete(d -> d.delete(file));
} else {
Files.deleteIfExists(output.toPath());
Files.deleteIfExists(file.toPath());
}
}

private File getOutputFile(File input) {
private String relativize(File input) {
File projectDir = getProjectDir().get().getAsFile();
String outputFileName = FormatExtension.relativize(projectDir, input);
if (outputFileName == null) {
throw new IllegalArgumentException(StringPrinter.buildString(printer -> {
printer.println("Spotless error! All target files must be within the project dir.");
printer.println(" project dir: " + projectDir.getAbsolutePath());
printer.println(" target: " + input.getAbsolutePath());
}));
if (outputFileName != null) {
return outputFileName;
}
return new File(outputDirectory, outputFileName);
throw new IllegalArgumentException(StringPrinter.buildString(printer -> {
printer.println("Spotless error! All target files must be within the project dir.");
printer.println(" project dir: " + projectDir.getAbsolutePath());
printer.println(" target: " + input.getAbsolutePath());
}));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -95,6 +95,14 @@ static abstract class ClientTask extends DefaultTask {
@Internal
abstract Property<File> getSpotlessOutDirectory();

File contentDir() {
return new File(getSpotlessOutDirectory().get(), SpotlessTaskImpl.CONTENT);
}

File lintDir() {
return new File(getSpotlessOutDirectory().get(), SpotlessTaskImpl.LINT);
}

@Internal
abstract Property<SpotlessTaskService> getTaskService();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,7 +47,7 @@ public BuildServiceParameters.None getParameters() {
@Test
void testLineEndings() throws Exception {
File testFile = setFile("testFile").toContent("\r\n");
File outputFile = new File(spotlessTask.getOutputDirectory(), "testFile");
File outputFile = new File(spotlessTask.contentDir(), "testFile");

spotlessTask.setTarget(Collections.singleton(testFile));
Tasks.execute(spotlessTask);
Expand All @@ -58,7 +58,7 @@ void testLineEndings() throws Exception {
@Test
void testStep() throws Exception {
File testFile = setFile("testFile").toContent("apple");
File outputFile = new File(spotlessTask.getOutputDirectory(), "testFile");
File outputFile = new File(spotlessTask.contentDir(), "testFile");
spotlessTask.setTarget(Collections.singleton(testFile));

spotlessTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,7 +59,7 @@ public BuildServiceParameters.None getParameters() {
source = createFormatTask(name, step);
check = createCheckTask(name, source);
apply = createApplyTask(name, source);
outputFile = new File(source.getOutputDirectory() + "/src", file.getName());
outputFile = new File(source.contentDir(), "src/" + file.getName());
}

private SpotlessTaskImpl createFormatTask(String name, FormatterStep step) {
Expand Down

0 comments on commit a25eded

Please sign in to comment.