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

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git

commit 6a6d5790a9eaa322152e6ccace3125191e32f660
Author: Josh Tynjala <[email protected]>
AuthorDate: Tue Jan 21 16:15:41 2025 -0800

    js-include-script compiler options
    
    Works with both mxmlc and compc. allows specifying a .js file that will be 
copied to the output directory, and included with a <script> tag in the 
generated HTML
---
 .../internal/codegen/js/node/NodePublisher.java    |  8 +-
 .../codegen/mxml/royale/MXMLRoyalePublisher.java   | 91 ++++++++++++++++++++--
 .../driver/js/goog/JSGoogCompcConfiguration.java   | 31 ++++++++
 .../driver/js/goog/JSGoogConfiguration.java        | 22 ++++++
 4 files changed, 143 insertions(+), 9 deletions(-)

diff --git 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/node/NodePublisher.java
 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/node/NodePublisher.java
index 3c2eb6f1c..73dd32923 100644
--- 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/node/NodePublisher.java
+++ 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/node/NodePublisher.java
@@ -19,6 +19,7 @@
 
 package org.apache.royale.compiler.internal.codegen.js.node;
 
+import org.apache.royale.compiler.clients.problems.ProblemQuery;
 import org.apache.royale.compiler.config.Configuration;
 import org.apache.royale.compiler.internal.codegen.js.jsc.JSCPublisher;
 import org.apache.royale.compiler.internal.projects.RoyaleJSProject;
@@ -37,7 +38,8 @@ public class NodePublisher extends JSCPublisher
     }
 
     @Override
-    protected String getTemplateDependencies(String type, String projectName, 
String mainClassQName, String deps)
+    protected String getTemplateDependencies(String type, String projectName, 
String mainClassQName,
+            String deps, ProblemQuery problems)
     {
         StringBuilder depsJS = new StringBuilder();
         if ("intermediate".equals(type))
@@ -82,10 +84,10 @@ public class NodePublisher extends JSCPublisher
 
     @Override
     protected void writeHTML(String type, String projectName, String 
mainClassQName, File targetDir,
-                             String deps, List<String> additionalHTML) throws 
IOException
+                             String deps, List<String> additionalHTML, 
ProblemQuery problems) throws IOException
     {
         StringBuilder contents = new StringBuilder();
-        contents.append(getTemplateDependencies(type, projectName, 
mainClassQName, deps));
+        contents.append(getTemplateDependencies(type, projectName, 
mainClassQName, deps, problems));
         contents.append(getTemplateBody(mainClassQName));
         writeFile(new File(targetDir, "index.js"), contents.toString(), false);
     }
diff --git 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/mxml/royale/MXMLRoyalePublisher.java
 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/mxml/royale/MXMLRoyalePublisher.java
index a72075ec1..9cfcafdac 100644
--- 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/mxml/royale/MXMLRoyalePublisher.java
+++ 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/mxml/royale/MXMLRoyalePublisher.java
@@ -47,6 +47,7 @@ import 
org.apache.royale.compiler.internal.graph.GoogDepsWriter;
 import org.apache.royale.compiler.internal.projects.RoyaleJSProject;
 import 
org.apache.royale.compiler.internal.scopes.ASProjectScope.DefinitionPromise;
 import org.apache.royale.compiler.internal.targets.ITargetAttributes;
+import org.apache.royale.compiler.problems.FileNotFoundProblem;
 import org.apache.royale.compiler.problems.HTMLTemplateFileNotFoundProblem;
 import org.apache.royale.compiler.utils.JSClosureCompilerWrapper;
 import org.apache.royale.swc.ISWC;
@@ -57,6 +58,7 @@ import java.io.*;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.nio.charset.Charset;
+import java.nio.file.Paths;
 import java.util.*;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
@@ -567,7 +569,7 @@ public class MXMLRoyalePublisher extends JSPublisher 
implements IJSRoyalePublish
                    if (template != null) {
                        writeTemplate(template, "intermediate", projectName, 
mainClassQName, intermediateDir, depsFileData, wrappedScript, problems);
                    } else {
-                       writeHTML("intermediate", projectName, mainClassQName, 
intermediateDir, depsFileData, wrappedScript);
+                       writeHTML("intermediate", projectName, mainClassQName, 
intermediateDir, depsFileData, wrappedScript, problems);
                    }
                }
                // Create the index.html for the release-js version.
