com.diffplug.spotless.maven.FormatterFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spotless-maven-plugin Show documentation
Show all versions of spotless-maven-plugin Show documentation
Spotless - keep your code spotless with Gradle
/*
* Copyright 2016-2020 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.toList;
import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import org.apache.maven.plugins.annotations.Parameter;
import com.diffplug.common.collect.Sets;
import com.diffplug.spotless.FormatExceptionPolicyStrict;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.maven.generic.*;
public abstract class FormatterFactory {
@Parameter
private String encoding;
@Parameter
private LineEnding lineEndings;
/** Sentinel to distinguish between "don't ratchet this format" and "use spotless parent format". */
private static final String RATCHETFROM_NOT_SET_AT_FORMAT_LEVEL = " not set at format level ";
@Parameter(defaultValue = RATCHETFROM_NOT_SET_AT_FORMAT_LEVEL)
private String ratchetFrom;
@Parameter
private String[] includes;
@Parameter
private String[] excludes;
private final List stepFactories = new ArrayList<>();
public abstract Set defaultIncludes();
public abstract String licenseHeaderDelimiter();
public final Set includes() {
return includes == null ? emptySet() : Sets.newHashSet(includes);
}
public final Set excludes() {
return excludes == null ? emptySet() : Sets.newHashSet(excludes);
}
public final Formatter newFormatter(List filesToFormat, FormatterConfig config) {
Charset formatterEncoding = encoding(config);
LineEnding formatterLineEndings = lineEndings(config);
LineEnding.Policy formatterLineEndingPolicy = formatterLineEndings.createPolicy(config.getFileLocator().getBaseDir(), () -> filesToFormat);
FormatterStepConfig stepConfig = stepConfig(formatterEncoding, config);
List factories = gatherStepFactories(config.getGlobalStepFactories(), stepFactories);
List formatterSteps = factories.stream()
.filter(Objects::nonNull) // all unrecognized steps from XML config appear as nulls in the list
.map(factory -> factory.newFormatterStep(stepConfig))
.collect(toList());
return Formatter.builder()
.encoding(formatterEncoding)
.lineEndingsPolicy(formatterLineEndingPolicy)
.exceptionPolicy(new FormatExceptionPolicyStrict())
.steps(formatterSteps)
.rootDir(config.getFileLocator().getBaseDir().toPath())
.build();
}
public final void addLicenseHeader(LicenseHeader licenseHeader) {
addStepFactory(licenseHeader);
}
public final void addEndWithNewline(EndWithNewline endWithNewline) {
addStepFactory(endWithNewline);
}
public final void addIndent(Indent indent) {
addStepFactory(indent);
}
public final void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) {
addStepFactory(trimTrailingWhitespace);
}
public final void addReplace(Replace replace) {
addStepFactory(replace);
}
public final void addReplaceRegex(ReplaceRegex replaceRegex) {
addStepFactory(replaceRegex);
}
public final void addEclipseWtp(EclipseWtp eclipseWtp) {
addStepFactory(eclipseWtp);
}
public final void addPrettier(Prettier prettier) {
addStepFactory(prettier);
}
protected final void addStepFactory(FormatterStepFactory stepFactory) {
Objects.requireNonNull(stepFactory);
stepFactories.add(stepFactory);
}
private Charset encoding(FormatterConfig config) {
return Charset.forName(encoding == null ? config.getEncoding() : encoding);
}
private LineEnding lineEndings(FormatterConfig config) {
return lineEndings == null ? config.getLineEndings() : lineEndings;
}
Optional ratchetFrom(FormatterConfig config) {
if (ratchetFrom == RATCHETFROM_NOT_SET_AT_FORMAT_LEVEL) {
return config.getRatchetFrom();
} else {
return Optional.ofNullable(ratchetFrom);
}
}
private FormatterStepConfig stepConfig(Charset encoding, FormatterConfig config) {
return new FormatterStepConfig(encoding, licenseHeaderDelimiter(), ratchetFrom(config), config.getProvisioner(), config.getFileLocator(), config.getSpotlessSetLicenseHeaderYearsFromGitHistory());
}
private static List gatherStepFactories(List allGlobal, List allConfigured) {
List result = new ArrayList<>();
for (FormatterStepFactory global : allGlobal) {
if (!formatterStepOverriden(global, allConfigured)) {
result.add(global);
}
}
result.addAll(allConfigured);
return result;
}
private static boolean formatterStepOverriden(FormatterStepFactory global, List allConfigured) {
return allConfigured.stream()
.anyMatch(configured -> configured.getClass() == global.getClass());
}
}