Skip to content

Commit 4e26117

Browse files
authored
Merge pull request #396 from fugerit-org/395-enhancement-fj-doc-maven-plugin-add-variables-to-direct-goal
add variables to 'direct' goal #395
2 parents 1808a60 + 813fe7f commit 4e26117

File tree

6 files changed

+89
-10
lines changed

6 files changed

+89
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- [fj-doc-maven-plugin] add variables to 'direct' goal <https://github.com/fugerit-org/fj-doc/pull/395>
13+
1014
### Changed
1115

1216
- quarkus-version set to 3.21.4 across all the modules <https://github.com/fugerit-org/fj-doc/pull/393>

fj-doc-guide/src/main/docs/asciidoc/chapters/02_4_maven_plugin_direct.adoc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ mvn org.fugerit.java:fj-doc-maven-plugin:verify -DtemplateBasePath=./src/test/re
3030
<configuration>
3131
<configPath>${project.basedir}/config/venus-direct-config-1.yaml</configPath>
3232
<outputAll>true</outputAll>
33+
<directEnv>
34+
<projectBasedir>${project.basedir}</projectBasedir>
35+
</directEnv>
3336
</configuration>
3437
</plugin>
3538
----
@@ -42,6 +45,7 @@ mvn org.fugerit.java:fj-doc-maven-plugin:verify -DtemplateBasePath=./src/test/re
4245
| configPath | true | | Path to the direct generation configuration file
4346
| outputAll | false | | set to 'true' to generate all the output in configuration
4447
| outputId | false | | List of outputId to generate
48+
| directEnv | false | | Environment to substitute on the configPath YAML file
4549
|====================================================================================================================================================================
4650

4751
==== Goal 'direct' generation configuration file
@@ -52,7 +56,7 @@ Here is an edample configuration file :
5256
----
5357
---
5458
configId: 'venus-direct-config-1'
55-
templatePath: 'src/test/resources/template/'
59+
templatePath: '${projectBasedir}/src/test/resources/template/'
5660
templateMode: 'folder'
5761
handlerList:
5862
- type: org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerUTF8
@@ -62,26 +66,26 @@ chainList: # a template named ${chainId}.ftl must exist in 'templatePath' folde
6266
dataModel: # inline data model definition
6367
docTitle: 'Venus Direct Extension - Test Doc'
6468
- chainId: 'test-doc-json-data-model'
65-
dataModelJson: 'src/test/resources/data-model/data-model-1.json' # JSON file data model
69+
dataModelJson: '${projectBasedir}/src/test/resources/data-model/data-model-1.json' # JSON file data model
6670
- chainId: 'test-doc-yaml-data-model'
67-
dataModelYaml: 'src/test/resources/data-model/data-model-1.yaml' # YAML file data model
71+
dataModelYaml: '${projectBasedir}/src/test/resources/data-model/data-model-1.yaml' # YAML file data model
6872
outputList:
6973
- outputId: 'test-doc-html'
7074
chainId: 'test-doc'
7175
handlerId: 'html' # a valid handler for this output type should be defined (i.e. org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerUTF8)
72-
file: 'target/test-doc.html'
76+
file: 't${projectBasedir}/arget/test-doc.html'
7377
- outputId: 'test-doc-md'
7478
chainId: 'test-doc'
7579
handlerId: 'md'
76-
file: 'target/test-doc.md'
80+
file: '${projectBasedir}/target/test-doc.md'
7781
- outputId: 'test-doc-json-data-model-html'
7882
chainId: 'test-doc-json-data-model'
7983
handlerId: 'html'
80-
file: 'target/test-doc-json-data-model.html'
84+
file: '${projectBasedir}/target/test-doc-json-data-model.html'
8185
- outputId: 'test-doc-yaml-data-model-md'
8286
chainId: 'test-doc-yaml-data-model'
8387
handlerId: 'md'
84-
file: 'target/test-doc-json-data-model.md'
88+
file: '${projectBasedir}/target/test-doc-json-data-model.md'
8589
----
8690

8791
TIP: _dataModel_ property in chain contains a map that can be used in the template (accessibile as 'dataModel' attribute).

fj-doc-lib-direct/src/main/java/org/fugerit/java/doc/lib/direct/VenusDirectFacade.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
55
import org.fugerit.java.core.cfg.ConfigRuntimeException;
66
import org.fugerit.java.core.function.SafeFunction;
7+
import org.fugerit.java.core.io.StreamIO;
8+
import org.fugerit.java.core.util.regex.ParamFinder;
79
import org.fugerit.java.doc.base.config.DocConfig;
810
import org.fugerit.java.doc.base.process.DocProcessContext;
911
import org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep;
@@ -17,6 +19,8 @@
1719
import java.io.File;
1820
import java.io.FileOutputStream;
1921
import java.io.Reader;
22+
import java.util.Map;
23+
import java.util.Properties;
2024

2125
public class VenusDirectFacade {
2226

@@ -34,6 +38,24 @@ public static VenusDirectConfig readConfig( Reader reader ) {
3438
} );
3539
}
3640

