You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2020/03/11 17:14:23 UTC

[sling-scriptingbundle-maven-plugin] branch master updated: SLING-9197 - Align script file name analysis to the Sling conventions

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

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


The following commit(s) were added to refs/heads/master by this push:
     new f260104  SLING-9197 - Align script file name analysis to the Sling conventions
f260104 is described below

commit f260104a17a7d139c99cb83e96d02bb77da983e7
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Wed Mar 11 18:13:15 2020 +0100

    SLING-9197 - Align script file name analysis to the Sling conventions
---
 .../maven/plugin/ResourceTypeFolderAnalyser.java   | 97 ++++++++++------------
 .../maven/plugin/MetadataMojoTest.java             | 20 ++++-
 .../{GET.txt.html => test.GET.html}                |  0
 .../{GET.test.html => test.txt.GET.html}           |  0
 4 files changed, 58 insertions(+), 59 deletions(-)

diff --git a/src/main/java/org/apache/sling/scriptingbundle/maven/plugin/ResourceTypeFolderAnalyser.java b/src/main/java/org/apache/sling/scriptingbundle/maven/plugin/ResourceTypeFolderAnalyser.java
index 7cd6918..1e6645d 100644
--- a/src/main/java/org/apache/sling/scriptingbundle/maven/plugin/ResourceTypeFolderAnalyser.java
+++ b/src/main/java/org/apache/sling/scriptingbundle/maven/plugin/ResourceTypeFolderAnalyser.java
@@ -182,69 +182,56 @@ class ResourceTypeFolderAnalyser {
                 }
             }
             String[] parts = scriptFile.toString().split("\\.");
