You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by er...@apache.org on 2014/06/24 15:49:22 UTC

[7/7] git commit: [flex-falcon] [refs/heads/develop] - Instead of the kinda 'hacked in' use of the AbstractCompilerRunner, we now have a full blown implementation of the Compiler class proper. This will give us much more flexibility when working with the

Instead of the kinda 'hacked in' use of the AbstractCompilerRunner, we now have a full blown implementation of the Compiler class proper. This will give us much more flexibility when working with the GC, as well as enable us to get some extra efficiency from the JS advanced compilation step.

Signed-off-by: Erik de Bruin <er...@ixsoftware.nl>


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

Branch: refs/heads/develop
Commit: aadd7dc5a3ec9e52ae90b58ac31291e7e4fd523a
Parents: ac765e6
Author: Erik de Bruin <er...@ixsoftware.nl>
Authored: Tue Jun 24 15:47:46 2014 +0200
Committer: Erik de Bruin <er...@ixsoftware.nl>
Committed: Tue Jun 24 15:47:46 2014 +0200

----------------------------------------------------------------------
 .../mxml/flexjs/MXMLFlexJSPublisher.java        |  58 +++----
 .../utils/JSClosureCompilerWrapper.java         | 160 +++++++++++++++++++
 2 files changed, 179 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/aadd7dc5/compiler.jx/src/org/apache/flex/compiler/internal/codegen/mxml/flexjs/MXMLFlexJSPublisher.java
----------------------------------------------------------------------
diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/mxml/flexjs/MXMLFlexJSPublisher.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/mxml/flexjs/MXMLFlexJSPublisher.java
index 31d434f..dff5d23 100644
--- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/mxml/flexjs/MXMLFlexJSPublisher.java
+++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/mxml/flexjs/MXMLFlexJSPublisher.java
@@ -46,9 +46,7 @@ import org.apache.flex.compiler.internal.codegen.js.goog.JSGoogPublisher;
 import org.apache.flex.compiler.internal.driver.js.goog.JSGoogConfiguration;
 import org.apache.flex.compiler.internal.graph.GoogDepsWriter;
 import org.apache.flex.compiler.internal.projects.FlexJSProject;
-import org.apache.flex.compiler.utils.JSClosureCompilerUtil;
-
-import com.google.javascript.jscomp.SourceMap;
+import org.apache.flex.compiler.utils.JSClosureCompilerWrapper;
 
 public class MXMLFlexJSPublisher extends JSGoogPublisher implements
         IJSPublisher
@@ -72,6 +70,7 @@ public class MXMLFlexJSPublisher extends JSGoogPublisher implements
             return new Integer(o1.lineNumber).compareTo(o2.lineNumber);
         }
     }
+    
     public MXMLFlexJSPublisher(Configuration config, FlexJSProject project)
     {
         super(config);
@@ -183,7 +182,7 @@ public class MXMLFlexJSPublisher extends JSGoogPublisher implements
             FileUtils.copyDirectory(new File(closureGoogSrcLibDirPath), new File(closureGoogTgtLibDirPath));
         }
         
-        ArrayList<String> optionList = new ArrayList<String>();
+        JSClosureCompilerWrapper compilerWrapper = new JSClosureCompilerWrapper();
 
         GoogDepsWriter gdw = new GoogDepsWriter(intermediateDir, projectName, (JSGoogConfiguration) configuration);
         try
@@ -255,7 +254,7 @@ public class MXMLFlexJSPublisher extends JSGoogPublisher implements
                 sb.append("goog.addDependency('base.js', ['goog'], []);\n");
                 File file = new File(closureGoogSrcLibDirPath + "/base.js");
                 FileUtils.copyFileToDirectory(file, new File(closureGoogTgtLibDirPath));
-                optionList.add("--js=" + file.getCanonicalPath());
+                compilerWrapper.addJSSourceFile(file.getCanonicalPath());
                 Collections.sort(subsetdeps, new DependencyLineComparator());
                 for (DependencyRecord subsetdeprec : subsetdeps)
                 {
@@ -272,7 +271,7 @@ public class MXMLFlexJSPublisher extends JSGoogPublisher implements
                         dir += File.separator + googfn.substring(0, googfn.lastIndexOf("/"));
                     }
                     FileUtils.copyFileToDirectory(file, new File(dir));
