You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2020/10/29 17:49:00 UTC

[royale-compiler] branch develop updated: COMPJSC: supports custom source-map-source-root when building .swc files

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 27e4eee  COMPJSC: supports custom source-map-source-root when building .swc files
27e4eee is described below

commit 27e4eee3f60b31533c172fc01a43719e4b2039c9
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Thu Oct 29 10:48:52 2020 -0700

    COMPJSC: supports custom source-map-source-root when building .swc files
---
 .../apache/royale/compiler/clients/COMPJSC.java    | 66 +++++++++++++++++---
 .../royale/compiler/clients/COMPJSCNative.java     | 66 +++++++++++++++++---
 .../royale/compiler/clients/COMPJSCRoyale.java     | 72 ++++++++++++++++++----
 3 files changed, 171 insertions(+), 33 deletions(-)

diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSC.java b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSC.java
index c61356a..e367473 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSC.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSC.java
@@ -26,6 +26,7 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.Charset;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.ZoneId;
@@ -37,6 +38,10 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipOutputStream;
 
+import com.google.debugging.sourcemap.SourceMapConsumerV3;
+import com.google.debugging.sourcemap.SourceMapGeneratorV3;
+import com.google.debugging.sourcemap.SourceMapParseException;
+
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.royale.compiler.clients.problems.ProblemPrinter;
@@ -60,6 +65,7 @@ import org.apache.royale.compiler.problems.UnexpectedExceptionProblem;
 import org.apache.royale.compiler.targets.ITarget.TargetType;
 import org.apache.royale.compiler.targets.ITargetSettings;
 import org.apache.royale.compiler.units.ICompilationUnit;
+import org.apache.royale.compiler.utils.SourceMapUtils;
 import org.apache.royale.swc.ISWCFileEntry;
 import org.apache.royale.swc.io.SWCReader;
 import org.apache.royale.utils.ArgumentUtil;
@@ -489,32 +495,33 @@ public class COMPJSC extends MXMLJSC
 	                        }
                             writer.writeTo(temp, sourceMapTemp, null);
 
-                    		String outputClassFile = getOutputClassFile(
+                    		File outputClassFile = getOutputClassFile(
                                     cu.getQualifiedNames().get(0),
                                     isExterns ? externsOut : jsOut,
-                                    false).getPath();
-                    		outputClassFile = outputClassFile.replace('\\', '/');
+                                    false);
+                            String outputClassFilePath = outputClassFile.getPath();
+                    		outputClassFilePath = outputClassFilePath.replace('\\', '/');
 	                        if (config.isVerbose())
                             {
-                                System.out.println("Writing file: " + outputClassFile);     	
+                                System.out.println("Writing file: " + outputClassFilePath);     	
                             }
 	                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
 	                        temp.writeTo(baos);
-                            writeFileToZip(zipOutputStream, outputClassFile, baos, fileList);
+                            writeFileToZip(zipOutputStream, outputClassFilePath, baos, fileList);
                             if(sourceMapTemp != null)
                             {
-                                String sourceMapFile = getOutputSourceMapFile(
+                                String sourceMapFilePath = getOutputSourceMapFile(
                                     cu.getQualifiedNames().get(0),
                                     isExterns ? externsOut : jsOut,
                                     false).getPath();
-                                sourceMapFile = sourceMapFile.replace('\\', '/');
+                                sourceMapFilePath = sourceMapFilePath.replace('\\', '/');
                                 if (config.isVerbose())
                                 {
-                                    System.out.println("Writing file: " + sourceMapFile);
+                                    System.out.println("Writing file: " + sourceMapFilePath);
                                 }
     	                        baos = new ByteArrayOutputStream();
-                                sourceMapTemp.writeTo(baos);
-                                writeFileToZip(zipOutputStream, sourceMapFile, baos, fileList);
+                                processSourceMap(sourceMapTemp, baos, outputClassFile, symbol);
+                                writeFileToZip(zipOutputStream, sourceMapFilePath, baos, fileList);
                             }
                             writer.close();
                         }
@@ -676,6 +683,45 @@ public class COMPJSC extends MXMLJSC
         return compilationSuccess;
     }
 
