Skip to content

Commit

Permalink
Merge pull request #363 from simschla/feature/customizable-tsfmt-and-…
Browse files Browse the repository at this point in the history
…prettier-version

Feature/customizable tsfmt and prettier version
  • Loading branch information
nedtwigg authored Mar 12, 2019
2 parents 9448b17 + 00b4096 commit 9843130
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 30 deletions.
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ You might be looking for:

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

* Made npm package versions of [`prettier`](https://prettier.io/) and [`tsfmt`](https://github.com/vvakame/typescript-formatter) (and its internal packages) configurable. ([#363](https://github.com/diffplug/spotless/pull/363))
* Updated default npm package version of `prettier` from 1.13.4 to 1.16.4
* Updated default npm package version of internally used typescript package from 2.9.2 to 3.3.3 and tslint package from 5.1.0 to 5.12.0 (both used by `tsfmt`)
* Updated default eclipse-wtp from 4.7.3a to 4.7.3b ([#371](https://github.com/diffplug/spotless/pull/371)).
* Default behavior of XML formatter changed to ignore external URIs ([#369](https://github.com/diffplug/spotless/issues/369)).
* Default behavior of XML formatter changed to ignore external URIs ([#369](https://github.com/diffplug/spotless/issues/369)).

### Version 1.19.0 - March 11th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.19.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.19.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -117,5 +121,30 @@ protected static String readFileFromClasspath(Class<?> clazz, String name) {
}
}

protected static String replaceDevDependencies(String template, Map<String, String> devDependencies) {
StringBuilder builder = new StringBuilder();
Iterator<Map.Entry<String, String>> entryIter = devDependencies.entrySet().iterator();
while (entryIter.hasNext()) {
Map.Entry<String, String> entry = entryIter.next();
builder.append("\t\t\"");
builder.append(entry.getKey());
builder.append("\": \"");
builder.append(entry.getValue());
builder.append("\"");
if (entryIter.hasNext()) {
builder.append(",\n");
}
}
return replacePlaceholders(template, Collections.singletonMap("devDependencies", builder.toString()));
}

private static String replacePlaceholders(String template, Map<String, String> replacements) {
String result = template;
for (Entry<String, String> entry : replacements.entrySet()) {
result = result.replaceAll("\\Q${" + entry.getKey() + "}\\E", entry.getValue());
}
return result;
}

public abstract FormatterFunc createFormatterFunc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

Expand All @@ -36,11 +37,25 @@ public class PrettierFormatterStep {

public static final String NAME = "prettier-format";

public static final Map<String, String> defaultDevDependencies() {
return defaultDevDependenciesWithPrettier("1.16.4");
}

public static final Map<String, String> defaultDevDependenciesWithPrettier(String version) {
return Collections.singletonMap("prettier", version);
}

@Deprecated
public static FormatterStep create(Provisioner provisioner, File buildDir, @Nullable File npm, PrettierConfig prettierConfig) {
return create(defaultDevDependencies(), provisioner, buildDir, npm, prettierConfig);
}

public static FormatterStep create(Map<String, String> devDependencies, Provisioner provisioner, File buildDir, @Nullable File npm, PrettierConfig prettierConfig) {
requireNonNull(devDependencies);
requireNonNull(provisioner);
requireNonNull(buildDir);
return FormatterStep.createLazy(NAME,
() -> new State(NAME, provisioner, buildDir, npm, prettierConfig),
() -> new State(NAME, devDependencies, provisioner, buildDir, npm, prettierConfig),
State::createFormatterFunc);
}

Expand All @@ -49,11 +64,13 @@ public static class State extends NpmFormatterStepStateBase implements Serializa
private static final long serialVersionUID = -3811104513825329168L;
private final PrettierConfig prettierConfig;

State(String stepName, Provisioner provisioner, File buildDir, @Nullable File npm, PrettierConfig prettierConfig) throws IOException {
State(String stepName, Map<String, String> devDependencies, Provisioner provisioner, File buildDir, @Nullable File npm, PrettierConfig prettierConfig) throws IOException {
super(stepName,
provisioner,
new NpmConfig(
readFileFromClasspath(PrettierFormatterStep.class, "/com/diffplug/spotless/npm/prettier-package.json"),
replaceDevDependencies(
readFileFromClasspath(PrettierFormatterStep.class, "/com/diffplug/spotless/npm/prettier-package.json"),
new TreeMap<>(devDependencies)),
"prettier"),
buildDir,
npm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,33 @@
import com.diffplug.spotless.ThrowingEx;

public class TsFmtFormatterStep {

public static final String NAME = "tsfmt-format";

@Deprecated
public static FormatterStep create(Provisioner provisioner, File buildDir, @Nullable File npm, File baseDir, @Nullable TypedTsFmtConfigFile configFile, @Nullable Map<String, Object> inlineTsFmtSettings) {
return create(defaultDevDependencies(), provisioner, buildDir, npm, configFile, inlineTsFmtSettings);
}

public static FormatterStep create(Map<String, String> versions, Provisioner provisioner, File buildDir, @Nullable File npm, @Nullable TypedTsFmtConfigFile configFile, @Nullable Map<String, Object> inlineTsFmtSettings) {
requireNonNull(provisioner);
requireNonNull(buildDir);
requireNonNull(baseDir);
return FormatterStep.createLazy(NAME,
() -> new State(NAME, provisioner, buildDir, npm, baseDir, configFile, inlineTsFmtSettings),
() -> new State(NAME, versions, provisioner, buildDir, npm, configFile, inlineTsFmtSettings),
State::createFormatterFunc);
}

public static Map<String, String> defaultDevDependencies() {
return defaultDevDependenciesWithTsFmt("7.2.2");
}

public static Map<String, String> defaultDevDependenciesWithTsFmt(String typescriptFormatter) {
TreeMap<String, String> defaults = new TreeMap<>();
defaults.put("typescript-formatter", typescriptFormatter);
defaults.put("typescript", "3.3.3");
defaults.put("tslint", "5.12.1");
return defaults;
}

public static class State extends NpmFormatterStepStateBase implements Serializable {

private static final long serialVersionUID = -3811104513825329168L;
Expand All @@ -55,18 +70,20 @@ public static class State extends NpmFormatterStepStateBase implements Serializa
@Nullable
private final TypedTsFmtConfigFile configFile;

private final File baseDir;
@Deprecated
public State(String stepName, Provisioner provisioner, File buildDir, @Nullable File npm, @Nullable TypedTsFmtConfigFile configFile, @Nullable Map<String, Object> inlineTsFmtSettings) throws IOException {
this(stepName, defaultDevDependencies(), provisioner, buildDir, npm, configFile, inlineTsFmtSettings);
}

public State(String stepName, Provisioner provisioner, File buildDir, @Nullable File npm, File baseDir, @Nullable TypedTsFmtConfigFile configFile, @Nullable Map<String, Object> inlineTsFmtSettings) throws IOException {
public State(String stepName, Map<String, String> versions, Provisioner provisioner, File buildDir, @Nullable File npm, @Nullable TypedTsFmtConfigFile configFile, @Nullable Map<String, Object> inlineTsFmtSettings) throws IOException {
super(stepName,
provisioner,
new NpmConfig(
readFileFromClasspath(TsFmtFormatterStep.class, "/com/diffplug/spotless/npm/tsfmt-package.json"),
replaceDevDependencies(readFileFromClasspath(TsFmtFormatterStep.class, "/com/diffplug/spotless/npm/tsfmt-package.json"), new TreeMap<>(versions)),
"typescript-formatter"),
buildDir,
npm);
this.buildDir = requireNonNull(buildDir);
this.baseDir = requireNonNull(baseDir);
this.configFile = configFile;
this.inlineTsFmtSettings = inlineTsFmtSettings == null ? new TreeMap<>() : new TreeMap<>(inlineTsFmtSettings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "spotless-prettier-formatter-step",
"version": "1.0.0",
"devDependencies": {
"prettier": "1.13.4"
${devDependencies}
},
"dependencies": {},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"name": "spotless-tsfmt-formatter-step",
"version": "1.0.0",
"devDependencies": {
"typescript-formatter": "7.2.2",
"typescript": "2.9.2",
"tslint": "5.1.0"
${devDependencies}
},
"dependencies": {},
"engines": {
Expand Down
5 changes: 4 additions & 1 deletion plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

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

* Made npm package versions of [`prettier`](https://prettier.io/) and [`tsfmt`](https://github.com/vvakame/typescript-formatter) (and its internal packages) configurable. ([#363](https://github.com/diffplug/spotless/pull/363))
* Updated default npm package version of `prettier` from 1.13.4 to 1.16.4
* Updated default npm package version of internally used typescript package from 2.9.2 to 3.3.3 and tslint package from 5.1.0 to 5.12.0 (both used by `tsfmt`)
* Updated default eclipse-wtp from 4.7.3a to 4.7.3b ([#371](https://github.com/diffplug/spotless/pull/371)).
* Default behavior of XML formatter changed to ignore external URIs ([#369](https://github.com/diffplug/spotless/issues/369)).
* Default behavior of XML formatter changed to ignore external URIs ([#369](https://github.com/diffplug/spotless/issues/369)).

### Version 3.19.0 - March 11th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.19.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.19.0))

Expand Down
4 changes: 4 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ spotless {
typescript {
// using existing config files
tsfmt().tslintFile('/path/to/repo/tslint.json')
// tsfmt('7.2.2') to specify specific version of tsfmt
// tsfmt(['typescript-formatter': '7.2.2', 'typescript': '3.3.3', 'tslint': '5.12.1') to specify all of the npm dependencies that you want
}
}
```
Expand Down Expand Up @@ -404,6 +406,8 @@ spotless {
// at least provide the parser to use
prettier().config(['parser': 'postcss'])
// prettier('1.16.4') to specify specific version of prettier
// prettier(['my-prettier-fork': '1.16.4']) to specify exactly which npm packages to use
// or provide a typical filename
prettier().config(['filepath': 'style.scss'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,16 @@ File npmFileOrNull() {
public class PrettierConfig extends NpmStepConfig<PrettierConfig> {

@Nullable
protected Object prettierConfigFile;
Object prettierConfigFile;

@Nullable
protected Map<String, Object> prettierConfig;
Map<String, Object> prettierConfig;

final Map<String, String> devDependencies;

PrettierConfig(Map<String, String> devDependencies) {
this.devDependencies = Objects.requireNonNull(devDependencies);
}

public PrettierConfig configFile(final Object prettierConfigFile) {
this.prettierConfigFile = prettierConfigFile;
Expand All @@ -508,6 +514,7 @@ public PrettierConfig config(final Map<String, Object> prettierConfig) {
FormatterStep createStep() {
final Project project = getProject();
return PrettierFormatterStep.create(
devDependencies,
GradleProvisioner.fromProject(project),
project.getBuildDir(),
npmFileOrNull(),
Expand All @@ -517,8 +524,19 @@ FormatterStep createStep() {
}
}

/** Uses the default version of prettier. */
public PrettierConfig prettier() {
final PrettierConfig prettierConfig = new PrettierConfig();
return prettier(PrettierFormatterStep.defaultDevDependencies());
}

/** Uses the specified version of prettier. */
public PrettierConfig prettier(String version) {
return prettier(PrettierFormatterStep.defaultDevDependenciesWithPrettier(version));
}

/** Uses exactly the npm packages specified in the map. */
public PrettierConfig prettier(Map<String, String> devDependencies) {
PrettierConfig prettierConfig = new PrettierConfig(devDependencies);
addStep(prettierConfig.createStep());
return prettierConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

import javax.annotation.Nullable;

import org.gradle.api.Project;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.npm.PrettierFormatterStep;
import com.diffplug.spotless.npm.TsConfigFileType;
import com.diffplug.spotless.npm.TsFmtFormatterStep;
import com.diffplug.spotless.npm.TypedTsFmtConfigFile;
Expand All @@ -38,8 +40,19 @@ public TypescriptExtension(SpotlessExtension root) {
super(root);
}

/** Uses the default version of typescript-format. */
public TypescriptFormatExtension tsfmt() {
TypescriptFormatExtension tsfmt = new TypescriptFormatExtension();
return tsfmt(TsFmtFormatterStep.defaultDevDependencies());
}

/** Uses the specified version of typescript-format. */
public TypescriptFormatExtension tsfmt(String version) {
return tsfmt(TsFmtFormatterStep.defaultDevDependenciesWithTsFmt(version));
}

/** Creates a {@code TypescriptFormatExtension} using exactly the specified npm packages. */
public TypescriptFormatExtension tsfmt(Map<String, String> devDependencies) {
TypescriptFormatExtension tsfmt = new TypescriptFormatExtension(devDependencies);
addStep(tsfmt.createStep());
return tsfmt;
}
Expand All @@ -54,6 +67,12 @@ public class TypescriptFormatExtension extends NpmStepConfig<TypescriptFormatExt
@Nullable
Object configFilePath = null;

private final Map<String, String> devDependencies;

TypescriptFormatExtension(Map<String, String> devDependencies) {
this.devDependencies = Objects.requireNonNull(devDependencies);
}

public void config(final Map<String, Object> config) {
this.config = new TreeMap<>(requireNonNull(config));
replaceStep(createStep());
Expand Down Expand Up @@ -85,10 +104,10 @@ public FormatterStep createStep() {
final Project project = getProject();

return TsFmtFormatterStep.create(
devDependencies,
GradleProvisioner.fromProject(project),
project.getBuildDir(),
npmFileOrNull(),
project.getProjectDir(),
typedConfigFile(),
config);
}
Expand All @@ -101,9 +120,22 @@ private TypedTsFmtConfigFile typedConfigFile() {
}
}

/** Uses the default version of prettier. */
@Override
public PrettierConfig prettier() {
PrettierConfig prettierConfig = new TypescriptPrettierConfig();
return prettier(PrettierFormatterStep.defaultDevDependencies());
}

/** Uses the specified version of prettier. */
@Override
public PrettierConfig prettier(String version) {
return prettier(PrettierFormatterStep.defaultDevDependenciesWithPrettier(version));
}

/** Uses exactly the npm packages specified in the map. */
@Override
public PrettierConfig prettier(Map<String, String> devDependencies) {
PrettierConfig prettierConfig = new TypescriptPrettierConfig(devDependencies);
addStep(prettierConfig.createStep());
return prettierConfig;
}
Expand All @@ -112,6 +144,10 @@ public PrettierConfig prettier() {
* Overrides the parser to be set to typescript, no matter what the user's config says.
*/
public class TypescriptPrettierConfig extends PrettierConfig {
TypescriptPrettierConfig(Map<String, String> devDependencies) {
super(devDependencies);
}

@Override
FormatterStep createStep() {
fixParserToTypescript();
Expand Down
Loading

0 comments on commit 9843130

Please sign in to comment.