Skip to content

Commit

Permalink
feat(gherkin): 🚀 inner processor - full gherkin syntax support (#111)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Boz <[email protected]>
  • Loading branch information
jboz and Julien Boz authored Dec 2, 2023
1 parent d45d6e7 commit 9ebe34e
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 325 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package ch.ifocusit.livingdoc.plugin;

import ch.ifocusit.livingdoc.plugin.baseMojo.AbstractDocsGeneratorMojo;
import ch.ifocusit.livingdoc.plugin.gherkin.GherkinToAsciidocTransformer;
import ch.ifocusit.livingdoc.plugin.gherkin.StandaloneGherkinProcessor;
import com.github.domgold.doctools.asciidoctor.gherkin.MapFormatter;
import io.github.robwin.markup.builder.asciidoc.AsciiDocBuilder;
import org.apache.commons.io.FileUtils;
Expand All @@ -34,6 +34,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -82,10 +83,16 @@ public class GherkinMojo extends AbstractDocsGeneratorMojo {
private boolean gerkinSeparateFeature;

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

/**
* Replace gherkin processor default template. Must be used with gherkinAsciidocPlugin option to false
*/
@Parameter(property = "livingdoc.gherkin.gherkinAsciidocTemplate")
private File gherkinAsciidocTemplate;

protected boolean somethingWasGenerated = false;

Expand Down Expand Up @@ -123,11 +130,14 @@ public void executeMojo() {
throw new IllegalStateException("Error reading " + path, e);
}
}
if (gherkinAsciidocPlugin) {
if (gherkinAsciidocMacro) {
getDocBuilder(pageCount.get()).textLine(String.format("gherkin::%s[%s]", path, gherkinOptions));
} else {
try {
getDocBuilder(pageCount.get()).textLine(new GherkinToAsciidocTransformer().transform(readFileToString(FileUtils.getFile(path), defaultCharset())));
getDocBuilder(pageCount.get()).textLine(StandaloneGherkinProcessor.builder()
.gherkinTemplate(gherkinAsciidocTemplate)
.build()
.process(readFileToString(FileUtils.getFile(path), defaultCharset())));
} catch (IOException e) {
throw new IllegalStateException("Error reading " + path, e);
}
Expand Down Expand Up @@ -166,6 +176,7 @@ private Stream<String> readFeatures() {
.filter(path -> Files.exists(Paths.get(path)))
.flatMap(path -> {
try {
//noinspection resource
return Files.walk(Paths.get(path)).filter(p -> p.toString().endsWith(".feature"));
} catch (IOException e) {
throw new IllegalStateException(String.format("Error browsing %s", path), e);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ch.ifocusit.livingdoc.plugin.gherkin;

import com.github.domgold.doctools.asciidoctor.gherkin.MapFormatter;
import lombok.Builder;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.jruby.embed.ScriptingContainer;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Objects;

@Builder
public class StandaloneGherkinProcessor {

private File gherkinTemplate;

@SneakyThrows
public String process(String fileContent) {
ScriptingContainer container = new ScriptingContainer();
container.put("feature_file_content", fileContent);
container.put("template_content", gherkinTemplate != null ? IOUtils.toString(gherkinTemplate.toURI(), Charset.defaultCharset()) : MapFormatter.getDefaultTemplate());
String scriptPath = "/standaloneGherkinProcessor.rb";
String script = IOUtils.toString(Objects.requireNonNull(getClass().getResourceAsStream(scriptPath)), Charset.defaultCharset());
return (String) container.runScriptlet(script);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'erb'

def preprocess_feature feature
if feature.key?('background')
preprocess_scenario feature['background']
end
if feature.key?('scenarios')
feature['scenarios'].each do |scenario|
preprocess_scenario scenario
end
end
end

def preprocess_scenario scenario
if scenario.key?('steps')
preprocess_steplist scenario['steps']
end
if scenario.key?('examples')
preprocess_table_comments scenario['examples']['rows']
end
end

def preprocess_steplist steplist
steplist.each do |step|
if step.key?('rows')
preprocess_table_comments step['rows']
end
end
end

def preprocess_table_comments rows
if rows.length > 0 && rows.first.key?('comments') && rows.first['comments'].length > 0 && rows.first['comments'].first['value'].match(/^#cols=/)
cols = rows.first['comments'].first['value'][1..-1]
rows.first['comments'].java_send :remove, [Java::int], 0
rows.first["cols"] = cols
end
rows.each do |row|
if row.key?('comments') && row['comments'].length > 0 && row['comments'].first['value'].match(/^#cells=/)
cells = row['comments'].first['value'][7..-1].split(/,/)
row['cell-styles'] = cells
row['comments'].java_send :remove, [Java::int], 0
end
end
end

# parse feature and make the result available to the template via binding as 'feature' hash.
feature = com.github.domgold.doctools.asciidoctor.gherkin.MapFormatter.parse(feature_file_content)

preprocess_feature(feature)

erb_template = ERB.new(template_content)
rendered_template_output = erb_template.result(binding())

rendered_template_output
Loading

0 comments on commit 9ebe34e

Please sign in to comment.