-
Notifications
You must be signed in to change notification settings - Fork 522
/
DocumentationUtils.java
668 lines (607 loc) · 32.5 KB
/
DocumentationUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.siddhi.doc.gen.core.utils;
import com.google.common.io.Files;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.wso2.siddhi.annotation.Example;
import org.wso2.siddhi.annotation.Extension;
import org.wso2.siddhi.annotation.Parameter;
import org.wso2.siddhi.annotation.ReturnAttribute;
import org.wso2.siddhi.annotation.SystemParameter;
import org.wso2.siddhi.doc.gen.commons.metadata.ExampleMetaData;
import org.wso2.siddhi.doc.gen.commons.metadata.ExtensionMetaData;
import org.wso2.siddhi.doc.gen.commons.metadata.ExtensionType;
import org.wso2.siddhi.doc.gen.commons.metadata.NamespaceMetaData;
import org.wso2.siddhi.doc.gen.commons.metadata.ParameterMetaData;
import org.wso2.siddhi.doc.gen.commons.metadata.ReturnAttributeMetaData;
import org.wso2.siddhi.doc.gen.commons.metadata.SystemParameterMetaData;
import org.wso2.siddhi.doc.gen.core.freemarker.FormatDescriptionMethod;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Utility class for getting the meta data for the extension processors in Siddhi
*/
public class DocumentationUtils {
private DocumentationUtils() { // To prevent instantiating utils class
}
/**
* Returns the extension extension meta data
* Gets the meta data from the siddhi manager
*
* @param targetDirectoryPath The path of the target directory of the maven module containing extensions
* @param logger The maven plugin logger
* @return NamespaceMetaData namespace meta data list
* @throws MojoFailureException If this fails to access project dependencies
* @throws MojoExecutionException If the classes directory from which classes are loaded is invalid
*/
public static List<NamespaceMetaData> getExtensionMetaData(String targetDirectoryPath,
List<String> runtimeClasspathElements,
Log logger)
throws MojoFailureException, MojoExecutionException {
List<NamespaceMetaData> namespaceMetaDataList = new ArrayList<>();
int urlCount = runtimeClasspathElements.size() + 1; // +1 to include the module's target/classes folder
// Creating a list of URLs with all project dependencies
URL[] urls = new URL[urlCount];
for (int i = 0; i < runtimeClasspathElements.size(); i++) {
try {
urls[i] = new File(runtimeClasspathElements.get(i)).toURI().toURL();
} catch (MalformedURLException e) {
throw new MojoFailureException("Unable to access project dependency: "
+ runtimeClasspathElements.get(i), e);
}
}
File classesDirectory = new File(targetDirectoryPath + File.separator + Constants.CLASSES_DIRECTORY);
try {
// Adding the generated classes to the class loader
urls[urlCount - 1] = classesDirectory.toURI().toURL();
ClassLoader urlClassLoader = AccessController.doPrivileged(
(PrivilegedAction<ClassLoader>) () -> new URLClassLoader(
urls, Thread.currentThread().getContextClassLoader()
)
);
// Getting extensions from all the class files in the classes directory
addExtensionInDirectory(classesDirectory, classesDirectory.getAbsolutePath(), urlClassLoader,
namespaceMetaDataList, logger);
} catch (MalformedURLException e) {
throw new MojoExecutionException("Invalid classes directory: " + classesDirectory.getAbsolutePath(), e);
}
return namespaceMetaDataList;
}
/**
* Generate documentation related files using metadata
*
* @param namespaceMetaDataList Metadata in this repository
* @param documentationBaseDirectory The path of the directory in which the documentation will be generated
* @param documentationVersion The version of the documentation being generated
* @throws MojoFailureException if the Mojo fails to find template file or create new documentation file
*/
public static void generateDocumentation(List<NamespaceMetaData> namespaceMetaDataList,
String documentationBaseDirectory, String documentationVersion)
throws MojoFailureException {
// Generating data model
Map<String, Object> rootDataModel = new HashMap<>();
rootDataModel.put("metaData", namespaceMetaDataList);
rootDataModel.put("formatDescription", new FormatDescriptionMethod());
String outputFileRelativePath = Constants.API_SUB_DIRECTORY + File.separator + documentationVersion
+ Constants.MARKDOWN_FILE_EXTENSION;
generateFileFromTemplate(
Constants.MARKDOWN_DOCUMENTATION_TEMPLATE + Constants.MARKDOWN_FILE_EXTENSION
+ Constants.FREEMARKER_TEMPLATE_FILE_EXTENSION,
rootDataModel, documentationBaseDirectory, outputFileRelativePath
);
}
/**
* Update the documentation home page
*
* @param inputFile The path to the input file
* @param outputFile The path to the output file
* @param extensionRepositoryName The name of the extension repository
* @param latestDocumentationVersion The version of the latest documentation generated
* @param namespaceMetaDataList Metadata in this repository
* @throws MojoFailureException if the Mojo fails to find template file or create new documentation file
*/
public static void updateHeadingsInMarkdownFile(File inputFile, File outputFile,
String extensionRepositoryName,
String latestDocumentationVersion,
List<NamespaceMetaData> namespaceMetaDataList)
throws MojoFailureException {
// Retrieving the content of the README.md file
List<String> inputFileLines = new ArrayList<>();
try {
inputFileLines = Files.readLines(inputFile, Constants.DEFAULT_CHARSET);
} catch (IOException ignored) {
}
// Generating data model
Map<String, Object> rootDataModel = new HashMap<>();
rootDataModel.put("inputFileLines", inputFileLines);
rootDataModel.put("extensionRepositoryName", extensionRepositoryName);
rootDataModel.put("latestDocumentationVersion", latestDocumentationVersion);
rootDataModel.put("metaData", namespaceMetaDataList);
rootDataModel.put("formatDescription", new FormatDescriptionMethod());
generateFileFromTemplate(
Constants.MARKDOWN_HEADINGS_UPDATE_TEMPLATE + Constants.MARKDOWN_FILE_EXTENSION
+ Constants.FREEMARKER_TEMPLATE_FILE_EXTENSION,
rootDataModel, outputFile.getParent(), outputFile.getName()
);
}
/**
* Remove the snapshot version documentation files from docs/api directory
*
* @param mkdocsConfigFile The mkdocs configuration file
* @param documentationBaseDirectory The path of the base directory in which the documentation will be generated
* @param logger The maven plugin logger
*/
public static void removeSnapshotAPIDocs(File mkdocsConfigFile, String documentationBaseDirectory, Log logger) {
// Retrieving the documentation file names
File apiDocsDirectory = new File(documentationBaseDirectory
+ File.separator + Constants.API_SUB_DIRECTORY);
String[] documentationFileNames = apiDocsDirectory.list(
(directory, fileName) -> fileName.endsWith(Constants.MARKDOWN_FILE_EXTENSION)
);
if (documentationFileNames != null) {
// Removing snapshot files and creating a list of the files that are left out
for (String documentationFileName : documentationFileNames) {
if (documentationFileName.endsWith(Constants.SNAPSHOT_VERSION_POSTFIX
+ Constants.MARKDOWN_FILE_EXTENSION)) {
// Removing the snapshot documentation file
File documentationFile = new File(apiDocsDirectory.getAbsolutePath()
+ File.separator + documentationFileName);
if (!documentationFile.delete()) {
logger.warn("Failed to delete SNAPSHOT documentation file "
+ documentationFile.getAbsolutePath());
}
}
}
// Updating the links in the home page to the mkdocs config
try {
updateAPIPagesInMkdocsConfig(mkdocsConfigFile, documentationBaseDirectory);
} catch (FileNotFoundException e) {
logger.warn("Unable to find mkdocs configuration file: "
+ mkdocsConfigFile.getAbsolutePath() + ". Mkdocs configuration file not updated.");
}
}
}
/**
* This add a new page to the list of pages in the mkdocs configuration
*
* @param mkdocsConfigFile The mkdocs configuration file
* @param documentationBaseDirectory The base directory of the documentation
* @throws FileNotFoundException If mkdocs configuration file is not found
*/
public static void updateAPIPagesInMkdocsConfig(File mkdocsConfigFile, String documentationBaseDirectory)
throws FileNotFoundException {
// Retrieving the documentation file names
File documentationDirectory = new File(documentationBaseDirectory
+ File.separator + Constants.API_SUB_DIRECTORY);
String[] documentationFiles = documentationDirectory.list(
(directory, fileName) -> fileName.endsWith(Constants.MARKDOWN_FILE_EXTENSION)
);
List<String> apiDirectoryContent;
if (documentationFiles == null) {
apiDirectoryContent = new ArrayList<>();
} else {
apiDirectoryContent = Arrays.asList(documentationFiles);
apiDirectoryContent.sort(String::compareTo);
}
// Creating yaml parser
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(dumperOptions);
// Reading the mkdocs configuration
Map<String, Object> yamlConfig = (Map<String, Object>) yaml.load(new InputStreamReader(
new FileInputStream(mkdocsConfigFile), Constants.DEFAULT_CHARSET)
);
// Getting the pages list
List<Map<String, Object>> yamlConfigPagesList =
(List<Map<String, Object>>) yamlConfig.get(Constants.MKDOCS_CONFIG_PAGES_KEY);
// Creating the new api pages list
List<Map<String, Object>> apiPagesList = new ArrayList<>();
for (String apiFile : apiDirectoryContent) {
String pageName = apiFile.substring(0, apiFile.length() - Constants.MARKDOWN_FILE_EXTENSION.length());
Map<String, Object> newPage = new HashMap<>();
newPage.put(pageName, Constants.API_SUB_DIRECTORY + Constants.MKDOCS_FILE_SEPARATOR + apiFile);
apiPagesList.add(newPage);
}
// Setting the new api pages
Map<String, Object> yamlConfigAPIPage = null;
for (Map<String, Object> yamlConfigPage : yamlConfigPagesList) {
if (yamlConfigPage.get(Constants.MKDOCS_CONFIG_PAGES_API_KEY) != null) {
yamlConfigAPIPage = yamlConfigPage;
break;
}
}
if (yamlConfigAPIPage == null) {
yamlConfigAPIPage = new HashMap<>();
yamlConfigPagesList.add(yamlConfigAPIPage);
}
yamlConfigAPIPage.put(Constants.MKDOCS_CONFIG_PAGES_API_KEY, apiPagesList);
// Saving the updated configuration
yaml.dump(yamlConfig, new OutputStreamWriter(
new FileOutputStream(mkdocsConfigFile), Constants.DEFAULT_CHARSET)
);
}
/**
* Generate a extension index file from the template file
*
* @param extensionRepositories The list of extension repository names
* @param extensionRepositoryOwner The extension repository owner's name
* @param documentationBaseDirectory The output directory path in which the extension index will be generated
* @param extensionsIndexFileName The name of the index file that will be generated
* @throws MojoFailureException if the Mojo fails to find template file or create new documentation file
*/
public static void createExtensionsIndex(List<String> extensionRepositories, String extensionRepositoryOwner,
String documentationBaseDirectory, String extensionsIndexFileName)
throws MojoFailureException {
// Separating Apache and GPL extensions based on siddhi repository prefix conventions
List<String> gplExtensionRepositories = new ArrayList<>();
List<String> apacheExtensionRepositories = new ArrayList<>();
for (String extensionRepository : extensionRepositories) {
if (extensionRepository.startsWith(Constants.GITHUB_GPL_EXTENSION_REPOSITORY_PREFIX)) {
gplExtensionRepositories.add(extensionRepository);
} else if (extensionRepository.startsWith(Constants.GITHUB_APACHE_EXTENSION_REPOSITORY_PREFIX)) {
apacheExtensionRepositories.add(extensionRepository);
}
}
// Generating data model
Map<String, Object> rootDataModel = new HashMap<>();
rootDataModel.put("extensionsOwner", extensionRepositoryOwner);
rootDataModel.put("gplExtensionRepositories", gplExtensionRepositories);
rootDataModel.put("apacheExtensionRepositories", apacheExtensionRepositories);
generateFileFromTemplate(
Constants.MARKDOWN_EXTENSIONS_INDEX_TEMPLATE + Constants.MARKDOWN_FILE_EXTENSION
+ Constants.FREEMARKER_TEMPLATE_FILE_EXTENSION,
rootDataModel, documentationBaseDirectory,
extensionsIndexFileName + Constants.MARKDOWN_FILE_EXTENSION
);
}
/**
* Deploy the mkdocs website on GitHub pages
*
* @param mkdocsConfigFile The mkdocs configuration file
* @param version The version of the documentation
* @param logger The maven logger
*/
public static void deployMkdocsOnGitHubPages(File mkdocsConfigFile, String version, Log logger) {
try {
executeCommand(new String[] {Constants.MKDOCS_COMMAND,
Constants.MKDOCS_GITHUB_DEPLOY_COMMAND,
Constants.MKDOCS_GITHUB_DEPLOY_COMMAND_CONFIG_FILE_ARGUMENT,
mkdocsConfigFile.getAbsolutePath(),
Constants.MKDOCS_GITHUB_DEPLOY_COMMAND_MESSAGE_ARGUMENT,
String.format(Constants.GIT_COMMIT_COMMAND_MESSAGE_FORMAT, version, version)}, logger);
} catch (Throwable t) {
logger.warn("Failed to execute mkdocs gh-deploy. Skipping deployment of documentation.", t);
}
}
/**
* Commit the documentation directory and the mkdocs config file
*
* @param docsDirectory The docs drectory
* @param mkdocsConfigFile The mkdocs configuration file
* @param readmeFile The read me file
* @param version The version of the documentation
* @param logger The maven logger
*/
public static void updateDocumentationOnGitHub(String docsDirectory, File mkdocsConfigFile, File readmeFile,
String version, Log logger) {
try {
executeCommand(new String[] {Constants.GIT_COMMAND,
Constants.GIT_ADD_COMMAND,
docsDirectory}, logger);
executeCommand(new String[] {Constants.GIT_COMMAND,
Constants.GIT_COMMIT_COMMAND,
Constants.GIT_COMMIT_COMMAND_MESSAGE_ARGUMENT,
String.format(Constants.GIT_COMMIT_COMMAND_MESSAGE_FORMAT, version, version),
Constants.GIT_COMMIT_COMMAND_FILES_ARGUMENT,
docsDirectory, mkdocsConfigFile.getAbsolutePath(), readmeFile.getAbsolutePath()}, logger);
executeCommand(new String[] {Constants.GIT_COMMAND,
Constants.GIT_PUSH_COMMAND,
Constants.GIT_PUSH_COMMAND_REMOTE,
Constants.GIT_PUSH_COMMAND_REMOTE_BRANCH}, logger);
} catch (Throwable t) {
logger.warn("Failed to update the documentation on GitHub repository", t);
}
}
/**
* Search for class files in the directory and add extensions from them
* This method recursively searches the sub directories
*
* @param moduleTargetClassesDirectory The directory from which the extension metadata will be loaded
* @param classesDirectoryPath The absolute path to the classes directory in the target folder in the module
* @param urlClassLoader The url class loader which should be used for loading the classes
* @param namespaceMetaDataList List of namespace meta data
* @param logger The maven logger
*/
private static void addExtensionInDirectory(File moduleTargetClassesDirectory, String classesDirectoryPath,
ClassLoader urlClassLoader,
List<NamespaceMetaData> namespaceMetaDataList, Log logger) {
File[] innerFiles = moduleTargetClassesDirectory.listFiles();
if (innerFiles != null) {
for (File innerFile : innerFiles) {
if (innerFile.isDirectory()) {
addExtensionInDirectory(innerFile, classesDirectoryPath, urlClassLoader, namespaceMetaDataList,
logger);
} else {
addExtensionInFile(innerFile, classesDirectoryPath, urlClassLoader, namespaceMetaDataList,
logger);
}
}
}
}
/**
* Add an extension annotation in a file
* This first checks if this is a class file and loads the class and adds the annotation if present
*
* @param file The file from which the extension metadata will be loaded
* @param classesDirectoryPath The absolute path to the classes directory in the target folder in the module
* @param urlClassLoader The url class loader which should be used for loading the classes
* @param namespaceMetaDataList List of namespace meta data
* @param logger The maven logger
*/
private static void addExtensionInFile(File file, String classesDirectoryPath, ClassLoader urlClassLoader,
List<NamespaceMetaData> namespaceMetaDataList, Log logger) {
String filePath = file.getAbsolutePath();
if (filePath.endsWith(Constants.CLASS_FILE_EXTENSION) &&
filePath.length() > classesDirectoryPath.length()) {
String relativePathToClass = filePath.substring((classesDirectoryPath + File.separator).length());
try {
// Loading class
Class<?> extensionClass = Class.forName(
relativePathToClass
.substring(0, relativePathToClass.length() - Constants.CLASS_FILE_EXTENSION.length())
.replace(File.separator, "."),
false, urlClassLoader
);
// Generating metadata and adding the it to the list of relevant extensions
addExtensionMetaDataIntoNamespaceList(namespaceMetaDataList, extensionClass, logger);
} catch (ClassNotFoundException ignored) {
logger.warn("Ignoring the failed class loading from " + file.getAbsolutePath());
}
}
}
/**
* Generate extension meta data from the annotated data in the class
*
* @param namespaceList The list of namespaces to which the new extension will be added
* @param extensionClass Class from which meta data should be extracted from
* @param logger The maven plugin logger
*/
private static void addExtensionMetaDataIntoNamespaceList(List<NamespaceMetaData> namespaceList,
Class<?> extensionClass, Log logger) {
Extension extensionAnnotation = extensionClass.getAnnotation(Extension.class);
if (extensionAnnotation != null) { // Discarding extension classes without annotation
ExtensionMetaData extensionMetaData = new ExtensionMetaData();
// Finding extension type
String extensionType = null;
for (Map.Entry<ExtensionType, Class<?>> entry : ExtensionType.getSuperClassMap().entrySet()) {
Class<?> superClass = entry.getValue();
if (superClass.isAssignableFrom(extensionClass) && superClass != extensionClass) {
extensionType = entry.getKey().getValue();
break;
}
}
// Discarding the extension if it belongs to an unknown type
if (extensionType == null) {
logger.warn("Discarding extension (belonging to an unknown extension type): "
+ extensionClass.getCanonicalName());
return;
}
extensionMetaData.setName(extensionAnnotation.name());
extensionMetaData.setDescription(extensionAnnotation.description());
// Adding query parameters
ParameterMetaData[] parameters = new ParameterMetaData[extensionAnnotation.parameters().length];
for (int i = 0; i < extensionAnnotation.parameters().length; i++) {
Parameter parameterAnnotation = extensionAnnotation.parameters()[i];
ParameterMetaData parameter = new ParameterMetaData();
parameter.setName(parameterAnnotation.name());
parameter.setType(Arrays.asList(parameterAnnotation.type()));
parameter.setDescription(parameterAnnotation.description());
parameter.setOptional(parameterAnnotation.optional());
parameter.setDynamic(parameterAnnotation.dynamic());
parameter.setDefaultValue(parameterAnnotation.defaultValue());
parameters[i] = parameter;
}
extensionMetaData.setParameters(Arrays.asList(parameters));
// Adding system parameters
SystemParameterMetaData[] systemParameters =
new SystemParameterMetaData[extensionAnnotation.systemParameter().length];
for (int i = 0; i < extensionAnnotation.systemParameter().length; i++) {
SystemParameter systemParameterAnnotation = extensionAnnotation.systemParameter()[i];
SystemParameterMetaData systemParameter = new SystemParameterMetaData();
systemParameter.setName(systemParameterAnnotation.name());
systemParameter.setDescription(systemParameterAnnotation.description());
systemParameter.setDefaultValue(systemParameterAnnotation.defaultValue());
systemParameter.setPossibleParameters(Arrays.asList(systemParameterAnnotation.possibleParameters()));
systemParameters[i] = systemParameter;
}
extensionMetaData.setSystemParameters(Arrays.asList(systemParameters));
// Adding return attributes
ReturnAttributeMetaData[] returnAttributes =
new ReturnAttributeMetaData[extensionAnnotation.returnAttributes().length];
for (int i = 0; i < extensionAnnotation.returnAttributes().length; i++) {
ReturnAttribute parameterAnnotation = extensionAnnotation.returnAttributes()[i];
ReturnAttributeMetaData returnAttribute = new ReturnAttributeMetaData();
returnAttribute.setName(parameterAnnotation.name());
returnAttribute.setType(Arrays.asList(parameterAnnotation.type()));
returnAttribute.setDescription(parameterAnnotation.description());
returnAttributes[i] = returnAttribute;
}
extensionMetaData.setReturnAttributes(Arrays.asList(returnAttributes));
// Adding examples
ExampleMetaData[] examples = new ExampleMetaData[extensionAnnotation.examples().length];
for (int i = 0; i < extensionAnnotation.examples().length; i++) {
Example exampleAnnotation = extensionAnnotation.examples()[i];
ExampleMetaData exampleMetaData = new ExampleMetaData();
exampleMetaData.setSyntax(exampleAnnotation.syntax());
exampleMetaData.setDescription(exampleAnnotation.description());
examples[i] = exampleMetaData;
}
extensionMetaData.setExamples(Arrays.asList(examples));
// Finding the namespace
String namespaceName = extensionAnnotation.namespace();
if (Objects.equals(namespaceName, "")) {
namespaceName = Constants.CORE_NAMESPACE;
}
// Finding the relevant namespace in the namespace list
NamespaceMetaData namespace = null;
for (NamespaceMetaData existingNamespace : namespaceList) {
if (Objects.equals(existingNamespace.getName(), namespaceName)) {
namespace = existingNamespace;
break;
}
}
// Creating namespace if it doesn't exist
if (namespace == null) {
namespace = new NamespaceMetaData();
namespace.setName(namespaceName);
namespace.setExtensionMap(new HashMap<>());
namespaceList.add(namespace);
}
// Adding to the relevant extension metadata list in the namespace
List<ExtensionMetaData> extensionMetaDataList = namespace.getExtensionMap()
.computeIfAbsent(extensionType, k -> new ArrayList<>());
extensionMetaDataList.add(extensionMetaData);
}
}
/**
* Generate a file from a template
*
* @param templateFile The template file name
* @param dataModel The data model to be used for generating the output files from template files
* @param outputDirectory The output directory in which the file will be generated
* @param outputFileName The name of the file that will be generated
* @throws MojoFailureException if the Mojo fails to find template file or create new documentation file
*/
private static void generateFileFromTemplate(String templateFile, Map<String, Object> dataModel,
String outputDirectory, String outputFileName)
throws MojoFailureException {
// Creating the free marker configuration
Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setClassForTemplateLoading(
DocumentationUtils.class,
File.separator + Constants.TEMPLATES_DIRECTORY
);
// Adding the constants to the freemarker data model
Map<String, String> constantsClassFieldMap = new HashMap<>();
for (Field field : Constants.class.getDeclaredFields()) {
try {
constantsClassFieldMap.put(field.getName(), field.get(null).toString());
} catch (IllegalAccessException ignored) { // Ignoring inaccessible variables
}
}
dataModel.put("CONSTANTS", constantsClassFieldMap);
// Adding the ExtensionType enum values to the freemarker data model
Map<String, String> extensionTypeEnumMap = new HashMap<>();
for (Field field : ExtensionType.class.getDeclaredFields()) {
try {
extensionTypeEnumMap.put(field.getName(), ((ExtensionType) field.get(null)).getValue());
} catch (IllegalAccessException ignored) { // Ignoring inaccessible variables
}
}
dataModel.put("EXTENSION_TYPE", extensionTypeEnumMap);
try {
// Fetching the template
Template template = cfg.getTemplate(templateFile);
// Generating empty documentation files
File outputFile = new File(outputDirectory + File.separator + outputFileName);
if (!outputFile.getParentFile().exists()) {
if (!outputFile.getParentFile().mkdirs()) {
throw new MojoFailureException("Unable to create directory " + outputFile.getParentFile());
}
}
if (!outputFile.exists()) {
if (!outputFile.createNewFile()) {
throw new MojoFailureException("Unable to create file " + outputFile.getAbsolutePath());
}
}
// Writing to the documentation file
try (OutputStream outputStream = new FileOutputStream(outputFile)) {
try (Writer writer = new OutputStreamWriter(outputStream, Charset.defaultCharset())) {
template.process(dataModel, writer);
}
} catch (TemplateException e) {
throw new MojoFailureException("Invalid Free Marker template found in " + templateFile, e);
}
} catch (IOException e) {
throw new MojoFailureException("Unable to find template file " + templateFile, e);
}
}
/**
* Executing a command
*
* @param command The command to be executed
* @param logger The maven plugin logger
* @throws Throwable if any error occurs during the execution of the command
*/
private static void executeCommand(String[] command, Log logger) throws Throwable {
logger.info("Executing: " + String.join(" ", command));
Process process = Runtime.getRuntime().exec(command);
// Logging the output of the command execution
InputStream[] inputStreams = new InputStream[] {process.getInputStream(), process.getErrorStream()};
BufferedReader bufferedReader = null;
try {
for (InputStream inputStream : inputStreams) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, Constants.DEFAULT_CHARSET));
String commandOutput;
while (true) {
commandOutput = bufferedReader.readLine();
if (commandOutput == null) {
break;
}
logger.info(commandOutput);
}
}
process.waitFor();
} finally {
IOUtils.closeQuietly(bufferedReader);
}
}
}