-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from source-knights/master
Add Prettier to Maven plugin
- Loading branch information
Showing
13 changed files
with
368 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Prettier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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.generic; | ||
|
||
import java.io.File; | ||
import java.util.LinkedHashMap; | ||
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.PrettierConfig; | ||
import com.diffplug.spotless.npm.PrettierFormatterStep; | ||
|
||
public class Prettier implements FormatterStepFactory { | ||
|
||
@Parameter | ||
private String prettierVersion; | ||
|
||
@Parameter | ||
private Map<String, String> devDependencies; | ||
|
||
@Parameter | ||
private Map<String, String> config; | ||
|
||
@Parameter | ||
private String configFile; | ||
|
||
@Parameter | ||
private String npmExecutable; | ||
|
||
@Override | ||
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { | ||
|
||
// check if config is only setup in one way | ||
if (this.prettierVersion != null && this.devDependencies != null) { | ||
throw onlyOneConfig(); | ||
} | ||
|
||
// set dev dependencies | ||
if (devDependencies == null) { | ||
if (prettierVersion == null || prettierVersion.isEmpty()) { | ||
devDependencies = PrettierFormatterStep.defaultDevDependencies(); | ||
} else { | ||
devDependencies = PrettierFormatterStep.defaultDevDependenciesWithPrettier(prettierVersion); | ||
} | ||
} | ||
|
||
File npm = npmExecutable != null ? stepConfig.getFileLocator().locateLocal(npmExecutable) : null; | ||
|
||
// process config file or inline config | ||
File configFileHandler; | ||
if (this.configFile != null) { | ||
configFileHandler = stepConfig.getFileLocator().locateLocal(this.configFile); | ||
} else { | ||
configFileHandler = null; | ||
} | ||
|
||
Map<String, Object> configInline; | ||
if (config != null) { | ||
configInline = new LinkedHashMap<>(); | ||
// try to parse string values as integers or booleans | ||
for (Map.Entry<String, String> 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 { | ||
configInline = null; | ||
} | ||
|
||
// create the format step | ||
PrettierConfig prettierConfig = new PrettierConfig(configFileHandler, configInline); | ||
File buildDir = stepConfig.getFileLocator().getBuildDir(); | ||
return PrettierFormatterStep.create(devDependencies, stepConfig.getProvisioner(), buildDir, npm, prettierConfig); | ||
} | ||
|
||
private static IllegalArgumentException onlyOneConfig() { | ||
return new IllegalArgumentException("must specify exactly one configFile or config"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
plugin-maven/src/test/java/com/diffplug/spotless/maven/prettier/PrettierFormatStepTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* 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.prettier; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.Test; | ||
|
||
import com.diffplug.spotless.maven.MavenIntegrationTest; | ||
import com.diffplug.spotless.maven.MavenRunner; | ||
|
||
public class PrettierFormatStepTest extends MavenIntegrationTest { | ||
|
||
private void run(String kind, String suffix) throws IOException, InterruptedException { | ||
String configPath = ".prettierrc.yml"; | ||
setFile(configPath).toResource("npm/prettier/filetypes/" + kind + "/" + ".prettierrc.yml"); | ||
String path = "src/main/" + kind + "/test." + suffix; | ||
setFile(path).toResource("npm/prettier/filetypes/" + kind + "/" + kind + ".dirty"); | ||
mavenRunner().withArguments("spotless:apply").runNoError(); | ||
assertFile(path).sameAsResource("npm/prettier/filetypes/" + kind + "/" + kind + ".clean"); | ||
} | ||
|
||
@Test | ||
public void prettier_typescript() throws Exception { | ||
String suffix = "ts"; | ||
writePomWithPrettierSteps("**/*." + suffix, | ||
"<prettier>", | ||
" <prettierVersion>1.16.4</prettierVersion>", | ||
" <configFile>.prettierrc.yml</configFile>", | ||
"</prettier>"); | ||
run("typescript", suffix); | ||
} | ||
|
||
@Test | ||
public void prettier_html() throws Exception { | ||
String suffix = "html"; | ||
writePomWithPrettierSteps("**/*." + suffix, | ||
"<prettier>", | ||
" <prettierVersion>1.16.4</prettierVersion>", | ||
" <configFile>.prettierrc.yml</configFile>", | ||
"</prettier>"); | ||
run("html", suffix); | ||
} | ||
|
||
@Test | ||
public void prettier_tsx() throws Exception { | ||
String suffix = "tsx"; | ||
writePomWithPrettierSteps("src/main/**/*." + suffix, | ||
"<includes><include>src/**/*.tsx</include></includes>", | ||
"<prettier>", | ||
" <prettierVersion>1.16.4</prettierVersion>", | ||
" <configFile>.prettierrc.yml</configFile>", | ||
"</prettier>"); | ||
run("tsx", suffix); | ||
} | ||
|
||
@Test | ||
public void prettier_tsx_inline_config() throws Exception { | ||
String suffix = "tsx"; | ||
writePomWithPrettierSteps("src/main/**/*." + suffix, | ||
"<prettier>", | ||
" <prettierVersion>1.16.4</prettierVersion>", | ||
" <config><parser>typescript</parser></config>", | ||
"</prettier>"); | ||
run("tsx", suffix); | ||
} | ||
|
||
@Test | ||
public void unique_dependency_config() throws Exception { | ||
writePomWithFormatSteps( | ||
"<includes><include>**/*.ts</include></includes>", | ||
"<prettier>", | ||
" <prettierVersion>1.16.4</prettierVersion>", | ||
" <devDependencies><prettier>1.16.4</prettier></devDependencies>", | ||
"</prettier>"); | ||
|
||
MavenRunner.Result result = mavenRunner().withArguments("spotless:apply").runHasError(); | ||
assertThat(result.output()).contains("must specify exactly one configFile or config"); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
testlib/src/main/resources/npm/prettier/filetypes/html/.prettierrc.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
parser: html |
13 changes: 13 additions & 0 deletions
13
testlib/src/main/resources/npm/prettier/filetypes/html/html.clean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html dir="ltr" lang="en"> | ||
<head> | ||
<meta charset="viewport" content="with=device-width" /> | ||
<title>Test</title> | ||
<script type="module" src="module.js"></script> | ||
<script type="module2" src="module2.js"></script> | ||
|
||
<link rel="stylesheet" type="text/css" href="style.css" /> | ||
</head> | ||
|
||
<body></body> | ||
</html> |
17 changes: 17 additions & 0 deletions
17
testlib/src/main/resources/npm/prettier/filetypes/html/html.dirty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html dir="ltr" lang="en"> | ||
|
||
<head> | ||
<meta | ||
charset="viewport" | ||
content="with=device-width"> | ||
<title>Test</title><script type="module" src="module.js"></script> | ||
<script type="module2" src="module2.js"></script> | ||
|
||
<link rel="stylesheet" type ="text/css" href="style.css"> | ||
</head> | ||
|
||
<body> | ||
|
||
</body></html> | ||
|
3 changes: 3 additions & 0 deletions
3
testlib/src/main/resources/npm/prettier/filetypes/tsx/.prettierrc.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
parser: typescript | ||
files: "scr/**/*.tsx" | ||
excludeFiles: "target/**" |
Oops, something went wrong.