use of org.apache.camel.maven.packaging.model.ExampleModel in project camel by apache.
the class PrepareExampleMojo method executeExamplesReadme.
protected void executeExamplesReadme() throws MojoExecutionException, MojoFailureException {
Set<File> examples = new TreeSet<>();
// only run in examples directory where the main readme.adoc file is located
String currentDir = Paths.get(".").normalize().toAbsolutePath().toString();
if (!currentDir.endsWith("examples")) {
return;
}
File dir = new File(".");
File[] files = dir.listFiles();
if (files != null) {
examples.addAll(Arrays.asList(files));
}
try {
List<ExampleModel> models = new ArrayList<>();
for (File file : examples) {
if (file.isDirectory() && file.getName().startsWith("camel-example")) {
File pom = new File(file, "pom.xml");
String existing = FileUtils.readFileToString(pom);
ExampleModel model = new ExampleModel();
model.setFileName(file.getName());
String name = StringHelper.between(existing, "<name>", "</name>");
String title = StringHelper.between(existing, "<title>", "</title>");
String description = StringHelper.between(existing, "<description>", "</description>");
String category = StringHelper.between(existing, "<category>", "</category>");
if (title != null) {
model.setTitle(title);
} else {
// fallback and use file name as title
model.setTitle(asTitle(file.getName()));
}
if (description != null) {
model.setDescription(description);
}
if (category != null) {
model.setCategory(category);
}
if (name != null && name.contains("(deprecated)")) {
model.setDeprecated("true");
} else {
model.setDeprecated("false");
}
// readme files is either readme.md or readme.adoc
String[] readmes = new File(file, ".").list((folder, fileName) -> fileName.toLowerCase().startsWith("readme"));
if (readmes != null && readmes.length == 1) {
model.setReadmeFileName(readmes[0]);
}
models.add(model);
}
}
// sort the models
Collections.sort(models, new ExampleComparator());
// how many deprecated
long deprecated = models.stream().filter(m -> "true".equals(m.getDeprecated())).count();
// update the big readme file in the examples dir
File file = new File(".", "README.adoc");
// update regular components
boolean exists = file.exists();
String changed = templateExamples(models, deprecated);
boolean updated = updateExamples(file, changed);
if (updated) {
getLog().info("Updated readme.adoc file: " + file);
} else if (exists) {
getLog().debug("No changes to readme.adoc file: " + file);
} else {
getLog().warn("No readme.adoc file: " + file);
}
} catch (IOException e) {
throw new MojoFailureException("Error due " + e.getMessage(), e);
}
}
Aggregations