Skip to content

Commit 218db79

Browse files
committed
eslint: extract base properties
to a common base class
1 parent e12c5ef commit 218db79

File tree

3 files changed

+65
-30
lines changed

3 files changed

+65
-30
lines changed

plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Prettier.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323

2424
import com.diffplug.spotless.FormatterStep;
2525
import com.diffplug.spotless.maven.FormatterStepConfig;
26-
import com.diffplug.spotless.maven.FormatterStepFactory;
26+
import com.diffplug.spotless.maven.npm.AbstractNpmFormatterStepFactory;
2727
import com.diffplug.spotless.npm.NpmPathResolver;
2828
import com.diffplug.spotless.npm.PrettierConfig;
2929
import com.diffplug.spotless.npm.PrettierFormatterStep;
3030

31-
public class Prettier implements FormatterStepFactory {
31+
public class Prettier extends AbstractNpmFormatterStepFactory {
3232

3333
public static final String ERROR_MESSAGE_ONLY_ONE_CONFIG = "must specify exactly one prettierVersion, devDependencies or devDependencyProperties";
3434

@@ -47,12 +47,6 @@ public class Prettier implements FormatterStepFactory {
4747
@Parameter
4848
private String configFile;
4949

50-
@Parameter
51-
private String npmExecutable;
52-
53-
@Parameter
54-
private String npmrc;
55-
5650
@Override
5751
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
5852

@@ -70,10 +64,6 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
7064
this.devDependencies = dependencyPropertiesAsMap();
7165
}
7266

73-
File npm = npmExecutable != null ? stepConfig.getFileLocator().locateFile(npmExecutable) : null;
74-
75-
File npmrcFile = npmrc != null ? stepConfig.getFileLocator().locateFile(npmrc) : null;
76-
7767
// process config file or inline config
7868
File configFileHandler;
7969
if (this.configFile != null) {
@@ -103,10 +93,10 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
10393
}
10494

10595
// create the format step
96+
File baseDir = baseDir(stepConfig);
97+
File buildDir = buildDir(stepConfig);
10698
PrettierConfig prettierConfig = new PrettierConfig(configFileHandler, configInline);
107-
File buildDir = stepConfig.getFileLocator().getBuildDir();
108-
File baseDir = stepConfig.getFileLocator().getBaseDir();
109-
NpmPathResolver npmPathResolver = new NpmPathResolver(npm, npmrcFile, baseDir);
99+
NpmPathResolver npmPathResolver = npmPathResolver(stepConfig);
110100
return PrettierFormatterStep.create(devDependencies, stepConfig.getProvisioner(), baseDir, buildDir, npmPathResolver, prettierConfig);
111101
}
112102

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2016-2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.npm;
17+
18+
import java.io.File;
19+
20+
import org.apache.maven.plugins.annotations.Parameter;
21+
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
import com.diffplug.spotless.npm.NpmPathResolver;
25+
26+
public abstract class AbstractNpmFormatterStepFactory implements FormatterStepFactory {
27+
28+
@Parameter
29+
private String npmExecutable;
30+
31+
@Parameter
32+
private String npmrc;
33+
34+
protected File npm(FormatterStepConfig stepConfig) {
35+
File npm = npmExecutable != null ? stepConfig.getFileLocator().locateFile(npmExecutable) : null;
36+
return npm;
37+
}
38+
39+
protected File npmrc(FormatterStepConfig stepConfig) {
40+
File npmrc = this.npmrc != null ? stepConfig.getFileLocator().locateFile(this.npmrc) : null;
41+
return npmrc;
42+
}
43+
44+
protected File buildDir(FormatterStepConfig stepConfig) {
45+
return stepConfig.getFileLocator().getBuildDir();
46+
}
47+
48+
protected File baseDir(FormatterStepConfig stepConfig) {
49+
return stepConfig.getFileLocator().getBaseDir();
50+
}
51+
52+
protected NpmPathResolver npmPathResolver(FormatterStepConfig stepConfig) {
53+
return new NpmPathResolver(npm(stepConfig), npmrc(stepConfig), baseDir(stepConfig));
54+
}
55+
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323

2424
import com.diffplug.spotless.FormatterStep;
2525
import com.diffplug.spotless.maven.FormatterStepConfig;
26-
import com.diffplug.spotless.maven.FormatterStepFactory;
26+
import com.diffplug.spotless.maven.npm.AbstractNpmFormatterStepFactory;
2727
import com.diffplug.spotless.npm.NpmPathResolver;
2828
import com.diffplug.spotless.npm.TsConfigFileType;
2929
import com.diffplug.spotless.npm.TsFmtFormatterStep;
3030
import com.diffplug.spotless.npm.TypedTsFmtConfigFile;
3131

32-
public class Tsfmt implements FormatterStepFactory {
32+
public class Tsfmt extends AbstractNpmFormatterStepFactory {
3333

3434
@Parameter
3535
private String tslintFile;
@@ -52,12 +52,6 @@ public class Tsfmt implements FormatterStepFactory {
5252
@Parameter
5353
private String tslintVersion;
5454

55-
@Parameter
56-
private String npmExecutable;
57-
58-
@Parameter
59-
private String npmrc;
60-
6155
@Parameter
6256
private Map<String, String> config;
6357

@@ -74,10 +68,6 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
7468
devDependencies.put("tslint", tslintVersion);
7569
}
7670

77-
File npm = npmExecutable != null ? stepConfig.getFileLocator().locateFile(npmExecutable) : null;
78-
79-
File npmrcFile = npmrc != null ? stepConfig.getFileLocator().locateFile(npmrc) : null;
80-
8171
TypedTsFmtConfigFile configFile;
8272
Map<String, Object> configInline;
8373
// check that there is only 1 config file or inline config
@@ -119,9 +109,9 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
119109
throw onlyOneConfig();
120110
}
121111

122-
File buildDir = stepConfig.getFileLocator().getBuildDir();
123-
File baseDir = stepConfig.getFileLocator().getBaseDir();
124-
NpmPathResolver npmPathResolver = new NpmPathResolver(npm, npmrcFile, baseDir);
112+
File buildDir = buildDir(stepConfig);
113+
File baseDir = baseDir(stepConfig);
114+
NpmPathResolver npmPathResolver = npmPathResolver(stepConfig);
125115
return TsFmtFormatterStep.create(devDependencies, stepConfig.getProvisioner(), baseDir, buildDir, npmPathResolver, configFile, configInline);
126116
}
127117

0 commit comments

Comments
 (0)