Skip to content

add variables to 'direct' goal #395 #396

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/fugerit-org/fj-doc/pull/395>

### Changed

- quarkus-version set to 3.21.4 across all the modules <https://github.com/fugerit-org/fj-doc/pull/393>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ mvn org.fugerit.java:fj-doc-maven-plugin:verify -DtemplateBasePath=./src/test/re
<configuration>
<configPath>${project.basedir}/config/venus-direct-config-1.yaml</configPath>
<outputAll>true</outputAll>
<directEnv>
<projectBasedir>${project.basedir}</projectBasedir>
</directEnv>
</configuration>
</plugin>
----
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -34,6 +38,24 @@ public static VenusDirectConfig readConfig( Reader reader ) {
} );
}

public static VenusDirectConfig readConfig(Reader reader, Map<String, String> 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() ) ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<String, String> envMap = new HashMap<>();
envMap.put( "baseDir", "/config" );
VenusDirectConfig config = VenusDirectFacade.readConfig( reader, envMap );
Assertions.assertEquals( "/config/src/test/resources/template/", config.getTemplatePath() );
}
}

}
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,6 +29,9 @@ public class MojoDirect extends AbstractMojo {
@Parameter(property = "outputId")
protected List<String> outputId;

@Parameter(property = "directEnv")
protected Map<String, String> directEnv;

public static void checkConfiguration( File configFile, boolean outputAll, List<String> outputId ) throws MojoExecutionException {
if ( !configFile.exists() ) {
throw new MojoExecutionException( String.format( "Config file does not exist : %s", configFile.getAbsolutePath() ) );
Expand All @@ -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 {
Expand Down