Search in sources :

Example 1 with ExampleModel

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);
    }
}
Also used : Arrays(java.util.Arrays) PackageHelper.writeText(org.apache.camel.maven.packaging.PackageHelper.writeText) MavenProjectHelper(org.apache.maven.project.MavenProjectHelper) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) TemplateRuntime(org.mvel2.templates.TemplateRuntime) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) TreeSet(java.util.TreeSet) Collections(edu.emory.mathcs.backport.java.util.Collections) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) List(java.util.List) Paths(java.nio.file.Paths) MavenProject(org.apache.maven.project.MavenProject) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) Map(java.util.Map) Comparator(java.util.Comparator) ExampleModel(org.apache.camel.maven.packaging.model.ExampleModel) AbstractMojo(org.apache.maven.plugin.AbstractMojo) ExampleModel(org.apache.camel.maven.packaging.model.ExampleModel) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) TreeSet(java.util.TreeSet) File(java.io.File)

Aggregations

Collections (edu.emory.mathcs.backport.java.util.Collections)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 PackageHelper.loadText (org.apache.camel.maven.packaging.PackageHelper.loadText)1 PackageHelper.writeText (org.apache.camel.maven.packaging.PackageHelper.writeText)1 ExampleModel (org.apache.camel.maven.packaging.model.ExampleModel)1 FileUtils (org.apache.commons.io.FileUtils)1 AbstractMojo (org.apache.maven.plugin.AbstractMojo)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1