http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c3dce49f/compiler.js/src/org/apache/flex/compiler/internal/driver/JSCompilationUnit.java ---------------------------------------------------------------------- diff --git a/compiler.js/src/org/apache/flex/compiler/internal/driver/JSCompilationUnit.java b/compiler.js/src/org/apache/flex/compiler/internal/driver/JSCompilationUnit.java deleted file mode 100644 index c00baa7..0000000 --- a/compiler.js/src/org/apache/flex/compiler/internal/driver/JSCompilationUnit.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * - * 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.internal.driver; - -import java.io.IOException; -import java.util.Collection; - -import org.apache.flex.compiler.common.DependencyType; -import org.apache.flex.compiler.definitions.IDefinition; -import org.apache.flex.compiler.internal.as.codegen.JSGeneratingReducer; -import org.apache.flex.compiler.internal.as.codegen.JSGenerator; -import org.apache.flex.compiler.internal.as.codegen.JSSharedData; -import org.apache.flex.compiler.internal.projects.CompilerProject; -import org.apache.flex.compiler.internal.projects.DefinitionPriority; -import org.apache.flex.compiler.internal.scopes.ASProjectScope; -import org.apache.flex.compiler.internal.units.ASCompilationUnit; -import org.apache.flex.compiler.problems.ICompilerProblem; -import org.apache.flex.compiler.targets.ITarget.TargetType; -import org.apache.flex.compiler.tree.as.IASNode; -import org.apache.flex.compiler.units.ICompilationUnit; -import org.apache.flex.compiler.units.requests.IABCBytesRequestResult; -import org.apache.flex.compiler.units.requests.IOutgoingDependenciesRequestResult; -import org.apache.flex.compiler.units.requests.ISyntaxTreeRequestResult; - -/** - * JSCompilationUnit is the CompilationUnit for compiling ActionScript source - * files to JavasScript. JSCompilationUnit is derived from ASCompilationUnit and - * overrides the parts that generate the code. JSCompilationUnit also supports - * requests for two-pass compilation (see m_needsSecondPass). - * JSSourceFileHandler provides JSCompilationUnit for *.as files. JSDriver - * registers JSSourceFileHandler at FlexApplicationProject. This implementation - * is part of FalconJS. For more details on FalconJS see - * org.apache.flex.compiler.JSDriver - */ - -public class JSCompilationUnit extends ASCompilationUnit -{ - private IABCBytesRequestResult m_abcBytes = null; - private Boolean m_needsSecondPass = false; - private Boolean m_inCodeGen = false; - - /** - * Create a compilation unit from an ABC file. - * - * @param project compiler project - * @param path ABC file path - * @throws IOException error - */ - public JSCompilationUnit(CompilerProject project, String path) throws IOException - { - this(project, path, DefinitionPriority.BasePriority.LIBRARY_PATH); - } - - public JSCompilationUnit(CompilerProject project, String path, DefinitionPriority.BasePriority basePriority) - { - super(project, path, basePriority); - } - - public JSCompilationUnit(CompilerProject project, String path, DefinitionPriority.BasePriority basePriority, String qname) - { - super(project, path, basePriority, 0, qname); - } - - protected IABCBytesRequestResult _handleABCBytesRequest(Operation buildPhase) throws InterruptedException - { - // If JSEmitter.needsSecondPass() returns true, JSGenerator.generate() will return null during scanning, - // which will result in JSCompilationUnit::handleSemanticProblemsRequest not caching any abcBytes for - // handleABCBytesRequest. The net result is that JSGenerator.generate() will be called again in handleABCBytesRequest. - // This mechanic will ensure selective two-pass compilation. - if (m_abcBytes != null && - !JSSharedData.instance.hasSymbols() && // Symbol support - !JSSharedData.instance.hasAnyClassInit()) // support for class inits - return m_abcBytes; - - JSGenerator jsGenerator = new JSGenerator(); - jsGenerator.m_compilationUnit = this; - jsGenerator.setBuildPhase(buildPhase); - - // Need to force the file scope request to happen first to get the ASFileScope - // for this compilation unit registered with the project. - // ** TODO this is a hack! - getFileScopeRequest().get(); - - // This is also a hack! If there are embed directives, need to ensure - // semantic pass has finished, as that is what will generate the embed classes - // which are needed by codegen - if (buildPhase != Operation.GET_SEMANTIC_PROBLEMS) - { - // AJH this was deadlocking as getOutgoingDependencies calls handleABCBytes - if (buildPhase != Operation.GET_ABC_BYTES) - getOutgoingDependenciesRequest().get(); - } - - final ISyntaxTreeRequestResult fsr = getSyntaxTreeRequest().get(); - final IASNode rootNode = fsr.getAST(); - - startProfile(buildPhase); - IABCBytesRequestResult result = jsGenerator.generate(getFilenameNoPath(), rootNode, this.getProject()); - stopProfile(buildPhase); - - m_needsSecondPass = jsGenerator.needsSecondPass(); - - return result; - } - - @Override - protected IABCBytesRequestResult handleABCBytesRequest() throws InterruptedException - { - final IABCBytesRequestResult result = _handleABCBytesRequest(Operation.GET_ABC_BYTES); - - /* - * // explicitly reference all classes this class depends on if( - * result.getProblems() == null || result.getProblems().length == 0 ) { - * final String code = new String( result.getABCBytes() ); if( - * code.contains(JSSharedData.REQUIRED_TAG_MARKER) ) { final - * ICompilationUnit cu = this; final Set<ICompilationUnit> deps = new - * HashSet<ICompilationUnit>(); deps.addAll( - * getProject().getDependencies(cu) ); if( !deps.isEmpty() ) { String - * depNames = ""; Boolean separator = false; final List<IDefinition> - * defs = MXMLJSC.getClassDefinitions( cu ); for( IDefinition def: defs - * ) { if( def instanceof ClassDefinition ) { final String defName = - * JSGeneratingReducer.createFullNameFromDefinition(def); if( defName != - * null && !defName.isEmpty() ) { if( separator ) depNames += ":"; else - * separator = true; depNames += defName; } } } - * code.replaceFirst(JSSharedData.REQUIRED_TAG_MARKER, depNames); return - * new ABCBytesRequestResult(code.getBytes(), result.getProblems()); } } - * } - */ - return result; - } - - @Override - protected IOutgoingDependenciesRequestResult handleOutgoingDependenciesRequest() throws InterruptedException - { - // Every CU is dependent on the class glue, which is implemented in browser.adobe. - // Add dependency from this JSCompilationUnit to browser.adobe's JSCompilationUnit. - addDependency(JSSharedData.JS_FRAMEWORK_NAME, DependencyType.INHERITANCE); - addDependency(JSSharedData.FRAMEWORK_CLASS, DependencyType.INHERITANCE); - - IOutgoingDependenciesRequestResult result = super.handleOutgoingDependenciesRequest(); - - // SWFTarget::startBuildAndFindAllCompilationUnits() is called by SWFTarget::collectProblems(), which is called by SWFTarget::addToSWF() in JSDriver::main(). - // This is our first pass. jsGenerator.generate() will return null if JSGeneratingReducer.getMember - // If JSEmitter.needsSecondPass() returns true, JSGenerator.generate() will return null during scanning, - // which will result in JSCompilationUnit::handleSemanticProblemsRequest not caching any abcBytes for - // handleABCBytesRequest. The net result is that JSGenerator.generate() will be called again in handleABCBytesRequest. - // This mechanic will ensure selective two-pass compilation. - if (result.getProblems().length == 0) - { - m_needsSecondPass = false; - m_abcBytes = _handleABCBytesRequest(Operation.GET_SEMANTIC_PROBLEMS); - if (m_needsSecondPass) - m_abcBytes = null; - } - - return result; - } - - public Boolean addDependency(String className, DependencyType dt) - { - if (JSGeneratingReducer.isReservedDataType(className)) - return false; - - final ICompilationUnit fromCU = this; - final CompilerProject compilerProject = this.getProject(); - final ASProjectScope projectScope = compilerProject.getScope(); - - final IDefinition classDef = projectScope.findDefinitionByName(className); - if (classDef == null) - return false; - - final ICompilationUnit toCU = projectScope.getCompilationUnitForDefinition(classDef); - if (fromCU == toCU) - return false; - - // sharedData.verboseMessage( "Adding dependency: " + className ); - compilerProject.addDependency(fromCU, toCU, dt); - - return true; - } - - @Override - public void startBuildAsync(TargetType targetType) - { - // super.startBuildAsync(targetType); - - getSyntaxTreeRequest(); - getFileScopeRequest(); - getOutgoingDependenciesRequest(); - - // scanning and code generating phases need to be separated - // in order to create two distinct passes for m_needSecondPass. - if (m_inCodeGen) - { - getABCBytesRequest(); - getSWFTagsRequest(); - } - } - - @Override - public void waitForBuildFinish(final Collection<ICompilerProblem> problems, TargetType targetType) throws InterruptedException - { - m_inCodeGen = true; - super.waitForBuildFinish(problems, targetType); - m_inCodeGen = false; - /* - * assert problems != null : - * "Expected 'problems'. Do not ignore problems."; //$NON-NLS-1$ - * Collections.addAll(problems, - * getSyntaxTreeRequest().get().getProblems()); - * Collections.addAll(problems, - * getFileScopeRequest().get().getProblems()); - * Collections.addAll(problems, - * getSemanticProblemsRequest().get().getProblems()); - * Collections.addAll(problems, - * getABCBytesRequest().get().getProblems()); - * Collections.addAll(problems, - * getSWFTagsRequest().get().getProblems()); - */ - } - -}
http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c3dce49f/compiler.js/src/org/apache/flex/compiler/internal/driver/JSSourceFileHandler.java ---------------------------------------------------------------------- diff --git a/compiler.js/src/org/apache/flex/compiler/internal/driver/JSSourceFileHandler.java b/compiler.js/src/org/apache/flex/compiler/internal/driver/JSSourceFileHandler.java deleted file mode 100644 index ca06784..0000000 --- a/compiler.js/src/org/apache/flex/compiler/internal/driver/JSSourceFileHandler.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * - * 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.internal.driver; - -import org.apache.flex.compiler.internal.projects.CompilerProject; -import org.apache.flex.compiler.internal.projects.DefinitionPriority; -import org.apache.flex.compiler.internal.projects.ISourceFileHandler; -import org.apache.flex.compiler.units.ICompilationUnit; - -/** - * Implementation of ISourceFileHandler that constructs - * {@link ASCompilationUnit}'s. JSSourceFileHandler is the SourceFileHandler - * that provides JSCompilationUnit for *.as files. JSDriver registers - * JSSourceFileHandler at FlexApplicationProject. This implementation is part of - * FalconJS. For more details on FalconJS see org.apache.flex.compiler.JSDriver - */ -public final class JSSourceFileHandler implements ISourceFileHandler -{ - - public static final String EXTENSION = "as"; //$NON-NLS-1$ - public static final JSSourceFileHandler INSTANCE = new JSSourceFileHandler(); - - private JSSourceFileHandler() - { - } - - /** - * {@inheritDoc} - */ - public String[] getExtensions() - { - return new String[] {EXTENSION}; - } - - /** - * {@inheritDoc} - */ - public ICompilationUnit createCompilationUnit(CompilerProject proj, - String path, - DefinitionPriority.BasePriority basePriority, - int order, - String qname, - String locale) - { - return new JSCompilationUnit(proj, path, basePriority, qname); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean needCompilationUnit(CompilerProject project, String path, String qname, String locale) - { - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean canCreateInvisibleCompilationUnit() - { - return false; - } -} http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c3dce49f/compiler.js/src/org/apache/flex/compiler/internal/driver/JSTarget.java ---------------------------------------------------------------------- diff --git a/compiler.js/src/org/apache/flex/compiler/internal/driver/JSTarget.java b/compiler.js/src/org/apache/flex/compiler/internal/driver/JSTarget.java deleted file mode 100644 index 5636872..0000000 --- a/compiler.js/src/org/apache/flex/compiler/internal/driver/JSTarget.java +++ /dev/null @@ -1,322 +0,0 @@ -/* - * - * 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.internal.driver; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import org.apache.flex.abc.ABCLinker.ABCLinkerSettings; -import org.apache.flex.compiler.clients.MXMLJSC; -import org.apache.flex.compiler.definitions.IDefinition; -import org.apache.flex.compiler.exceptions.BuildCanceledException; -import org.apache.flex.compiler.internal.as.codegen.JSGeneratingReducer; -import org.apache.flex.compiler.internal.as.codegen.JSSharedData; -import org.apache.flex.compiler.internal.definitions.DefinitionBase; -import org.apache.flex.compiler.internal.projects.CompilerProject; -import org.apache.flex.compiler.internal.targets.AppSWFTarget; -import org.apache.flex.compiler.internal.targets.Target; -import org.apache.flex.compiler.internal.units.ResourceBundleCompilationUnit; -import org.apache.flex.compiler.problems.ICompilerProblem; -import org.apache.flex.compiler.problems.UnableToBuildSWFTagProblem; -import org.apache.flex.compiler.targets.ITargetProgressMonitor; -import org.apache.flex.compiler.targets.ITargetSettings; -import org.apache.flex.compiler.units.ICompilationUnit; -import org.apache.flex.swf.ISWF; -import org.apache.flex.swf.SWFFrame; -import org.apache.flex.swf.tags.DoABCTag; - -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; - -/** - * Concrete implementation of ITarget for building a collection of source files - * into a SWF. - */ - -public class JSTarget extends AppSWFTarget -{ - private ICompilationUnit mainCU; - - /** - * Initialize a SWF target with the owner project and root compilation - * units. - * - * @param project the owner project - */ - - public JSTarget(CompilerProject project, ITargetSettings targetSettings, ITargetProgressMonitor progressMonitor) - { - super(project, targetSettings, progressMonitor); - } - - /* - * private void printDefinitionsFromCompilationUnit( ICompilationUnit cu ) - * throws InterruptedException { final List<IDefinition> defs = - * MXMLJSC.getDefinitions(cu, true); String s = cu.toString() + ": " + - * cu.getShortNames(); JSSharedData.instance.verboseMessage(s); } - */ - - protected void buildAndCollectProblems( - final Set<ICompilationUnit> compilationUnits, - final Collection<ICompilerProblem> problems) - throws InterruptedException - { - final JSSharedData sharedData = JSSharedData.instance; - sharedData.beginCodeGen(); - - BuiltCompilationUnitSet builtCompilationUnits = getBuiltCompilationUnitSet(); - - if (JSSharedData.OUTPUT_ISOLATED) - { - final ICompilationUnit rootCU = getRootClassCompilationUnit(); - compilationUnits.clear(); - compilationUnits.add(rootCU); - } - else - { - final List<ICompilationUnit> allUnits = new ArrayList<ICompilationUnit>(); - allUnits.addAll(project.getReachableCompilationUnitsInSWFOrder(builtCompilationUnits.compilationUnits)); - final List<ICompilationUnit> cuList = sortCompilationUnits(allUnits); - compilationUnits.clear(); - for (ICompilationUnit cu : cuList) - compilationUnits.add(cu); - } - sharedData.endCodeGen(); - } - - public ISWF build(ICompilationUnit mainCU, Collection<ICompilerProblem> problems) - { - this.mainCU = mainCU; - return build(problems); - } - - @Override - public ISWF build(Collection<ICompilerProblem> problems) - { - buildStarted(); - try - { - Iterable<ICompilerProblem> fatalProblems = getFatalProblems(); - if (!Iterables.isEmpty(fatalProblems)) - { - Iterables.addAll(problems, fatalProblems); - return null; - } - - Set<ICompilationUnit> compilationUnitSet = new HashSet<ICompilationUnit>(); - Target.RootedCompilationUnits rootedCompilationUnits = getRootedCompilationUnits(); - Iterables.addAll(problems, rootedCompilationUnits.getProblems()); - - compilationUnitSet.addAll(rootedCompilationUnits.getUnits()); - - buildAndCollectProblems(compilationUnitSet, problems); - - List<ICompilationUnit> reachableCompilationUnits = project.getReachableCompilationUnitsInSWFOrder(rootedCompilationUnits.getUnits()); - ISWF swf = initializeSWF(reachableCompilationUnits); - - // make main frame for DoABC tags - final SWFFrame mainFrame = new SWFFrame(); - swf.addFrame(mainFrame); - - // Add definitions. - for (final ICompilationUnit cu : compilationUnitSet) - { - // ignore externals - if (isLinkageExternal(cu, targetSettings)) - continue; - - // ignore any resource bundles - if (cu instanceof ResourceBundleCompilationUnit) - continue; - - // Create a DoABC tag per compilation unit. - - // Please add this API to SWFTarget. Thx. - // protected Boolean addToFrame(ICompilationUnit cu, SWFFrame mainFrame) throws InterruptedException - // final boolean tagsAdded = cu.getSWFTagsRequest().get().addToFrame(mainFrame); - final boolean tagsAdded = addToFrame(cu, mainFrame); - if (!tagsAdded) - { - ICompilerProblem problem = new UnableToBuildSWFTagProblem(cu.getAbsoluteFilename()); - problems.add(problem); - } - } - - createLinkReport(problems); - - return swf; - } - catch (BuildCanceledException bce) - { - return null; - } - catch (InterruptedException ie) - { - return null; - } - finally - { - buildFinished(); - } - } - - // Please add this API to SWFTarget. Thx. - protected Boolean addToFrame(ICompilationUnit cu, SWFFrame mainFrame) throws InterruptedException - { - // SWFTarget's implementation: - // return cu.getSWFTagsRequest().get().addToFrame(mainFrame); - - final JSSharedData sharedData = JSSharedData.instance; - - String code = ""; - final List<IDefinition> defs = MXMLJSC.getDefinitions(cu, false); - for (IDefinition def : defs) - { - final String fullName = def.getQualifiedName(); - if (sharedData.hasJavaScript(fullName)) - { - final String jsCode = sharedData.getJavaScript(fullName); - code += jsCode; - } - } - - if (!code.isEmpty()) - { - final DoABCTag abcTag = new DoABCTag(); - abcTag.setABCData(code.getBytes()); - mainFrame.addTag(abcTag); - } - else - { - return cu.getSWFTagsRequest().get().addToFrame(mainFrame); - } - sharedData.registerSWFFrame(mainFrame, cu); - return true; - } - - /** - * sortCompilationUnits() is a workaround for DependencyGraph bugs. There - * are three problem areas: 1. The order of the CUs is somewhat random - * depending on the thread that compiled a CU. 2. Dependencies through - * static initializers are not always correctly detected. 3. Dependencies to - * classes provided by SWCs are not correctly detected. - */ - public static List<ICompilationUnit> sortCompilationUnits(List<ICompilationUnit> frameCompilationUnits) throws InterruptedException - { - ICompilationUnit frameWorkCU = null; - ICompilationUnit xmlCU = null; - ICompilationUnit xmlListCU = null; - - // extract framework CU, AS3XML CU, and AS3XMLList CU from frameCompilationUnits - Iterator<ICompilationUnit> it = frameCompilationUnits.iterator(); - while (it.hasNext()) - { - // get class name for compilation unit. - ICompilationUnit cu = it.next(); - final List<IDefinition> defs = MXMLJSC.getDefinitions(cu, false); - for (IDefinition def : defs) - { - final String fullName = JSGeneratingReducer.createFullNameFromDefinition(cu.getProject(), def); - if (frameWorkCU == null && fullName.equals(JSSharedData.JS_FRAMEWORK_NAME)) - { - frameWorkCU = cu; - it.remove(); - } - else if (xmlCU == null && fullName.equals(JSSharedData.AS3XML)) - { - xmlCU = cu; - it.remove(); - } - else if (xmlListCU == null && fullName.equals(JSSharedData.AS3XMLList)) - { - xmlListCU = cu; - it.remove(); - } - - if (def instanceof DefinitionBase) - { - JSSharedData.instance.registerReferencedDefinition(def.getQualifiedName()); - } - - JSSharedData.instance.registerPackage(def.getPackageName()); - } - } - - // insist on framework CU - if (frameWorkCU == null) - throw JSSharedData.backend.createException("JSTarget: cannot find " + JSSharedData.JS_FRAMEWORK_NAME + " compilation unit."); - - // add the framework CU at pos 0 - frameCompilationUnits.add(0, frameWorkCU); - - // add AS3XML and AS3XMLList framework CUs if necessary - if (xmlCU != null) - { - // add the AS3XML CU at pos 1 - frameCompilationUnits.add(1, xmlCU); - - // insist on AS3XMLList CU - if (xmlListCU == null) - throw JSSharedData.backend.createException("JSTarget: cannot find " + JSSharedData.AS3XMLList + " compilation unit."); - - // add the AS3XMLList CU at pos 2 - frameCompilationUnits.add(2, xmlListCU); - } - - return frameCompilationUnits; - } - - @Override - public Target.RootedCompilationUnits computeRootedCompilationUnits() throws InterruptedException - { - if (mainCU != null) - { - return new Target.RootedCompilationUnits(ImmutableSet.of(mainCU), Collections.<ICompilerProblem> emptyList()); - } - - assert false; - return new Target.RootedCompilationUnits(Collections.<ICompilationUnit> emptySet(), Collections.<ICompilerProblem> emptyList()); - } - - @Override - protected FramesInformation computeFramesInformation() throws InterruptedException - { - assert false; - return null; - } - - @Override - protected void addLinkedABCToFrame(SWFFrame targetFrame, Iterable<DoABCTag> inputABCs, ABCLinkerSettings linkSettings) throws Exception - { - assert false; - } - - @Override - protected void setKeepAS3MetadataLinkerSetting(ABCLinkerSettings linkSettings) - { - assert false; - } - -} http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c3dce49f/compiler.js/src/org/apache/flex/compiler/internal/graph/GoogDepsWriter.java ---------------------------------------------------------------------- diff --git a/compiler.js/src/org/apache/flex/compiler/internal/graph/GoogDepsWriter.java b/compiler.js/src/org/apache/flex/compiler/internal/graph/GoogDepsWriter.java deleted file mode 100644 index 6a601fc..0000000 --- a/compiler.js/src/org/apache/flex/compiler/internal/graph/GoogDepsWriter.java +++ /dev/null @@ -1,299 +0,0 @@ -package org.apache.flex.compiler.internal.graph; - -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Scanner; -import java.util.Set; - -import org.apache.commons.io.FileUtils; -import org.apache.flex.compiler.internal.projects.DependencyGraph; -import org.apache.flex.compiler.units.ICompilationUnit; - -public class GoogDepsWriter { - - public GoogDepsWriter(ICompilationUnit mainCU, File outputFolder) - { - this.mainCU = mainCU; - this.outputFolderPath = outputFolder.getAbsolutePath(); - this.sourceFolderPath = mainCU.getAbsoluteFilename(); - File sourceFile = new File(sourceFolderPath); - sourceFile = sourceFile.getParentFile(); - this.sourceFolderPath = sourceFile.getAbsolutePath(); - String flexJSHome = System.getenv("FLEXJS_HOME"); - if (flexJSHome == null || flexJSHome.length() == 0) - System.out.println("FLEXJS_HOME not defined. Should point to root of FlexJS source."); - frameworkRoot = flexJSHome; - } - - private ICompilationUnit mainCU; - private String outputFolderPath; - private String sourceFolderPath; - - private HashMap<String,GoogDep> depMap = new HashMap<String,GoogDep>(); - - public ArrayList<String> getListOfFiles() throws InterruptedException - { - buildDB(); - String mainName = mainCU.getShortNames().get(0); - ArrayList<GoogDep> dps = sort(mainName); - ArrayList<String> files = new ArrayList<String>(); - for (GoogDep gd : dps) - { - files.add(gd.filePath); - } - return files; - } - - public void writeToStream(OutputStream outputStream) throws InterruptedException, FileNotFoundException - { - buildDB(); - String mainName = mainCU.getShortNames().get(0); - ArrayList<GoogDep> dps = sort(mainName); - String outString = "// generated by FalconJS" + "\n"; - int n = dps.size(); - for (int i = n - 1; i >= 0; i--) - { - GoogDep gd = dps.get(i); - String s = "goog.addDependency('"; - s += relativePath(gd.filePath); - s += "', ['"; - s += gd.className; - s += "'], ["; - s += getDependencies(gd.deps); - s += "]);\n"; - outString += s; - } - System.out.println("Assumptions:"); - System.out.println(" 1) Output folder has copy of goog folder from GoogleClosure/library/closure/goog"); - System.out.println(" 2) Output folder has copy of FlexJS source in FlexJS folder"); - System.out.println(" 3) Output Folder structure is:"); - System.out.println(" OutputFolder/"); - System.out.println(" goog/"); - System.out.println(" base.js"); - System.out.println(" ..."); - System.out.println(" flash/ (from FlexJS/src)"); - System.out.println(" events/ (from FlexJS/src)"); - System.out.println(" org/ (from FlexJS/src)"); - System.out.println(" apache/ (from FlexJS/src)"); - System.out.println(" 4) Output folder has html file of the form:"); - System.out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); - System.out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); - System.out.println("<head>"); - System.out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"); - System.out.println("<script type=\"text/javascript\" src=\"goog/base.js\" ></script>"); - System.out.println("<script type=\"text/javascript\" src=\"" + mainName + "Deps.js" + "\" ></script>"); - System.out.println("<script type=\"text/javascript\">"); - System.out.println(" goog.require('" + mainName + "')"); - System.out.println("</script>"); - System.out.println("<script type=\"text/javascript\">"); - System.out.println(" var app = new " + mainName + "();"); - System.out.println("</script>"); - System.out.println("<title>" + mainName + "</title>"); - System.out.println("</head>"); - System.out.println("<body onload=\"app.start()\">"); - System.out.println("</body>"); - System.out.println("</html>"); - File htmlFile = new File(outputFolderPath + File.separator + mainName + ".example.html"); - BufferedOutputStream outputbuffer = new BufferedOutputStream(new FileOutputStream(htmlFile)); - String html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; - html += "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"; - html += "<head>\n"; - html += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"; - html += "<script type=\"text/javascript\" src=\"goog/base.js\" ></script>\n"; - html += "<script type=\"text/javascript\" src=\"" + mainName + "Deps.js" + "\" ></script>\n"; - html += "<script type=\"text/javascript\">\n"; - html += " goog.require('" + mainName + "')\n"; - html += "</script>\n"; - html += "<script type=\"text/javascript\">\n"; - html += " var app = new " + mainName + "();\n"; - html += "</script>\n"; - html += "<title>" + mainName + "</title>\n"; - html += "</head>\n"; - html += "<body onload=\"app.start()\">\n"; - html += "</body>\n"; - html += "</html>\n"; - try - { - outputbuffer.write(html.getBytes()); - outputbuffer.flush(); - outputbuffer.close(); - outputStream.write(outString.getBytes()); - } - catch (IOException e) - { - - } - } - - private void buildDB() - { - try { - addDeps(mainCU.getShortNames().get(0)); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - private HashMap<String, GoogDep> visited = new HashMap<String, GoogDep>(); - - private ArrayList<GoogDep> sort(String rootClassName) - { - ArrayList<GoogDep> arr = new ArrayList<GoogDep>(); - GoogDep current = depMap.get(rootClassName); - sortFunction(current, arr); - return arr; - } - - private void sortFunction(GoogDep current, ArrayList<GoogDep> arr) - { - visited.put(current.className, current); - - ArrayList<String> deps = current.deps; - for (String className : deps) - { - if (!visited.containsKey(className)) - { - GoogDep gd = depMap.get(className); - sortFunction(gd, arr); - } - } - arr.add(current); - } - - private void addDeps(String className) - { - if (depMap.containsKey(className)) - return; - - // build goog dependency list - GoogDep gd = new GoogDep(); - gd.className = className; - gd.filePath = getFilePath(className); - depMap.put(gd.className, gd); - ArrayList<String> deps = getDirectDependencies(gd.filePath); - gd.deps = new ArrayList<String>(); - for (String dep : deps) - { - gd.deps.add(dep); - addDeps(dep); - } - } - - String frameworkRoot; - - String getFilePath(String className) - { - System.out.println("Finding file for class: " + className); - String classPath = className.replace(".", File.separator); - String fn = frameworkRoot + File.separator + classPath + ".js"; - File f = new File(fn); - if (f.exists()) - return fn; - fn = sourceFolderPath + File.separator + classPath + ".js"; - f = new File(fn); - if (f.exists()) - { - fn = outputFolderPath + File.separator + classPath + ".js"; - File destFile = new File(fn); - // copy source to output - try { - FileUtils.copyFile(f, destFile); - System.out.println("Copying file for class: " + className); - } catch (IOException e) { - // TODO Auto-generated catch block - System.out.println("Error copying file for class: " + className); - } - return fn; - } - fn = outputFolderPath + File.separator + classPath + ".js"; - f = new File(fn); - if (f.exists()) - return fn; - if (className.indexOf('.') > -1) - { - classPath = className.substring(className.lastIndexOf('.') + 1); - fn = outputFolderPath + File.separator + classPath + ".js"; - f = new File(fn); - if (f.exists()) - return fn; - } - System.out.println("Could not find file for class: " + className); - return ""; - } - - private ArrayList<String> getDirectDependencies(String fn) - { - ArrayList<String> deps = new ArrayList<String>(); - - FileInputStream fis; - try { - fis = new FileInputStream(fn); - Scanner scanner = new Scanner(fis, "UTF-8"); - while (scanner.hasNextLine()) - { - String s = scanner.nextLine(); - if (s.indexOf("goog.inherits") > -1) - break; - int c = s.indexOf("goog.require"); - if (c > -1) - { - int c2 = s.indexOf(")"); - s = s.substring(c + 14, c2 - 1); - deps.add(s); - } - } - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return deps; - } - - private String getDependencies(ArrayList<String> deps) - { - String s = ""; - for (String dep : deps) - { - if (s.length() > 0) - { - s += ", "; - } - s += "'" + dep + "'"; - } - return s; - } - - String relativePath(String path) - { - if (path.indexOf(frameworkRoot) == 0) - { - path = path.replace(frameworkRoot, ""); - } - else if (path.indexOf(outputFolderPath) == 0) - { - path = path.replace(outputFolderPath, ""); - } - // paths are actually URIs and always have forward slashes - path = path.replace('\\', '/'); - return ".." + path; - } - private class GoogDep - { - public String filePath; - public String className; - public ArrayList<String> deps; - - } -}