You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by ft...@apache.org on 2015/09/01 22:19:34 UTC

[6/6] git commit: [flex-falcon] [refs/heads/JsToAs] - Build now FLEXJS_DUAL + AS3 / JS conditional compilation in one shot.

Build now FLEXJS_DUAL + AS3 / JS conditional compilation in one shot.


Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/bb642545
Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/bb642545
Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/bb642545

Branch: refs/heads/JsToAs
Commit: bb64254502c6004352dc436ae118a62a6c346238
Parents: e96ef0b
Author: Frédéric THOMAS <we...@gmail.com>
Authored: Tue Sep 1 21:16:06 2015 +0100
Committer: Frédéric THOMAS <we...@gmail.com>
Committed: Tue Sep 1 21:16:06 2015 +0100

----------------------------------------------------------------------
 .../src/org/apache/flex/utils/ArgumentUtil.java | 105 ++++++++++++++-
 flex-compiler-oem/src/flex2/tools/Tool.java     | 134 ++++++++++++++-----
 2 files changed, 197 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bb642545/compiler/src/org/apache/flex/utils/ArgumentUtil.java
----------------------------------------------------------------------
diff --git a/compiler/src/org/apache/flex/utils/ArgumentUtil.java b/compiler/src/org/apache/flex/utils/ArgumentUtil.java
index 6987cba..696e7eb 100644
--- a/compiler/src/org/apache/flex/utils/ArgumentUtil.java
+++ b/compiler/src/org/apache/flex/utils/ArgumentUtil.java
@@ -19,8 +19,13 @@
 
 package org.apache.flex.utils;
 
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+
 import java.io.File;
 import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Collection;
 
 public class ArgumentUtil {
 
@@ -40,13 +45,26 @@ public class ArgumentUtil {
         return newArgs;
     }
 
+    public static String[] removeEachElement(String[] args, String element)
+    {
+        String[] newArgs = args.clone();
+
+        while (getValue(newArgs, element) != null)
+        {
+            newArgs = removeElement(args, element);
+        }
+
+        return newArgs;
+    }
+
     public static String[] removeElement(String[] args, String element) {
 
         int length = Array.getLength(args);
         int index = -1;
 
         for (int i = 0; i < length; i++) {
-            final String[] kvp = args[i].split("=");
+            final boolean plusEqual = args[i].contains("+=");
+            String[] kvp = args[i].split(plusEqual ? "\\+=" : "=");
             if (element.equals(kvp[0])) {
                 index = i;
                 break;
@@ -67,13 +85,42 @@ public class ArgumentUtil {
         return newArgs;
     }
 
+    public static String[] removeElementWithValue(String[] args, String element, String value) {
+
+        int length = Array.getLength(args);
+        int index = -1;
+
+        for (int i = 0; i < length; i++) {
+            final boolean plusEqual = args[i].contains("+=");
+            String[] kvp = args[i].split(plusEqual ? "\\+=" : "=");
+            if (element.equals(kvp[0]) && (kvp.length == 1 || (value != null && value.equals(kvp[1])))) {
+                index = i;
+                break;
+            }
+        }
+
+        String[] newArgs = new String[length - 1];
+
+        if (index < 0 || index >= length) {
+            System.arraycopy(args, 0, newArgs, 0, length - 1);
+        } else  {
+            System.arraycopy(args, 0, newArgs, 0, index);
+            if (index < length - 1) {
+                System.arraycopy(args, index + 1, newArgs, index, length - index - 1);
+            }
+        }
+
+        return newArgs;
+    }
+
     public static String getValue(String[] args, String element) {
 
         boolean found = false;
         String[] kvp = new String[0];
 
         for (String s : args) {
-            kvp = s.split("=");
+            final boolean plusEqual = s.contains("+=");
+            kvp = s.split(plusEqual ? "\\+=" : "=");
 
             if (kvp[0].equals(element)) {
                 found = true;
@@ -81,23 +128,66 @@ public class ArgumentUtil {
             }
         }
 
-        return found ? kvp[1] : null;
+        return found ? kvp.length == 2 ? kvp[1] : null : null;
+    }
+
+    public static Collection<String> getValues(String[] args, String element) {
+
+        String[] kvp;
+        final Multimap<String, String> argsMap = ArrayListMultimap.create();
+
+        for (String s : args) {
+            final boolean plusEqual = s.contains("+=");
+            kvp = s.split(plusEqual ? "\\+=" : "=");
+
+            if (plusEqual || !argsMap.containsKey(kvp[0])) {
+                argsMap.put(kvp[0], kvp.length == 2 ? kvp[1] : null);
+            }
+            else {
+                ArrayList<String> replacement = null;
+                if (kvp.length > 1) {
+                    replacement = new ArrayList<String>();
+                    replacement.add(kvp[1]);
+                    argsMap.replaceValues(kvp[0], replacement);
+                }
+            }
+        }
+
+        return argsMap.get(element);
     }
 
     public static void setValue(String[] args, String element, String value) {
         String[] kvp;
 
         for (int i = 0, argsLength = args.length; i < argsLength; i++) {
-            kvp = args[i].split("=");
+            final boolean plusEqual = args[i].contains("+=");
+            kvp = args[i].split(plusEqual ? "\\+=" : "=");
 
             if (kvp[0].equals(element)) {
-                args[i] = kvp[0] + "=" + value;
+                String affectationSign = plusEqual ? "+=" : "=";
+                args[i] = kvp[0] + affectationSign + value;
                 break;
             }
         }
     }
 
-    public static String[] addValueAt(String[] args, String elemrnt, String value, int index) {
+    public static String[] addValue(String[] args, String element) {
+        return  addValue(args, element, null, args.length - 1, false);
+    }
+
+    public static String[] addValue(String[] args, String element, String value) {
+        return  addValue(args, element, value, args.length - 1, false);
+    }
+
+    public static String[] addValue(String[] args, String element, String value, boolean plusEqual) {
+        return  addValue(args, element, value, args.length - 1, plusEqual);
+    }
+
+    public static String[] addValue(String[] args, String element, String value, int index) {
+        return  addValue(args, element, value, index, false);
+    }
+
+    public static String[] addValue(String[] args, String element, String value, int index, boolean plusEqual) {
 
         int length = Array.getLength(args);
 
@@ -111,7 +201,8 @@ public class ArgumentUtil {
             if (i < index) {
                 newArgs[i] = args[i];
             } else if (i == index) {
-                newArgs[i] = elemrnt + "=" + value;
+                String affectationSign = plusEqual ? "+=" : "=";
+                newArgs[i] = value != null ? element + affectationSign + value : element;
             } else {
                 newArgs[i] = args[i - 1];
             }

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bb642545/flex-compiler-oem/src/flex2/tools/Tool.java
----------------------------------------------------------------------
diff --git a/flex-compiler-oem/src/flex2/tools/Tool.java b/flex-compiler-oem/src/flex2/tools/Tool.java
index 1e4c516..7d1f138 100644
--- a/flex-compiler-oem/src/flex2/tools/Tool.java
+++ b/flex-compiler-oem/src/flex2/tools/Tool.java
@@ -60,8 +60,8 @@ public class Tool
     protected static Class<? extends MXMLC> COMPILER;
     protected static Class<? extends MxmlJSC> JS_COMPILER;
 
-    protected static int compile(String[] args) throws NoSuchMethodException, InstantiationException,
-            IllegalAccessException, InvocationTargetException
+    protected static int compile(String[] args)
+            throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException
     {
         int exitCode;
 
@@ -75,7 +75,7 @@ public class Tool
 
         if (jsOutputType != null)
         {
-            ArgumentBag bag = preparePhase1(new ArgumentBag(args));
+            ArgumentBag bag = prepareJs(new ArgumentBag(args));
 
             MxmlJSC mxmlJSC = JS_COMPILER.newInstance();
             exitCode = mxmlJSC.execute(bag.args);
@@ -84,7 +84,7 @@ public class Tool
             {
                 if (jsOutputType.equals(FLEXJS_DUAL))
                 {
-                    preparePhase2(bag);
+                    prepareAs3(bag);
                     exitCode = flexCompile(bag.args);
                 }
             }
@@ -101,10 +101,33 @@ public class Tool
         return exitCode;
     }
 
-    protected static ArgumentBag preparePhase1(ArgumentBag bag)
+    protected static ArgumentBag prepareJs(ArgumentBag bag)
     {
-        bag.isCommandLineOutput = true;
+        SwitchDefineToCompileJs(bag);
+        prepareJsOutput(bag);
+
+        return bag;
+    }
+
+    protected static ArgumentBag prepareAs3(ArgumentBag bag)
+    {
+        SwitchDefineToCompileAs3(bag);
+        prepareAs3Output(bag);
+        removeNativeJSLibraries(bag);
+
+        return bag;
+    }
+
+    private static void removeNativeJSLibraries(ArgumentBag bag)
+    {
+        final List<String> argList = new ArrayList<String>(Arrays.asList(bag.args));
+        argList.add("--exclude-native-js-libraries=true");
+        bag.args = argList.toArray(new String[argList.size()]);
+    }
 
+    private static void prepareJsOutput(ArgumentBag bag)
+    {
+        bag.isCommandLineOutput = true;
         bag.oldOutputPath = ArgumentUtil.getValue(bag.args, "-output");
 
         if (bag.oldOutputPath == null)
@@ -123,19 +146,25 @@ public class Tool
         if (bag.oldOutputPath == null)
         {
             bag.isCommandLineOutput = false;
-            ConfigurationBuffer flexConfig = null;
+            List<ConfigurationBuffer> flexConfigs = null;
 
             try
             {
-                flexConfig = loadConfig(bag.args);
+                flexConfigs = loadConfig(bag.args);
+
+                for (ConfigurationBuffer flexConfig : flexConfigs)
+                {
+                    if (bag.oldOutputPath == null) {
+                        final List<ConfigurationValue> output = flexConfig != null ? flexConfig.getVar("output") : null;
+                        final ConfigurationValue configurationValue = output != null ? output.get(0) : null;
+                        bag.oldOutputPath = configurationValue != null ? configurationValue.getArgs().get(0)
+                                : bag.oldOutputPath;
+                    }
+                }
             }
             catch (org.apache.flex.compiler.exceptions.ConfigurationException ignored)
             {
             }
-
-            final List<ConfigurationValue> output = flexConfig != null ? flexConfig.getVar("output") : null;
-            final ConfigurationValue configurationValue = output != null ? output.get(0) : null;
-            bag.oldOutputPath = configurationValue != null ? configurationValue.getArgs().get(0) : null;
         }
 
         if (bag.oldOutputPath != null)
@@ -144,8 +173,8 @@ public class Tool
 
             if (lastIndexOf > -1)
             {
-                bag.newOutputPath = bag.oldOutputPath.substring(0, lastIndexOf) + File.separator + "js"
-                        + File.separator + "out";
+                bag.newOutputPath = bag.oldOutputPath.substring(0, lastIndexOf) + File.separator + "js" + File.separator
+                        + "out";
 
                 if (bag.isCommandLineOutput)
                 {
@@ -153,18 +182,29 @@ public class Tool
                 }
                 else
                 {
-                    bag.args = ArgumentUtil.addValueAt(bag.args, "-output", bag.newOutputPath, bag.args.length - 1);
+                    bag.args = ArgumentUtil.addValue(bag.args, "-output", bag.newOutputPath);
                 }
             }
         }
-
-        return bag;
     }
 
-    protected static ArgumentBag preparePhase2(ArgumentBag bag)
+    private static void SwitchDefineToCompileJs(ArgumentBag bag)
     {
-        bag.args = ArgumentUtil.removeElement(bag.args, "-js-output-type");
+        final Collection<String> defines = ArgumentUtil.getValues(bag.args, "-define");
+        if (defines.contains("COMPILE::AS3,AUTO") && defines.contains("COMPILE::JS,AUTO"))
+        {
+            bag.args = ArgumentUtil.removeElementWithValue(bag.args, "-define", "COMPILE::JS,AUTO");
+            bag.args = ArgumentUtil.removeElementWithValue(bag.args, "-define", "COMPILE::AS3,AUTO");
+
+            bag.args = ArgumentUtil.addValue(bag.args, "-define", "COMPILE::JS,true", true);
+            bag.args = ArgumentUtil.addValue(bag.args, "-define", "COMPILE::AS3,false", true);
+
+            bag.args = ArgumentUtil.addValue(bag.args, "-keep-asdoc", null);
+        }
+    }
 
+    private static void prepareAs3Output(ArgumentBag bag)
+    {
         if (bag.oldOutputPath != null)
         {
             if (bag.isCommandLineOutput)
@@ -179,29 +219,53 @@ public class Tool
 
         if (COMPILER.getName().equals(COMPC.class.getName()))
         {
-            bag.args = ArgumentUtil.addValueAt(bag.args, "-include-file", "js" + File.separator + "out"
-                    + File.separator + "*," + bag.newOutputPath, bag.args.length - 1);
+            bag.args = ArgumentUtil.addValue(bag.args, "-include-file",
+                    "js" + File.separator + "out" + File.separator + "*," + bag.newOutputPath);
         }
+    }
 
-        return bag;
+    private static void SwitchDefineToCompileAs3(ArgumentBag bag)
+    {
+        final Collection<String> defines = ArgumentUtil.getValues(bag.args, "-define");
+        if (defines.contains("COMPILE::AS3,false") && defines.contains("COMPILE::JS,true"))
+        {
+            bag.args = ArgumentUtil.removeElement(bag.args, "-keep-asdoc");
+            bag.args = ArgumentUtil.removeElementWithValue(bag.args, "-define", "COMPILE::AS3,false");
+            bag.args = ArgumentUtil.removeElementWithValue(bag.args, "-define", "COMPILE::JS,true");
+
+            bag.args = ArgumentUtil.addValue(bag.args, "-define", "COMPILE::JS,false", true);
+            bag.args = ArgumentUtil.addValue(bag.args, "-define", "COMPILE::AS3,true", true);
+        }
+
+        bag.args = ArgumentUtil.removeElement(bag.args, "-js-output-type");
     }
 
-    private static ConfigurationBuffer loadConfig(String[] args)
+    private static List<ConfigurationBuffer> loadConfig(String[] args)
             throws org.apache.flex.compiler.exceptions.ConfigurationException
     {
-        final String configFilePath = ArgumentUtil.getValue(args, "-load-config");
-        if (configFilePath == null)
+        List<ConfigurationBuffer> configurationBuffers = null;
+
+        final Collection<String> configFilePaths = ArgumentUtil.getValues(args, "-load-config");
+        if (configFilePaths != null)
         {
-            return null;
-        }
-        final File configFile = new File(configFilePath);
-        final FileSpecification fileSpecification = new FileSpecification(configFile.getAbsolutePath());
-        final ConfigurationBuffer cfgbuf = createConfigurationBuffer(Configuration.class);
+            for (String configFilePath : configFilePaths)
+            {
+                final File configFile = new File(configFilePath);
+                final FileSpecification fileSpecification = new FileSpecification(configFile.getAbsolutePath());
+                final ConfigurationBuffer cfgbuf = createConfigurationBuffer(Configuration.class);
 
-        FileConfigurator.load(cfgbuf, fileSpecification, new File(configFile.getPath()).getParent(), "flex-config",
-                false);
+                FileConfigurator.load(cfgbuf, fileSpecification, new File(configFile.getPath()).getParent(),
+                        "flex-config", false);
+
+                if (configurationBuffers == null)
+                {
+                    configurationBuffers = new ArrayList<ConfigurationBuffer>(0);
+                }
+                configurationBuffers.add(cfgbuf);
+            }
+        }
 
-        return cfgbuf;
+        return configurationBuffers;
     }
 
     private static ConfigurationBuffer createConfigurationBuffer(Class<? extends Configuration> configurationClass)
@@ -209,8 +273,8 @@ public class Tool
         return new ConfigurationBuffer(configurationClass, Configuration.getAliases());
     }
 
-    protected static int flexCompile(String[] args) throws NoSuchMethodException, InstantiationException,
-            IllegalAccessException, InvocationTargetException
+    protected static int flexCompile(String[] args)
+            throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException
     {
         int exitCode;