You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by ah...@apache.org on 2018/10/14 21:22:39 UTC

[royale-compiler] 03/03: JS support for old Flex ResourceManager

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

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

commit 00d6308f3f3d5a6f2941731412a9b3dcf864a1c4
Author: Alex Harui <ah...@apache.org>
AuthorDate: Sun Oct 14 14:22:07 2018 -0700

    JS support for old Flex ResourceManager
---
 .../royale/compiler/clients/MXMLJSCRoyale.java     | 113 ++++++++++++++++++++-
 .../codegen/mxml/royale/MXMLRoyaleEmitter.java     |  55 ++++++++++
 .../internal/projects/RoyaleJSProject.java         |   3 +
 3 files changed, 170 insertions(+), 1 deletion(-)

diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyale.java b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyale.java
index 2b54fe8..475082f 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyale.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyale.java
@@ -20,15 +20,20 @@
 package org.apache.royale.compiler.clients;
 
 import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 
@@ -59,6 +64,7 @@ import org.apache.royale.compiler.internal.projects.RoyaleJSProject;
 import org.apache.royale.compiler.internal.projects.ISourceFileHandler;
 import org.apache.royale.compiler.internal.targets.RoyaleJSTarget;
 import org.apache.royale.compiler.internal.targets.JSTarget;
+import org.apache.royale.compiler.internal.units.ResourceBundleCompilationUnit;
 import org.apache.royale.compiler.internal.units.ResourceModuleCompilationUnit;
 import org.apache.royale.compiler.internal.units.SourceCompilationUnitFactory;
 import org.apache.royale.compiler.internal.workspaces.Workspace;
@@ -74,6 +80,9 @@ 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.flex.tools.FlexTool;
+import org.apache.royale.swc.ISWC;
+import org.apache.royale.swc.ISWCFileEntry;
+import org.apache.royale.swc.ISWCManager;
 import org.apache.royale.utils.ArgumentUtil;
 import org.apache.royale.utils.FilenameNormalization;
 
@@ -336,6 +345,15 @@ public class MXMLJSCRoyale implements JSCompilerEntryPoint, ProblemQueryProvider
 	                List<ICompilationUnit> reachableCompilationUnits = project.getReachableCompilationUnitsInSWFOrder(roots);
 	                ((RoyaleJSTarget)target).collectMixinMetaData(project.mixinClassNames, reachableCompilationUnits);
 	                ((RoyaleJSTarget)target).collectRemoteClassMetaData(project.remoteClassAliasMap, reachableCompilationUnits);
+	                // run through looking for resource bundles so we can have the list ready for the mainCU in the second loop
+	                for (final ICompilationUnit cu : reachableCompilationUnits)
+	                {
+	                    ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
+	                    if (cuType == ICompilationUnit.UnitType.RESOURCE_UNIT)
+	                    {
+	                    	outputResourceBundle((ResourceBundleCompilationUnit)cu, outputFolder);
+	                    }
+	                }
 	                for (final ICompilationUnit cu : reachableCompilationUnits)
 	                {
 	                    ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
@@ -402,7 +420,100 @@ public class MXMLJSCRoyale implements JSCompilerEntryPoint, ProblemQueryProvider
         return compilationSuccess && (errs.size() == 0);
     }
 
