-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
824 additions
and
5 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
60 changes: 60 additions & 0 deletions
60
lib/src/gherkin/java/com/diffplug/spotless/glue/gherkin/GherkinUtilsFormatterFunc.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,60 @@ | ||
/* | ||
* Copyright 2023 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.glue.gherkin; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.gherkin.GherkinUtilsConfig; | ||
|
||
import io.cucumber.gherkin.GherkinParser; | ||
import io.cucumber.gherkin.utils.pretty.Pretty; | ||
import io.cucumber.gherkin.utils.pretty.Syntax; | ||
import io.cucumber.messages.types.Envelope; | ||
import io.cucumber.messages.types.GherkinDocument; | ||
import io.cucumber.messages.types.Source; | ||
import io.cucumber.messages.types.SourceMediaType; | ||
|
||
public class GherkinUtilsFormatterFunc implements FormatterFunc { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(GherkinUtilsFormatterFunc.class); | ||
|
||
private final GherkinUtilsConfig gherkinSimpleConfig; | ||
|
||
public GherkinUtilsFormatterFunc(GherkinUtilsConfig gherkinSimpleConfig) { | ||
this.gherkinSimpleConfig = gherkinSimpleConfig; | ||
} | ||
|
||
// Follows https://github.com/cucumber/gherkin-utils/blob/main/java/src/test/java/io/cucumber/gherkin/utils/pretty/PrettyTest.java | ||
private GherkinDocument parse(String gherkin) { | ||
GherkinParser parser = GherkinParser | ||
.builder() | ||
.includeSource(false) | ||
.build(); | ||
return parser.parse(Envelope.of(new Source("test.feature", gherkin, SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN))) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("No envelope")) | ||
.getGherkinDocument() | ||
.orElseThrow(() -> new IllegalArgumentException("No gherkin document")); | ||
} | ||
|
||
@Override | ||
public String apply(String inputString) { | ||
GherkinDocument gherkinDocument = parse(inputString); | ||
|
||
return Pretty.prettyPrint(gherkinDocument, Syntax.gherkin); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
lib/src/main/java/com/diffplug/spotless/gherkin/GherkinUtilsConfig.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,38 @@ | ||
/* | ||
* Copyright 2023 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.gherkin; | ||
|
||
import java.io.Serializable; | ||
|
||
public class GherkinUtilsConfig implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public static int defaultIndentSpaces() { | ||
// https://cucumber.io/docs/gherkin/reference/ | ||
// Recommended indentation is 2 spaces | ||
return 2; | ||
} | ||
|
||
final int indentSpaces; | ||
|
||
public GherkinUtilsConfig(int indentSpaces) { | ||
this.indentSpaces = indentSpaces; | ||
} | ||
|
||
public int getIndentSpaces() { | ||
return indentSpaces; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
lib/src/main/java/com/diffplug/spotless/gherkin/GherkinUtilsStep.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,65 @@ | ||
/* | ||
* Copyright 2021-2023 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.gherkin; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Objects; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.JarState; | ||
import com.diffplug.spotless.Provisioner; | ||
|
||
public class GherkinUtilsStep { | ||
private static final String MAVEN_COORDINATE = "io.cucumber:gherkin-utils:"; | ||
private static final String DEFAULT_VERSION = "8.0.2"; | ||
|
||
public static String defaultVersion() { | ||
return DEFAULT_VERSION; | ||
} | ||
|
||
public static FormatterStep create(GherkinUtilsConfig gherkinSimpleConfig, | ||
String formatterVersion, Provisioner provisioner) { | ||
Objects.requireNonNull(provisioner, "provisioner cannot be null"); | ||
return FormatterStep.createLazy("gherkin", () -> new GherkinUtilsStep.State(gherkinSimpleConfig, formatterVersion, provisioner), GherkinUtilsStep.State::toFormatter); | ||
} | ||
|
||
private static final class State implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final GherkinUtilsConfig gherkinSimpleConfig; | ||
private final JarState jarState; | ||
|
||
private State(GherkinUtilsConfig gherkinSimpleConfig, String formatterVersion, Provisioner provisioner) throws IOException { | ||
this.gherkinSimpleConfig = gherkinSimpleConfig; | ||
this.jarState = JarState.from(MAVEN_COORDINATE + formatterVersion, provisioner); | ||
} | ||
|
||
FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, | ||
InstantiationException, IllegalAccessException { | ||
Class<?> formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.gherkin.GherkinUtilsFormatterFunc"); | ||
Constructor<?> constructor = formatterFunc.getConstructor(GherkinUtilsConfig.class); | ||
return (FormatterFunc) constructor.newInstance(gherkinSimpleConfig); | ||
} | ||
} | ||
|
||
private GherkinUtilsStep() { | ||
// cannot be directly instantiated | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GherkinExtension.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,63 @@ | ||
/* | ||
* Copyright 2016-2023 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.gradle.spotless; | ||
|
||
import javax.inject.Inject; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.gherkin.GherkinUtilsStep; | ||
|
||
public class GherkinExtension extends FormatExtension { | ||
static final String NAME = "gherkin"; | ||
|
||
@Inject | ||
public GherkinExtension(SpotlessExtension spotless) { | ||
super(spotless); | ||
} | ||
|
||
@Override | ||
protected void setupTask(SpotlessTask task) { | ||
if (target == null) { | ||
throw noDefaultTargetException(); | ||
} | ||
super.setupTask(task); | ||
} | ||
|
||
public GherkinUtilsConfig gherkinUtils() { | ||
return new GherkinUtilsConfig(); | ||
} | ||
|
||
public class GherkinUtilsConfig { | ||
private String version; | ||
private int indent; | ||
|
||
public GherkinUtilsConfig() { | ||
this.version = GherkinUtilsStep.defaultVersion(); | ||
this.indent = com.diffplug.spotless.gherkin.GherkinUtilsConfig.defaultIndentSpaces(); | ||
addStep(createStep()); | ||
} | ||
|
||
public void version(String version) { | ||
this.version = version; | ||
replaceStep(createStep()); | ||
} | ||
|
||
private FormatterStep createStep() { | ||
return GherkinUtilsStep.create(new com.diffplug.spotless.gherkin.GherkinUtilsConfig(indent), version, provisioner()); | ||
} | ||
} | ||
|
||
} |
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
44 changes: 44 additions & 0 deletions
44
plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GherkinExtensionTest.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,44 @@ | ||
/* | ||
* Copyright 2021-2023 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.gradle.spotless; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class GherkinExtensionTest extends GradleIntegrationHarness { | ||
@Test | ||
public void defaultFormatting() throws IOException { | ||
setFile("build.gradle").toLines( | ||
"plugins {", | ||
" id 'java'", | ||
" id 'com.diffplug.spotless'", | ||
"}", | ||
"repositories { mavenCentral() }", | ||
"spotless {", | ||
" gherkin {", | ||
" target 'examples/**/*.feature'", | ||
" gherkinUtils()", | ||
" }", | ||
"}"); | ||
setFile("src/main/resources/example.feature").toResource("gherkin/minimalBefore.feature"); | ||
setFile("examples/main/resources/example.feature").toResource("gherkin/minimalBefore.feature"); | ||
gradleRunner().withArguments("spotlessApply").build(); | ||
assertFile("src/main/resources/example.feature").sameAsResource("gherkin/minimalBefore.feature"); | ||
assertFile("examples/main/resources/example.feature").sameAsResource("gherkin/minimalAfter.feature"); | ||
} | ||
|
||
} |
Oops, something went wrong.