getFormatterFactories() {
- return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, xml))
+ return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, css, xml))
.filter(Objects::nonNull)
.collect(toList());
}
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/css/Css.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/css/Css.java
new file mode 100644
index 0000000000..6f43c2c42a
--- /dev/null
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/css/Css.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2016 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.css;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import com.diffplug.spotless.css.CssDefaults;
+import com.diffplug.spotless.maven.FormatterFactory;
+import com.diffplug.spotless.maven.generic.LicenseHeader;
+
+/**
+ * A {@link FormatterFactory} implementation that corresponds to {@code ...} configuration element.
+ *
+ * It defines a formatter for CSS source files that can execute both language agnostic (e.g. {@link LicenseHeader})
+ * and css-specific (e.g. {@link Eclipse}) steps.
+ */
+public class Css extends FormatterFactory {
+
+ private static final Set DEFAULT_INCLUDES = CssDefaults.FILE_FILTER
+ .stream().map(s -> "src/" + s).collect(Collectors.toSet());
+
+ @Override
+ public Set defaultIncludes() {
+ return DEFAULT_INCLUDES;
+ }
+
+ public void addEclipse(Eclipse eclipse) {
+ addStepFactory(eclipse);
+ }
+
+ @Override
+ public String licenseHeaderDelimiter() {
+ return CssDefaults.DELIMITER_EXPR;
+ }
+
+}
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/css/Eclipse.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/css/Eclipse.java
new file mode 100644
index 0000000000..34fe14d76f
--- /dev/null
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/css/Eclipse.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2016 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.css;
+
+import java.io.File;
+import java.util.Arrays;
+
+import org.apache.maven.plugins.annotations.Parameter;
+
+import com.diffplug.spotless.FormatterStep;
+import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
+import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;
+import com.diffplug.spotless.maven.FormatterStepConfig;
+import com.diffplug.spotless.maven.FormatterStepFactory;
+
+public class Eclipse implements FormatterStepFactory {
+
+ @Parameter
+ private String file;
+
+ @Parameter
+ private String version;
+
+ @Override
+ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
+ EclipseBasedStepBuilder eclipseConfig = EclipseWtpFormatterStep.createCssBuilder(stepConfig.getProvisioner());
+ eclipseConfig.setVersion(version == null ? EclipseWtpFormatterStep.defaultVersion() : version);
+ if (null != file) {
+ File settingsFile = stepConfig.getFileLocator().locateFile(file);
+ eclipseConfig.setPreferences(Arrays.asList(settingsFile));
+ }
+ return eclipseConfig.build();
+ }
+}
diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java
index d7528e05e4..8f0b5b2171 100644
--- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java
+++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java
@@ -110,6 +110,10 @@ protected void writePomWithCppSteps(String... steps) throws IOException {
writePom(groupWithSteps("cpp", steps));
}
+ protected void writePomWithCssSteps(String... steps) throws IOException {
+ writePom(groupWithSteps("css", steps));
+ }
+
protected void writePom(String... configuration) throws IOException {
writePom(null, configuration);
}
diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java
index f5ca6f1e08..8f05932530 100644
--- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java
+++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java
@@ -50,6 +50,23 @@ public void fromContentCpp() throws Exception {
assertFile(path).hasContent(cppLicense + '\n' + cppContent);
}
+ @Test
+ public void fromContentCss() throws Exception {
+ String license = "/* my license */";
+ writePomWithCssSteps(
+ "",
+ " ",
+ license,
+ " ",
+ "");
+
+ String path = "src/file.css";
+ String content = "p {}";
+ setFile(path).toContent(content);
+ mavenRunner().withArguments("spotless:apply").runNoError();
+ assertFile(path).hasContent(license + '\n' + content);
+ }
+
@Test
public void fromContentJava() throws Exception {
writePomWithJavaSteps(
diff --git a/testlib/src/test/java/com/diffplug/spotless/css/CssDefaultsTest.java b/testlib/src/test/java/com/diffplug/spotless/css/CssDefaultsTest.java
new file mode 100644
index 0000000000..e5bc9beb61
--- /dev/null
+++ b/testlib/src/test/java/com/diffplug/spotless/css/CssDefaultsTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2016 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.css;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.File;
+import java.util.Arrays;
+
+import org.junit.Test;
+
+import com.diffplug.spotless.FormatterStep;
+import com.diffplug.spotless.ResourceHarness;
+import com.diffplug.spotless.generic.LicenseHeaderStep;
+
+public class CssDefaultsTest extends ResourceHarness {
+
+ @Test
+ public void testDelimiterExpr() throws Exception {
+ final String header = "/*My tests header*/";
+ FormatterStep step = LicenseHeaderStep.createFromHeader(header, CssDefaults.DELIMITER_EXPR);
+ final File dummyFile = setFile("src/main/cpp/file1.dummy").toContent("");
+ for (String testSource : Arrays.asList(
+ "/* Starts with element selector */@\np {",
+ "/* Starts with ID selector */@\n#i {",
+ "/* Starts with class selector */@\n.i {")) {
+ String output = null;
+ try {
+ output = step.format(testSource, dummyFile);
+ } catch (IllegalArgumentException e) {
+ throw new AssertionError(String.format("No delimiter found in '%s'", testSource), e);
+ }
+ String expected = testSource.replaceAll("(.*?)\\@", header);
+ assertThat(output).isEqualTo(expected).as("Unexpected header insertion for '$s'.", testSource);
+ }
+ }
+
+}