Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wtp integration maven gradle #325

Merged
merged 8 commits into from
Dec 30, 2018
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ You might be looking for:

### 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/))

* CSS and XML extensions are discontinued ([#325](https://github.com/diffplug/spotless/pull/325)).

### 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)))

* 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)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,83 @@

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;

/** Formatter step which calls out to the Groovy-Eclipse formatter. */
public final class EclipseWtpFormatterStep {
// prevent direct instantiation
private EclipseWtpFormatterStep() {}
public enum EclipseWtpFormatterStep {
// @formatter:off
CSS ("EclipseCssFormatterStepImpl", EclipseWtpFormatterStep::applyWithoutFile),
HTML("EclipseHtmlFormatterStepImpl", EclipseWtpFormatterStep::applyWithoutFile),
JS ("EclipseJsFormatterStepImpl", EclipseWtpFormatterStep::applyWithoutFile),
JSON("EclipseJsonFormatterStepImpl", EclipseWtpFormatterStep::applyWithoutFile),
XML ("EclipseXmlFormatterStepImpl", EclipseWtpFormatterStep::applyWithFile);
// @formatter:on

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

private final String implementationClassName;
private final ThrowingEx.BiFunction<String, EclipseBasedStepBuilder.State, FormatterFunc> formatterCall;

EclipseWtpFormatterStep(String implementationClassName, ThrowingEx.BiFunction<String, EclipseBasedStepBuilder.State, FormatterFunc> formatterCall) {
this.implementationClassName = implementationClassName;
this.formatterCall = formatterCall;
}

public EclipseBasedStepBuilder createBuilder(Provisioner provisioner) {
return new EclipseBasedStepBuilder(NAME, " - " + toString(), provisioner, state -> formatterCall.apply(implementationClassName, state));
}

public static String defaultVersion() {
return DEFAULT_VERSION;
}

/** Provides default configuration for CSSformatter */
/**
* Provides default configuration for CSSformatter.
* Method is deprecated. Use {@link EclipseWtpFormatterStep#createBuilder(Provisioner)} instead.
*/
@Deprecated
public static EclipseBasedStepBuilder createCssBuilder(Provisioner provisioner) {
return new EclipseBasedStepBuilder(NAME, " - css", provisioner, state -> applyWithoutFile("EclipseCssFormatterStepImpl", state));
}

/** Provides default configuration for HTML formatter */
/**
* Provides default configuration for HTML formatter.
* Method is deprecated. Use {@link EclipseWtpFormatterStep#createBuilder(Provisioner)} instead.
*/
@Deprecated
public static EclipseBasedStepBuilder createHtmlBuilder(Provisioner provisioner) {
fvgh marked this conversation as resolved.
Show resolved Hide resolved
return new EclipseBasedStepBuilder(NAME, " - html", provisioner, state -> applyWithoutFile("EclipseHtmlFormatterStepImpl", state));
return HTML.createBuilder(provisioner);
}

/** Provides default configuration for Java Script formatter */
/**
* Provides default configuration for Java Script formatter.
* Method is deprecated. Use {@link EclipseWtpFormatterStep#createBuilder(Provisioner)} instead.
*/
@Deprecated
public static EclipseBasedStepBuilder createJsBuilder(Provisioner provisioner) {
return new EclipseBasedStepBuilder(NAME, " - js", provisioner, state -> applyWithoutFile("EclipseJsFormatterStepImpl", state));
return JS.createBuilder(provisioner);
}

/** Provides default configuration for JSON formatter */
/**
* Provides default configuration for JSON formatter.
* Method is deprecated. Use {@link EclipseWtpFormatterStep#createBuilder(Provisioner)} instead.
*/
@Deprecated
public static EclipseBasedStepBuilder createJsonBuilder(Provisioner provisioner) {
return new EclipseBasedStepBuilder(NAME, " - json", provisioner, state -> applyWithoutFile("EclipseJsonFormatterStepImpl", state));
return JSON.createBuilder(provisioner);
}

/** Provides default configuration for XML formatter */
/**
* Provides default configuration for XML formatter.
* Method is deprecated. Use {@link EclipseWtpFormatterStep#createBuilder(Provisioner)} instead.
*/
@Deprecated
public static EclipseBasedStepBuilder createXmlBuilder(Provisioner provisioner) {
return new EclipseBasedStepBuilder(NAME, " - xml", provisioner, state -> applyWithFile("EclipseXmlFormatterStepImpl", state));
return XML.createBuilder(provisioner);
}

private static FormatterFunc applyWithoutFile(String className, EclipseBasedStepBuilder.State state) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Arrays;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.function.Function;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -33,7 +32,6 @@
import org.junit.runners.Parameterized.Parameters;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.TestProvisioner;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.eclipse.EclipseCommonTests;
Expand All @@ -44,29 +42,27 @@ public class EclipseWtpFormatterStepTest extends EclipseCommonTests {
private enum WTP {
// @formatter:off
CSS( "body {\na: v; b: \nv;\n} \n",
"body {\n\ta: v;\n\tb: v;\n}",
EclipseWtpFormatterStep::createCssBuilder),
"body {\n\ta: v;\n\tb: v;\n}"),
HTML( "<!DOCTYPE html> <html>\t<head> <meta charset=\"UTF-8\"></head>\n</html> ",
"<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"UTF-8\">\n</head>\n</html>\n",
EclipseWtpFormatterStep::createHtmlBuilder),
"<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"UTF-8\">\n</head>\n</html>\n"),
JS( "function f( ) {\na.b(1,\n2);}",
"function f() {\n a.b(1, 2);\n}",
EclipseWtpFormatterStep::createJsBuilder),
"function f() {\n a.b(1, 2);\n}"),
JSON( "{\"a\": \"b\", \"c\": { \"d\": \"e\",\"f\": \"g\"}}",
"{\n\t\"a\": \"b\",\n\t\"c\": {\n\t\t\"d\": \"e\",\n\t\t\"f\": \"g\"\n\t}\n}",
EclipseWtpFormatterStep::createJsonBuilder),
XML( "<a><b> c</b></a>", "<a>\n\t<b> c</b>\n</a>",
EclipseWtpFormatterStep::createXmlBuilder);
"{\n\t\"a\": \"b\",\n\t\"c\": {\n\t\t\"d\": \"e\",\n\t\t\"f\": \"g\"\n\t}\n}"),
XML( "<a><b> c</b></a>", "<a>\n\t<b> c</b>\n</a>");
// @formatter:on

public final String input;
public final String expectation;
public final Function<Provisioner, EclipseBasedStepBuilder> builderMethod;

private WTP(String input, final String expectation, Function<Provisioner, EclipseBasedStepBuilder> builderMethod) {
private WTP(String input, final String expectation) {
this.input = input;
this.expectation = expectation;
this.builderMethod = builderMethod;
}

public EclipseBasedStepBuilder createBuilder() {
EclipseWtpFormatterStep stepType = EclipseWtpFormatterStep.valueOf(this.toString());
return stepType.createBuilder(TestProvisioner.mavenCentral());
}
}

Expand Down Expand Up @@ -95,7 +91,7 @@ protected String getTestExpectation(String version) {

@Override
protected FormatterStep createStep(String version) {
EclipseBasedStepBuilder builder = wtp.builderMethod.apply(TestProvisioner.mavenCentral());
EclipseBasedStepBuilder builder = wtp.createBuilder();
builder.setVersion(version);
return builder.build();
}
Expand Down Expand Up @@ -131,7 +127,7 @@ private FormatterStep createStepForDefaultVersion(Consumer<Properties> config) t
File tempFile = File.createTempFile("EclipseWtpFormatterStepTest-", ".properties");
OutputStream tempOut = new FileOutputStream(tempFile);
configProps.store(tempOut, "test properties");
EclipseBasedStepBuilder builder = wtp.builderMethod.apply(TestProvisioner.mavenCentral());
EclipseBasedStepBuilder builder = wtp.createBuilder();
builder.setVersion(EclipseWtpFormatterStep.defaultVersion());
builder.setPreferences(Arrays.asList(tempFile));
return builder.build();
Expand Down
7 changes: 6 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/css/CssDefaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
import java.util.Collections;
import java.util.List;

/** Common utilities for CSS */
/**
* Common utilities for CSS
* <br/>
* The CSS extension is discontinued.
*/
@Deprecated
public class CssDefaults {
//Prevent instantiation
private CssDefaults() {};
Expand Down
7 changes: 6 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/xml/XmlDefaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
import java.util.List;
import java.util.stream.Collectors;

/** Common utilities for XML */
/**
* Common utilities for XML
* <br/>
* The XML extension is discontinued.
*/
@Deprecated
public class XmlDefaults {
//Prevent instantiation
private XmlDefaults() {};
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### 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/))

* Provided eclipse-wtp formatters in generic formatter extension. ([#325](https://github.com/diffplug/spotless/pull/325)). This change obsoletes the CSS and XML extensions.

### 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))

* 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)).
Expand Down
87 changes: 36 additions & 51 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ Spotless can check and apply formatting to any plain-text file, using simple rul

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

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.

<a name="css-wtp"></a>

## Applying to CSS sources

```gradle
spotless {
css {
target '**/*.css' '**/*.css2'// Change file filter. By default files with 'css' extension are supported
eclipse().configFile './css-formatter.prefs' // Properties file of the Eclipse WTP formatter
// Use for example eclipse('4.7.3a') to specify a specific version of Eclipse,
// available versions are: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters
// also supports license headers
licenseHeader '<!-- Licensed under Apache-2.0 -->' // License header
licenseHeaderFile './license.txt' // License header file
}
}
```

<a name="eclipse-wtp-css"></a>

### Eclipse [WTP](https://www.eclipse.org/webtools/) CSS formatter
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.

<a name="xml-wtp"></a>

## Applying to XML sources

```gradle
spotless {
xml {
target '**/*.xml' // Change file filter. By default files with 'xml', 'xsl', 'xslt', 'wsdl', 'xsd', 'exsd' and 'xmi' extension are supported
eclipse().configFile './xml-formatter.prefs' // Properties file of the Eclipse WTP formatter
// Use for example eclipse('4.7.3a') to specify a specific version of Eclipse,
// available versions are: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters
// also supports license headers
licenseHeader '<!-- Licensed under Apache-2.0 -->' // License header
licenseHeaderFile './license.txt' // License header file
}
}
```

<a name="eclipse-wtp-xml"></a>

### Eclipse [WTP](https://www.eclipse.org/webtools/) XML formatter
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.

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`.


<a name="typescript"></a>

## Applying to Typescript source
Expand Down Expand Up @@ -510,6 +460,41 @@ spotless {

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

<a name="eclipse-wtp"></a>

## Applying [Eclipse WTP](https://www.eclipse.org/webtools/) to css | html | etc.

The Eclipse [WTP](https://www.eclipse.org/webtools/) formatter can be applied as follows:

```gradle
spotless {
format 'xml', {
target fileTree('.') {
include '**/*.xml', '**/*.xsd'
exclude '**/build/**'
}
// Use for example eclipseWtp('xml', '4.7.3a') to specify a specific version of Eclipse,
// available versions are: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters
eclipseWtp('xml').configFile 'spotless.xml.prefs' 'spotless.common.properties'
}
}
```
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:

| Type | Configuration | File location
| ---- | ------------------- | -------------
| CSS | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
| HTML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.html.core.prefs
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.html.core.prefs
| | embedded CSS | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
| | embedded JS | Use the export in the Eclipse editor configuration dialog
| JS | editor preferences | Use the export in the Eclipse editor configuration dialog
| JSON | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.json.core.prefs
| XML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.xml.core.prefs

Note that `HTML` should be used for `X-HTML` sources instead of `XML`.

<a name="license-header"></a>

## License header options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;

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

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

/**
* The CSS Eclipse configuration is deprecated. Use the {@link FormatExtension.EclipseWtpConfig} instead.
*/
@Deprecated
public class EclipseConfig {
private final EclipseBasedStepBuilder builder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import com.diffplug.spotless.LazyForwardingEquality;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;
import com.diffplug.spotless.generic.EndWithNewlineStep;
import com.diffplug.spotless.generic.IndentStep;
import com.diffplug.spotless.generic.LicenseHeaderStep;
Expand Down Expand Up @@ -483,6 +485,31 @@ public PrettierConfig prettier() {
return prettierConfig;
}

public class EclipseWtpConfig {
private final EclipseBasedStepBuilder builder;

EclipseWtpConfig(EclipseWtpFormatterStep type, String version) {
builder = type.createBuilder(GradleProvisioner.fromProject(getProject()));
builder.setVersion(version);
addStep(builder.build());
}

public void configFile(Object... configFiles) {
requireElementsNonNull(configFiles);
Project project = getProject();
builder.setPreferences(project.files(configFiles).getFiles());
replaceStep(builder.build());
}
}

public EclipseWtpConfig eclipseWtp(EclipseWtpFormatterStep type) {
return eclipseWtp(type, EclipseWtpFormatterStep.defaultVersion());
}

public EclipseWtpConfig eclipseWtp(EclipseWtpFormatterStep type, String version) {
return new EclipseWtpConfig(type, version);
}

/** Sets up a format task according to the values in this extension. */
protected void setupTask(SpotlessTask task) {
task.setPaddedCell(paddedCell);
Expand Down
Loading