41+
public static VenusDirectConfig readConfig(Reader reader, Map<String, String> envMap) {
42+
return SafeFunction.get( () -> {
43+
VenusDirectConfig config = null;
44+
if ( envMap != null ) {
45+
String yamlContent = StreamIO.readString( reader );
46+
ParamFinder paramFinder = ParamFinder.newFinder();
47+
Properties params = new Properties();
48+
envMap.forEach(params::setProperty);
49+
yamlContent = paramFinder.substitute( yamlContent, params );
50+
config = YAML_MAPPER.readValue( yamlContent, VenusDirectConfig.class );
51+
} else {
52+
config = YAML_MAPPER.readValue( reader, VenusDirectConfig.class );
53+
}
54+
config.setupFreemarkerDocProcessConfig();
55+
return config;
56+
} );
57+
}
58+
3759
public static void handleAllOutput(VenusDirectConfig config) {
3860
SafeFunction.applyIfNotNull( config.getOutputList(), () ->
3961
config.getOutputList().forEach( output -> handleOutput( config, output.getOutputId() ) ) );

fj-doc-lib-direct/src/test/java/org/fugerit/java/doc/lib/direct/TestVenusDirectFacade.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.io.IOException;
1010
import java.io.InputStreamReader;
1111
import java.io.Reader;
12+
import java.util.HashMap;
13+
import java.util.Map;
1214

1315
class TestVenusDirectFacade {
1416

@@ -23,4 +25,14 @@ void testDoc() throws IOException {
2325
}
2426
}
2527

28+
@Test
29+
void testSubstitute() throws IOException {
30+
try (Reader reader = new InputStreamReader(ClassHelper.loadFromDefaultClassLoader( "config/venus-direct-config-2.yaml" ) )) {
31+
Map<String, String> envMap = new HashMap<>();
32+
envMap.put( "baseDir", "/config" );
33+
VenusDirectConfig config = VenusDirectFacade.readConfig( reader, envMap );
34+
Assertions.assertEquals( "/config/src/test/resources/template/", config.getTemplatePath() );
35+
}
36+
}
37+
2638
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
configId: 'venus-direct-config-1'
3+
templatePath: '${baseDir}/src/test/resources/template/'
4+
templateMode: 'folder'
5+
handlerList:
6+
- type: org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerUTF8
7+
- type: org.fugerit.java.doc.base.typehandler.markdown.SimpleMarkdownExtTypeHandlerNoCommentsUTF8
8+
chainList: # a template named ${chainId}.ftl must exist in 'templatePath' folder
9+
- chainId: 'test-doc'
10+
dataModel: # inline data model definition
11+
docTitle: 'Venus Direct Extension - Test Doc'
12+
- chainId: 'test-doc-json-data-model'
13+
dataModelJson: 'src/test/resources/data-model/data-model-1.json' # JSON file data model
14+
- chainId: 'test-doc-yaml-data-model'
15+
dataModelYaml: 'src/test/resources/data-model/data-model-1.yaml' # YAML file data model
16+
outputList:
17+
- outputId: 'test-doc-html'
18+
chainId: 'test-doc'
19+
handlerId: 'html' # a valid handler for this output type should be defined (i.e. org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerUTF8)
20+
file: 'target/test-doc.html'
21+
- outputId: 'test-doc-md'
22+
chainId: 'test-doc'
23+
handlerId: 'md'
24+
file: 'target/test-doc.md'
25+
- outputId: 'test-doc-json-data-model-html'
26+
chainId: 'test-doc-json-data-model'
27+
handlerId: 'html'
28+
file: 'target/test-doc-json-data-model.html'
29+
- outputId: 'test-doc-yaml-data-model-md'
30+
chainId: 'test-doc-yaml-data-model'
31+
handlerId: 'md'
32+
file: 'target/test-doc-json-data-model.md'

fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/maven/MojoDirect.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
import org.fugerit.java.doc.lib.direct.config.VenusDirectConfig;
1212

1313
import java.io.File;
14-
import java.io.FileInputStream;
1514
import java.io.InputStreamReader;
1615
import java.io.Reader;
16+
import java.nio.file.Files;
1717
import java.util.List;
18+
import java.util.Map;
1819

1920
@Mojo( name = "direct" )
2021
public class MojoDirect extends AbstractMojo {
@@ -28,6 +29,9 @@ public class MojoDirect extends AbstractMojo {
2829
@Parameter(property = "outputId")
2930
protected List<String> outputId;
3031

32+
@Parameter(property = "directEnv")
33+
protected Map<String, String> directEnv;
34+
3135
public static void checkConfiguration( File configFile, boolean outputAll, List<String> outputId ) throws MojoExecutionException {
3236
if ( !configFile.exists() ) {
3337
throw new MojoExecutionException( String.format( "Config file does not exist : %s", configFile.getAbsolutePath() ) );
@@ -46,8 +50,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
4650
this.getLog().info( String.format( "Direct config file : %s", configFile.getAbsolutePath() ) );
4751
checkConfiguration( configFile, this.outputAll, this.outputId );
4852
SafeFunction.apply( () -> {
49-
try (Reader reader = new InputStreamReader( new FileInputStream( configFile ))) {
50-
VenusDirectConfig config = VenusDirectFacade.readConfig( reader );
53+
try (Reader reader = new InputStreamReader(Files.newInputStream(configFile.toPath()))) {
54+
this.getLog().info( String.format( "Direct config env : %s", this.directEnv ) );
55+
VenusDirectConfig config = VenusDirectFacade.readConfig( reader, this.directEnv );
5156
if ( this.outputAll ) {
5257
VenusDirectFacade.handleAllOutput( config );
5358
} else {

0 commit comments

Comments
 (0)