13
13
14
14
package org .eclipse .esmf .aspectmodel ;
15
15
16
+ import static java .lang .String .format ;
17
+
18
+ import java .io .File ;
19
+ import java .io .FileInputStream ;
16
20
import java .io .FileOutputStream ;
17
21
import java .io .IOException ;
22
+ import java .io .InputStream ;
23
+ import java .nio .charset .StandardCharsets ;
18
24
import java .nio .file .Files ;
19
25
import java .nio .file .Path ;
20
26
import java .util .ArrayList ;
42
48
import org .eclipse .esmf .metamodel .Aspect ;
43
49
import org .eclipse .esmf .metamodel .AspectModel ;
44
50
51
+ import com .fasterxml .jackson .databind .ObjectMapper ;
52
+ import com .fasterxml .jackson .databind .node .ObjectNode ;
53
+ import com .fasterxml .jackson .dataformat .yaml .YAMLGenerator ;
54
+ import com .fasterxml .jackson .dataformat .yaml .YAMLMapper ;
45
55
import io .vavr .control .Either ;
56
+ import io .vavr .control .Try ;
57
+ import org .apache .commons .io .FilenameUtils ;
58
+ import org .apache .commons .io .IOUtils ;
59
+ import org .apache .commons .lang3 .StringUtils ;
46
60
import org .apache .maven .execution .MavenSession ;
47
61
import org .apache .maven .plugin .AbstractMojo ;
48
62
import org .apache .maven .plugin .MojoExecutionException ;
53
67
import org .codehaus .plexus .util .xml .Xpp3Dom ;
54
68
55
69
public abstract class AspectModelMojo extends AbstractMojo {
56
- @ Parameter ( defaultValue = "${basedir}" )
57
- private String basedir ;
70
+ protected static final ObjectMapper YAML_MAPPER = new YAMLMapper (). enable ( YAMLGenerator . Feature . MINIMIZE_QUOTES );
71
+ protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper () ;
58
72
59
73
@ Parameter
60
- private String modelsRootDirectory ;
74
+ protected String modelsRootDirectory ;
61
75
62
76
@ Parameter ( required = true , property = "include" )
63
77
protected Set <String > includes ;
64
78
65
79
@ Parameter
66
80
protected String outputDirectory = "" ;
67
81
82
+ @ Parameter
83
+ protected String generatedSourcesDirectory = "" ;
84
+
68
85
@ Parameter ( defaultValue = "false" )
69
86
protected boolean detailedValidationMessages ;
70
87
71
88
@ Parameter
72
89
protected String githubServerId ;
73
90
74
91
@ Parameter ( defaultValue = "${session}" , readonly = true )
75
- private MavenSession session ;
92
+ protected MavenSession mavenSession ;
76
93
77
94
protected GithubModelSourceConfig gitHubConfig ;
78
95
96
+ private Map <AspectModel , Aspect > aspects ;
97
+
98
+ public AspectModelMojo () {
99
+ }
100
+
101
+ protected AspectModelMojo ( final Map <AspectModel , Aspect > aspects ) {
102
+ this .aspects = aspects ;
103
+ }
104
+
79
105
/**
80
106
* Skip the execution.
81
107
*/
@@ -97,6 +123,9 @@ protected Set<AspectModel> loadModels() throws MojoExecutionException {
97
123
}
98
124
99
125
private Map <AspectModel , Aspect > loadAspectModels () throws MojoExecutionException {
126
+ if ( aspects != null ) {
127
+ return aspects ;
128
+ }
100
129
final List <ResolutionStrategy > strategies = new ArrayList <>();
101
130
if ( modelsRootDirectory != null ) {
102
131
final Path modelsRoot = Path .of ( modelsRootDirectory );
@@ -110,7 +139,7 @@ private Map<AspectModel, Aspect> loadAspectModels() throws MojoExecutionExceptio
110
139
"Neither modelsRootDirectory nor gitHubServerId were configured, don't know how to resolve Aspect Models" );
111
140
}
112
141
113
- final Map < AspectModel , Aspect > result = new HashMap <>();
142
+ aspects = new HashMap <>();
114
143
115
144
final AspectModelLoader aspectModelLoader = new AspectModelLoader ( strategies );
116
145
for ( final String inputUrn : includes ) {
@@ -129,18 +158,27 @@ private Map<AspectModel, Aspect> loadAspectModels() throws MojoExecutionExceptio
129
158
.filter ( theAspect -> theAspect .urn ().equals ( urn ) )
130
159
.findFirst ()
131
160
.orElseThrow ( () -> new MojoExecutionException ( "Loaded Aspect Model does not contain Aspect " + urn ) );
132
- result .put ( aspectModel , aspect );
161
+ aspects .put ( aspectModel , aspect );
133
162
}
134
- return result ;
163
+ return aspects ;
135
164
}
136
165
137
- protected FileOutputStream getOutputStreamForFile ( final String artifactName , final String outputDirectory ) {
166
+ protected File getOutFile ( final String artifactName , final String outputDirectory ) throws MojoExecutionException {
138
167
try {
139
168
final Path outputPath = Path .of ( outputDirectory );
140
169
Files .createDirectories ( outputPath );
141
- return new FileOutputStream ( outputPath .resolve ( artifactName ).toFile () );
170
+ return outputPath .resolve ( artifactName ).toFile ();
171
+ } catch ( final IOException exception ) {
172
+ throw new MojoExecutionException ( "Could not create missing directories for path " + outputDirectory );
173
+ }
174
+ }
175
+
176
+ protected FileOutputStream getOutputStreamForFile ( final String artifactName , final String outputDirectory )
177
+ throws MojoExecutionException {
178
+ try {
179
+ return new FileOutputStream ( getOutFile ( artifactName , outputDirectory ) );
142
180
} catch ( final IOException exception ) {
143
- throw new RuntimeException ( "Could not write to output " + outputDirectory );
181
+ throw new MojoExecutionException ( "Could not write to output " + outputDirectory );
144
182
}
145
183
}
146
184
@@ -152,10 +190,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
152
190
}
153
191
154
192
if ( githubServerId != null ) {
155
- if ( session == null ) {
193
+ if ( mavenSession == null ) {
156
194
getLog ().warn ( "Could not read Maven session, ignoring GitHub server configuration." );
157
195
} else {
158
- final Server server = session .getSettings ().getServer ( githubServerId );
196
+ final Server server = mavenSession .getSettings ().getServer ( githubServerId );
159
197
if ( server != null ) {
160
198
final Xpp3Dom dom = (Xpp3Dom ) server .getConfiguration ();
161
199
final String [] repositoryParts = Optional .ofNullable ( dom .getChild ( "repository" ) )
@@ -177,7 +215,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
177
215
.map ( GithubRepository .Tag ::new ) )
178
216
.orElse ( new GithubRepository .Branch ( "main" ) );
179
217
180
- final List <Proxy > proxies = session .getSettings ().getProxies ();
218
+ final List <Proxy > proxies = mavenSession .getSettings ().getProxies ();
181
219
final ProxyConfig proxyConfig = Optional .ofNullable ( proxies ).stream ().flatMap ( Collection ::stream )
182
220
.filter ( proxy -> proxy .getProtocol ().equals ( "https" ) )
183
221
.findFirst ()
@@ -203,4 +241,38 @@ public void execute() throws MojoExecutionException, MojoFailureException {
203
241
}
204
242
205
243
abstract void executeGeneration () throws MojoExecutionException , MojoFailureException ;
244
+
245
+ protected ObjectNode readFile ( final String file ) throws MojoExecutionException {
246
+ if ( StringUtils .isBlank ( file ) ) {
247
+ return null ;
248
+ }
249
+ final String extension = FilenameUtils .getExtension ( file ).toUpperCase ();
250
+ final Try <String > fileData = Try .of ( () -> getFileAsString ( file ) ).mapTry ( Optional ::get );
251
+ return switch ( extension ) {
252
+ case "YAML" , "YML" -> (ObjectNode ) fileData
253
+ .mapTry ( data -> YAML_MAPPER .readValue ( data , Object .class ) )
254
+ .mapTry ( OBJECT_MAPPER ::writeValueAsString )
255
+ .mapTry ( OBJECT_MAPPER ::readTree )
256
+ .get ();
257
+ case "JSON" -> (ObjectNode ) fileData
258
+ .mapTry ( OBJECT_MAPPER ::readTree )
259
+ .get ();
260
+ default -> throw new MojoExecutionException ( format ( "File extension [%s] not supported." , extension ) );
261
+ };
262
+ }
263
+
264
+ private static Optional <String > getFileAsString ( final String filePath ) throws MojoExecutionException {
265
+ if ( filePath == null || filePath .isEmpty () ) {
266
+ return Optional .empty ();
267
+ }
268
+ final File f = new File ( filePath );
269
+ if ( f .exists () && !f .isDirectory () ) {
270
+ try ( final InputStream inputStream = new FileInputStream ( filePath ) ) {
271
+ return Optional .of ( IOUtils .toString ( inputStream , StandardCharsets .UTF_8 ) );
272
+ } catch ( final IOException e ) {
273
+ throw new MojoExecutionException ( format ( "Could not load file %s." , filePath ), e );
274
+ }
275
+ }
276
+ throw new MojoExecutionException ( format ( "File does not exist %s." , filePath ) );
277
+ }
206
278
}
0 commit comments