-                    optionList.add("--js=" + file.getCanonicalPath());
+                    compilerWrapper.addJSSourceFile(file.getCanonicalPath());
                 }
             }
         }
@@ -309,53 +308,34 @@ public class MXMLFlexJSPublisher extends JSGoogPublisher implements
                     DirectoryFileFilter.DIRECTORY);
             for (File file : files)
             {
-                optionList.add("--js=" + file.getCanonicalPath());
+                compilerWrapper.addJSSourceFile(file.getCanonicalPath());
             }
         }
         
         // (erikdebruin) add project files
         for (String filePath : gdw.filePathsInOrder)
         {
-            optionList.add("--js=" + new File(filePath).getCanonicalPath());
+            compilerWrapper.addJSSourceFile(
+                    new File(filePath).getCanonicalPath());   
         }
         
-        if (useStrictPublishing)
-        {
-            // (erikdebruin) set compiler flags to 'strictest' to allow maximum
-            //               code optimization
-            optionList.add("--define='goog.DEBUG=false'");
-            optionList.add("--language_in=ECMASCRIPT5_STRICT");
-            optionList.add("--warning_level=VERBOSE");
-            optionList.add("--jscomp_warning=accessControls");
-            optionList.add("--jscomp_warning=const");
-            optionList.add("--jscomp_warning=constantProperty");
-            optionList.add("--jscomp_warning=strictModuleDepCheck");
-            optionList.add("--jscomp_warning=visibility");
-            optionList.add("--jscomp_off=deprecated");
-        }
+        compilerWrapper.setOptions(
+                projectReleaseJSFilePath, useStrictPublishing);
         
         // (erikdebruin) Include the 'goog' deps to allow the compiler to resolve
         //               dependencies.
-        optionList.add("--js=" + closureGoogSrcLibDirPath + File.separator + "deps.js");
-
-        optionList.add("--closure_entry_point=" + projectName);
-        optionList.add("--only_closure_dependencies");
-        optionList.add("--compilation_level=ADVANCED_OPTIMIZATIONS");
-        optionList.add("--js_output_file=" + projectReleaseJSFilePath);
-        optionList.add("--output_manifest="
-                + releaseDirPath + File.separator + "manifest.txt");
-        optionList.add("--create_source_map="
-                + projectReleaseJSFilePath + ".map");
-        optionList.add("--source_map_format=" + SourceMap.Format.V3);
+        compilerWrapper.addJSSourceFile(
+                closureGoogSrcLibDirPath + File.separator + "deps.js");
         
         List<String> externs = ((JSGoogConfiguration)configuration).getExternalJSLib();
         for (String extern : externs)