+    private void processSourceMap(ByteArrayOutputStream sourceMapTemp, ByteArrayOutputStream baos, File outputClassFile, String symbol)
+    {
+        String sourceMapSourceRoot = project.config.getSourceMapSourceRoot();
+        if(sourceMapSourceRoot != null && sourceMapSourceRoot.length() > 0)
+        {
+            String sourceMapContents = sourceMapTemp.toString(Charset.forName("utf8"));
+            SourceMapConsumerV3 sourceMapConsumer = new SourceMapConsumerV3();
+            try
+            {
+                sourceMapConsumer.parse(sourceMapContents);
+            }
+            catch(SourceMapParseException e)
+            {
+                sourceMapConsumer = null;
+            }
+            if (sourceMapConsumer != null && !sourceMapSourceRoot.equals(sourceMapConsumer.getSourceRoot()))
+            {
+                SourceMapGeneratorV3 sourceMapGenerator = SourceMapUtils.sourceMapConsumerToGeneratorWithRemappedSourceRoot(sourceMapConsumer, sourceMapSourceRoot, symbol);
+                String newSourceMapContents = SourceMapUtils.sourceMapGeneratorToString(sourceMapGenerator, outputClassFile.getName());
+                try
+                {
+                    IOUtils.write(newSourceMapContents, baos, Charset.forName("utf8"));
+                }
+                catch(IOException e)
+                {
+                }
+                return;
+            }
+        }
+        try
+        {
+            sourceMapTemp.writeTo(baos);
+        }
+        catch(IOException e)
+        {
+
+        }
+    }
+
     private void writeFileToZip(ZipOutputStream zipOutputStream, String entryFilePath, ByteArrayOutputStream baos, StringBuilder fileList) throws IOException
     {
         long fileDate = System.currentTimeMillis();
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSCNative.java b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSCNative.java
index 125e93d..7f0f662 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSCNative.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSCNative.java
@@ -26,6 +26,7 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.Charset;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.ZoneId;
@@ -45,6 +46,10 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipOutputStream;
 
+import com.google.debugging.sourcemap.SourceMapConsumerV3;
+import com.google.debugging.sourcemap.SourceMapGeneratorV3;
+import com.google.debugging.sourcemap.SourceMapParseException;
+
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.royale.compiler.clients.problems.ProblemQuery;
@@ -69,6 +74,7 @@ import org.apache.royale.compiler.problems.UnableToBuildSWFProblem;
 import org.apache.royale.compiler.targets.ITarget.TargetType;
 import org.apache.royale.compiler.targets.ITargetSettings;
 import org.apache.royale.compiler.units.ICompilationUnit;
+import org.apache.royale.compiler.utils.SourceMapUtils;
 import org.apache.royale.swc.ISWCFileEntry;
 import org.apache.royale.swc.io.SWCReader;
 
@@ -354,32 +360,33 @@ public class COMPJSCNative extends MXMLJSCNative
 	                        }
                             writer.writeTo(temp, sourceMapTemp, null);
 
-                    		String outputClassFile = getOutputClassFile(
+                    		File outputClassFile = getOutputClassFile(
                                     cu.getQualifiedNames().get(0),
                                     isExterns ? externsOut : jsOut,
-                                    false).getPath();
-                            outputClassFile = outputClassFile.replace('\\', '/');
+                                    false);
+                            String outputClassFilePath = outputClassFile.getPath();
+                            outputClassFilePath = outputClassFilePath.replace('\\', '/');
                             if (config.isVerbose())
                             {
-                                System.out.println("Writing file: " + outputClassFile);     	
+                                System.out.println("Writing file: " + outputClassFilePath);     	
                             }
 	                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
 	                        temp.writeTo(baos);
