Skip to content

Commit

Permalink
feat: 🎸 gherkin asciidoc options (#113)
Browse files Browse the repository at this point in the history
* feat: 🎸 gherkin asciidoc options

* feat: 🎸 gherkin asciidoc options

---------

Co-authored-by: Julien Boz <[email protected]>
  • Loading branch information
jboz and Julien Boz authored Feb 8, 2024
1 parent 10eff68 commit 3e4678e
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 113 deletions.
2 changes: 1 addition & 1 deletion livingdoc-examples/aggregate/aggregate-cargo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>aggregate</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>

<artifactId>aggregate-cargo</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion livingdoc-examples/aggregate/aggregate-handling/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>aggregate</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>

<artifactId>aggregate-handling</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion livingdoc-examples/aggregate/aggregate-location/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>aggregate</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>

<artifactId>aggregate-location</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion livingdoc-examples/aggregate/aggregate-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>aggregate</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>

<artifactId>aggregate-shared</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>aggregate</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>

<artifactId>aggregate-sharedhandling</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion livingdoc-examples/aggregate/aggregate-voyage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>aggregate</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>

<artifactId>aggregate-voyage</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>aggregate</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>

<artifactId>aggregate-zz-livingdoc</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion livingdoc-examples/aggregate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>livingdoc-examples</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>

<artifactId>aggregate</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion livingdoc-examples/ddd-annotated/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>livingdoc-examples</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>

<artifactId>ddd-annotated</artifactId>
Expand Down
3 changes: 1 addition & 2 deletions livingdoc-examples/ddd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>livingdoc-examples</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>

<artifactId>ddd</artifactId>
Expand Down Expand Up @@ -72,7 +72,6 @@
<glossaryTitle>= Glossary</glossaryTitle>
<gherkinTitle>= Use Case - Invoice management - </gherkinTitle> <!-- will be used as a prefix for the confluence page name -->
<gerkinSeparateFeature>true</gerkinSeparateFeature> <!-- set false to merge all feature into one page, default is true -->
<gherkinOptions>withoutTitle=true,withChildSeparator=true</gherkinOptions>
<format>adoc</format>
<publish>
<provider>confluence</provider> <!-- actually only confluence is supported -->
Expand Down
2 changes: 1 addition & 1 deletion livingdoc-examples/microservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>livingdoc-examples</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
</parent>
<artifactId>microservice</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion livingdoc-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>ch.ifocusit.livingdoc.example</groupId>
<artifactId>livingdoc-examples</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Living Documentation example</name>
Expand Down
7 changes: 6 additions & 1 deletion livingdoc-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>ch.ifocusit.livingdoc</groupId>
<artifactId>livingdoc-maven-plugin</artifactId>
<version>2.9-SNAPSHOT</version>
<version>2.10-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Living Documentation maven plugin</name>
Expand Down Expand Up @@ -112,6 +112,11 @@
<artifactId>compiler</artifactId>
<version>0.9.11</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>gherkin</artifactId>
<version>27.0.0</version>
</dependency>

<!-- asciidoctor -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
* Copyright (C) 2024 Focus IT
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* regarding copyright ownership. The ASF licenses this file
* to you 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
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* 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
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
Expand All @@ -25,7 +25,6 @@
import static java.nio.charset.Charset.defaultCharset;
import static org.apache.commons.io.FileUtils.readFileToString;
import static org.apache.commons.lang3.StringUtils.EMPTY;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
Expand All @@ -36,15 +35,13 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import ch.ifocusit.asciidoctor.gherkin.GherkinAsciidocBuilder;
import ch.ifocusit.asciidoctor.gherkin.GherkinExtensionHelper;
import ch.ifocusit.livingdoc.plugin.baseMojo.AbstractDocsGeneratorMojo;
Expand All @@ -54,27 +51,40 @@
/**
* @author Julien Boz
*/
@Mojo(name = "gherkin", requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM, defaultPhase = LifecyclePhase.PROCESS_TEST_RESOURCES)
@Mojo(name = "gherkin", requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM,
defaultPhase = LifecyclePhase.PROCESS_TEST_RESOURCES)
public class GherkinMojo extends AbstractDocsGeneratorMojo {

/**
* List of source directories to browse
* List of source directories to browse.
*/
@Parameter(property = "livingdoc.gherkin.features", defaultValue = "${project.basedir}/src/test/resources/features")
private List<String> features;

/**
* Options like use a custom template (template=path_to_my_template.erb).
* Add or not the page title. Default true.
*/
@Parameter(property = "livingdoc.gherkin.withTitle")
private Boolean gherkinWithTitle;

/**
* Separate gherkin element with a line. Default false.
*/
@Parameter(property = "livingdoc.gherkin.withChildSeparator", defaultValue = "false")
private boolean gherkinWithChildSeparator;

/**
* Append child element keyword before title (Scenario, Rule, Background, ...). Default false.
*/
@Parameter(property = "livingdoc.gherkin.options")
private String gherkinOptions;
@Parameter(property = "livingdoc.gherkin.withKeyword", defaultValue = "false")
private boolean gherkinWithKeyword;

@Parameter(property = "livingdoc.gherkin.output.filename", defaultValue = "gherkin", required = true)
private String gherkinOutputFilename;

/**
* Page title or page title prefix when use in combinaison with
* {@link #gerkinSeparateFeature} option to true
* Page title or page title prefix when use in combinaison with {@link #gerkinSeparateFeature}
* option to true
*/
@Parameter(property = "livingdoc.gherkin.title")
private String gherkinTitle;
Expand All @@ -86,15 +96,18 @@ public class GherkinMojo extends AbstractDocsGeneratorMojo {
private boolean gerkinSeparateFeature;

/**
* Flag to indicate if generated asciidoc file must use the asciidoc gherkin
* macro (like include macro)
* Flag to indicate if generated asciidoc file must use the asciidoc gherkin macro (like include
* macro).
*/
@Parameter(property = "livingdoc.gherkin.gherkinAsciidocMacro", defaultValue = "false")
private boolean gherkinAsciidocMacro;

/**
* Replace gherkin processor default template. Must be used with
* gherkinAsciidocPlugin option to false
* Replace gherkin processor default template.
* Default is used from https://github.com/jboz/asciidoctor-gherkin-extension.
*
* @see
* https://github.com/jboz/asciidoctor-gherkin-extension/blob/main/src/main/resources/ch/ifocusit/asciidoctor/gherkin/default_template.erb
*/
@Parameter(property = "livingdoc.gherkin.gherkinAsciidocTemplate")
private File gherkinAsciidocTemplate;
Expand All @@ -116,6 +129,9 @@ protected String getTitle() {

@Override
public void executeMojo() {
if (gherkinWithTitle == null) {
gherkinWithTitle = !gerkinSeparateFeature;
}

if (!gerkinSeparateFeature) {
appendTitle(getDocBuilder(pageCount.get()));
Expand All @@ -126,22 +142,24 @@ public void executeMojo() {
getLog().info("Gherkin goal - read " + path);

if (gerkinSeparateFeature && StringUtils.isNotBlank(getTitle())) {
// read feature title
try {
Feature feature = GherkinExtensionHelper
.parse(readFileToString(FileUtils.getFile(path), defaultCharset()));
String title = StringUtils.defaultString(getTitle(), EMPTY) + " " + feature.getName();
Feature feature = GherkinExtensionHelper.parse(readFileToString(FileUtils.getFile(path), defaultCharset()));
String title = getTitle() + " " + feature.getName();
appendTitle(getDocBuilder(pageCount.get()), title);
} catch (IOException e) {
throw new IllegalStateException("Error reading " + path, e);
}
}
if (gherkinWithTitle) {
getDocBuilder(pageCount.get()).textLine(":leveloffset: +1");
}
if (gherkinAsciidocMacro) {
getDocBuilder(pageCount.get()).textLine(String.format("gherkin::%s[%s]", path, gherkinOptions));
getDocBuilder(pageCount.get()).textLine(String.format("gherkin::%s[withTitle=%s,withChildSeparator=%s,withKeyword=%s]",
path, gherkinWithTitle, gherkinWithChildSeparator, gherkinWithKeyword));
} else {
try {
getDocBuilder(pageCount.get()).textLine(
gherkinBuilder(gherkinOptions)
gherkinBuilder()
.featureContent(readFileToString(FileUtils.getFile(path), defaultCharset()))
.build());
} catch (IOException e) {
Expand Down Expand Up @@ -170,25 +188,15 @@ public void executeMojo() {
}
}

public static GherkinAsciidocBuilder gherkinBuilder(String input) throws IOException {
public GherkinAsciidocBuilder gherkinBuilder() throws IOException {
GherkinAsciidocBuilder builder = GherkinAsciidocBuilder.builder();

String[] optionsString = input.split(",");
for (String option : optionsString) {
String[] pair = option.split("=");
if (pair.length == 2) {
if ("withChildSeparator".equalsIgnoreCase(pair[0])) {
builder.withChildSeparator(Boolean.valueOf(pair[1]));
} else if ("withKeyword".equalsIgnoreCase(pair[0])) {
builder.withKeyword(Boolean.valueOf(pair[1]));
} else if ("withTitle".equalsIgnoreCase(pair[0])) {
builder.withTitle(Boolean.valueOf(pair[1]));
} else if ("template".equalsIgnoreCase(pair[0])) {
builder.templateContent(readFileToString(FileUtils.getFile(pair[1]), Charset.defaultCharset()));
}
}
builder.withChildSeparator(gherkinWithChildSeparator);
builder.withKeyword(gherkinWithKeyword);
builder.withTitle(gherkinWithTitle);
if (gherkinAsciidocTemplate != null) {
builder.templateContent(readFileToString(gherkinAsciidocTemplate, Charset.defaultCharset()));
}

return builder;
}

Expand Down
Loading

0 comments on commit 3e4678e

Please sign in to comment.