@@ -575,7 +577,7 @@ public class MXMLRoyalePublisher extends JSPublisher 
implements IJSRoyalePublish
                    if (template != null) {
                        writeTemplate(template, "release", projectName, 
mainClassQName, releaseDir, depsFileData, wrappedScript, problems);
                    } else {
-                       writeHTML("release", projectName, mainClassQName, 
releaseDir, null, wrappedScript);
+                       writeHTML("release", projectName, mainClassQName, 
releaseDir, null, wrappedScript, problems);
                    }
                }
         }        
@@ -952,7 +954,7 @@ public class MXMLRoyalePublisher extends JSPublisher 
implements IJSRoyalePublish
 
         StringBuilder addHTML = new StringBuilder();
         addHTML.append(getTemplateAdditionalHTML(additionalHTML));
-               addHTML.append(getTemplateDependencies(type, projectName, 
mainClassQName, deps));
+               addHTML.append(getTemplateDependencies(type, projectName, 
mainClassQName, deps, problems));
         result = result.replaceAll("\\$\\{head\\}", 
safeReplacement(addHTML.toString()));
 
         String templateBody = getTemplateBody("release".equals(type) ? 
projectName : mainClassQName);
@@ -971,9 +973,85 @@ public class MXMLRoyalePublisher extends JSPublisher 
implements IJSRoyalePublish
         return htmlFile.toString();
     }
 