-                            writeFileToZip(zipOutputStream, outputClassFile, baos, fileList);
+                            writeFileToZip(zipOutputStream, outputClassFilePath, baos, fileList);
                             if(sourceMapTemp != null)
                             {
-                                String sourceMapFile = getOutputSourceMapFile(
+                                String sourceMapFilePath = getOutputSourceMapFile(
                                     cu.getQualifiedNames().get(0),
                                     isExterns ? externsOut : jsOut,
                                     false).getPath();
-                                sourceMapFile = sourceMapFile.replace('\\', '/');
+                                sourceMapFilePath = sourceMapFilePath.replace('\\', '/');
                                 if (config.isVerbose())
                                 {
-                                    System.out.println("Writing file: " + sourceMapFile);
+                                    System.out.println("Writing file: " + sourceMapFilePath);
                                 }
     	                        baos = new ByteArrayOutputStream();
-                                sourceMapTemp.writeTo(baos);
-                                writeFileToZip(zipOutputStream, sourceMapFile, baos, fileList);
+                                processSourceMap(sourceMapTemp, baos, outputClassFile, symbol);
+                                writeFileToZip(zipOutputStream, sourceMapFilePath, baos, fileList);
                             }
                             writer.close();
                         }
@@ -473,6 +480,45 @@ public class COMPJSCNative extends MXMLJSCNative
         return compilationSuccess;
     }
 
+    private void processSourceMap(ByteArrayOutputStream sourceMapTemp, ByteArrayOutputStream baos, File outputClassFile, String symbol)
+    {
+        String sourceMapSourceRoot = project.config.getSourceMapSourceRoot();
+        if(sourceMapSourceRoot != null && sourceMapSourceRoot.length() > 0)
+        {
+            String sourceMapContents = sourceMapTemp.toString(Charset.forName("utf8"));
+            SourceMapConsumerV3 sourceMapConsumer = new SourceMapConsumerV3();
+            try
+            {
+                sourceMapConsumer.parse(sourceMapContents);
+            }
+            catch(SourceMapParseException e)
+            {
+                sourceMapConsumer = null;
+            }
+            if (sourceMapConsumer != null && !sourceMapSourceRoot.equals(sourceMapConsumer.getSourceRoot()))
+            {
+                SourceMapGeneratorV3 sourceMapGenerator = SourceMapUtils.sourceMapConsumerToGeneratorWithRemappedSourceRoot(sourceMapConsumer, sourceMapSourceRoot, symbol);
+                String newSourceMapContents = SourceMapUtils.sourceMapGeneratorToString(sourceMapGenerator, outputClassFile.getName());
+                try
+                {
+                    IOUtils.write(newSourceMapContents, baos, Charset.forName("utf8"));
+                }
+                catch(IOException e)
+                {
+                }
+                return;
+            }
+        }
+        try
+        {
+            sourceMapTemp.writeTo(baos);
+        }
+        catch(IOException e)
+        {
+
+        }
+    }
+
     private void writeFileToZip(ZipOutputStream zipOutputStream, String entryFilePath, ByteArrayOutputStream baos, StringBuilder fileList) throws IOException
     {
         long fileDate = System.currentTimeMillis();
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSCRoyale.java b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSCRoyale.java
index 366de7e..a16bfd5 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSCRoyale.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/COMPJSCRoyale.java
@@ -26,6 +26,7 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.Charset;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.ZoneId;
@@ -37,6 +38,10 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipOutputStream;
 
+import com.google.debugging.sourcemap.SourceMapConsumerV3;
+import com.google.debugging.sourcemap.SourceMapGeneratorV3;
+import com.google.debugging.sourcemap.SourceMapParseException;
+
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.royale.compiler.clients.problems.ProblemQuery;
@@ -62,6 +67,7 @@ import org.apache.royale.compiler.targets.ITarget.TargetType;
 import org.apache.royale.compiler.targets.ITargetSettings;
 import org.apache.royale.compiler.units.ICompilationUnit;
 import org.apache.royale.compiler.units.ICompilationUnit.UnitType;
+import org.apache.royale.compiler.utils.SourceMapUtils;
 import org.apache.royale.swc.ISWCFileEntry;
 import org.apache.royale.swc.io.SWCReader;
 
@@ -375,33 +381,34 @@ public class COMPJSCRoyale extends MXMLJSCRoyale
 	                        }
                             writer.writeTo(temp, sourceMapTemp, null);
 
