Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

tsfmt maven plugin #553

Merged
merged 32 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ed9612b
Add Typescript Tsfmt to Maven Plugin
source-knights Mar 26, 2020
0e429a5
Update README.md
source-knights Mar 26, 2020
bb72ebd
Update README to include tsfmt maven plugin
source-knights Mar 26, 2020
ae05010
Update CHANGES.md
source-knights Mar 26, 2020
588990a
Update CHANGES.md
source-knights Mar 26, 2020
e973a2e
Fix unit test, correct xml end tag
source-knights Mar 26, 2020
2ebe163
Spotless apply
source-knights Mar 26, 2020
6a71794
Merge branch 'master' of https://github.com/source-knights/spotless
source-knights Mar 26, 2020
588aa43
Spotless apply
source-knights Mar 26, 2020
528e708
Merge branch 'master' into master
nedtwigg Mar 28, 2020
811c661
Make the Typescript defaults a little less expensive to compute, and …
nedtwigg Mar 29, 2020
90a1870
Condense the typescript docs for plugin-maven.
nedtwigg Mar 29, 2020
97beb15
DRY on test files - reveals that tsfmtInline and tsconfig are both no…
nedtwigg Mar 29, 2020
8c10f56
Make it easier to run single maven-plugin tests from the IDE.
nedtwigg Mar 29, 2020
7bd2220
Fix warning about -color
nedtwigg Mar 29, 2020
e9da142
Fix types in the inline config.
nedtwigg Mar 29, 2020
2641254
Fix buildDir going into Tsfmt
source-knights Mar 30, 2020
9ac7a49
Fix tsconfig file
source-knights Mar 30, 2020
33b13f6
spotless apply
source-knights Mar 30, 2020
80cc770
Fix issue with FileLocator and tsconfig file
source-knights Mar 30, 2020
daff537
spotless apply
source-knights Mar 30, 2020
839bcbd
spotless apply
source-knights Mar 30, 2020
56ddfcb
add tsconfig test to plugin gradle
source-knights Mar 30, 2020
5593769
Make the buildDir a non-optional part of FileLocator.
nedtwigg Mar 31, 2020
5f4bbfa
Give FileLocator an in-place locator capability.
nedtwigg Mar 31, 2020
6d21884
Make the gradle tests use the same test files as the maven and lib te…
nedtwigg Mar 31, 2020
9567b07
Remove @Nullable from the maven plugin.
nedtwigg Mar 31, 2020
2f77a77
Revert all ned's commits since the last good commit by @source-knights
nedtwigg Apr 2, 2020
3d791da
Put the baseDir into FileLocator, and ensure that names match their "…
nedtwigg Apr 2, 2020
f5d9e07
Make the gradle tests use the same test files as the maven and lib te…
nedtwigg Mar 31, 2020
7d21bf4
Extracted `Tsfmt.locateFile` into `Tsfmt.locateLocal`.
nedtwigg Apr 2, 2020
1bb33d6
Remove maven-plugin info from the lib changelog.
nedtwigg Apr 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}} |',
Expand All @@ -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: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {",
Expand All @@ -110,6 +106,24 @@ public void useTsfmtFileConfig() throws IOException {
assertFile("test.ts").sameAsResource("npm/tsfmt/tsfmt/tsfmt.clean");
}

@Test
public void useTsConfigFileConfig() throws IOException {
setFile("tsconfig.json").toResource("npm/tsfmt/tsconfig/tsconfig.json");
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/tsconfig/tsconfig.dirty");
gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
assertFile("src/main/typescript/test.ts").sameAsResource("npm/tsfmt/tsconfig/tsconfig.clean");
}

@Test
public void usePrettier() throws IOException {
setFile("build.gradle").toLines(
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
### Fixed
* Eclipse-WTP formatter (web tools platform, not java) handles some character encodings incorrectly on OS with non-unicode default file encoding [#545](https://github.com/diffplug/spotless/issues/545). Fixed for Eclipse-WTP formatter Eclipse version 4.13.0 (default version).

Expand Down
50 changes: 50 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,56 @@ By default, all files matching `src/main/cpp/**/*.<ext>` 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 `<file>`. If no `<file>` is provided, the CDT default configuration is used.

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

## Applying to Typescript source

```xml
<configuration>
<typescript>
<tsfmt>
<!-- optionally define which files will be formatted. -->
<includes>
<include>src/**/*.ts</include> <!-- default value if nothing is specified -->
</includes>
<!-- must specify exactly one of the following "{foo}File" or "config" elements -->
<tslintFile>${basedir}/path/to/repo/tslint.json</tslintFile>
<tsfmtFile>${basedir}/path/to/repo/tsfmt.json</tsfmtFile>
<tsconfigFile>${basedir}/path/to/repo/tsconfig.json</tsconfigFile>
<vscodeFile>${basedir}/path/to/repo/vscode.json</vscodeFile>
<config>
<indentSize>1</indentSize>
<convertTabsToSpaces>true</convertTabsToSpaces>
</config>
<!-- optionally configure following versions to use, shown values are defaults-->
<typescriptFormatterVersion>7.2.2</typescriptFormatterVersion>
<typescriptVersion>3.3.3</typescriptVersion>
<tslintVersion>5.12.1</tslintVersion>
</tsfmt>
</typescript>
</configuration>
```

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

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
<configuration><typescript><tsfmt>
...
<npmExecutable>/usr/bin/npm</npmExecutable>
```

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

<a name="format"></a>

## Applying to custom sources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -67,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;
Expand Down Expand Up @@ -98,6 +99,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Parameter
private Cpp cpp;

@Parameter
private Typescript typescript;

/** The CSS extension is discontinued. */
@Parameter
@Deprecated
Expand Down Expand Up @@ -131,7 +135,7 @@ private List<File> collectFiles(FormatterFactory formatterFactory) throws MojoEx
Set<String> includes = configuredIncludes.isEmpty() ? formatterFactory.defaultIncludes() : configuredIncludes;

Set<String> excludes = new HashSet<>(FileUtils.getDefaultExcludesAsList());
excludes.add(withTrailingSeparator(targetDir.toString()));
excludes.add(withTrailingSeparator(buildDir.toString()));
excludes.addAll(configuredExcludes);

String includesString = String.join(",", includes);
Expand Down Expand Up @@ -174,12 +178,12 @@ private FormatterConfig getFormatterConfig() {
private FileLocator getFileLocator() {
resourceManager.addSearchPath(FileResourceLoader.ID, baseDir.getAbsolutePath());
resourceManager.addSearchPath("url", "");
resourceManager.setOutputDirectory(targetDir);
return new FileLocator(resourceManager);
resourceManager.setOutputDirectory(buildDir);
return new FileLocator(resourceManager, baseDir, buildDir);
}

private List<FormatterFactory> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,11 +31,15 @@ public class FileLocator {
static final String TMP_RESOURCE_FILE_PREFIX = "spotless-resource-";

private final ResourceManager resourceManager;
private final File baseDir, buildDir;

public FileLocator(ResourceManager resourceManager) {
this.resourceManager = resourceManager;
public FileLocator(ResourceManager resourceManager, File baseDir, File buildDir) {
this.resourceManager = Objects.requireNonNull(resourceManager);
this.baseDir = Objects.requireNonNull(baseDir);
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;
Expand All @@ -54,4 +59,26 @@ private static String tmpOutputFileName(String path) {
String extension = FileUtils.extension(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;
}

public File getBuildDir() {
return buildDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

public class FormatterConfig {

private final File baseDir;
private final String encoding;
private final LineEnding lineEndings;
private final Provisioner provisioner;
Expand All @@ -34,18 +33,13 @@ public class FormatterConfig {

public FormatterConfig(File baseDir, String encoding, LineEnding lineEndings, Provisioner provisioner,
FileLocator fileLocator, List<FormatterStepFactory> globalStepFactories) {
this.baseDir = baseDir;
this.encoding = encoding;
this.lineEndings = lineEndings;
this.provisioner = provisioner;
this.fileLocator = fileLocator;
this.globalStepFactories = globalStepFactories;
}

public File getBaseDir() {
return baseDir;
}

public String getEncoding() {
return encoding;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public final Set<String> excludes() {
public final Formatter newFormatter(List<File> 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<FormatterStepFactory> factories = gatherStepFactories(config.getGlobalStepFactories(), stepFactories);
Expand All @@ -79,7 +79,7 @@ public final Formatter newFormatter(List<File> filesToFormat, FormatterConfig co
.lineEndingsPolicy(formatterLineEndingPolicy)
.exceptionPolicy(new FormatExceptionPolicyStrict())
.steps(formatterSteps)
.rootDir(config.getBaseDir().toPath())
.rootDir(config.getFileLocator().getBaseDir().toPath())
.build();
}

Expand Down
Loading