-            optionList.add("--externs=" + extern);
-
-        String[] options = (String[]) optionList.toArray(new String[0]);
-
-        JSClosureCompilerUtil.run(options);
-
+        {
+            compilerWrapper.addJSExternsFile(extern);
+        }
+        
+        compilerWrapper.targetFilePath = projectReleaseJSFilePath;
+        compilerWrapper.compile();
+        
         appendSourceMapLocation(projectReleaseJSFilePath, projectName);
 
         if (!isMarmotinniRun)

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/aadd7dc5/compiler.jx/src/org/apache/flex/compiler/utils/JSClosureCompilerWrapper.java
----------------------------------------------------------------------
diff --git a/compiler.jx/src/org/apache/flex/compiler/utils/JSClosureCompilerWrapper.java b/compiler.jx/src/org/apache/flex/compiler/utils/JSClosureCompilerWrapper.java
new file mode 100644
index 0000000..e09ad62
--- /dev/null
+++ b/compiler.jx/src/org/apache/flex/compiler/utils/JSClosureCompilerWrapper.java
@@ -0,0 +1,160 @@
+/*
+ *
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.flex.compiler.utils;
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+
+import com.google.javascript.jscomp.CheckLevel;
+import com.google.javascript.jscomp.CommandLineRunner;
+import com.google.javascript.jscomp.CompilationLevel;
+import com.google.javascript.jscomp.Compiler;
+import com.google.javascript.jscomp.CompilerOptions;
+import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
+import com.google.javascript.jscomp.DiagnosticGroups;
+import com.google.javascript.jscomp.JSError;
+import com.google.javascript.jscomp.SourceFile;
+import com.google.javascript.jscomp.SourceMap;
+import com.google.javascript.jscomp.WarningLevel;
+import com.google.javascript.rhino.Node;
+import com.google.javascript.rhino.Token;
+
+public class JSClosureCompilerWrapper
+{
+
+    public JSClosureCompilerWrapper()
+    {
+        Compiler.setLoggingLevel(Level.INFO);
+
+        compiler_ = new Compiler();
+
+        options_ = new CompilerOptions();
+        initOptions();
+        
+        jsExternsFiles_ = new ArrayList<SourceFile>();
+        initExterns();
+
+        jsSourceFiles_ = new ArrayList<SourceFile>();
+    }
+
+    private Compiler compiler_;
+    private CompilerOptions options_;
+    private List<SourceFile> jsExternsFiles_;
+    private List<SourceFile> jsSourceFiles_;
+    
+    public String targetFilePath;
+    
+    public void addJSExternsFile(String fileName)
+    {
+        addJSExternsFile(SourceFile.fromFile(fileName));
+    }
+    
+    public void addJSExternsFile(SourceFile file)
+    {
+        jsExternsFiles_.add(file);
+    }
+    
+    public void addJSSourceFile(String fileName)
+    {
+        jsSourceFiles_.add(SourceFile.fromFile(fileName));
+    }
+    
+    public void compile()
+    {
+        compiler_.compile(jsExternsFiles_, jsSourceFiles_, options_);
+
+        try
+        {
+            FileWriter targetFile = new FileWriter(targetFilePath);
+            targetFile.write(compiler_.toSource());
+            targetFile.close();
+            
+            FileWriter sourceMapFile = new FileWriter(options_.sourceMapOutputPath);
+            compiler_.getSourceMap().appendTo(sourceMapFile, "");
+            sourceMapFile.close();
+        }
+        catch (IOException error)
+        {
+            System.out.println(error);
+        }
+        
+        for (JSError message : compiler_.getWarnings())
+        {
+            System.err.println("Warning message: " + message.toString());
+        }
+     
+        for (JSError message : compiler_.getErrors())
+        {
+            System.err.println("Error message: " + message.toString());
+        }
+    }
+    
+    private void initExterns()
+    {
+        try
+        {
+            List<SourceFile> defaultExterns = CommandLineRunner.getDefaultExterns();
+            for (SourceFile defaultExtern : defaultExterns)
+            {
+                this.addJSExternsFile(defaultExtern);
+            }
+        }
+        catch (IOException error)
+        {
+            System.out.println(error);
+        }
+    }
+    
+    private void initOptions()
+    {
+        CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(
+                options_);
+        
+        WarningLevel.VERBOSE.setOptionsForWarningLevel(options_);
+    }
+    
+    public void setOptions(String sourceMapPath, boolean useStrictPublishing)
+    {
+        if (useStrictPublishing)
+        {
+            // (erikdebruin) set compiler flags to 'strictest' to allow maximum
+            //               code optimization
+
+            options_.getDefineReplacements().put(
+                    "goog.DEBUG", new Node(Token.TRUE));
+            
+            options_.setLanguageIn(LanguageMode.ECMASCRIPT5_STRICT);
+            
+            options_.setWarningLevel(DiagnosticGroups.ACCESS_CONTROLS, CheckLevel.WARNING);
+            options_.setWarningLevel(DiagnosticGroups.CONST, CheckLevel.WARNING);
+            options_.setWarningLevel(DiagnosticGroups.CONSTANT_PROPERTY, CheckLevel.WARNING);
+            options_.setWarningLevel(DiagnosticGroups.STRICT_MODULE_DEP_CHECK, CheckLevel.WARNING);
+            options_.setWarningLevel(DiagnosticGroups.VISIBILITY, CheckLevel.WARNING);
+            options_.setWarningLevel(DiagnosticGroups.DEPRECATED, CheckLevel.OFF);
+        }
+        
+        options_.sourceMapFormat = SourceMap.Format.V3;
+        options_.sourceMapOutputPath = sourceMapPath + ".map";
+    }
+
+}