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

Filter by extension

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
Expand Up @@ -39,6 +39,7 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
Expand Down Expand Up @@ -107,6 +108,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Component
protected BuildContext buildContext;

@Component
private MavenSession mavenSession;

@Parameter(defaultValue = "${mojoExecution.goal}", required = true, readonly = true)
private String goal;

Expand Down Expand Up @@ -401,9 +405,10 @@ private FormatterConfig getFormatterConfig() {
P2Provisioner p2Provisioner = P2Provisioner.createDefault();
List<FormatterStepFactory> formatterStepFactories = getFormatterStepFactories();
FileLocator fileLocator = getFileLocator();
final Optional<String> userRatchetFrom = Optional.ofNullable((String) mavenSession.getUserProperties().get("ratchetFrom"));
final Optional<String> optionalRatchetFrom = Optional.ofNullable(this.ratchetFrom)
.filter(ratchet -> !RATCHETFROM_NONE.equals(ratchet));
return new FormatterConfig(baseDir, encoding, lineEndings, optionalRatchetFrom, provisioner, p2Provisioner, fileLocator, formatterStepFactories, Optional.ofNullable(setLicenseHeaderYearsFromGitHistory), lintSuppressions);
return new FormatterConfig(baseDir, encoding, lineEndings, userRatchetFrom, optionalRatchetFrom, provisioner, p2Provisioner, fileLocator, formatterStepFactories, Optional.ofNullable(setLicenseHeaderYearsFromGitHistory), lintSuppressions);
}

private FileLocator getFileLocator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class FormatterConfig {

private final String encoding;
private final LineEnding lineEndings;
private final Optional<String> userRatchetFrom;
private final Optional<String> ratchetFrom;
private final Provisioner provisioner;
private final P2Provisioner p2Provisioner;
Expand All @@ -38,10 +39,11 @@ public class FormatterConfig {
private final Optional<String> spotlessSetLicenseHeaderYearsFromGitHistory;
private final List<LintSuppression> lintSuppressions;

public FormatterConfig(File baseDir, String encoding, LineEnding lineEndings, Optional<String> ratchetFrom, Provisioner provisioner,
public FormatterConfig(File baseDir, String encoding, LineEnding lineEndings, Optional<String> userRatchetFrom, Optional<String> ratchetFrom, Provisioner provisioner,
P2Provisioner p2Provisioner, FileLocator fileLocator, List<FormatterStepFactory> globalStepFactories, Optional<String> spotlessSetLicenseHeaderYearsFromGitHistory, List<LintSuppression> lintSuppressions) {
this.encoding = encoding;
this.lineEndings = lineEndings;
this.userRatchetFrom = userRatchetFrom;
this.ratchetFrom = ratchetFrom;
this.provisioner = provisioner;
this.p2Provisioner = p2Provisioner;
Expand All @@ -59,6 +61,10 @@ public LineEnding getLineEndings() {
return lineEndings;
}

public Optional<String> getUserRatchetFrom() {
return userRatchetFrom;
}

public Optional<String> getRatchetFrom() {
return ratchetFrom;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,15 @@ private LineEnding lineEndings(FormatterConfig config) {
}

Optional<String> ratchetFrom(FormatterConfig config) {
if (RATCHETFROM_NOT_SET_AT_FORMAT_LEVEL.equals(ratchetFrom)) {
return config.getRatchetFrom();
} else if (RATCHETFROM_NONE.equals(ratchetFrom)) {
return Optional.empty();
} else {
return Optional.ofNullable(ratchetFrom);
}
return config.getUserRatchetFrom()
.or(this::optionalRatchetFrom)
.or(config::getRatchetFrom)
.filter(ratchet -> !RATCHETFROM_NONE.equals(ratchet));
}

private Optional<String> optionalRatchetFrom() {
return Optional.ofNullable(ratchetFrom)
.filter(ratchet -> !RATCHETFROM_NOT_SET_AT_FORMAT_LEVEL.equals(ratchet));
}

private FormatterStepConfig stepConfig(Charset encoding, FormatterConfig config) {
Expand Down