-            if (parts.length >= 2 && parts.length <= 4) {
-                String method = null;
+            if (parts.length < 2 || parts.length > 4) {
+                log.warn(String.format("Skipping script %s since it either does not target a script engine or it provides too many " +
+                        "selector parts in its name.", script.toString()));
+                return;
+            }
+            String name = parts[0];
+            String scriptEngineExtension = parts[parts.length - 1];
+            String scriptEngine = scriptEngineMappings.get(scriptEngineExtension);
+            if (StringUtils.isNotEmpty(scriptEngine)) {
                 String requestExtension = null;
-                String scriptExtension = parts[parts.length - 1];
-                String first = parts[0];
-                if (!first.equals(resourceTypeLabel)) {
-                    if (MetadataMojo.METHODS.contains(first)) {
-                        // method script
-                        method = first;
-                        if ("HEAD".equals(method) || "GET".equals(method)) {
-                            if (parts.length == 4) {
-                                selectors.add(parts[1]);
-                                requestExtension = parts[2];
-                            } else if (parts.length == 3) {
-                                String middlePart = parts[1];
-                                if (MimeTypeChecker.hasMimeType(middlePart)) {
-                                    requestExtension = middlePart;
-                                } else {
-                                    selectors.add(middlePart);
-                                }
-                            }
-                        } else {
-                            if (parts.length != 2) {
-                                log.warn(String.format("Script %s doesn't respect naming conventions. Only GET and HEAD scripts are " +
-                                        "allowed to use additional selectors or request extensions.", script.toString()));
-                                return;
-                            }
-                        }
+                String requestMethod = null;
+                if (parts.length == 2 && !resourceTypeLabel.equals(name)) {
+                    if (MetadataMojo.METHODS.contains(name)) {
+                        requestMethod = name;
+                    } else if (MimeTypeChecker.hasMimeType(name)) {
+                        requestExtension = name;
                     } else {
-                        // selector script
-                        if (parts.length == 3) {
-                            selectors.add(first);
-                            requestExtension = parts[1];
-                        } else if (parts.length == 2) {
-                            selectors.add(first);
-                        } else {
-                            log.warn(String.format("Script %s doesn't respect naming conventions. A selector script can contain at max " +
-                                    "the request extension, but no other selectors."));
-                            return;
-                        }
+                        selectors.add(name);
                     }
-                } else {
-                    // main script
-                    if (parts.length > 3) {
-                        log.warn(String.format("Script %s doesn't respect naming conventions. The main script can contain at max " +
-                                "the request extension, but no other selectors."));
-                        return;
+                }
+                if (parts.length == 3) {
+                    String middle = parts[1];
+                    if (MetadataMojo.METHODS.contains(middle)) {
+                        requestMethod = middle;
+                    } else if (MimeTypeChecker.hasMimeType(middle)) {
+                        requestExtension = middle;
                     }
-                    if (parts.length == 3) {
-                        requestExtension = parts[parts.length - 2];
+                    if (!resourceTypeLabel.equals(name)) {
+                        selectors.add(name);
                     }
                 }
-                String scriptEngine = scriptEngineMappings.get(scriptExtension);
-                if (StringUtils.isNotEmpty(scriptEngine)) {
-                    providedCapabilities.add(ProvidedCapability.builder().withResourceType(resourceType).withScriptEngine(scriptEngine)
-                            .withVersion(version).withSelectors(selectors).withRequestExtension(requestExtension).withRequestMethod(method)
-                            .build());
-                } else {
-                    log.warn(String.format("Unknown script engine mapping for script %s. Skipping.", scriptFile.toString()));
+                if (parts.length == 4){
+                    requestExtension = parts[1];
+                    requestMethod = parts[2];
+                    if (!resourceTypeLabel.equals(name)) {
+                        selectors.add(name);
+                    }
                 }
+                providedCapabilities.add(
+                        ProvidedCapability.builder()
+                                .withResourceType(resourceType)
+                                .withVersion(version)
+                                .withSelectors(selectors)
+                                .withRequestExtension(requestExtension)
+                                .withRequestMethod(requestMethod)
+                                .withScriptEngine(scriptEngine)
+                                .build()
+                );
             } else {
-                log.warn(String.format("Skipping script %s since it either does not target a script engine or it provides too many " +
-                        "selector parts in its name.", script.toString()));
+                log.warn(String.format("Cannot find a script engine mapping for script %s.", script.toString()));
             }
         } else {
             log.warn(String.format("Skipping path %s since it has 0 elements.", script.toString()));
diff --git a/src/test/java/org/apache/sling/scriptingbundle/maven/plugin/MetadataMojoTest.java b/src/test/java/org/apache/sling/scriptingbundle/maven/plugin/MetadataMojoTest.java
index 0ed437f..b8f9f97 100644
--- a/src/test/java/org/apache/sling/scriptingbundle/maven/plugin/MetadataMojoTest.java
+++ b/src/test/java/org/apache/sling/scriptingbundle/maven/plugin/MetadataMojoTest.java
@@ -34,7 +34,7 @@ import org.junit.Test;
 import org.osgi.framework.VersionRange;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 public class MetadataMojoTest {
 
@@ -91,7 +91,7 @@ public class MetadataMojoTest {
                         , "depth2", "100")).build(),
                 ProvidedCapability.builder().withResourceType("org.apache.sling.foobar").withScriptEngine("htl").withRequestMethod("GET").build(),
                 ProvidedCapability.builder().withResourceType("org.apache.sling.foobar").withScriptEngine("htl").withRequestMethod("GET").withSelectors(Arrays.asList("test")).build(),
-                ProvidedCapability.builder().withResourceType("org.apache.sling.foobar").withScriptEngine("htl").withRequestMethod("GET").withRequestExtension("txt").build(),
+                ProvidedCapability.builder().withResourceType("org.apache.sling.foobar").withScriptEngine("htl").withRequestMethod("GET").withSelectors(Arrays.asList("test")).withRequestExtension("txt").build(),
                 ProvidedCapability.builder().withResourceType("org.apache.sling.foobar").withScriptEngine("htl").withSelectors(Arrays.asList("test")).withRequestExtension("txt").build(),
 
                 // sling
@@ -99,9 +99,15 @@ public class MetadataMojoTest {
         ));
         Set<ProvidedCapability> provided = new HashSet<>(capabilities.getProvidedCapabilities());
         assertEquals(pExpected.size(), provided.size());
+        StringBuilder missingProvided = new StringBuilder();
         for (ProvidedCapability capability : pExpected) {
             boolean removed = provided.remove(capability);
-            assertTrue(String.format("Did not find expected provided capability %s.", capability), removed);
+            if (!removed) {
+                missingProvided.append("Missing capability: ").append(capability.toString()).append(System.lineSeparator());
+            }
+        }
+        if (missingProvided.length() > 0) {
+            fail(missingProvided.toString());
         }
 
         Set<RequiredCapability> rExpected = new HashSet<>(Arrays.asList(
@@ -111,9 +117,15 @@ public class MetadataMojoTest {
         ));
         Set<RequiredCapability> required = new HashSet<>(capabilities.getRequiredCapabilities());
         assertEquals(rExpected.size(), required.size());
+        StringBuilder missingRequired = new StringBuilder();
         for (RequiredCapability capability : rExpected) {
             boolean removed = required.remove(capability);
-            assertTrue(String.format("Did not find expected required capability %s.", capability), removed);
+            if (!removed) {
+                missingRequired.append("Missing required capability: ").append(capability.toString()).append(System.lineSeparator());
+            }
+        }
+        if (missingRequired.length() > 0) {
+            fail(missingRequired.toString());
         }
     }
 
diff --git a/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.txt.html b/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/test.GET.html
similarity index 100%
rename from src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.txt.html
rename to src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/test.GET.html
diff --git a/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.test.html b/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/test.txt.GET.html
similarity index 100%
rename from src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.test.html
rename to src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/test.txt.GET.html