-
Notifications
You must be signed in to change notification settings - Fork 472
Add Prettier to Maven plugin #555
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
abe3caf
Add prettier to maven plugin
source-knights 78ad914
Add prettier to maven plugin Docs change
source-knights 9ba42f8
Add prettier to maven plugin spotless apply
source-knights f4b3e16
Add prettier to maven plugin spotless apply doc update
source-knights c8b14be
try to fix build in CI
source-knights 2148549
Add prettier to maven plugin spotless apply doc update
source-knights 9b1ac00
delete accidentally committed file
source-knights 73db418
doc update
source-knights 525f398
fix
source-knights 214a25c
fix
source-knights 5a2926d
addressed review comments
source-knights 7d59454
re-add deleted line in doc
source-knights 550532d
update copyright date in new files
source-knights ef591ef
spotless apply
source-knights File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
103 changes: 103 additions & 0 deletions
103
plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Prettier.java
This file contains hidden or 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,103 @@ | ||
/* | ||
* 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; | ||
Map<String, Object> configInline; | ||
if (this.configFile != null) { | ||
configInline = null; | ||
configFileHandler = stepConfig.getFileLocator().locateLocal(this.configFile); | ||
} else { | ||
configFileHandler = null; | ||
} | ||
source-knights marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 hidden or 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 hidden or 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>", | ||
" <devDependencies><prettier>1.16.4</prettier><node>10</node></devDependencies>", | ||
" <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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.