Skip to content

Commit bf6abd2

Browse files
authored
Wtp integration maven gradle (#325)
Provided WTP formatter step.
1 parent bb055c1 commit bf6abd2

File tree

27 files changed

+362
-160
lines changed

27 files changed

+362
-160
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ You might be looking for:
77

88
### Version 1.18.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/))
99

10+
* CSS and XML extensions are discontinued ([#325](https://github.com/diffplug/spotless/pull/325)).
11+
1012
### Version 1.17.0 - October 30th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.17.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.17.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))
1113

1214
* Updated default eclipse-jdt from 4.7.3a to 4.9.0 ([#316](https://github.com/diffplug/spotless/pull/316)). New version addresses enum-tab formatting bug in 4.8 ([#314](https://github.com/diffplug/spotless/issues/314)).

lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStep.java

+50-12
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,83 @@
2222

2323
import com.diffplug.spotless.FormatterFunc;
2424
import com.diffplug.spotless.Provisioner;
25+
import com.diffplug.spotless.ThrowingEx;
2526
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
2627

2728
/** Formatter step which calls out to the Groovy-Eclipse formatter. */
28-
public final class EclipseWtpFormatterStep {
29-
// prevent direct instantiation
30-
private EclipseWtpFormatterStep() {}
29+
public enum EclipseWtpFormatterStep {
30+
// @formatter:off
31+
CSS ("EclipseCssFormatterStepImpl", EclipseWtpFormatterStep::applyWithoutFile),
32+
HTML("EclipseHtmlFormatterStepImpl", EclipseWtpFormatterStep::applyWithoutFile),
33+
JS ("EclipseJsFormatterStepImpl", EclipseWtpFormatterStep::applyWithoutFile),
34+
JSON("EclipseJsonFormatterStepImpl", EclipseWtpFormatterStep::applyWithoutFile),
35+
XML ("EclipseXmlFormatterStepImpl", EclipseWtpFormatterStep::applyWithFile);
36+
// @formatter:on
3137

3238
private static final String NAME = "eclipse wtp formatters";
3339
private static final String FORMATTER_PACKAGE = "com.diffplug.spotless.extra.eclipse.wtp.";
3440
private static final String DEFAULT_VERSION = "4.7.3a";
3541
private static final String FORMATTER_METHOD = "format";
3642

43+
private final String implementationClassName;
44+
private final ThrowingEx.BiFunction<String, EclipseBasedStepBuilder.State, FormatterFunc> formatterCall;
45+
46+
EclipseWtpFormatterStep(String implementationClassName, ThrowingEx.BiFunction<String, EclipseBasedStepBuilder.State, FormatterFunc> formatterCall) {
47+
this.implementationClassName = implementationClassName;
48+
this.formatterCall = formatterCall;
49+
}
50+
51+
public EclipseBasedStepBuilder createBuilder(Provisioner provisioner) {
52+
return new EclipseBasedStepBuilder(NAME, " - " + toString(), provisioner, state -> formatterCall.apply(implementationClassName, state));
53+
}
54+
3755
public static String defaultVersion() {
3856
return DEFAULT_VERSION;
3957
}
4058

41-
/** Provides default configuration for CSSformatter */
59+
/**
60+
* Provides default configuration for CSSformatter.
61+
* Method is deprecated. Use {@link EclipseWtpFormatterStep#createBuilder(Provisioner)} instead.
62+
*/
63+
@Deprecated
4264
public static EclipseBasedStepBuilder createCssBuilder(Provisioner provisioner) {
4365
return new EclipseBasedStepBuilder(NAME, " - css", provisioner, state -> applyWithoutFile("EclipseCssFormatterStepImpl", state));
4466
}
4567

46-
/** Provides default configuration for HTML formatter */
68+
/**
69+
* Provides default configuration for HTML formatter.
70+
* Method is deprecated. Use {@link EclipseWtpFormatterStep#createBuilder(Provisioner)} instead.
71+
*/
72+
@Deprecated
4773
public static EclipseBasedStepBuilder createHtmlBuilder(Provisioner provisioner) {
48-
return new EclipseBasedStepBuilder(NAME, " - html", provisioner, state -> applyWithoutFile("EclipseHtmlFormatterStepImpl", state));
74+
return HTML.createBuilder(provisioner);
4975
}
5076

51-
/** Provides default configuration for Java Script formatter */
77+
/**
78+
* Provides default configuration for Java Script formatter.
79+
* Method is deprecated. Use {@link EclipseWtpFormatterStep#createBuilder(Provisioner)} instead.
80+
*/
81+
@Deprecated
5282
public static EclipseBasedStepBuilder createJsBuilder(Provisioner provisioner) {
53-
return new EclipseBasedStepBuilder(NAME, " - js", provisioner, state -> applyWithoutFile("EclipseJsFormatterStepImpl", state));
83+
return JS.createBuilder(provisioner);
5484
}
5585

56-
/** Provides default configuration for JSON formatter */
86+
/**
87+
* Provides default configuration for JSON formatter.
88+
* Method is deprecated. Use {@link EclipseWtpFormatterStep#createBuilder(Provisioner)} instead.
89+
*/
90+
@Deprecated
5791
public static EclipseBasedStepBuilder createJsonBuilder(Provisioner provisioner) {
58-
return new EclipseBasedStepBuilder(NAME, " - json", provisioner, state -> applyWithoutFile("EclipseJsonFormatterStepImpl", state));
92+
return JSON.createBuilder(provisioner);
5993
}
6094

61-
/** Provides default configuration for XML formatter */
95+
/**
96+
* Provides default configuration for XML formatter.
97+
* Method is deprecated. Use {@link EclipseWtpFormatterStep#createBuilder(Provisioner)} instead.
98+
*/
99+
@Deprecated
62100
public static EclipseBasedStepBuilder createXmlBuilder(Provisioner provisioner) {
63-
return new EclipseBasedStepBuilder(NAME, " - xml", provisioner, state -> applyWithFile("EclipseXmlFormatterStepImpl", state));
101+
return XML.createBuilder(provisioner);
64102
}
65103

66104
private static FormatterFunc applyWithoutFile(String className, EclipseBasedStepBuilder.State state) throws Exception {

lib-extra/src/test/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStepTest.java

+13-17
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Arrays;
2525
import java.util.Properties;
2626
import java.util.function.Consumer;
27-
import java.util.function.Function;
2827

2928
import org.junit.Test;
3029
import org.junit.runner.RunWith;
@@ -33,7 +32,6 @@
3332
import org.junit.runners.Parameterized.Parameters;
3433

3534
import com.diffplug.spotless.FormatterStep;
36-
import com.diffplug.spotless.Provisioner;
3735
import com.diffplug.spotless.TestProvisioner;
3836
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
3937
import com.diffplug.spotless.extra.eclipse.EclipseCommonTests;
@@ -44,29 +42,27 @@ public class EclipseWtpFormatterStepTest extends EclipseCommonTests {
4442
private enum WTP {
4543
// @formatter:off
4644
CSS( "body {\na: v; b: \nv;\n} \n",
47-
"body {\n\ta: v;\n\tb: v;\n}",
48-
EclipseWtpFormatterStep::createCssBuilder),
45+
"body {\n\ta: v;\n\tb: v;\n}"),
4946
HTML( "<!DOCTYPE html> <html>\t<head> <meta charset=\"UTF-8\"></head>\n</html> ",
50-
"<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"UTF-8\">\n</head>\n</html>\n",
51-
EclipseWtpFormatterStep::createHtmlBuilder),
47+
"<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"UTF-8\">\n</head>\n</html>\n"),
5248
JS( "function f( ) {\na.b(1,\n2);}",
53-
"function f() {\n a.b(1, 2);\n}",
54-
EclipseWtpFormatterStep::createJsBuilder),
49+
"function f() {\n a.b(1, 2);\n}"),
5550
JSON( "{\"a\": \"b\", \"c\": { \"d\": \"e\",\"f\": \"g\"}}",
56-
"{\n\t\"a\": \"b\",\n\t\"c\": {\n\t\t\"d\": \"e\",\n\t\t\"f\": \"g\"\n\t}\n}",
57-
EclipseWtpFormatterStep::createJsonBuilder),
58-
XML( "<a><b> c</b></a>", "<a>\n\t<b> c</b>\n</a>",
59-
EclipseWtpFormatterStep::createXmlBuilder);
51+
"{\n\t\"a\": \"b\",\n\t\"c\": {\n\t\t\"d\": \"e\",\n\t\t\"f\": \"g\"\n\t}\n}"),
52+
XML( "<a><b> c</b></a>", "<a>\n\t<b> c</b>\n</a>");
6053
// @formatter:on
6154

6255
public final String input;
6356
public final String expectation;
64-
public final Function<Provisioner, EclipseBasedStepBuilder> builderMethod;
6557

66-
private WTP(String input, final String expectation, Function<Provisioner, EclipseBasedStepBuilder> builderMethod) {
58+
private WTP(String input, final String expectation) {
6759
this.input = input;
6860
this.expectation = expectation;
69-
this.builderMethod = builderMethod;
61+
}
62+
63+
public EclipseBasedStepBuilder createBuilder() {
64+
EclipseWtpFormatterStep stepType = EclipseWtpFormatterStep.valueOf(this.toString());
65+
return stepType.createBuilder(TestProvisioner.mavenCentral());
7066
}
7167
}
7268

@@ -95,7 +91,7 @@ protected String getTestExpectation(String version) {
9591

9692
@Override
9793
protected FormatterStep createStep(String version) {
98-
EclipseBasedStepBuilder builder = wtp.builderMethod.apply(TestProvisioner.mavenCentral());
94+
EclipseBasedStepBuilder builder = wtp.createBuilder();
9995
builder.setVersion(version);
10096
return builder.build();
10197
}
@@ -131,7 +127,7 @@ private FormatterStep createStepForDefaultVersion(Consumer<Properties> config) t
131127
File tempFile = File.createTempFile("EclipseWtpFormatterStepTest-", ".properties");
132128
OutputStream tempOut = new FileOutputStream(tempFile);
133129
configProps.store(tempOut, "test properties");
134-
EclipseBasedStepBuilder builder = wtp.builderMethod.apply(TestProvisioner.mavenCentral());
130+
EclipseBasedStepBuilder builder = wtp.createBuilder();
135131
builder.setVersion(EclipseWtpFormatterStep.defaultVersion());
136132
builder.setPreferences(Arrays.asList(tempFile));
137133
return builder.build();

lib/src/main/java/com/diffplug/spotless/css/CssDefaults.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121

22-
/** Common utilities for CSS */
22+
/**
23+
* Common utilities for CSS
24+
* <br/>
25+
* The CSS extension is discontinued.
26+
*/
27+
@Deprecated
2328
public class CssDefaults {
2429
//Prevent instantiation
2530
private CssDefaults() {};

lib/src/main/java/com/diffplug/spotless/xml/XmlDefaults.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
import java.util.List;
2121
import java.util.stream.Collectors;
2222

23-
/** Common utilities for XML */
23+
/**
24+
* Common utilities for XML
25+
* <br/>
26+
* The XML extension is discontinued.
27+
*/
28+
@Deprecated
2429
public class XmlDefaults {
2530
//Prevent instantiation
2631
private XmlDefaults() {};

plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Version 3.18.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/))
44

5+
* Provided eclipse-wtp formatters in generic formatter extension. ([#325](https://github.com/diffplug/spotless/pull/325)). This change obsoletes the CSS and XML extensions.
6+
57
### Version 3.17.0 - December 13th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.17.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.17.0))
68

79
* Updated default eclipse-jdt from 4.7.3a to 4.9.0 ([#316](https://github.com/diffplug/spotless/pull/316)). New version addresses enum-tab formatting bug in 4.8 ([#314](https://github.com/diffplug/spotless/issues/314)).

plugin-gradle/README.md

+36-51
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ Spotless can check and apply formatting to any plain-text file, using simple rul
7979

8080
* Eclipse's [CDT](#eclipse-cdt) C/C++ code formatter
8181
* Eclipse's java code formatter (including style and import ordering)
82-
* Eclipse's [WTP-CSS](#eclipse-wtp-css) CSS code formatter
83-
* Eclipse's [WTP-XML](#eclipse-wtp-xml) XML code formatter
82+
* Eclipse's [WTP](#eclipse-wtp) HTML, XML, ... code formatters
8483
* Google's [google-java-format](https://github.com/google/google-java-format)
8584
* [Groovy Eclipse](#groovy-eclipse)'s groovy code formatter
8685
* [FreshMark](https://github.com/diffplug/freshmark) (markdown with variables)
@@ -321,55 +320,6 @@ spotless {
321320

322321
Use the Eclipse to define the *Code Style preferences* (see [Eclipse documentation](https://www.eclipse.org/documentation/)). Within the preferences *Edit...* dialog, you can export your configuration as XML file, which can be used as a `configFile`. If no `configFile` is provided, the CDT default configuration is used.
323322

324-
<a name="css-wtp"></a>
325-
326-
## Applying to CSS sources
327-
328-
```gradle
329-
spotless {
330-
css {
331-
target '**/*.css' '**/*.css2'// Change file filter. By default files with 'css' extension are supported
332-
eclipse().configFile './css-formatter.prefs' // Properties file of the Eclipse WTP formatter
333-
// Use for example eclipse('4.7.3a') to specify a specific version of Eclipse,
334-
// available versions are: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters
335-
// also supports license headers
336-
licenseHeader '<!-- Licensed under Apache-2.0 -->' // License header
337-
licenseHeaderFile './license.txt' // License header file
338-
}
339-
}
340-
```
341-
342-
<a name="eclipse-wtp-css"></a>
343-
344-
### Eclipse [WTP](https://www.eclipse.org/webtools/) CSS formatter
345-
Use Eclipse to define the *CSS Files* editor preferences (see [Eclipse documentation](http://help.eclipse.org/photon/topic/org.eclipse.wst.sse.doc.user/topics/tsrcedt025.html)) and the *Cleanup* preferences available in the *Source* menu (when editing a CSS file). The preferences are stored below your Eclipse workspace directory in `.metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs`. Note that only the differences to the default configuration are stored within the file. Omit the 'configFile' entirely to use the default Eclipse configuration.
346-
347-
<a name="xml-wtp"></a>
348-
349-
## Applying to XML sources
350-
351-
```gradle
352-
spotless {
353-
xml {
354-
target '**/*.xml' // Change file filter. By default files with 'xml', 'xsl', 'xslt', 'wsdl', 'xsd', 'exsd' and 'xmi' extension are supported
355-
eclipse().configFile './xml-formatter.prefs' // Properties file of the Eclipse WTP formatter
356-
// Use for example eclipse('4.7.3a') to specify a specific version of Eclipse,
357-
// available versions are: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters
358-
// also supports license headers
359-
licenseHeader '<!-- Licensed under Apache-2.0 -->' // License header
360-
licenseHeaderFile './license.txt' // License header file
361-
}
362-
}
363-
```
364-
365-
<a name="eclipse-wtp-xml"></a>
366-
367-
### Eclipse [WTP](https://www.eclipse.org/webtools/) XML formatter
368-
Use Eclipse to define the *XML editor preferences* (see [Eclipse documentation](https://www.eclipse.org/documentation/)). The preferences are stored below your Eclipse workspace directory in `.metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.xml.core.prefs`. Note that only the differences to the default configuration are stored within the file. Omit the 'configFile' entirely to use the default Eclipse configuration.
369-
370-
The Eclipse WTP formatter supports DTD/XSD restrictions on white spaces. For XSD/DTD lookup, relative and absolute XSD/DTD URIs are supported. Furthermore a user catalog can be configured using the `userCatalog` property key. Add the property to the preference file or add an additional preference or properties files as an additional argument to the `configFile`.
371-
372-
373323
<a name="typescript"></a>
374324

375325
## Applying to Typescript source
@@ -510,6 +460,41 @@ spotless {
510460

511461
Spotless uses npm to install necessary packages locally. It runs prettier using [J2V8](https://github.com/eclipsesource/J2V8) internally after that.
512462

463+
<a name="eclipse-wtp"></a>
464+
465+
## Applying [Eclipse WTP](https://www.eclipse.org/webtools/) to css | html | etc.
466+
467+
The Eclipse [WTP](https://www.eclipse.org/webtools/) formatter can be applied as follows:
468+
469+
```gradle
470+
spotless {
471+
format 'xml', {
472+
target fileTree('.') {
473+
include '**/*.xml', '**/*.xsd'
474+
exclude '**/build/**'
475+
}
476+
// Use for example eclipseWtp('xml', '4.7.3a') to specify a specific version of Eclipse,
477+
// available versions are: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters
478+
eclipseWtp('xml').configFile 'spotless.xml.prefs' 'spotless.common.properties'
479+
}
480+
}
481+
```
482+
The WTP formatter accept multiple configuration files. All Eclipse configuration file formats are accepted as well as simple Java property files. Omit the `configFile` entirely to use the default Eclipse configuration. The following formatters and configurations are supported:
483+
484+
| Type | Configuration | File location
485+
| ---- | ------------------- | -------------
486+
| CSS | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
487+
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
488+
| HTML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.html.core.prefs
489+
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.html.core.prefs
490+
| | embedded CSS | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
491+
| | embedded JS | Use the export in the Eclipse editor configuration dialog
492+
| JS | editor preferences | Use the export in the Eclipse editor configuration dialog
493+
| JSON | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.json.core.prefs
494+
| XML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.xml.core.prefs
495+
496+
Note that `HTML` should be used for `X-HTML` sources instead of `XML`.
497+
513498
<a name="license-header"></a>
514499

515500
## License header options

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/CssExtension.java

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
2424
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;
2525

26+
/**
27+
* The CSS extension is deprecated. Use the generic {@link FormatExtension} instead.
28+
*/
29+
@Deprecated
2630
public class CssExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
2731
static final String NAME = "css";
2832

@@ -38,6 +42,10 @@ public EclipseConfig eclipse(String version) {
3842
return new EclipseConfig(version);
3943
}
4044

45+
/**
46+
* The CSS Eclipse configuration is deprecated. Use the {@link FormatExtension.EclipseWtpConfig} instead.
47+
*/
48+
@Deprecated
4149
public class EclipseConfig {
4250
private final EclipseBasedStepBuilder builder;
4351

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

+27
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.diffplug.spotless.LazyForwardingEquality;
3636
import com.diffplug.spotless.LineEnding;
3737
import com.diffplug.spotless.ThrowingEx;
38+
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
39+
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;
3840
import com.diffplug.spotless.generic.EndWithNewlineStep;
3941
import com.diffplug.spotless.generic.IndentStep;
4042
import com.diffplug.spotless.generic.LicenseHeaderStep;
@@ -483,6 +485,31 @@ public PrettierConfig prettier() {
483485
return prettierConfig;
484486
}
485487

488+
public class EclipseWtpConfig {
489+
private final EclipseBasedStepBuilder builder;
490+
491+
EclipseWtpConfig(EclipseWtpFormatterStep type, String version) {
492+
builder = type.createBuilder(GradleProvisioner.fromProject(getProject()));
493+
builder.setVersion(version);
494+
addStep(builder.build());
495+
}
496+
497+
public void configFile(Object... configFiles) {
498+
requireElementsNonNull(configFiles);
499+
Project project = getProject();
500+
builder.setPreferences(project.files(configFiles).getFiles());
501+
replaceStep(builder.build());
502+
}
503+
}
504+
505+
public EclipseWtpConfig eclipseWtp(EclipseWtpFormatterStep type) {
506+
return eclipseWtp(type, EclipseWtpFormatterStep.defaultVersion());
507+
}
508+
509+
public EclipseWtpConfig eclipseWtp(EclipseWtpFormatterStep type, String version) {
510+
return new EclipseWtpConfig(type, version);
511+
}
512+
486513
/** Sets up a format task according to the values in this extension. */
487514
protected void setupTask(SpotlessTask task) {
488515
task.setPaddedCell(paddedCell);

0 commit comments

Comments
 (0)