-    /**
+    private void outputResourceBundle(ResourceBundleCompilationUnit cu, File outputFolder) {
+		// TODO Auto-generated method stub
+        final ISWCManager swcManager = project.getWorkspace().getSWCManager();
+        // find the SWC
+        final ISWC swc = swcManager.get(new File(cu.getAbsoluteFilename()));
+        if (swc != null)
+        {
+        	String bundleName = cu.getBundleNameInColonSyntax();
+        	String propFileName = "locale/" + cu.getLocale() + "/" + bundleName + ".properties";
+        	String bundleClassName = cu.getLocale() + "$" + bundleName + "_properties";
+            Map<String, ISWCFileEntry> files = swc.getFiles();
+            for (String key : files.keySet())
+            {
+                if (key.equals(propFileName))
+                {
+                	if (!project.compiledResourceBundleNames.contains(bundleName))
+                		project.compiledResourceBundleNames.add(bundleName);
+                	project.compiledResourceBundleClasses.add(bundleClassName);
+                	StringBuilder sb = new StringBuilder();
+                    ISWCFileEntry fileEntry = swc.getFile(key);
+                    if (fileEntry != null)
+                    {
+                        try {
+							InputStream is = fileEntry.createInputStream();
+							BufferedReader br = new BufferedReader(new InputStreamReader(is));
+							String line;
+							while ((line = br.readLine()) != null)
+							{
+								if (line.contains("="))
+								{
+									if (sb.length() == 0)
+									{
+										sb.append("/**\n");
+										sb.append(" * Generated by Apache Royale Compiler from " + bundleClassName + ".properties\n");
+										sb.append(" * " + bundleClassName + "\n");
+										sb.append(" *\n");
+										sb.append(" * @fileoverview\n");
+										sb.append(" *\n");
+										sb.append(" * @suppress {checkTypes|accessControls}\n");
+										sb.append(" */\n\n");
+										sb.append("goog.provide('" + bundleClassName + "');\n\n");
+										sb.append("goog.require('mx.resources.IResourceBundle');\n");
+										sb.append("goog.require('mx.resources.ResourceBundle');\n\n\n");
+										sb.append("/**\n");
+										sb.append(" * @constructor\n");
+										sb.append(" * @extends {mx.resources.ResourceBundle}\n");
+										sb.append(" * @implements {mx.resources.IResourceBundle}\n");
+										sb.append(" */\n");
+										sb.append(bundleClassName + " = function() {\n");
+										sb.append("    " + bundleClassName + ".base(this, 'constructor');\n");
+										sb.append("};\n");
+										sb.append("goog.inherits(" + bundleClassName + ", mx.resources.ResourceBundle);\n\n");
+										sb.append("/**\n");
+										sb.append(" * Prevent renaming of class. Needed for reflection.\n");
+										sb.append(" */\n");
+										sb.append("goog.exportSymbol('" + bundleClassName + "', " + bundleClassName + ");\n\n");
+										sb.append(bundleClassName + ".prototype.getContent = function() { return {\n");
+									}
+									int c = line.indexOf("=");
+									String propName = line.substring(0, c);
+									String value = line.substring(c + 1);
+									while (value.endsWith("/"))
+									{
+										value = value.substring(0, value.length() - 1);
+										value += br.readLine();
+									}
+									sb.append(propName + ": \"" + value + "\",\n");
+								}
+							}
+							sb.append("__end_of_bundle__: 0\n};};\n");
+						} catch (IOException e) {
+							// TODO Auto-generated catch block
+							e.printStackTrace();
+						}
+                    }
+                    final File outputClassFile = getOutputClassFile(
+                            bundleClassName, outputFolder);
+                    System.out.println("Generating resource file: " + outputClassFile);
+                    FileWriter fw;
+					try {
+						fw = new FileWriter(outputClassFile, false);
+	                    fw.write(sb.toString());
+	                    fw.close();
+					} catch (IOException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					}
+                }
+            }
+        }
+
+	}
+
+	/**
      * Build target artifact.
      * 
      * @throws InterruptedException threading error
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/mxml/royale/MXMLRoyaleEmitter.java b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/mxml/royale/MXMLRoyaleEmitter.java
index 1fa01e3..0634768 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/mxml/royale/MXMLRoyaleEmitter.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/mxml/royale/MXMLRoyaleEmitter.java
@@ -27,6 +27,7 @@ import static org.apache.royale.abc.ABCConstants.OP_pushtrue;
 import java.io.File;
 import java.io.FilterWriter;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -357,6 +358,60 @@ public class MXMLRoyaleEmitter extends MXMLEmitter implements
 		            	infoInject += aliasInject;
 		            	sep = ",\n";
 	            	}
+	                Collection<String> locales = royaleProject.getLocales();
+	                if (locales.size() > 0)
+	                {
+		            	String localeInject = sep + "compiledLocales: [";
+		            	boolean firstOne = true;
+		            	String[] localeNames = new String[locales.size()];
+		            	locales.toArray(localeNames);
+		            	for (String locale : localeNames)
+		            	{
+		            		if (!firstOne)
+		            			localeInject += ", "; 
+		            		localeInject += "\"" + locale + "\"";
+		            		firstOne = false;
+		            	}
+		            	localeInject += "]";
+		            	infoInject += localeInject;
+		            	sep = ",\n";
+	                	
+	                }
+	                List<String> bundles = royaleProject.compiledResourceBundleNames;
+	                if (bundles.size() > 0)
+	                {
+		            	String bundleInject = sep + "compiledResourceBundleNames: [";
+		            	boolean firstOne = true;
+		            	for (String bundle : bundles)
+		            	{
+		            		if (!firstOne)
+		            			bundleInject += ", "; 
+		            		bundleInject += "\"" + bundle + "\"";
+		            		firstOne = false;
+		            	}
+		            	bundleInject += "]";
+		            	infoInject += bundleInject;
+		            	sep = ",\n";
+	                	
+	                }
+	                List<String> bundleClasses = royaleProject.compiledResourceBundleClasses;
+	                if (bundles.size() > 0)
+	                {
+		            	for (String bundleClass : bundleClasses)
+		            	{
+		                    StringBuilder appendString = new StringBuilder();
+		                    appendString.append(JSGoogEmitterTokens.GOOG_REQUIRE.getToken());
+		                    appendString.append(ASEmitterTokens.PAREN_OPEN.getToken());
+		                    appendString.append(ASEmitterTokens.SINGLE_QUOTE.getToken());
+		                    appendString.append(bundleClass);
+		                    appendString.append(ASEmitterTokens.SINGLE_QUOTE.getToken());
+		                    appendString.append(ASEmitterTokens.PAREN_CLOSE.getToken());
+		                    appendString.append(ASEmitterTokens.SEMICOLON.getToken());
+	                        finalLines.add(endRequires, appendString.toString());
+	                        addLineToMappings(endRequires);
+                            endRequires++;
+		            	}
+	                }
 	            	boolean isMX = false;
 	            	List<ISWC> swcs = royaleProject.getLibraries();
 	            	for (ISWC swc : swcs)
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/projects/RoyaleJSProject.java b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/projects/RoyaleJSProject.java
index 6070852..aa2cb9b 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/projects/RoyaleJSProject.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/projects/RoyaleJSProject.java
@@ -494,6 +494,9 @@ public class RoyaleJSProject extends RoyaleProject
 
 	private HashSet<String> exportedNames = new HashSet<String>();
 	
+	public List<String> compiledResourceBundleNames = new ArrayList<String>();
+	public List<String> compiledResourceBundleClasses = new ArrayList<String>();
+	
 	public void addExportedName(String name)
 	{
 		exportedNames.add(name);