-    protected String getTemplateDependencies(String type, String projectName, 
String mainClassQName, String deps)
+    protected String getTemplateDependencies(String type, String projectName, 
String mainClassQName,
+            String deps, ProblemQuery problems)
     {
         StringBuilder depsHTML = new StringBuilder();
+        for (ISWC swc : project.getLibraries())
+        {
+            for (String key : swc.getFiles().keySet())
+            {
+                if (key.startsWith("js/scripts") || 
key.startsWith("js\\scripts"))
+                {
+                    String scriptPath = 
Paths.get("js").relativize(Paths.get(key)).toString();
+                    depsHTML.append("\t<script type=\"text/javascript\" 
src=\"");
+                    depsHTML.append(scriptPath);
+                    depsHTML.append("\"></script>\n");
+
+                    ISWCFileEntry swcFileEntry = swc.getFile(key);
+                    try
+                    {
+                        InputStream is = swcFileEntry.createInputStream();
+                        int n = is.available();
+                        int total = 0;
+                        byte[] data = new byte[n];
+                        while (total < n)
+                        {
+                            total += is.read(data, total, n - total);
+                        }
+                        if ("intermediate".equals(type))
+                        {
+                            final File intermediateDir = outputFolder;
+                            FileUtils.writeByteArrayToFile(new 
File(intermediateDir, scriptPath), data);
+                        }
+                        else
+                        {
+                            final File releaseDir = new 
File(outputParentFolder, ROYALE_RELEASE_DIR_NAME);
+                            FileUtils.writeByteArrayToFile(new 
File(releaseDir, scriptPath), data);
+                        }
+                    }
+                    catch (IOException e)
+                    {
+                        throw new RuntimeException("Unable to copy script 
file: " + key + " from library: " + swc.getSWCFile().getAbsolutePath());
+                    }
+                }
+            }
+        }
+
+        for (String script : googConfiguration.getJSIncludeScript())
+        {
+            String scriptPath = Paths.get(script).getFileName().toString();
+            depsHTML.append("\t<script type=\"text/javascript\" 
src=\"scripts/");
+            depsHTML.append(scriptPath);
+            depsHTML.append("\"></script>\n");
+
+            File scriptFile = new File(script);
+            if (scriptFile.exists() && !scriptFile.isDirectory())
+            {
+                try
+                {
+                    if ("intermediate".equals(type))
+                    {
+                        final File intermediateDir = outputFolder;
+                        FileUtils.copyFile(scriptFile, new 
File(intermediateDir, "scripts" + File.separator + scriptPath));
+                    }
+                    else
+                    {
+                        final File releaseDir = new File(outputParentFolder, 
ROYALE_RELEASE_DIR_NAME);
+                        FileUtils.copyFile(scriptFile, new File(releaseDir, 
"scripts" + File.separator + scriptPath));
+                    }
+                }
+                catch (IOException e)
+                {
+                    throw new RuntimeException("Unable to copy script file: " 
+ scriptFile.getAbsolutePath());
+                }
+            }
+            else
+            {
+                problems.add(new FileNotFoundProblem(script));
+            }
+        }
+
         if ("intermediate".equals(type))
         {
             depsHTML.append("\t<script type=\"text/javascript\" 
src=\"./library/closure/goog/base.js\"></script>\n");
@@ -1005,7 +1083,8 @@ public class MXMLRoyalePublisher extends JSPublisher 
implements IJSRoyalePublish
         return bodyHTML.toString();
     }
 
-    protected void writeHTML(String type, String projectName, String 
mainClassQName, File targetDir, String deps, List<String> additionalHTML)
+    protected void writeHTML(String type, String projectName, String 
mainClassQName, File targetDir,
+        String deps, List<String> additionalHTML, ProblemQuery problems)
             throws IOException
     {
         String htmlOutputFileName = googConfiguration.getHtmlOutputFileName();
@@ -1048,7 +1127,7 @@ public class MXMLRoyalePublisher extends JSPublisher 
implements IJSRoyalePublish
         }
 
         htmlFile.append(getTemplateAdditionalHTML(additionalHTML));
-        htmlFile.append(getTemplateDependencies(type, projectName, 
mainClassQName, deps));
+        htmlFile.append(getTemplateDependencies(type, projectName, 
mainClassQName, deps, problems));
 
         htmlFile.append("</head>\n");
         htmlFile.append("<body>\n");
diff --git 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/driver/js/goog/JSGoogCompcConfiguration.java
 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/driver/js/goog/JSGoogCompcConfiguration.java
index 2a248fd48..a75348b2c 100644
--- 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/driver/js/goog/JSGoogCompcConfiguration.java
+++ 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/driver/js/goog/JSGoogCompcConfiguration.java
@@ -22,6 +22,7 @@ package org.apache.royale.compiler.internal.driver.js.goog;
 import java.io.File;
 import java.io.IOException;
 import java.net.URLDecoder;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
@@ -539,4 +540,34 @@ public class JSGoogCompcConfiguration extends 
JSConfiguration
         jsSetterPrefix = value;
     }
 
+    //
+    // 'js-include-script'
+    //
+
+    protected List<String> jsIncludeScript = new ArrayList<String>();
+
+    public List<String> getJSIncludeScript()
+    {   
+        return jsIncludeScript;
+    }
+
+    @Config(allowMultiple = true)
+    @Mapping("js-include-script")
+    @Arguments(Arguments.PATH_ELEMENT)
+    @InfiniteArguments
+    public void setJSIncludeScript(ConfigurationValue cv, List<String> value)
+            throws ConfigurationException
+    {
+        jsIncludeScript.addAll(value);
+        for (String current : value)
+        {
+            String name = "js/scripts/" + 
Paths.get(current).getFileName().toString();
+            if (includeFilesNamePath.containsKey(name))
+            {
+                throw new ConfigurationException.RedundantFile(name, 
cv.getVar(), cv.getSource(), cv.getLine());
+            }
+            includeFilesNamePath.put(name, current);
+        }
+    }
+
 }
diff --git 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/driver/js/goog/JSGoogConfiguration.java
 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/driver/js/goog/JSGoogConfiguration.java
index 85034ab2b..41d9d87f8 100644
--- 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/driver/js/goog/JSGoogConfiguration.java
+++ 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/driver/js/goog/JSGoogConfiguration.java
@@ -22,6 +22,7 @@ package org.apache.royale.compiler.internal.driver.js.goog;
 import java.io.File;
 import java.io.IOException;
 import java.net.URLDecoder;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
@@ -1051,4 +1052,25 @@ public class JSGoogConfiguration extends JSConfiguration
         jsSetterPrefix = value;
     }
 
+    //
+    // 'js-include-script'
+    //
+
+    protected List<String> jsIncludeScript = new ArrayList<String>();
+
+    public List<String> getJSIncludeScript()
+    {   
+        return jsIncludeScript;
+    }
+
+    @Config(allowMultiple = true)
+    @Mapping("js-include-script")
+    @Arguments(Arguments.PATH_ELEMENT)
+    @InfiniteArguments
+    public void setJSIncludeScript(ConfigurationValue cv, List<String> value)
+            throws ConfigurationException
+    {
+        jsIncludeScript.addAll(value);
+    }
+
 }

Reply via email to