-                    		String outputClassFile = getOutputClassFile(
+                    		File outputClassFile = getOutputClassFile(
                                     cu.getQualifiedNames().get(0),
                                     isExterns ? externsOut : jsOut,
-                                    false).getPath();
-                    		outputClassFile = outputClassFile.replace('\\', '/');
+                                    false);
+                            String outputClassFilePath = outputClassFile.getPath();
+                    		outputClassFilePath = outputClassFilePath.replace('\\', '/');
 	                        if (config.isVerbose())
                             {
-                                System.out.println("Writing file: " + outputClassFile);     	
+                                System.out.println("Writing file: " + outputClassFilePath);     	
                             }
 	                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                             temp.writeTo(baos);
-                            writeFileToZip(zipOutputStream, outputClassFile, baos, fileList);
+                            writeFileToZip(zipOutputStream, outputClassFilePath, baos, fileList);
                             
                             if(sourceMapTemp != null)
                             {
-                                String sourceMapFile = getOutputSourceMapFile(
-                                                                                cu.getQualifiedNames().get(0),
-                                                                                isExterns ? externsOut : jsOut,
-                                                                                false).getPath();
-                                sourceMapFile = sourceMapFile.replace('\\', '/');
+                                String sourceMapFilePath = getOutputSourceMapFile(
+                                    cu.getQualifiedNames().get(0),
+                                    isExterns ? externsOut : jsOut,
+                                    false).getPath();
+                                sourceMapFilePath = sourceMapFilePath.replace('\\', '/');
                                 if (config.isVerbose())
                                 {
-                                    System.out.println("Writing file: " + sourceMapFile);
+                                    System.out.println("Writing file: " + sourceMapFilePath);
                                 }
                                 baos = new ByteArrayOutputStream();
-                                sourceMapTemp.writeTo(baos);
-                                writeFileToZip(zipOutputStream, sourceMapFile, baos, fileList);
+                                processSourceMap(sourceMapTemp, baos, outputClassFile, symbol);
+                                writeFileToZip(zipOutputStream, sourceMapFilePath, baos, fileList);
                             }
 	                        writer.close();
                     	}
@@ -540,6 +547,45 @@ public class COMPJSCRoyale extends MXMLJSCRoyale
         return compilationSuccess;
     }
 
+    private void processSourceMap(ByteArrayOutputStream sourceMapTemp, ByteArrayOutputStream baos, File outputClassFile, String symbol)
+    {
+        String sourceMapSourceRoot = project.config.getSourceMapSourceRoot();
+        if(sourceMapSourceRoot != null && sourceMapSourceRoot.length() > 0)
+        {
+            String sourceMapContents = sourceMapTemp.toString(Charset.forName("utf8"));
+            SourceMapConsumerV3 sourceMapConsumer = new SourceMapConsumerV3();
+            try
+            {
+                sourceMapConsumer.parse(sourceMapContents);
+            }
+            catch(SourceMapParseException e)
+            {
+                sourceMapConsumer = null;
+            }
+            if (sourceMapConsumer != null && !sourceMapSourceRoot.equals(sourceMapConsumer.getSourceRoot()))
+            {
+                SourceMapGeneratorV3 sourceMapGenerator = SourceMapUtils.sourceMapConsumerToGeneratorWithRemappedSourceRoot(sourceMapConsumer, sourceMapSourceRoot, symbol);
+                String newSourceMapContents = SourceMapUtils.sourceMapGeneratorToString(sourceMapGenerator, outputClassFile.getName());
+                try
+                {
+                    IOUtils.write(newSourceMapContents, baos, Charset.forName("utf8"));
+                }
+                catch(IOException e)
+                {
+                }
+                return;
+            }
+        }
+        try
+        {
+            sourceMapTemp.writeTo(baos);
+        }
+        catch(IOException e)
+        {
+
+        }
+    }
+
     private void writeFileToZip(ZipOutputStream zipOutputStream, String entryFilePath, ByteArrayOutputStream baos, StringBuilder fileList) throws IOException
     {
         long fileDate = System.currentTimeMillis();