diff --git a/CHANGELOG.md b/CHANGELOG.md index f4c39845..78505c27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- [fj-doc-maven-plugin] add variables to 'direct' goal + ### Changed - quarkus-version set to 3.21.4 across all the modules diff --git a/fj-doc-guide/src/main/docs/asciidoc/chapters/02_4_maven_plugin_direct.adoc b/fj-doc-guide/src/main/docs/asciidoc/chapters/02_4_maven_plugin_direct.adoc index bab37e9f..826e066d 100644 --- a/fj-doc-guide/src/main/docs/asciidoc/chapters/02_4_maven_plugin_direct.adoc +++ b/fj-doc-guide/src/main/docs/asciidoc/chapters/02_4_maven_plugin_direct.adoc @@ -30,6 +30,9 @@ mvn org.fugerit.java:fj-doc-maven-plugin:verify -DtemplateBasePath=./src/test/re ${project.basedir}/config/venus-direct-config-1.yaml true + + ${project.basedir} + ---- @@ -42,6 +45,7 @@ mvn org.fugerit.java:fj-doc-maven-plugin:verify -DtemplateBasePath=./src/test/re | configPath | true | | Path to the direct generation configuration file | outputAll | false | | set to 'true' to generate all the output in configuration | outputId | false | | List of outputId to generate +| directEnv | false | | Environment to substitute on the configPath YAML file |==================================================================================================================================================================== ==== Goal 'direct' generation configuration file @@ -52,7 +56,7 @@ Here is an edample configuration file : ---- --- configId: 'venus-direct-config-1' -templatePath: 'src/test/resources/template/' +templatePath: '${projectBasedir}/src/test/resources/template/' templateMode: 'folder' handlerList: - type: org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerUTF8 @@ -62,26 +66,26 @@ chainList: # a template named ${chainId}.ftl must exist in 'templatePath' folde dataModel: # inline data model definition docTitle: 'Venus Direct Extension - Test Doc' - chainId: 'test-doc-json-data-model' - dataModelJson: 'src/test/resources/data-model/data-model-1.json' # JSON file data model + dataModelJson: '${projectBasedir}/src/test/resources/data-model/data-model-1.json' # JSON file data model - chainId: 'test-doc-yaml-data-model' - dataModelYaml: 'src/test/resources/data-model/data-model-1.yaml' # YAML file data model + dataModelYaml: '${projectBasedir}/src/test/resources/data-model/data-model-1.yaml' # YAML file data model outputList: - outputId: 'test-doc-html' chainId: 'test-doc' handlerId: 'html' # a valid handler for this output type should be defined (i.e. org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerUTF8) - file: 'target/test-doc.html' + file: 't${projectBasedir}/arget/test-doc.html' - outputId: 'test-doc-md' chainId: 'test-doc' handlerId: 'md' - file: 'target/test-doc.md' + file: '${projectBasedir}/target/test-doc.md' - outputId: 'test-doc-json-data-model-html' chainId: 'test-doc-json-data-model' handlerId: 'html' - file: 'target/test-doc-json-data-model.html' + file: '${projectBasedir}/target/test-doc-json-data-model.html' - outputId: 'test-doc-yaml-data-model-md' chainId: 'test-doc-yaml-data-model' handlerId: 'md' - file: 'target/test-doc-json-data-model.md' + file: '${projectBasedir}/target/test-doc-json-data-model.md' ---- TIP: _dataModel_ property in chain contains a map that can be used in the template (accessibile as 'dataModel' attribute). diff --git a/fj-doc-lib-direct/src/main/java/org/fugerit/java/doc/lib/direct/VenusDirectFacade.java b/fj-doc-lib-direct/src/main/java/org/fugerit/java/doc/lib/direct/VenusDirectFacade.java index f191fb27..6e6a9094 100644 --- a/fj-doc-lib-direct/src/main/java/org/fugerit/java/doc/lib/direct/VenusDirectFacade.java +++ b/fj-doc-lib-direct/src/main/java/org/fugerit/java/doc/lib/direct/VenusDirectFacade.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; import org.fugerit.java.core.cfg.ConfigRuntimeException; import org.fugerit.java.core.function.SafeFunction; +import org.fugerit.java.core.io.StreamIO; +import org.fugerit.java.core.util.regex.ParamFinder; import org.fugerit.java.doc.base.config.DocConfig; import org.fugerit.java.doc.base.process.DocProcessContext; import org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep; @@ -17,6 +19,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.Reader; +import java.util.Map; +import java.util.Properties; public class VenusDirectFacade { @@ -34,6 +38,24 @@ public static VenusDirectConfig readConfig( Reader reader ) { } ); } + public static VenusDirectConfig readConfig(Reader reader, Map envMap) { + return SafeFunction.get( () -> { + VenusDirectConfig config = null; + if ( envMap != null ) { + String yamlContent = StreamIO.readString( reader ); + ParamFinder paramFinder = ParamFinder.newFinder(); + Properties params = new Properties(); + envMap.forEach(params::setProperty); + yamlContent = paramFinder.substitute( yamlContent, params ); + config = YAML_MAPPER.readValue( yamlContent, VenusDirectConfig.class ); + } else { + config = YAML_MAPPER.readValue( reader, VenusDirectConfig.class ); + } + config.setupFreemarkerDocProcessConfig(); + return config; + } ); + } + public static void handleAllOutput(VenusDirectConfig config) { SafeFunction.applyIfNotNull( config.getOutputList(), () -> config.getOutputList().forEach( output -> handleOutput( config, output.getOutputId() ) ) ); diff --git a/fj-doc-lib-direct/src/test/java/org/fugerit/java/doc/lib/direct/TestVenusDirectFacade.java b/fj-doc-lib-direct/src/test/java/org/fugerit/java/doc/lib/direct/TestVenusDirectFacade.java index d9ab208b..b537be4f 100644 --- a/fj-doc-lib-direct/src/test/java/org/fugerit/java/doc/lib/direct/TestVenusDirectFacade.java +++ b/fj-doc-lib-direct/src/test/java/org/fugerit/java/doc/lib/direct/TestVenusDirectFacade.java @@ -9,6 +9,8 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; +import java.util.HashMap; +import java.util.Map; class TestVenusDirectFacade { @@ -23,4 +25,14 @@ void testDoc() throws IOException { } } + @Test + void testSubstitute() throws IOException { + try (Reader reader = new InputStreamReader(ClassHelper.loadFromDefaultClassLoader( "config/venus-direct-config-2.yaml" ) )) { + Map envMap = new HashMap<>(); + envMap.put( "baseDir", "/config" ); + VenusDirectConfig config = VenusDirectFacade.readConfig( reader, envMap ); + Assertions.assertEquals( "/config/src/test/resources/template/", config.getTemplatePath() ); + } + } + } diff --git a/fj-doc-lib-direct/src/test/resources/config/venus-direct-config-2.yaml b/fj-doc-lib-direct/src/test/resources/config/venus-direct-config-2.yaml new file mode 100644 index 00000000..66c07264 --- /dev/null +++ b/fj-doc-lib-direct/src/test/resources/config/venus-direct-config-2.yaml @@ -0,0 +1,32 @@ +--- +configId: 'venus-direct-config-1' +templatePath: '${baseDir}/src/test/resources/template/' +templateMode: 'folder' +handlerList: + - type: org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerUTF8 + - type: org.fugerit.java.doc.base.typehandler.markdown.SimpleMarkdownExtTypeHandlerNoCommentsUTF8 +chainList: # a template named ${chainId}.ftl must exist in 'templatePath' folder + - chainId: 'test-doc' + dataModel: # inline data model definition + docTitle: 'Venus Direct Extension - Test Doc' + - chainId: 'test-doc-json-data-model' + dataModelJson: 'src/test/resources/data-model/data-model-1.json' # JSON file data model + - chainId: 'test-doc-yaml-data-model' + dataModelYaml: 'src/test/resources/data-model/data-model-1.yaml' # YAML file data model +outputList: + - outputId: 'test-doc-html' + chainId: 'test-doc' + handlerId: 'html' # a valid handler for this output type should be defined (i.e. org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerUTF8) + file: 'target/test-doc.html' + - outputId: 'test-doc-md' + chainId: 'test-doc' + handlerId: 'md' + file: 'target/test-doc.md' + - outputId: 'test-doc-json-data-model-html' + chainId: 'test-doc-json-data-model' + handlerId: 'html' + file: 'target/test-doc-json-data-model.html' + - outputId: 'test-doc-yaml-data-model-md' + chainId: 'test-doc-yaml-data-model' + handlerId: 'md' + file: 'target/test-doc-json-data-model.md' \ No newline at end of file diff --git a/fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/maven/MojoDirect.java b/fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/maven/MojoDirect.java index c94fc568..bf5233c1 100644 --- a/fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/maven/MojoDirect.java +++ b/fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/maven/MojoDirect.java @@ -11,10 +11,11 @@ import org.fugerit.java.doc.lib.direct.config.VenusDirectConfig; import java.io.File; -import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.file.Files; import java.util.List; +import java.util.Map; @Mojo( name = "direct" ) public class MojoDirect extends AbstractMojo { @@ -28,6 +29,9 @@ public class MojoDirect extends AbstractMojo { @Parameter(property = "outputId") protected List outputId; + @Parameter(property = "directEnv") + protected Map directEnv; + public static void checkConfiguration( File configFile, boolean outputAll, List outputId ) throws MojoExecutionException { if ( !configFile.exists() ) { throw new MojoExecutionException( String.format( "Config file does not exist : %s", configFile.getAbsolutePath() ) ); @@ -46,8 +50,9 @@ public void execute() throws MojoExecutionException, MojoFailureException { this.getLog().info( String.format( "Direct config file : %s", configFile.getAbsolutePath() ) ); checkConfiguration( configFile, this.outputAll, this.outputId ); SafeFunction.apply( () -> { - try (Reader reader = new InputStreamReader( new FileInputStream( configFile ))) { - VenusDirectConfig config = VenusDirectFacade.readConfig( reader ); + try (Reader reader = new InputStreamReader(Files.newInputStream(configFile.toPath()))) { + this.getLog().info( String.format( "Direct config env : %s", this.directEnv ) ); + VenusDirectConfig config = VenusDirectFacade.readConfig( reader, this.directEnv ); if ( this.outputAll ) { VenusDirectFacade.handleAllOutput( config ); } else {