From ed9612b73c16c8f18e17d777c22514532b741a95 Mon Sep 17 00:00:00 2001 From: naciron Date: Thu, 26 Mar 2020 13:49:00 +0000 Subject: [PATCH 01/30] Add Typescript Tsfmt to Maven Plugin --- .../spotless/maven/AbstractSpotlessMojo.java | 6 +- .../spotless/maven/typescript/Tsfmt.java | 98 ++++++++++++++ .../spotless/maven/typescript/Typescript.java | 49 +++++++ .../spotless/maven/MavenIntegrationTest.java | 4 + .../typescript/TypescriptFormatStepTest.java | 125 ++++++++++++++++++ .../tsfmt/TypescriptCodeFormatted.test | 4 + .../tsfmt/TypescriptCodeUnformatted.test | 4 + .../resources/typescript/tsfmt/tsconfig.json | 3 + .../resources/typescript/tsfmt/tsfmt.json | 24 ++++ .../resources/typescript/tsfmt/tslint.json | 11 ++ .../resources/typescript/tsfmt/vscode.json | 15 +++ 11 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java create mode 100644 testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test create mode 100644 testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test create mode 100644 testlib/src/main/resources/typescript/tsfmt/tsconfig.json create mode 100644 testlib/src/main/resources/typescript/tsfmt/tsfmt.json create mode 100644 testlib/src/main/resources/typescript/tsfmt/tslint.json create mode 100644 testlib/src/main/resources/typescript/tsfmt/vscode.json diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index f045348117..e43d1c7850 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -45,6 +45,7 @@ import com.diffplug.spotless.maven.java.Java; import com.diffplug.spotless.maven.kotlin.Kotlin; import com.diffplug.spotless.maven.scala.Scala; +import com.diffplug.spotless.maven.typescript.Typescript; public abstract class AbstractSpotlessMojo extends AbstractMojo { @@ -97,6 +98,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter private Cpp cpp; + + @Parameter + private Typescript typescript; /** The CSS extension is discontinued. */ @Parameter @@ -179,7 +183,7 @@ private FileLocator getFileLocator() { } private List getFormatterFactories() { - return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, css, xml)) + return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, typescript, css, xml)) .filter(Objects::nonNull) .collect(toList()); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java new file mode 100644 index 0000000000..c5613eb275 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -0,0 +1,98 @@ +/* + * 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.typescript; + +import java.io.File; +import java.util.Map; + +import org.apache.maven.plugins.annotations.Parameter; +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; +import com.diffplug.spotless.npm.TsConfigFileType; +import com.diffplug.spotless.npm.TsFmtFormatterStep; +import com.diffplug.spotless.npm.TypedTsFmtConfigFile; + +public class Tsfmt implements FormatterStepFactory { + + @Parameter + private String tslintFile; + + @Parameter + private String tsconfigFile; + + @Parameter + private String vscodeFile; + + @Parameter + private String tsfmtFile; + + @Parameter + private String typescriptFormatterVersion; + + @Parameter + private String typescriptVersion; + + @Parameter + private String tslintVersion; + + @Parameter + private String npmExecutable; + + @Parameter + private Map config; + + @Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true) + private File buildDir; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { + + Map devDependencies = TsFmtFormatterStep.defaultDevDependencies(); + if (typescriptFormatterVersion != null) { devDependencies.put("typescript-formatter", typescriptFormatterVersion); } + if (typescriptVersion != null) { devDependencies.put("typescript", typescriptVersion); } + if (tslintVersion != null) { devDependencies.put("tslint", tslintVersion); } + + File npm = npmExecutable != null ? stepConfig.getFileLocator().locateFile(npmExecutable) : null; + + TypedTsFmtConfigFile configFile = null; + + // check that there is only 1 config file or inline config + if (this.tsconfigFile != null + ^ this.tsfmtFile != null + ^ this.tslintFile != null + ^ this.vscodeFile != null) { + if (this.tsconfigFile != null) { + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().locateFile(tsconfigFile)); + } else if (this.tsfmtFile != null) { + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, stepConfig.getFileLocator().locateFile(tsfmtFile)); + } else if (this.tslintFile != null) { + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().locateFile(tslintFile)); + } else if (this.vscodeFile != null) { + configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().locateFile(vscodeFile)); + } + } else { + if (config == null) { + throw new IllegalArgumentException("must specify exactly one configFile or config"); + } + } + + if (buildDir == null) { + buildDir = new File("."); + } + return TsFmtFormatterStep.create(devDependencies, stepConfig.getProvisioner(), buildDir, npm, configFile, config); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java new file mode 100644 index 0000000000..9fe596e4ab --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java @@ -0,0 +1,49 @@ +/* + * 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.typescript; + +import java.util.Set; + +import com.diffplug.common.collect.ImmutableSet; +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 typescript source files. + */ +public class Typescript extends FormatterFactory { + + private static final Set DEFAULT_INCLUDES = ImmutableSet.of("**/*.ts"); + + private static final String LICENSE_HEADER_DELIMITER = null; + + @Override + public Set defaultIncludes() { + return DEFAULT_INCLUDES; + } + + @Override + public String licenseHeaderDelimiter() { + return LICENSE_HEADER_DELIMITER; + } + + public void addTsfmt(Tsfmt tsfmt) { + addStepFactory(tsfmt); + } + +} 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 8036b1899e..e27f965a6a 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 @@ -118,6 +118,10 @@ protected void writePomWithCssSteps(String... steps) throws IOException { writePom(groupWithSteps("css", steps)); } + protected void writePomWithTypescriptSteps(String... steps) throws IOException { + writePom(groupWithSteps("typescript", steps)); + } + protected void writePom(String... configuration) throws IOException { writePom(null, configuration); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java new file mode 100644 index 0000000000..c56ceeb5d6 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java @@ -0,0 +1,125 @@ +/* + * 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.typescript; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Ignore; +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; +import com.diffplug.spotless.maven.MavenRunner; + +public class TypescriptFormatStepTest extends MavenIntegrationTest { + + @Test + public void testTypescriptTsfmtTslintFile() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/tslint.json", + " 7.2.2", + ""); + setFile("tslint.json").toResource("typescript/tsfmt/tslint.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + } + + @Test + @Ignore + public void testTypescriptTsfmtTsConfigFile() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/", + " ${basedir}/tsconfig.json", + " ${basedir}/tsfmt.json", + " 7.2.2", + ""); + setFile("tsconfig.json").toResource("typescript/tsfmt/tsconfig.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + } + + @Test + public void testTypescriptTsfmtTsFmtFile() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/tsfmt.json", + " 7.2.2", + ""); + setFile("tsfmt.json").toResource("typescript/tsfmt/tsfmt.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + } + + @Test + public void testTypescriptTsfmtVsCodeFile() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/vscode.json", + " 7.2.2", + ""); + setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + } + + @Test + public void testTypescriptTsfmtInlineConfig() throws Exception { + writePomWithTypescriptSteps( + "", + " ", + " 1", + " true", + " ", + " 7.2.2", + ""); + setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + } + + @Test + public void testTypescript_2_Configs() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/tslint.json", + " ${basedir}/tslint.json", + " 7.2.2", + ""); + setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); + setFile("tsfmt.json").toResource("typescript/tsfmt/tsfmt.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + MavenRunner.Result result = mavenRunner().withArguments("spotless:apply").runHasError(); + assertThat(result.output()).contains("must specify exactly one configFile or config"); + } +} diff --git a/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test b/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test new file mode 100644 index 0000000000..63d84d87ca --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test @@ -0,0 +1,4 @@ +import '@stencil/router'; + +class Sample { hello(word = "world") { return "Hello, " + word; } } +new Sample().hello("TypeScript"); \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test b/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test new file mode 100644 index 0000000000..e366b51e0d --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test @@ -0,0 +1,4 @@ +import '@stencil/router'; + +class Sample{hello(word="world"){return "Hello, "+word;}} +new Sample().hello("TypeScript"); \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/tsconfig.json b/testlib/src/main/resources/typescript/tsfmt/tsconfig.json new file mode 100644 index 0000000000..4b87261319 --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/tsconfig.json @@ -0,0 +1,3 @@ +{ + "include": ["src/main/typescript/*.ts"] +} \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/tsfmt.json b/testlib/src/main/resources/typescript/tsfmt/tsfmt.json new file mode 100644 index 0000000000..86f08eced6 --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/tsfmt.json @@ -0,0 +1,24 @@ +{ + "baseIndentSize": 0, + "indentSize": 4, + "tabSize": 4, + "indentStyle": 2, + "newLineCharacter": "\r\n", + "convertTabsToSpaces": true, + "insertSpaceAfterCommaDelimiter": true, + "insertSpaceAfterSemicolonInForStatements": true, + "insertSpaceBeforeAndAfterBinaryOperators": true, + "insertSpaceAfterConstructor": false, + "insertSpaceAfterKeywordsInControlFlowStatements": true, + "insertSpaceAfterFunctionKeywordForAnonymousFunctions": false, + "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false, + "insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false, + "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true, + "insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false, + "insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false, + "insertSpaceAfterTypeAssertion": false, + "insertSpaceBeforeFunctionParenthesis": false, + "insertSpaceBeforeTypeAnnotation": true, + "placeOpenBraceOnNewLineForFunctions": false, + "placeOpenBraceOnNewLineForControlBlocks": false +} \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/tslint.json b/testlib/src/main/resources/typescript/tsfmt/tslint.json new file mode 100644 index 0000000000..3c77774f17 --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/tslint.json @@ -0,0 +1,11 @@ +{ + "rules": { + "indent": [true, 4], + "whitespace": [true, + "check-branch", + "check-operator", + "check-separator", + "check-typecast" + ] + } +} \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/vscode.json b/testlib/src/main/resources/typescript/tsfmt/vscode.json new file mode 100644 index 0000000000..9f3ee12bf0 --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/vscode.json @@ -0,0 +1,15 @@ +{ + // Place your settings in this file to overwrite default and user settings. + "typescript.format.enable": true, + "typescript.format.insertSpaceAfterCommaDelimiter": true, + "typescript.format.insertSpaceAfterSemicolonInForStatements": true, + "typescript.format.insertSpaceBeforeAndAfterBinaryOperators": true, + "typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": true, + "typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false, + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false, + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false, + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false, + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false, + "typescript.format.placeOpenBraceOnNewLineForFunctions": false, + "typescript.format.placeOpenBraceOnNewLineForControlBlocks": false +} \ No newline at end of file From 0e429a5304cd60d727f2fab228c2ccb35060657d Mon Sep 17 00:00:00 2001 From: Naciron Date: Thu, 26 Mar 2020 14:51:58 +0100 Subject: [PATCH 02/30] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5c311133da..8fe571ec2a 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ extra('java.EclipseFormatterStep') +'{{yes}} | {{yes}} lib('kotlin.KtLintStep') +'{{yes}} | {{yes}} | {{no}} |', lib('markdown.FreshMarkStep') +'{{yes}} | {{no}} | {{no}} |', lib('npm.PrettierFormatterStep') +'{{yes}} | {{no}} | {{no}} |', -lib('npm.TsFmtFormatterStep') +'{{yes}} | {{no}} | {{no}} |', +lib('npm.TsFmtFormatterStep') +'{{yes}} | {{yes}} | {{no}} |', lib('scala.ScalaFmtStep') +'{{yes}} | {{yes}} | {{no}} |', lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} | {{no}} |', extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}} | {{no}} |', From bb72ebdc4da6106970af8950c574f3a71c1b7964 Mon Sep 17 00:00:00 2001 From: Naciron Date: Thu, 26 Mar 2020 15:02:26 +0100 Subject: [PATCH 03/30] Update README to include tsfmt maven plugin --- plugin-maven/README.md | 74 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 7e03e07eee..6b46764f09 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -193,6 +193,80 @@ By default, all files matching `src/main/cpp/**/*.` and `src/test/cpp/**/*. ``` 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 configuration ``. If no `` is provided, the CDT default configuration is used. + + +## Applying to Typescript source + +To use tsfmt, you first have to specify the files that you want it to apply to. +Then you specify `tsfmt`, and optionally how you want to apply it. + +By default, all typescript source sets will be formatted. To change this, +set the `target` parameter as described in the [Custom rules](#custom) section. + +```xml + + + + + ${basedir}/path/to/repo/tslint.json + ${basedir}/path/to/repo/tsfmt.json + ${basedir}/path/to/repo/tsconfig.json + ${basedir}/path/to/repo/vscode.json + + 7.2.2 + 3.3.3 + 5.12.1 + + + +``` +Supported config file types are `tsconfigFile`, `tslintFile`, `vscodeFile` and `tsfmtFile`. They are corresponding to the respective +[tsfmt-parameters](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/index.ts#L27L34). + +*Please note:* +The auto-discovery of config files (up the file tree) will not work when using tsfmt within spotless, + hence you are required to provide resolvable file paths for config files. + +... or alternatively provide the configuration inline ... + +```xml + + + + + + 1 + true + + + 7.2.2 + 3.3.3 + 5.12.1 + + + +``` + +See [tsfmt's default config settings](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/utils.ts#L11L32) for what is available. + +### Prerequisite: tsfmt requires a working NodeJS version + +tsfmt is based on NodeJS, so to use it, a working NodeJS installation (especially npm) is required on the host running spotless. +Spotless will try to auto-discover an npm installation. If that is not working for you, it is possible to directly configure the npm binary to use. + +```xml + + + + ${basedir}/path/to/repo/tslint.json + /usr/bin/npm + + + +``` + +Spotless uses npm to install necessary packages locally. It runs tsfmt using [J2V8](https://github.com/eclipsesource/J2V8) internally after that. + ## Applying to custom sources From ae0501087c5a34ce0088ab28d67493c2062320cb Mon Sep 17 00:00:00 2001 From: Naciron Date: Thu, 26 Mar 2020 15:10:01 +0100 Subject: [PATCH 04/30] Update CHANGES.md --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 6f119b2b42..bbe8edd319 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Tsfmt Maven Plugin ([#548](https://github.com/diffplug/spotless/pull/548)) ## [1.28.0] - 2020-03-20 ### Added From 588990aa94268758d8f9cbe5e738034a6aa691ef Mon Sep 17 00:00:00 2001 From: Naciron Date: Thu, 26 Mar 2020 15:10:25 +0100 Subject: [PATCH 05/30] Update CHANGES.md --- plugin-maven/CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 0bc301fa73..1027efba3e 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Tsfmt Maven Plugin ([#548](https://github.com/diffplug/spotless/pull/548)) ## [1.28.0] - 2020-03-20 ### Added From e973a2eb4901667c3ab25ad6bc7ff858001eac4b Mon Sep 17 00:00:00 2001 From: naciron Date: Thu, 26 Mar 2020 14:13:07 +0000 Subject: [PATCH 06/30] Fix unit test, correct xml end tag --- .../spotless/maven/typescript/TypescriptFormatStepTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java index c56ceeb5d6..2d89c2f2ed 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java @@ -94,7 +94,7 @@ public void testTypescriptTsfmtInlineConfig() throws Exception { "", " ", " 1", - " true", + " true", " ", " 7.2.2", ""); From 2ebe1630860da3bf78e6d9111c734814f0ebd974 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 26 Mar 2020 14:24:41 +0000 Subject: [PATCH 07/30] Spotless apply --- .../spotless/maven/AbstractSpotlessMojo.java | 2 +- .../spotless/maven/typescript/Tsfmt.java | 39 +++++++++++-------- .../spotless/maven/typescript/Typescript.java | 5 +-- .../spotless/maven/MavenIntegrationTest.java | 2 +- .../typescript/TypescriptFormatStepTest.java | 14 +++---- 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index e43d1c7850..195614a768 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -98,7 +98,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter private Cpp cpp; - + @Parameter private Typescript typescript; diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java index c5613eb275..1eb2fbd079 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -19,6 +19,7 @@ import java.util.Map; import org.apache.maven.plugins.annotations.Parameter; + import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.maven.FormatterStepConfig; import com.diffplug.spotless.maven.FormatterStepFactory; @@ -39,7 +40,7 @@ public class Tsfmt implements FormatterStepFactory { @Parameter private String tsfmtFile; - + @Parameter private String typescriptFormatterVersion; @@ -51,7 +52,7 @@ public class Tsfmt implements FormatterStepFactory { @Parameter private String npmExecutable; - + @Parameter private Map config; @@ -60,36 +61,42 @@ public class Tsfmt implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { - + Map devDependencies = TsFmtFormatterStep.defaultDevDependencies(); - if (typescriptFormatterVersion != null) { devDependencies.put("typescript-formatter", typescriptFormatterVersion); } - if (typescriptVersion != null) { devDependencies.put("typescript", typescriptVersion); } - if (tslintVersion != null) { devDependencies.put("tslint", tslintVersion); } - + if (typescriptFormatterVersion != null) { + devDependencies.put("typescript-formatter", typescriptFormatterVersion); + } + if (typescriptVersion != null) { + devDependencies.put("typescript", typescriptVersion); + } + if (tslintVersion != null) { + devDependencies.put("tslint", tslintVersion); + } + File npm = npmExecutable != null ? stepConfig.getFileLocator().locateFile(npmExecutable) : null; - + TypedTsFmtConfigFile configFile = null; - + // check that there is only 1 config file or inline config - if (this.tsconfigFile != null + if (this.tsconfigFile != null ^ this.tsfmtFile != null ^ this.tslintFile != null ^ this.vscodeFile != null) { if (this.tsconfigFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().locateFile(tsconfigFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().locateFile(tsconfigFile)); } else if (this.tsfmtFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, stepConfig.getFileLocator().locateFile(tsfmtFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, stepConfig.getFileLocator().locateFile(tsfmtFile)); } else if (this.tslintFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().locateFile(tslintFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().locateFile(tslintFile)); } else if (this.vscodeFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().locateFile(vscodeFile)); - } + configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().locateFile(vscodeFile)); + } } else { if (config == null) { throw new IllegalArgumentException("must specify exactly one configFile or config"); } } - + if (buildDir == null) { buildDir = new File("."); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java index 9fe596e4ab..5e4a162cbd 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java @@ -19,7 +19,6 @@ import com.diffplug.common.collect.ImmutableSet; import com.diffplug.spotless.maven.FormatterFactory; -import com.diffplug.spotless.maven.generic.LicenseHeader; /** * A {@link FormatterFactory} implementation that corresponds to {@code ...} configuration element. @@ -31,7 +30,7 @@ public class Typescript extends FormatterFactory { private static final Set DEFAULT_INCLUDES = ImmutableSet.of("**/*.ts"); private static final String LICENSE_HEADER_DELIMITER = null; - + @Override public Set defaultIncludes() { return DEFAULT_INCLUDES; @@ -41,7 +40,7 @@ public Set defaultIncludes() { public String licenseHeaderDelimiter() { return LICENSE_HEADER_DELIMITER; } - + public void addTsfmt(Tsfmt tsfmt) { addStepFactory(tsfmt); } 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 e27f965a6a..09da08e2e5 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 @@ -121,7 +121,7 @@ protected void writePomWithCssSteps(String... steps) throws IOException { protected void writePomWithTypescriptSteps(String... steps) throws IOException { writePom(groupWithSteps("typescript", steps)); } - + protected void writePom(String... configuration) throws IOException { writePom(null, configuration); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java index 2d89c2f2ed..3ef3d734da 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java @@ -39,7 +39,7 @@ public void testTypescriptTsfmtTslintFile() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); } - + @Test @Ignore public void testTypescriptTsfmtTsConfigFile() throws Exception { @@ -57,7 +57,7 @@ public void testTypescriptTsfmtTsConfigFile() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); } - + @Test public void testTypescriptTsfmtTsFmtFile() throws Exception { writePomWithTypescriptSteps( @@ -72,12 +72,12 @@ public void testTypescriptTsfmtTsFmtFile() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); } - + @Test public void testTypescriptTsfmtVsCodeFile() throws Exception { writePomWithTypescriptSteps( "", - " ${basedir}/vscode.json", + " ${basedir}/vscode.json", " 7.2.2", ""); setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); @@ -87,7 +87,7 @@ public void testTypescriptTsfmtVsCodeFile() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); } - + @Test public void testTypescriptTsfmtInlineConfig() throws Exception { writePomWithTypescriptSteps( @@ -95,7 +95,7 @@ public void testTypescriptTsfmtInlineConfig() throws Exception { " ", " 1", " true", - " ", + " ", " 7.2.2", ""); setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); @@ -105,7 +105,7 @@ public void testTypescriptTsfmtInlineConfig() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); } - + @Test public void testTypescript_2_Configs() throws Exception { writePomWithTypescriptSteps( From 588aa43bd906c496ebdbc8af339507a42973d13e Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 26 Mar 2020 14:41:27 +0000 Subject: [PATCH 08/30] Spotless apply --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8fe571ec2a..c5a1de180d 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}} | [`kotlin.KtLintStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java) | :+1: | :+1: | :white_large_square: | | [`markdown.FreshMarkStep`](lib/src/main/java/com/diffplug/spotless/markdown/FreshMarkStep.java) | :+1: | :white_large_square: | :white_large_square: | | [`npm.PrettierFormatterStep`](lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: | -| [`npm.TsFmtFormatterStep`](lib/src/main/java/com/diffplug/spotless/npm/TsFmtFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: | +| [`npm.TsFmtFormatterStep`](lib/src/main/java/com/diffplug/spotless/npm/TsFmtFormatterStep.java) | :+1: | :+1: | :white_large_square: | | [`scala.ScalaFmtStep`](lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java) | :+1: | :+1: | :white_large_square: | | [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: | | [`wtp.EclipseWtpFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStep.java) | :+1: | :+1: | :white_large_square: | From 811c66134b2a31354d112f753419c04a2d69b4c7 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 28 Mar 2020 22:09:40 -0700 Subject: [PATCH 09/30] Make the Typescript defaults a little less expensive to compute, and closer to the other defaults. --- .../java/com/diffplug/spotless/maven/typescript/Typescript.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java index 5e4a162cbd..e2cd953268 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java @@ -27,7 +27,7 @@ */ public class Typescript extends FormatterFactory { - private static final Set DEFAULT_INCLUDES = ImmutableSet.of("**/*.ts"); + private static final Set DEFAULT_INCLUDES = ImmutableSet.of("src/**/*.ts"); private static final String LICENSE_HEADER_DELIMITER = null; From 90a187039eefb16eb21ea41ff0d40e8363577b5c Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 28 Mar 2020 22:10:15 -0700 Subject: [PATCH 10/30] Condense the typescript docs for plugin-maven. --- plugin-maven/README.md | 52 ++++++++++++------------------------------ 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 6b46764f09..1e1948dea9 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -197,43 +197,19 @@ Use the Eclipse to define the *Code Style preferences* (see [Eclipse documentati ## Applying to Typescript source -To use tsfmt, you first have to specify the files that you want it to apply to. -Then you specify `tsfmt`, and optionally how you want to apply it. - -By default, all typescript source sets will be formatted. To change this, -set the `target` parameter as described in the [Custom rules](#custom) section. - ```xml - + + + src/**/*.ts + + ${basedir}/path/to/repo/tslint.json ${basedir}/path/to/repo/tsfmt.json ${basedir}/path/to/repo/tsconfig.json ${basedir}/path/to/repo/vscode.json - - 7.2.2 - 3.3.3 - 5.12.1 - - - -``` -Supported config file types are `tsconfigFile`, `tslintFile`, `vscodeFile` and `tsfmtFile`. They are corresponding to the respective -[tsfmt-parameters](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/index.ts#L27L34). - -*Please note:* -The auto-discovery of config files (up the file tree) will not work when using tsfmt within spotless, - hence you are required to provide resolvable file paths for config files. - -... or alternatively provide the configuration inline ... - -```xml - - - - 1 true @@ -247,7 +223,12 @@ The auto-discovery of config files (up the file tree) will not work when using t ``` -See [tsfmt's default config settings](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/utils.ts#L11L32) for what is available. +Supported config file types are `tsconfigFile`, `tslintFile`, `vscodeFile` and `tsfmtFile`. They are corresponding to the respective +[tsfmt-parameters](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/index.ts#L27L34). See [tsfmt's default config settings](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/utils.ts#L11L32) for what is available. + +*Please note:* +The auto-discovery of config files (up the file tree) will not work when using tsfmt within spotless, + hence you are required to provide resolvable file paths for config files. ### Prerequisite: tsfmt requires a working NodeJS version @@ -255,14 +236,9 @@ tsfmt is based on NodeJS, so to use it, a working NodeJS installation (especiall Spotless will try to auto-discover an npm installation. If that is not working for you, it is possible to directly configure the npm binary to use. ```xml - - - - ${basedir}/path/to/repo/tslint.json - /usr/bin/npm - - - + + ... + /usr/bin/npm ``` Spotless uses npm to install necessary packages locally. It runs tsfmt using [J2V8](https://github.com/eclipsesource/J2V8) internally after that. From 97beb152d56af22c239136af6659df114d253d2e Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 29 Mar 2020 00:09:49 -0700 Subject: [PATCH 11/30] DRY on test files - reveals that tsfmtInline and tsconfig are both not working as expected. --- .../typescript/TypescriptFormatStepTest.java | 85 +++++++------------ .../tsfmt/TypescriptCodeFormatted.test | 4 - .../tsfmt/TypescriptCodeUnformatted.test | 4 - .../resources/typescript/tsfmt/tsconfig.json | 3 - .../resources/typescript/tsfmt/tsfmt.json | 24 ------ .../resources/typescript/tsfmt/tslint.json | 11 --- .../resources/typescript/tsfmt/vscode.json | 15 ---- 7 files changed, 31 insertions(+), 115 deletions(-) delete mode 100644 testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test delete mode 100644 testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test delete mode 100644 testlib/src/main/resources/typescript/tsfmt/tsconfig.json delete mode 100644 testlib/src/main/resources/typescript/tsfmt/tsfmt.json delete mode 100644 testlib/src/main/resources/typescript/tsfmt/tslint.json delete mode 100644 testlib/src/main/resources/typescript/tsfmt/vscode.json diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java index 3ef3d734da..052b1b2a7c 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java @@ -17,93 +17,71 @@ import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Ignore; +import java.io.IOException; + import org.junit.Test; import com.diffplug.spotless.maven.MavenIntegrationTest; import com.diffplug.spotless.maven.MavenRunner; public class TypescriptFormatStepTest extends MavenIntegrationTest { - - @Test - public void testTypescriptTsfmtTslintFile() throws Exception { - writePomWithTypescriptSteps( - "", - " ${basedir}/tslint.json", - " 7.2.2", - ""); - setFile("tslint.json").toResource("typescript/tsfmt/tslint.json"); - + private void run(String kind) throws IOException, InterruptedException { String path = "src/main/typescript/test.ts"; - setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + setFile(path).toResource("npm/tsfmt/" + kind + "/" + kind + ".dirty"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + assertFile(path).sameAsResource("npm/tsfmt/" + kind + "/" + kind + ".clean"); } @Test - @Ignore - public void testTypescriptTsfmtTsConfigFile() throws Exception { + public void tslint() throws Exception { writePomWithTypescriptSteps( "", - " ${basedir}/", - " ${basedir}/tsconfig.json", - " ${basedir}/tsfmt.json", - " 7.2.2", + " ${basedir}/tslint.json", ""); - setFile("tsconfig.json").toResource("typescript/tsfmt/tsconfig.json"); - - String path = "src/main/typescript/test.ts"; - setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); - mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + setFile("tslint.json").toResource("npm/tsfmt/tslint/tslint.json"); + run("tslint"); } @Test - public void testTypescriptTsfmtTsFmtFile() throws Exception { + public void vscode() throws Exception { writePomWithTypescriptSteps( "", - " ${basedir}/tsfmt.json", - " 7.2.2", + " ${basedir}/vscode.json", ""); - setFile("tsfmt.json").toResource("typescript/tsfmt/tsfmt.json"); - - String path = "src/main/typescript/test.ts"; - setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); - mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + setFile("vscode.json").toResource("npm/tsfmt/vscode/vscode.json"); + run("vscode"); } @Test - public void testTypescriptTsfmtVsCodeFile() throws Exception { + public void tsfmt() throws Exception { writePomWithTypescriptSteps( "", - " ${basedir}/vscode.json", - " 7.2.2", + " ${basedir}/tsfmt.json", ""); - setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); - - String path = "src/main/typescript/test.ts"; - setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); - mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + setFile("tsfmt.json").toResource("npm/tsfmt/tsfmt/tsfmt.json"); + run("tsfmt"); } @Test - public void testTypescriptTsfmtInlineConfig() throws Exception { + public void tsfmtInline() throws Exception { writePomWithTypescriptSteps( "", " ", " 1", " true", " ", - " 7.2.2", ""); - setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); + run("tsfmt"); + } - String path = "src/main/typescript/test.ts"; - setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); - mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + @Test + public void tsconfig() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/tsconfig.json", + ""); + setFile("tsconfig.json").toResource("npm/tsfmt/tsconfig/tsconfig.json"); + run("tsconfig"); } @Test @@ -112,13 +90,12 @@ public void testTypescript_2_Configs() throws Exception { "", " ${basedir}/tslint.json", " ${basedir}/tslint.json", - " 7.2.2", ""); - setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); - setFile("tsfmt.json").toResource("typescript/tsfmt/tsfmt.json"); + setFile("vscode.json").toResource("npm/tsfmt/vscode/vscode.json"); + setFile("tsfmt.json").toResource("npm/tsfmt/tsfmt/tsfmt.json"); String path = "src/main/typescript/test.ts"; - setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + setFile(path).toResource("npm/tsfmt/tsfmt/tsfmt.dirty"); MavenRunner.Result result = mavenRunner().withArguments("spotless:apply").runHasError(); assertThat(result.output()).contains("must specify exactly one configFile or config"); } diff --git a/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test b/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test deleted file mode 100644 index 63d84d87ca..0000000000 --- a/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test +++ /dev/null @@ -1,4 +0,0 @@ -import '@stencil/router'; - -class Sample { hello(word = "world") { return "Hello, " + word; } } -new Sample().hello("TypeScript"); \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test b/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test deleted file mode 100644 index e366b51e0d..0000000000 --- a/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test +++ /dev/null @@ -1,4 +0,0 @@ -import '@stencil/router'; - -class Sample{hello(word="world"){return "Hello, "+word;}} -new Sample().hello("TypeScript"); \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/tsconfig.json b/testlib/src/main/resources/typescript/tsfmt/tsconfig.json deleted file mode 100644 index 4b87261319..0000000000 --- a/testlib/src/main/resources/typescript/tsfmt/tsconfig.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "include": ["src/main/typescript/*.ts"] -} \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/tsfmt.json b/testlib/src/main/resources/typescript/tsfmt/tsfmt.json deleted file mode 100644 index 86f08eced6..0000000000 --- a/testlib/src/main/resources/typescript/tsfmt/tsfmt.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "baseIndentSize": 0, - "indentSize": 4, - "tabSize": 4, - "indentStyle": 2, - "newLineCharacter": "\r\n", - "convertTabsToSpaces": true, - "insertSpaceAfterCommaDelimiter": true, - "insertSpaceAfterSemicolonInForStatements": true, - "insertSpaceBeforeAndAfterBinaryOperators": true, - "insertSpaceAfterConstructor": false, - "insertSpaceAfterKeywordsInControlFlowStatements": true, - "insertSpaceAfterFunctionKeywordForAnonymousFunctions": false, - "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false, - "insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false, - "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true, - "insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false, - "insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false, - "insertSpaceAfterTypeAssertion": false, - "insertSpaceBeforeFunctionParenthesis": false, - "insertSpaceBeforeTypeAnnotation": true, - "placeOpenBraceOnNewLineForFunctions": false, - "placeOpenBraceOnNewLineForControlBlocks": false -} \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/tslint.json b/testlib/src/main/resources/typescript/tsfmt/tslint.json deleted file mode 100644 index 3c77774f17..0000000000 --- a/testlib/src/main/resources/typescript/tsfmt/tslint.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "rules": { - "indent": [true, 4], - "whitespace": [true, - "check-branch", - "check-operator", - "check-separator", - "check-typecast" - ] - } -} \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/vscode.json b/testlib/src/main/resources/typescript/tsfmt/vscode.json deleted file mode 100644 index 9f3ee12bf0..0000000000 --- a/testlib/src/main/resources/typescript/tsfmt/vscode.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - // Place your settings in this file to overwrite default and user settings. - "typescript.format.enable": true, - "typescript.format.insertSpaceAfterCommaDelimiter": true, - "typescript.format.insertSpaceAfterSemicolonInForStatements": true, - "typescript.format.insertSpaceBeforeAndAfterBinaryOperators": true, - "typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": true, - "typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false, - "typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false, - "typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false, - "typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false, - "typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false, - "typescript.format.placeOpenBraceOnNewLineForFunctions": false, - "typescript.format.placeOpenBraceOnNewLineForControlBlocks": false -} \ No newline at end of file From 8c10f56c7213885f0f3d8932eefe518b330d1802 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 29 Mar 2020 09:42:31 -0700 Subject: [PATCH 12/30] Make it easier to run single maven-plugin tests from the IDE. --- .../spotless/maven/MavenIntegrationTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 09da08e2e5..13302a41a1 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 @@ -39,10 +39,18 @@ import com.github.mustachejava.Mustache; import com.github.mustachejava.MustacheFactory; +import com.diffplug.common.base.Unhandled; import com.diffplug.common.io.Resources; import com.diffplug.spotless.ResourceHarness; public class MavenIntegrationTest extends ResourceHarness { + /** + * To run tests in the IDE, run `gradlew :plugin-maven:changelogPrint`, then + * put the last version it prints into `SPOTLESS_MAVEN_VERSION_IDE`. From now + * on, if you run `gradlew :plugin-maven:runMavenBuild`, then you can run tests + * in the IDE and they will run against the results of the last `runMavenBuild` + */ + private static final String SPOTLESS_MAVEN_VERSION_IDE = null; private static final String LOCAL_MAVEN_REPOSITORY_DIR = "localMavenRepositoryDir"; private static final String SPOTLESS_MAVEN_PLUGIN_VERSION = "spotlessMavenPluginVersion"; @@ -177,6 +185,15 @@ private static Map buildPomXmlParams(String[] executions, String } private static String getSystemProperty(String name) { + if (SPOTLESS_MAVEN_VERSION_IDE != null) { + if (name.equals("spotlessMavenPluginVersion")) { + return SPOTLESS_MAVEN_VERSION_IDE; + } else if (name.equals("localMavenRepositoryDir")) { + return new File("build/localMavenRepository").getAbsolutePath(); + } else { + throw Unhandled.stringException(name); + } + } String value = System.getProperty(name); if (isNullOrEmpty(value)) { fail("System property '" + name + "' is not defined"); From 7bd22209235feeab536054e74db1c4b137bf49fe Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 29 Mar 2020 09:43:46 -0700 Subject: [PATCH 13/30] Fix warning about -color --- .../main/java/com/diffplug/spotless/npm/NodeJSWrapper.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/src/main/java/com/diffplug/spotless/npm/NodeJSWrapper.java b/lib/src/main/java/com/diffplug/spotless/npm/NodeJSWrapper.java index bdb535591a..85cb8467f2 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/NodeJSWrapper.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/NodeJSWrapper.java @@ -20,6 +20,8 @@ import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; +import com.diffplug.spotless.LineEnding; + class NodeJSWrapper extends ReflectiveObjectWrapper { public static final String V8_RUNTIME_CLASS = "com.eclipsesource.v8.V8"; @@ -33,7 +35,7 @@ public NodeJSWrapper(ClassLoader classLoader) { super(Reflective.withClassLoader(classLoader), reflective -> { final boolean firstRun = flagsSet.compareAndSet(false, true); - if (firstRun) { + if (firstRun && LineEnding.PLATFORM_NATIVE.str().equals("\r\n")) { reflective.invokeStaticMethod(V8_RUNTIME_CLASS, "setFlags", "-color=false"); // required to run prettier on windows } return reflective.invokeStaticMethod(WRAPPED_CLASS, "createNodeJS"); From e9da14288a2aee87cc8a820f32010b870b92b420 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 29 Mar 2020 10:44:43 -0700 Subject: [PATCH 14/30] Fix types in the inline config. --- .../spotless/maven/typescript/Tsfmt.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java index 1eb2fbd079..faa4674876 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -16,6 +16,7 @@ package com.diffplug.spotless.maven.typescript; import java.io.File; +import java.util.LinkedHashMap; import java.util.Map; import org.apache.maven.plugins.annotations.Parameter; @@ -54,7 +55,7 @@ public class Tsfmt implements FormatterStepFactory { private String npmExecutable; @Parameter - private Map config; + private Map config; @Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true) private File buildDir; @@ -75,13 +76,17 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { File npm = npmExecutable != null ? stepConfig.getFileLocator().locateFile(npmExecutable) : null; - TypedTsFmtConfigFile configFile = null; - + TypedTsFmtConfigFile configFile; + Map configInline; // check that there is only 1 config file or inline config if (this.tsconfigFile != null ^ this.tsfmtFile != null ^ this.tslintFile != null ^ this.vscodeFile != null) { + if (this.config != null) { + throw onlyOneConfig(); + } + configInline = null; if (this.tsconfigFile != null) { configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().locateFile(tsconfigFile)); } else if (this.tsfmtFile != null) { @@ -90,16 +95,35 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().locateFile(tslintFile)); } else if (this.vscodeFile != null) { configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().locateFile(vscodeFile)); + } else { + throw new Error("Programming error: the xors did not match the cases"); } - } else { - if (config == null) { - throw new IllegalArgumentException("must specify exactly one configFile or config"); + } else if (config != null) { + configFile = null; + configInline = new LinkedHashMap<>(); + // try to parse string values as integers or booleans + for (Map.Entry e : config.entrySet()) { + try { + configInline.put(e.getKey(), Integer.parseInt(e.getValue())); + } catch (NumberFormatException ignore) { + try { + configInline.put(e.getKey(), Boolean.parseBoolean(e.getValue())); + } catch (IllegalArgumentException ignore2) { + configInline.put(e.getKey(), e.getValue()); + } + } } + } else { + throw onlyOneConfig(); } if (buildDir == null) { buildDir = new File("."); } - return TsFmtFormatterStep.create(devDependencies, stepConfig.getProvisioner(), buildDir, npm, configFile, config); + return TsFmtFormatterStep.create(devDependencies, stepConfig.getProvisioner(), buildDir, npm, configFile, configInline); + } + + private static IllegalArgumentException onlyOneConfig() { + return new IllegalArgumentException("must specify exactly one configFile or config"); } } From 2641254ff3f843bf9c40a6c75de614150a00bcbd Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 30 Mar 2020 09:07:48 +0100 Subject: [PATCH 15/30] Fix buildDir going into Tsfmt This does not work in the Tsfmt class but in the AbstractSpotlessMojo class: @Parameter(defaultValue = "${project.build.directory}" So the change is to set it in the Mojo class to the FileLocator and get it from there if required --- .../diffplug/spotless/maven/AbstractSpotlessMojo.java | 2 +- .../java/com/diffplug/spotless/maven/FileLocator.java | 10 ++++++++++ .../com/diffplug/spotless/maven/typescript/Tsfmt.java | 7 +------ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index 195614a768..bb24b3edfa 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -179,7 +179,7 @@ private FileLocator getFileLocator() { resourceManager.addSearchPath(FileResourceLoader.ID, baseDir.getAbsolutePath()); resourceManager.addSearchPath("url", ""); resourceManager.setOutputDirectory(targetDir); - return new FileLocator(resourceManager); + return new FileLocator(resourceManager, targetDir); } private List getFormatterFactories() { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java index 126ac4d49e..43bb39ddf4 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java @@ -30,9 +30,15 @@ public class FileLocator { static final String TMP_RESOURCE_FILE_PREFIX = "spotless-resource-"; private final ResourceManager resourceManager; + private final File buildDir; public FileLocator(ResourceManager resourceManager) { + this(resourceManager, null); + } + + public FileLocator(ResourceManager resourceManager, File buildDir) { this.resourceManager = resourceManager; + this.buildDir = buildDir; } public File locateFile(String path) { @@ -54,4 +60,8 @@ private static String tmpOutputFileName(String path) { String extension = FileUtils.extension(path); return TMP_RESOURCE_FILE_PREFIX + UUID.randomUUID() + '.' + extension; } + + public File getBuildDir() { + return this.buildDir; + } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java index faa4674876..818f0b7220 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -57,9 +57,6 @@ public class Tsfmt implements FormatterStepFactory { @Parameter private Map config; - @Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true) - private File buildDir; - @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { @@ -117,9 +114,7 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { throw onlyOneConfig(); } - if (buildDir == null) { - buildDir = new File("."); - } + File buildDir = stepConfig.getFileLocator().getBuildDir(); return TsFmtFormatterStep.create(devDependencies, stepConfig.getProvisioner(), buildDir, npm, configFile, configInline); } From 9ac7a4993b67d30a190ac48195b9c0ba6f4ff58e Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 30 Mar 2020 09:45:06 +0100 Subject: [PATCH 16/30] Fix tsconfig file When I use "files" with a direct link to "src/main/typescript/test.ts" it works. If I leave "files" out (and just use "include": [ "src/main/typescript/*.ts" ] ) it does fail with C:\Users\xyz\AppData\Local\Temp\junit3377288995823290344\target\spotless-node-modules-tsfmt-format\node_modules\typescript-formatter\lib\utils.js:64: Error: No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '["src/main/typescript/*.ts"]' and 'exclude' paths were '["nothing.xml"]'. -> [Help 1] --- .../main/resources/npm/tsfmt/tsconfig/tsconfig.json | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/testlib/src/main/resources/npm/tsfmt/tsconfig/tsconfig.json b/testlib/src/main/resources/npm/tsfmt/tsconfig/tsconfig.json index 4b233e423e..0551c651d7 100644 --- a/testlib/src/main/resources/npm/tsfmt/tsconfig/tsconfig.json +++ b/testlib/src/main/resources/npm/tsfmt/tsconfig/tsconfig.json @@ -1,4 +1,11 @@ { - // comment - /* comment */ + "files": [ + "src/main/typescript/test.ts" + ], + "include": [ + "src/main/typescript/*.ts" + ], + "exclude": [ + "nothing.xml" + ] } From 33b13f694924c69b5c3d22d9c2ab2cf6dbde55e2 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 30 Mar 2020 10:59:47 +0100 Subject: [PATCH 17/30] spotless apply --- .../main/java/com/diffplug/spotless/maven/FileLocator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java index 43bb39ddf4..f5ee5a287c 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java @@ -35,7 +35,7 @@ public class FileLocator { public FileLocator(ResourceManager resourceManager) { this(resourceManager, null); } - + public FileLocator(ResourceManager resourceManager, File buildDir) { this.resourceManager = resourceManager; this.buildDir = buildDir; @@ -60,7 +60,7 @@ private static String tmpOutputFileName(String path) { String extension = FileUtils.extension(path); return TMP_RESOURCE_FILE_PREFIX + UUID.randomUUID() + '.' + extension; } - + public File getBuildDir() { return this.buildDir; } From 80cc770d044ddceef11451faeeb98e22097bfc97 Mon Sep 17 00:00:00 2001 From: naciron Date: Mon, 30 Mar 2020 18:56:39 +0100 Subject: [PATCH 18/30] Fix issue with FileLocator and tsconfig file --- .../spotless/TypescriptExtensionTest.java | 21 +++++++++++++++++ .../spotless/maven/typescript/Tsfmt.java | 23 +++++++++++++++---- .../typescript/TypescriptFormatStepTest.java | 2 +- .../npm/tsfmt/tsconfig/tsconfig.json | 3 --- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java index 51905a0fde..3b322e6436 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java @@ -110,6 +110,27 @@ public void useTsfmtFileConfig() throws IOException { assertFile("test.ts").sameAsResource("npm/tsfmt/tsfmt/tsfmt.clean"); } + @Test + public void useTsConfigFileConfig() throws IOException { + setFile("tsconfig.json").toLines( + "{", + " \"include\": [\"*.ts\"]", + "}"); + setFile("build.gradle").toLines( + "buildscript { repositories { mavenCentral() } }", + "plugins {", + " id 'com.diffplug.gradle.spotless'", + "}", + "spotless {", + " typescript {", + " tsfmt().tsconfigFile('tsconfig.json')", + " }", + "}"); + setFile("src/main/typescript/test.ts").toResource("npm/tsfmt/tsfmt/tsfmt.dirty"); + gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); + assertFile("src/main/typescript/test.ts").sameAsResource("npm/tsfmt/tsfmt/tsfmt.clean"); + } + @Test public void usePrettier() throws IOException { setFile("build.gradle").toLines( diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java index 818f0b7220..5ddacf82bd 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -15,6 +15,8 @@ */ package com.diffplug.spotless.maven.typescript; +import static com.diffplug.common.base.Strings.isNullOrEmpty; + import java.io.File; import java.util.LinkedHashMap; import java.util.Map; @@ -57,6 +59,19 @@ public class Tsfmt implements FormatterStepFactory { @Parameter private Map config; + private File locateFile(String path) { + if (isNullOrEmpty(path)) { + return null; + } + + File exists = new File(path); + if (exists.exists()) { + return exists; + } + + throw new RuntimeException("Unable to locate file with path: " + path); + } + @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { @@ -85,13 +100,13 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { } configInline = null; if (this.tsconfigFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().locateFile(tsconfigFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, locateFile(tsconfigFile)); } else if (this.tsfmtFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, stepConfig.getFileLocator().locateFile(tsfmtFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, locateFile(tsfmtFile)); } else if (this.tslintFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().locateFile(tslintFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, locateFile(tslintFile)); } else if (this.vscodeFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().locateFile(vscodeFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, locateFile(vscodeFile)); } else { throw new Error("Programming error: the xors did not match the cases"); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java index 052b1b2a7c..2a2c95a516 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java @@ -78,7 +78,7 @@ public void tsfmtInline() throws Exception { public void tsconfig() throws Exception { writePomWithTypescriptSteps( "", - " ${basedir}/tsconfig.json", + " ${project.basedir}/tsconfig.json", ""); setFile("tsconfig.json").toResource("npm/tsfmt/tsconfig/tsconfig.json"); run("tsconfig"); diff --git a/testlib/src/main/resources/npm/tsfmt/tsconfig/tsconfig.json b/testlib/src/main/resources/npm/tsfmt/tsconfig/tsconfig.json index 0551c651d7..bc7da8603f 100644 --- a/testlib/src/main/resources/npm/tsfmt/tsconfig/tsconfig.json +++ b/testlib/src/main/resources/npm/tsfmt/tsconfig/tsconfig.json @@ -1,7 +1,4 @@ { - "files": [ - "src/main/typescript/test.ts" - ], "include": [ "src/main/typescript/*.ts" ], From daff537f0dcec5a1fb8a926794fc8a7818cfb498 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 30 Mar 2020 19:02:35 +0100 Subject: [PATCH 19/30] spotless apply --- .../java/com/diffplug/spotless/maven/typescript/Tsfmt.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java index 5ddacf82bd..241395c3c4 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -63,15 +63,15 @@ private File locateFile(String path) { if (isNullOrEmpty(path)) { return null; } - + File exists = new File(path); if (exists.exists()) { return exists; } - + throw new RuntimeException("Unable to locate file with path: " + path); } - + @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { From 839bcbd6e18b263149e90f5129262e42278ae789 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 30 Mar 2020 19:09:11 +0100 Subject: [PATCH 20/30] spotless apply --- .../com/diffplug/gradle/spotless/TypescriptExtensionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java index 3b322e6436..91ecf37dd4 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java @@ -130,7 +130,7 @@ public void useTsConfigFileConfig() throws IOException { gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); assertFile("src/main/typescript/test.ts").sameAsResource("npm/tsfmt/tsfmt/tsfmt.clean"); } - + @Test public void usePrettier() throws IOException { setFile("build.gradle").toLines( From 56ddfcbc196fe1544ef6808529573723f79042f3 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 30 Mar 2020 19:47:46 +0100 Subject: [PATCH 21/30] add tsconfig test to plugin gradle --- .../com/diffplug/gradle/spotless/TypescriptExtensionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java index 91ecf37dd4..12f1a4d0f5 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java @@ -126,9 +126,9 @@ public void useTsConfigFileConfig() throws IOException { " tsfmt().tsconfigFile('tsconfig.json')", " }", "}"); - setFile("src/main/typescript/test.ts").toResource("npm/tsfmt/tsfmt/tsfmt.dirty"); + setFile("test.ts").toResource("npm/tsfmt/tsconfig/tsconfig.dirty"); gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); - assertFile("src/main/typescript/test.ts").sameAsResource("npm/tsfmt/tsfmt/tsfmt.clean"); + assertFile("test.ts").sameAsResource("npm/tsfmt/tsconfig/tsconfig.clean"); } @Test From 5593769998ccbca8ce9078e4accbd0f064f51080 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 30 Mar 2020 20:39:04 -0700 Subject: [PATCH 22/30] Make the buildDir a non-optional part of FileLocator. --- .../diffplug/spotless/maven/FileLocator.java | 9 +++----- .../spotless/maven/FileLocatorTest.java | 21 ++++++++++++++----- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java index f5ee5a287c..40118be979 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java @@ -18,6 +18,7 @@ import static com.diffplug.common.base.Strings.isNullOrEmpty; import java.io.File; +import java.util.Objects; import java.util.UUID; import org.codehaus.plexus.resource.ResourceManager; @@ -32,13 +33,9 @@ public class FileLocator { private final ResourceManager resourceManager; private final File buildDir; - public FileLocator(ResourceManager resourceManager) { - this(resourceManager, null); - } - public FileLocator(ResourceManager resourceManager, File buildDir) { - this.resourceManager = resourceManager; - this.buildDir = buildDir; + this.resourceManager = Objects.requireNonNull(resourceManager); + this.buildDir = Objects.requireNonNull(buildDir); } public File locateFile(String path) { diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java index c2c1dfe380..4e24aa3a59 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java @@ -21,19 +21,30 @@ import static org.junit.Assert.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.io.File; import java.nio.file.Paths; import org.codehaus.plexus.resource.ResourceManager; +import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; -public class FileLocatorTest { +import com.diffplug.spotless.ResourceHarness; - private final ResourceManager resourceManager = mock(ResourceManager.class); - private final FileLocator fileLocator = new FileLocator(resourceManager); +public class FileLocatorTest extends ResourceHarness { + + private ResourceManager resourceManager; + private FileLocator fileLocator; + + @Before + public void setup() { + resourceManager = mock(ResourceManager.class); + fileLocator = new FileLocator(resourceManager, rootFolder()); + } @Test public void locateEmptyString() { @@ -64,7 +75,7 @@ public void locateConfFileWithIncorrectSeparators() throws Exception { } private void testFileLocator(String path, String extension) throws Exception { - File tmpOutputFile = new File("tmp-file"); + File tmpOutputFile = newFile("tmp-file"); when(resourceManager.getResourceAsFile(any(), any())).thenReturn(tmpOutputFile); File locatedFile = fileLocator.locateFile(path); From 5f4bbfa5616f46acbf65c08d362e85d6ff26e5cd Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 30 Mar 2020 21:05:43 -0700 Subject: [PATCH 23/30] Give FileLocator an in-place locator capability. --- .../diffplug/spotless/maven/FileLocator.java | 19 +++++++++++--- .../spotless/maven/typescript/Tsfmt.java | 26 ++++--------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java index 40118be979..67424d70c0 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java @@ -21,6 +21,8 @@ import java.util.Objects; import java.util.UUID; +import javax.annotation.Nullable; + import org.codehaus.plexus.resource.ResourceManager; import org.codehaus.plexus.resource.loader.FileResourceCreationException; import org.codehaus.plexus.resource.loader.ResourceNotFoundException; @@ -38,7 +40,11 @@ public FileLocator(ResourceManager resourceManager, File buildDir) { this.buildDir = Objects.requireNonNull(buildDir); } - public File locateFile(String path) { + /** + * Resolves the given file (not folder) from the project directory, but does so by copying it to a random + * filename with the same extension in the build directory. + */ + public @Nullable File locateFile(@Nullable String path) { if (isNullOrEmpty(path)) { return null; } @@ -58,7 +64,14 @@ private static String tmpOutputFileName(String path) { return TMP_RESOURCE_FILE_PREFIX + UUID.randomUUID() + '.' + extension; } - public File getBuildDir() { - return this.buildDir; + /** + * Resolves the given file or folder from the project directory, doesn't have to exist. + */ + public File resolve(String path) { + if (path.isEmpty() || path.equals(".")) { + return buildDir; + } else { + return new File(buildDir, path); + } } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java index 241395c3c4..02ebd74dca 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -15,8 +15,6 @@ */ package com.diffplug.spotless.maven.typescript; -import static com.diffplug.common.base.Strings.isNullOrEmpty; - import java.io.File; import java.util.LinkedHashMap; import java.util.Map; @@ -59,22 +57,8 @@ public class Tsfmt implements FormatterStepFactory { @Parameter private Map config; - private File locateFile(String path) { - if (isNullOrEmpty(path)) { - return null; - } - - File exists = new File(path); - if (exists.exists()) { - return exists; - } - - throw new RuntimeException("Unable to locate file with path: " + path); - } - @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { - Map devDependencies = TsFmtFormatterStep.defaultDevDependencies(); if (typescriptFormatterVersion != null) { devDependencies.put("typescript-formatter", typescriptFormatterVersion); @@ -100,13 +84,13 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { } configInline = null; if (this.tsconfigFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, locateFile(tsconfigFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().resolve(tsconfigFile)); } else if (this.tsfmtFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, locateFile(tsfmtFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, stepConfig.getFileLocator().resolve(tsfmtFile)); } else if (this.tslintFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, locateFile(tslintFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().resolve(tslintFile)); } else if (this.vscodeFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, locateFile(vscodeFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().resolve(vscodeFile)); } else { throw new Error("Programming error: the xors did not match the cases"); } @@ -129,7 +113,7 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { throw onlyOneConfig(); } - File buildDir = stepConfig.getFileLocator().getBuildDir(); + File buildDir = stepConfig.getFileLocator().resolve("."); return TsFmtFormatterStep.create(devDependencies, stepConfig.getProvisioner(), buildDir, npm, configFile, configInline); } From 6d21884f9eec8d504c5a0b9666d909018fbb8e58 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 30 Mar 2020 21:23:34 -0700 Subject: [PATCH 24/30] Make the gradle tests use the same test files as the maven and lib tests. --- .../gradle/spotless/TypescriptExtensionTest.java | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java index 12f1a4d0f5..677f135b70 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java @@ -89,11 +89,7 @@ public void useTsfmtInlineConfig() throws IOException { @Test public void useTsfmtFileConfig() throws IOException { - setFile("tsfmt.json").toLines( - "{", - " \"indentSize\": 1,", - " \"convertTabsToSpaces\": true", - "}"); + setFile("tsfmt.json").toResource("npm/tsfmt/tsfmt/tsfmt.json"); setFile("build.gradle").toLines( "buildscript { repositories { mavenCentral() } }", "plugins {", @@ -112,10 +108,7 @@ public void useTsfmtFileConfig() throws IOException { @Test public void useTsConfigFileConfig() throws IOException { - setFile("tsconfig.json").toLines( - "{", - " \"include\": [\"*.ts\"]", - "}"); + setFile("tsconfig.json").toResource("npm/tsfmt/tsconfig/tsconfig.json"); setFile("build.gradle").toLines( "buildscript { repositories { mavenCentral() } }", "plugins {", @@ -126,9 +119,9 @@ public void useTsConfigFileConfig() throws IOException { " tsfmt().tsconfigFile('tsconfig.json')", " }", "}"); - setFile("test.ts").toResource("npm/tsfmt/tsconfig/tsconfig.dirty"); + setFile("src/main/typescript/test.ts").toResource("npm/tsfmt/tsconfig/tsconfig.dirty"); gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); - assertFile("test.ts").sameAsResource("npm/tsfmt/tsconfig/tsconfig.clean"); + assertFile("src/main/typescript/test.ts").sameAsResource("npm/tsfmt/tsconfig/tsconfig.clean"); } @Test From 9567b07f6a9d5fee093133ebe81e5e0d2202a3d6 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 30 Mar 2020 21:36:38 -0700 Subject: [PATCH 25/30] Remove @Nullable from the maven plugin. --- .../main/java/com/diffplug/spotless/maven/FileLocator.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java index 67424d70c0..df169b835e 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java @@ -21,8 +21,6 @@ import java.util.Objects; import java.util.UUID; -import javax.annotation.Nullable; - import org.codehaus.plexus.resource.ResourceManager; import org.codehaus.plexus.resource.loader.FileResourceCreationException; import org.codehaus.plexus.resource.loader.ResourceNotFoundException; @@ -44,7 +42,7 @@ public FileLocator(ResourceManager resourceManager, File buildDir) { * Resolves the given file (not folder) from the project directory, but does so by copying it to a random * filename with the same extension in the build directory. */ - public @Nullable File locateFile(@Nullable String path) { + public File locateFile(String path) { if (isNullOrEmpty(path)) { return null; } From 2f77a776b909874ba16179f79581a6cede1a8547 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 1 Apr 2020 23:42:30 -0700 Subject: [PATCH 26/30] Revert all ned's commits since the last good commit by @source-knights --- .../spotless/TypescriptExtensionTest.java | 15 ++++++++--- .../diffplug/spotless/maven/FileLocator.java | 24 ++++++----------- .../spotless/maven/typescript/Tsfmt.java | 26 +++++++++++++++---- .../spotless/maven/FileLocatorTest.java | 21 ++++----------- 4 files changed, 45 insertions(+), 41 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java index 677f135b70..12f1a4d0f5 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java @@ -89,7 +89,11 @@ public void useTsfmtInlineConfig() throws IOException { @Test public void useTsfmtFileConfig() throws IOException { - setFile("tsfmt.json").toResource("npm/tsfmt/tsfmt/tsfmt.json"); + setFile("tsfmt.json").toLines( + "{", + " \"indentSize\": 1,", + " \"convertTabsToSpaces\": true", + "}"); setFile("build.gradle").toLines( "buildscript { repositories { mavenCentral() } }", "plugins {", @@ -108,7 +112,10 @@ public void useTsfmtFileConfig() throws IOException { @Test public void useTsConfigFileConfig() throws IOException { - setFile("tsconfig.json").toResource("npm/tsfmt/tsconfig/tsconfig.json"); + setFile("tsconfig.json").toLines( + "{", + " \"include\": [\"*.ts\"]", + "}"); setFile("build.gradle").toLines( "buildscript { repositories { mavenCentral() } }", "plugins {", @@ -119,9 +126,9 @@ public void useTsConfigFileConfig() throws IOException { " tsfmt().tsconfigFile('tsconfig.json')", " }", "}"); - setFile("src/main/typescript/test.ts").toResource("npm/tsfmt/tsconfig/tsconfig.dirty"); + setFile("test.ts").toResource("npm/tsfmt/tsconfig/tsconfig.dirty"); gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); - assertFile("src/main/typescript/test.ts").sameAsResource("npm/tsfmt/tsconfig/tsconfig.clean"); + assertFile("test.ts").sameAsResource("npm/tsfmt/tsconfig/tsconfig.clean"); } @Test diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java index df169b835e..f5ee5a287c 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java @@ -18,7 +18,6 @@ import static com.diffplug.common.base.Strings.isNullOrEmpty; import java.io.File; -import java.util.Objects; import java.util.UUID; import org.codehaus.plexus.resource.ResourceManager; @@ -33,15 +32,15 @@ public class FileLocator { private final ResourceManager resourceManager; private final File buildDir; + public FileLocator(ResourceManager resourceManager) { + this(resourceManager, null); + } + public FileLocator(ResourceManager resourceManager, File buildDir) { - this.resourceManager = Objects.requireNonNull(resourceManager); - this.buildDir = Objects.requireNonNull(buildDir); + this.resourceManager = resourceManager; + this.buildDir = buildDir; } - /** - * Resolves the given file (not folder) from the project directory, but does so by copying it to a random - * filename with the same extension in the build directory. - */ public File locateFile(String path) { if (isNullOrEmpty(path)) { return null; @@ -62,14 +61,7 @@ private static String tmpOutputFileName(String path) { return TMP_RESOURCE_FILE_PREFIX + UUID.randomUUID() + '.' + extension; } - /** - * Resolves the given file or folder from the project directory, doesn't have to exist. - */ - public File resolve(String path) { - if (path.isEmpty() || path.equals(".")) { - return buildDir; - } else { - return new File(buildDir, path); - } + public File getBuildDir() { + return this.buildDir; } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java index 02ebd74dca..241395c3c4 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -15,6 +15,8 @@ */ package com.diffplug.spotless.maven.typescript; +import static com.diffplug.common.base.Strings.isNullOrEmpty; + import java.io.File; import java.util.LinkedHashMap; import java.util.Map; @@ -57,8 +59,22 @@ public class Tsfmt implements FormatterStepFactory { @Parameter private Map config; + private File locateFile(String path) { + if (isNullOrEmpty(path)) { + return null; + } + + File exists = new File(path); + if (exists.exists()) { + return exists; + } + + throw new RuntimeException("Unable to locate file with path: " + path); + } + @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { + Map devDependencies = TsFmtFormatterStep.defaultDevDependencies(); if (typescriptFormatterVersion != null) { devDependencies.put("typescript-formatter", typescriptFormatterVersion); @@ -84,13 +100,13 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { } configInline = null; if (this.tsconfigFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().resolve(tsconfigFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, locateFile(tsconfigFile)); } else if (this.tsfmtFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, stepConfig.getFileLocator().resolve(tsfmtFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, locateFile(tsfmtFile)); } else if (this.tslintFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().resolve(tslintFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, locateFile(tslintFile)); } else if (this.vscodeFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().resolve(vscodeFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, locateFile(vscodeFile)); } else { throw new Error("Programming error: the xors did not match the cases"); } @@ -113,7 +129,7 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { throw onlyOneConfig(); } - File buildDir = stepConfig.getFileLocator().resolve("."); + File buildDir = stepConfig.getFileLocator().getBuildDir(); return TsFmtFormatterStep.create(devDependencies, stepConfig.getProvisioner(), buildDir, npm, configFile, configInline); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java index 4e24aa3a59..c2c1dfe380 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java @@ -21,30 +21,19 @@ import static org.junit.Assert.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.io.File; import java.nio.file.Paths; import org.codehaus.plexus.resource.ResourceManager; -import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; -import com.diffplug.spotless.ResourceHarness; +public class FileLocatorTest { -public class FileLocatorTest extends ResourceHarness { - - private ResourceManager resourceManager; - private FileLocator fileLocator; - - @Before - public void setup() { - resourceManager = mock(ResourceManager.class); - fileLocator = new FileLocator(resourceManager, rootFolder()); - } + private final ResourceManager resourceManager = mock(ResourceManager.class); + private final FileLocator fileLocator = new FileLocator(resourceManager); @Test public void locateEmptyString() { @@ -75,7 +64,7 @@ public void locateConfFileWithIncorrectSeparators() throws Exception { } private void testFileLocator(String path, String extension) throws Exception { - File tmpOutputFile = newFile("tmp-file"); + File tmpOutputFile = new File("tmp-file"); when(resourceManager.getResourceAsFile(any(), any())).thenReturn(tmpOutputFile); File locatedFile = fileLocator.locateFile(path); From 3d791da0a3d5a4c8c1b5d7f70bb3bbdd97214ad9 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 1 Apr 2020 23:39:13 -0700 Subject: [PATCH 27/30] Put the baseDir into FileLocator, and ensure that names match their "maven canonical" names (e.g. erroneously calling buildDir "target"). --- .../spotless/maven/AbstractSpotlessMojo.java | 8 ++++---- .../diffplug/spotless/maven/FileLocator.java | 20 ++++++++++--------- .../spotless/maven/FormatterConfig.java | 6 ------ .../spotless/maven/FormatterFactory.java | 4 ++-- .../spotless/maven/FileLocatorTest.java | 15 +++++++++++--- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index bb24b3edfa..ede8e58389 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -68,7 +68,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { private File baseDir; @Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true) - private File targetDir; + private File buildDir; @Parameter(defaultValue = DEFAULT_ENCODING) private String encoding; @@ -135,7 +135,7 @@ private List collectFiles(FormatterFactory formatterFactory) throws MojoEx Set includes = configuredIncludes.isEmpty() ? formatterFactory.defaultIncludes() : configuredIncludes; Set excludes = new HashSet<>(FileUtils.getDefaultExcludesAsList()); - excludes.add(withTrailingSeparator(targetDir.toString())); + excludes.add(withTrailingSeparator(buildDir.toString())); excludes.addAll(configuredExcludes); String includesString = String.join(",", includes); @@ -178,8 +178,8 @@ private FormatterConfig getFormatterConfig() { private FileLocator getFileLocator() { resourceManager.addSearchPath(FileResourceLoader.ID, baseDir.getAbsolutePath()); resourceManager.addSearchPath("url", ""); - resourceManager.setOutputDirectory(targetDir); - return new FileLocator(resourceManager, targetDir); + resourceManager.setOutputDirectory(buildDir); + return new FileLocator(resourceManager, baseDir, buildDir); } private List getFormatterFactories() { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java index f5ee5a287c..9bce2a8624 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java @@ -18,6 +18,7 @@ import static com.diffplug.common.base.Strings.isNullOrEmpty; import java.io.File; +import java.util.Objects; import java.util.UUID; import org.codehaus.plexus.resource.ResourceManager; @@ -30,15 +31,12 @@ public class FileLocator { static final String TMP_RESOURCE_FILE_PREFIX = "spotless-resource-"; private final ResourceManager resourceManager; - private final File buildDir; + private final File baseDir, buildDir; - public FileLocator(ResourceManager resourceManager) { - this(resourceManager, null); - } - - public FileLocator(ResourceManager resourceManager, File buildDir) { - this.resourceManager = resourceManager; - this.buildDir = buildDir; + public FileLocator(ResourceManager resourceManager, File baseDir, File buildDir) { + this.resourceManager = Objects.requireNonNull(resourceManager); + this.baseDir = Objects.requireNonNull(baseDir); + this.buildDir = Objects.requireNonNull(buildDir); } public File locateFile(String path) { @@ -61,7 +59,11 @@ private static String tmpOutputFileName(String path) { return TMP_RESOURCE_FILE_PREFIX + UUID.randomUUID() + '.' + extension; } + public File getBaseDir() { + return baseDir; + } + public File getBuildDir() { - return this.buildDir; + return buildDir; } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java index 096771c92d..bc2e5812a4 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java @@ -25,7 +25,6 @@ public class FormatterConfig { - private final File baseDir; private final String encoding; private final LineEnding lineEndings; private final Provisioner provisioner; @@ -34,7 +33,6 @@ public class FormatterConfig { public FormatterConfig(File baseDir, String encoding, LineEnding lineEndings, Provisioner provisioner, FileLocator fileLocator, List globalStepFactories) { - this.baseDir = baseDir; this.encoding = encoding; this.lineEndings = lineEndings; this.provisioner = provisioner; @@ -42,10 +40,6 @@ public FormatterConfig(File baseDir, String encoding, LineEnding lineEndings, Pr this.globalStepFactories = globalStepFactories; } - public File getBaseDir() { - return baseDir; - } - public String getEncoding() { return encoding; } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java index 0b96f07fb0..8796fa255c 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java @@ -64,7 +64,7 @@ public final Set excludes() { public final Formatter newFormatter(List filesToFormat, FormatterConfig config) { Charset formatterEncoding = encoding(config); LineEnding formatterLineEndings = lineEndings(config); - LineEnding.Policy formatterLineEndingPolicy = formatterLineEndings.createPolicy(config.getBaseDir(), () -> filesToFormat); + LineEnding.Policy formatterLineEndingPolicy = formatterLineEndings.createPolicy(config.getFileLocator().getBaseDir(), () -> filesToFormat); FormatterStepConfig stepConfig = stepConfig(formatterEncoding, config); List factories = gatherStepFactories(config.getGlobalStepFactories(), stepFactories); @@ -79,7 +79,7 @@ public final Formatter newFormatter(List filesToFormat, FormatterConfig co .lineEndingsPolicy(formatterLineEndingPolicy) .exceptionPolicy(new FormatExceptionPolicyStrict()) .steps(formatterSteps) - .rootDir(config.getBaseDir().toPath()) + .rootDir(config.getFileLocator().getBaseDir().toPath()) .build(); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java index c2c1dfe380..8b95a78fe7 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java @@ -27,13 +27,22 @@ import java.nio.file.Paths; import org.codehaus.plexus.resource.ResourceManager; +import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; -public class FileLocatorTest { +import com.diffplug.spotless.ResourceHarness; - private final ResourceManager resourceManager = mock(ResourceManager.class); - private final FileLocator fileLocator = new FileLocator(resourceManager); +public class FileLocatorTest extends ResourceHarness { + + private ResourceManager resourceManager; + private FileLocator fileLocator; + + @Before + public void setup() { + resourceManager = mock(ResourceManager.class); + fileLocator = new FileLocator(resourceManager, rootFolder(), rootFolder()); + } @Test public void locateEmptyString() { From f5d9e0724b2a27e8b89243e6ed54919e7036dc92 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 30 Mar 2020 21:23:34 -0700 Subject: [PATCH 28/30] Make the gradle tests use the same test files as the maven and lib tests. --- .../gradle/spotless/TypescriptExtensionTest.java | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java index 12f1a4d0f5..677f135b70 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java @@ -89,11 +89,7 @@ public void useTsfmtInlineConfig() throws IOException { @Test public void useTsfmtFileConfig() throws IOException { - setFile("tsfmt.json").toLines( - "{", - " \"indentSize\": 1,", - " \"convertTabsToSpaces\": true", - "}"); + setFile("tsfmt.json").toResource("npm/tsfmt/tsfmt/tsfmt.json"); setFile("build.gradle").toLines( "buildscript { repositories { mavenCentral() } }", "plugins {", @@ -112,10 +108,7 @@ public void useTsfmtFileConfig() throws IOException { @Test public void useTsConfigFileConfig() throws IOException { - setFile("tsconfig.json").toLines( - "{", - " \"include\": [\"*.ts\"]", - "}"); + setFile("tsconfig.json").toResource("npm/tsfmt/tsconfig/tsconfig.json"); setFile("build.gradle").toLines( "buildscript { repositories { mavenCentral() } }", "plugins {", @@ -126,9 +119,9 @@ public void useTsConfigFileConfig() throws IOException { " tsfmt().tsconfigFile('tsconfig.json')", " }", "}"); - setFile("test.ts").toResource("npm/tsfmt/tsconfig/tsconfig.dirty"); + setFile("src/main/typescript/test.ts").toResource("npm/tsfmt/tsconfig/tsconfig.dirty"); gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); - assertFile("test.ts").sameAsResource("npm/tsfmt/tsconfig/tsconfig.clean"); + assertFile("src/main/typescript/test.ts").sameAsResource("npm/tsfmt/tsconfig/tsconfig.clean"); } @Test From 7d21bf4a09795e5061b918945b33e845040f219f Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 1 Apr 2020 23:56:47 -0700 Subject: [PATCH 29/30] Extracted `Tsfmt.locateFile` into `Tsfmt.locateLocal`. --- .../diffplug/spotless/maven/FileLocator.java | 15 +++++++++++ .../spotless/maven/typescript/Tsfmt.java | 26 ++++--------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java index 9bce2a8624..22a93d80d7 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java @@ -39,6 +39,7 @@ public FileLocator(ResourceManager resourceManager, File baseDir, File buildDir) this.buildDir = Objects.requireNonNull(buildDir); } + /** Asserts that the given path is a file, then copies it with a new random name into the build folder. */ public File locateFile(String path) { if (isNullOrEmpty(path)) { return null; @@ -59,6 +60,20 @@ private static String tmpOutputFileName(String path) { return TMP_RESOURCE_FILE_PREFIX + UUID.randomUUID() + '.' + extension; } + /** Asserts that the given path exists as a file or folder. */ + public File locateLocal(String path) { + if (isNullOrEmpty(path)) { + return null; + } + + File exists = new File(path); + if (exists.exists()) { + return exists; + } + + throw new RuntimeException("Unable to locate file with path: " + path); + } + public File getBaseDir() { return baseDir; } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java index 241395c3c4..c57350c145 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -15,8 +15,6 @@ */ package com.diffplug.spotless.maven.typescript; -import static com.diffplug.common.base.Strings.isNullOrEmpty; - import java.io.File; import java.util.LinkedHashMap; import java.util.Map; @@ -59,22 +57,8 @@ public class Tsfmt implements FormatterStepFactory { @Parameter private Map config; - private File locateFile(String path) { - if (isNullOrEmpty(path)) { - return null; - } - - File exists = new File(path); - if (exists.exists()) { - return exists; - } - - throw new RuntimeException("Unable to locate file with path: " + path); - } - @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { - Map devDependencies = TsFmtFormatterStep.defaultDevDependencies(); if (typescriptFormatterVersion != null) { devDependencies.put("typescript-formatter", typescriptFormatterVersion); @@ -86,7 +70,7 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { devDependencies.put("tslint", tslintVersion); } - File npm = npmExecutable != null ? stepConfig.getFileLocator().locateFile(npmExecutable) : null; + File npm = npmExecutable != null ? stepConfig.getFileLocator().locateLocal(npmExecutable) : null; TypedTsFmtConfigFile configFile; Map configInline; @@ -100,13 +84,13 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { } configInline = null; if (this.tsconfigFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, locateFile(tsconfigFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().locateLocal(tsconfigFile)); } else if (this.tsfmtFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, locateFile(tsfmtFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, stepConfig.getFileLocator().locateLocal(tsfmtFile)); } else if (this.tslintFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, locateFile(tslintFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().locateLocal(tslintFile)); } else if (this.vscodeFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, locateFile(vscodeFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().locateLocal(vscodeFile)); } else { throw new Error("Programming error: the xors did not match the cases"); } From 1bb33d6281c1d769c1030f07bcfa07454b84daa5 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 2 Apr 2020 00:10:57 -0700 Subject: [PATCH 30/30] Remove maven-plugin info from the lib changelog. --- CHANGES.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index fded6016fe..9a9ba22e6d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,8 +10,6 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] -### Added -* Tsfmt Maven Plugin ([#548](https://github.com/diffplug/spotless/pull/548)) ### Fixed * Javadoc for the `ext/eclipse-*` projects. * Replace the deprecated `compile` with `implementation` for the `ext/eclipse-*` projects.