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


The following commit(s) were added to refs/heads/develop by this push:
     new 08160d263 js-include-css compiler options
08160d263 is described below

commit 08160d2639e0bd88c705111d8ee49c4f16396183
Author: Josh Tynjala <[email protected]>
AuthorDate: Tue Mar 11 13:59:53 2025 -0700

    js-include-css compiler options
    
    Works with both mxmlc and compc. allows specifying a .css file that will be 
copied to the output directory, and included with a <link> tag in the generated 
HTML
---
 .../codegen/mxml/royale/MXMLRoyalePublisher.java   | 149 +++++++++++++--------
 .../driver/js/goog/JSGoogCompcConfiguration.java   |  30 +++++
 .../driver/js/goog/JSGoogConfiguration.java        |  21 +++
 3 files changed, 147 insertions(+), 53 deletions(-)

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 9cfcafdac..facbc1874 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
@@ -973,10 +973,102 @@ public class MXMLRoyalePublisher extends JSPublisher 
implements IJSRoyalePublish
         return htmlFile.toString();
     }
 
+    private void copyIncludeFileFromSwcToOutput(String type, ISWC swc, String 
swcFileKey, String fileOutputPath, ProblemQuery problems)
+    {
+        ISWCFileEntry swcFileEntry = swc.getFile(swcFileKey);
+        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, 
fileOutputPath), data);
+            }
+            else
+            {
+                final File releaseDir = new File(outputParentFolder, 
ROYALE_RELEASE_DIR_NAME);
+                FileUtils.writeByteArrayToFile(new File(releaseDir, 
fileOutputPath), data);
+            }
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException("Unable to copy file: " + swcFileKey + 
" from library: " + swc.getSWCFile().getAbsolutePath());
+        }
+    }
+
+    private void copyIncludeFileToOutput(String type, String originFilePath, 
String targetOutputPath, ProblemQuery problems)
+    {
+        File scriptFile = new File(originFilePath);
+        if (scriptFile.exists() && !scriptFile.isDirectory())
+        {
+            try
+            {
+                if ("intermediate".equals(type))
+                {
+                    final File intermediateDir = outputFolder;
+                    FileUtils.copyFile(scriptFile, new File(intermediateDir, 
targetOutputPath));
+                }
+                else
+                {
+                    final File releaseDir = new File(outputParentFolder, 
ROYALE_RELEASE_DIR_NAME);
+                    FileUtils.copyFile(scriptFile, new File(releaseDir, 
targetOutputPath));
+                }
+            }
+            catch (IOException e)
+            {
+                throw new RuntimeException("Unable to copy script file: " + 
scriptFile.getAbsolutePath());
+            }
+        }
+        else
+        {
+            problems.add(new FileNotFoundProblem(originFilePath));
+        }
+    }
+
     protected String getTemplateDependencies(String type, String projectName, 
String mainClassQName,
             String deps, ProblemQuery problems)
     {
         StringBuilder depsHTML = new StringBuilder();
+
+        // included CSS appears before included JS scripts
+        // included CSS from SWC libraries appears before included CSS from 
the app
+        for (ISWC swc : project.getLibraries())
+        {
+            for (String key : swc.getFiles().keySet())
+            {
+                if (key.startsWith("js/css") || key.startsWith("js\\css"))
+                {
+                    String cssPath = 
Paths.get("js").relativize(Paths.get(key)).toString();
+
+                    depsHTML.append("\t<link rel=\"stylesheet\" 
type=\"text/css\" href=\"");
+                    depsHTML.append(cssPath);
+                    depsHTML.append("\">\n");
+
+                    copyIncludeFileFromSwcToOutput(type, swc, key, cssPath, 
problems);
+                }
+            }
+        }
+
+        for (String css : googConfiguration.getJSIncludeCss())
+        {
+            String cssOutputPath = 
Paths.get("css").resolve(Paths.get(css).getFileName()).toString();
+            depsHTML.append("\t<link rel=\"stylesheet\" type=\"text/css\" 
href=\"");
+            depsHTML.append(cssOutputPath);
+            depsHTML.append("\">\n");
+
+            copyIncludeFileToOutput(type, css, cssOutputPath, problems);
+        }
+
+        // included JS scripts appear after included CSS
+        // included JS scripts from SWC libraries appear before included JS
+        // scripts from the app
         for (ISWC swc : project.getLibraries())
         {
             for (String key : swc.getFiles().keySet())
@@ -988,68 +1080,19 @@ public class MXMLRoyalePublisher extends JSPublisher 
implements IJSRoyalePublish
                     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());
-                    }
+                    copyIncludeFileFromSwcToOutput(type, swc, key, scriptPath, 
problems);
                 }
             }
         }
 
         for (String script : googConfiguration.getJSIncludeScript())
         {
-            String scriptPath = Paths.get(script).getFileName().toString();
+            String scriptOutputPath = 
Paths.get("scripts").resolve(Paths.get(script).getFileName()).toString();
             depsHTML.append("\t<script type=\"text/javascript\" 
src=\"scripts/");
-            depsHTML.append(scriptPath);
+            depsHTML.append(scriptOutputPath);
             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));
-            }
+            copyIncludeFileToOutput(type, script, scriptOutputPath, problems);
         }
 
         if ("intermediate".equals(type))
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 a75348b2c..8d4883942 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
@@ -570,4 +570,34 @@ public class JSGoogCompcConfiguration extends 
JSConfiguration
         }
     }
 
+    //
+    // 'js-include-css'
+    //
+
+    protected List<String> jsIncludeCss = new ArrayList<String>();
+
+    public List<String> getJSIncludeCss()
+    {   
+        return jsIncludeCss;
+    }
+
+    @Config(allowMultiple = true)
+    @Mapping("js-include-css")
+    @Arguments(Arguments.PATH_ELEMENT)
+    @InfiniteArguments
+    public void setJSIncludeCss(ConfigurationValue cv, List<String> value)
+            throws ConfigurationException
+    {
+        jsIncludeCss.addAll(value);
+        for (String current : value)
+        {
+            String name = "js/css/" + 
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 41d9d87f8..2e0cb2289 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
@@ -1073,4 +1073,25 @@ public class JSGoogConfiguration extends JSConfiguration
         jsIncludeScript.addAll(value);
     }
 
+    //
+    // 'js-include-css'
+    //
+
+    protected List<String> jsIncludeCss = new ArrayList<String>();
+
+    public List<String> getJSIncludeCss()
+    {   
+        return jsIncludeCss;
+    }
+
+    @Config(allowMultiple = true)
+    @Mapping("js-include-css")
+    @Arguments(Arguments.PATH_ELEMENT)
+    @InfiniteArguments
+    public void setJSIncludeCss(ConfigurationValue cv, List<String> value)
+            throws ConfigurationException
+    {
+        jsIncludeCss.addAll(value);
+    }
+
 }

Reply via email to