This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 8368e5b  SLING-11987 : Allow report into a single file
8368e5b is described below

commit 8368e5b9ce1d252f116d219eb2af393a0d1d7785
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Fri Jul 28 11:42:36 2023 +0200

    SLING-11987 : Allow report into a single file
---
 README.md                                          |  2 +-
 .../maven/mojos/AbstractRepositoryMojo.java        |  1 -
 .../apache/sling/feature/maven/mojos/InfoMojo.java | 87 +++++++++++++++-------
 .../mojos/reports/ExportPackagesReporter.java      |  2 +-
 .../mojos/reports/ImportPackagesReporter.java      |  9 ++-
 5 files changed, 69 insertions(+), 32 deletions(-)

diff --git a/README.md b/README.md
index a731a31..e73956d 100644
--- a/README.md
+++ b/README.md
@@ -479,7 +479,7 @@ With the repository goal, a directory with all artifacts 
from the selected featu
 
 The `info` goal allows to extract information about one or more features and 
generate some reports. It can be used within a Maven project or standalone.
 
-The `reports` configuration should be configured with a comma separated list 
of the reports to be generated. The `outputFormat` configuration can be 
configured with either *log* in which case all output goes to the log or *file* 
(the default) in which case files for each report are generated.
+The `reports` configuration should be configured with a comma separated list 
of the reports to be generated. The `outputFormat` configuration can be 
configured with either *log* in which case all output goes to the log or *file* 
(the default) in which case files for each feature and report are generated or 
*singlefile* in which case a single file for each report across all features is 
generated.
 
 ### Duplicates Report (duplicates)
 
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractRepositoryMojo.java
 
b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractRepositoryMojo.java
index c25d249..51e3435 100644
--- 
a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractRepositoryMojo.java
+++ 
b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractRepositoryMojo.java
@@ -24,7 +24,6 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.io.FileUtils;
-import org.apache.maven.artifact.Artifact;
 import org.apache.maven.model.Dependency;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/InfoMojo.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/InfoMojo.java
index 6d8838f..0dcea13 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/InfoMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/InfoMojo.java
@@ -25,6 +25,8 @@ import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -85,7 +87,7 @@ public class InfoMojo extends AbstractIncludingFeatureMojo {
     private String reports;
 
     /**
-     * Output format, either file or log.
+     * Output format, either file, singlefile or log.
      */
     @Parameter(property = "outputFormat", defaultValue = "file")
     private String outputFormat;
@@ -125,6 +127,11 @@ public class InfoMojo extends AbstractIncludingFeatureMojo 
{
     @Parameter(readonly = true, defaultValue = 
"${project.build.directory}/feature-reports")
     private File buildDirectory;
 
+    public enum OutputFormat {
+        FILE,
+        LOG,
+        SINGLEFILE
+    };
 
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
@@ -144,9 +151,11 @@ public class InfoMojo extends AbstractIncludingFeatureMojo 
{
         if ( isStandalone && infoFeatureFiles == null ) {
             throw new MojoExecutionException("Required configuration for 
standalone execution is missing. Please specify 'infoFeatureFiles'.");
         }
-        boolean outputFile = "file".equals(outputFormat);
-        if ( !outputFile && !"log".equals(outputFormat)) {
-            throw new MojoExecutionException("Invalid value for 
'outputFormat', allowed values are file or log, configured : 
".concat(outputFormat));
+        OutputFormat format = OutputFormat.FILE;
+        try {
+            format = OutputFormat.valueOf(outputFormat.toUpperCase());
+        } catch ( final IllegalArgumentException iae) {
+            throw new MojoExecutionException("Invalid value for 
'outputFormat', allowed values are file, log or singlefile, configured : 
".concat(outputFormat));
         }
 
         final List<Reporter> reporters = getReporters(this.reports);
@@ -166,7 +175,7 @@ public class InfoMojo extends AbstractIncludingFeatureMojo {
         } else {
             matcher = null;
         }
-        final Map<String, List<String>> reports = new LinkedHashMap<>();
+        final Map<String, List<String>> reportsFromSingleReporter = new 
LinkedHashMap<>();
         final ReportContext ctx = new ReportContext() {
 
             @Override
@@ -181,7 +190,7 @@ public class InfoMojo extends AbstractIncludingFeatureMojo {
 
             @Override
             public void addReport(final String key, final List<String> output) 
{
-                reports.put(key, output);
+                reportsFromSingleReporter.put(key, output);
             }
 
             @Override
@@ -189,9 +198,12 @@ public class InfoMojo extends AbstractIncludingFeatureMojo 
{
                 return matcher == null || matcher.matches(id) != null;
             }
         };
+        final Map<String, Map<String, List<String>>> allReports = new 
HashMap<>();
         for(final Reporter reporter : reporters) {
             getLog().info("Generating report 
".concat(reporter.getName().concat("...")));
             reporter.generateReport(ctx);
+            allReports.put(reporter.getName(), new 
HashMap<>(reportsFromSingleReporter));
+            reportsFromSingleReporter.clear();
         }
 
         final File directory;
@@ -203,25 +215,50 @@ public class InfoMojo extends 
AbstractIncludingFeatureMojo {
         } else {
             directory = buildDirectory;
         }
-        if ( outputFile ) {
-            directory.mkdirs();
-        }
-        for(final Map.Entry<String, List<String>> entry : reports.entrySet()) {
-            if ( outputFile ) {
-                try {
-                    final File out = new File(directory, entry.getKey());
-                    getLog().info("Writing " + out + "...");
-                    Files.write(out.toPath(), entry.getValue());
-                } catch (final IOException e) {
-                    throw new MojoExecutionException("Unable to write file: " 
+ e.getMessage(), e);
-                }
-            } else {
-                getLog().info("");
-                getLog().info("Report ".concat(entry.getKey()));
-                
getLog().info("================================================================");
-                entry.getValue().stream().forEach(l -> getLog().info(l));
-                getLog().info("");
-            }
+        switch (format) {
+            case FILE:
+                directory.mkdirs();
+                allReports.values().forEach(map -> {
+                    map.forEach((key, value) -> {
+                        try {
+                            final File out = new File(directory, key);
+                            getLog().info("Writing " + out + "...");
+                            Files.write(out.toPath(), value);
+                        } catch (final IOException e) {
+                            throw new RuntimeException("Unable to write file: 
" + e.getMessage(), e);
+                        }
+                    });
+                });
+                break;
+            case SINGLEFILE:
+                directory.mkdirs();
+                allReports.entrySet().forEach(entry -> {
+                    final List<String> result = new ArrayList<>();
+                    for(final List<String> value : entry.getValue().values()) {
+                        result.addAll(value);
+                    }
+                    Collections.sort(result);
+                    try {
+                        final File out = new File(directory, "report-" + 
entry.getKey() + ".txt");
+                        getLog().info("Writing " + out + "...");
+                        Files.write(out.toPath(), result);
+                    } catch (final IOException e) {
+                        throw new RuntimeException("Unable to write file: " + 
e.getMessage(), e);
+                    }
+                });
+
+                break;
+            case LOG:
+                allReports.values().forEach(map -> {
+                    map.forEach((key, value) -> {
+                        getLog().info("");
+                        getLog().info("Report ".concat(key));
+                        
getLog().info("================================================================");
+                        value.stream().forEach(l -> getLog().info(l));
+                        getLog().info("");
+                    });
+                });
+                break;
         }
     }
 
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/reports/ExportPackagesReporter.java
 
b/src/main/java/org/apache/sling/feature/maven/mojos/reports/ExportPackagesReporter.java
index f65da60..e02b45d 100644
--- 
a/src/main/java/org/apache/sling/feature/maven/mojos/reports/ExportPackagesReporter.java
+++ 
b/src/main/java/org/apache/sling/feature/maven/mojos/reports/ExportPackagesReporter.java
@@ -61,7 +61,7 @@ public class ExportPackagesReporter implements Reporter {
                 if ( version == null ) {
                     version = "----";
                 }
-                packages.add(p.getName().concat("     
").concat(version).concat("    ").concat(bd.getArtifact().getId().toMvnId()));
+                packages.add(p.getName().concat("    
").concat(version).concat("    ").concat(bd.getArtifact().getId().toMvnId()));
             }
         }
 
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/reports/ImportPackagesReporter.java
 
b/src/main/java/org/apache/sling/feature/maven/mojos/reports/ImportPackagesReporter.java
index 72e5f46..c132fdc 100644
--- 
a/src/main/java/org/apache/sling/feature/maven/mojos/reports/ImportPackagesReporter.java
+++ 
b/src/main/java/org/apache/sling/feature/maven/mojos/reports/ImportPackagesReporter.java
@@ -59,11 +59,12 @@ public class ImportPackagesReporter implements Reporter {
             for (final PackageInfo p : bd.getImportedPackages()) {
                 String version = p.getVersion();
                 if ( version == null ) {
-                    version = "";
-                } else {
-                    version = ":".concat(version);
+                    version = "any";
                 }
-                packages.add(p.getName().concat(version).concat(" : 
").concat(bd.getArtifact().getId().toMvnId()));
+                if (p.isOptional()) {
+                    version = version.concat(";optional");
+                }
+                packages.add(p.getName().concat("    
").concat(version).concat("    ").concat(bd.getArtifact().getId().toMvnId()));
             }
         }
 

Reply via email to