[Buix-commits] r1323 - in guix/trunk/guix-compiler: . src/main/java/org/codelutin/guix
Author: kmorin Date: 2009-04-17 12:56:37 +0000 (Fri, 17 Apr 2009) New Revision: 1323 Added: guix/trunk/guix-compiler/src/main/java/org/codelutin/guix/GuixLauncher.java Modified: guix/trunk/guix-compiler/pom.xml Log: Modified: guix/trunk/guix-compiler/pom.xml =================================================================== --- guix/trunk/guix-compiler/pom.xml 2009-04-17 08:35:00 UTC (rev 1322) +++ guix/trunk/guix-compiler/pom.xml 2009-04-17 12:56:37 UTC (rev 1323) @@ -12,7 +12,7 @@ <version>1.0-SNAPSHOT</version> </parent> - <artifactId>guix-parser</artifactId> + <artifactId>guix-compiler</artifactId> <dependencies> <dependency> @@ -26,7 +26,7 @@ <!-- *** Project Information ************************************* --> <!-- ************************************************************* --> <name>guix-compiler</name> - <description>Guix parser</description> + <description>Guix compiler</description> <!-- ************************************************************* --> <!-- *** Build Settings ****************************************** --> @@ -43,18 +43,4 @@ <!--url>http://labs.libre-entreprise.org/plugins/scmsvn/viewcvs.php/guix/trunk/?root=buix</url--> </scm> - <profiles> - <profile> - <build> - <plugins> - <plugin> - <groupId>org.codelutin.guix</groupId> - <artifactId>guix-maven-plugin</artifactId> - <version>${project.version}</version> - </plugin> - </plugins> - </build> - <id>default</id> - </profile> - </profiles> </project> Added: guix/trunk/guix-compiler/src/main/java/org/codelutin/guix/GuixLauncher.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/codelutin/guix/GuixLauncher.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/codelutin/guix/GuixLauncher.java 2009-04-17 12:56:37 UTC (rev 1323) @@ -0,0 +1,263 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.codelutin.guix; + +import org.codelutin.guix.compiler.GuixCompiler; +import org.codelutin.guix.compiler.*; +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.guix.model.ClassDescriptor; +import org.codelutin.guix.model.GuixModelObject; + +/** + * Launch Guix files compilation + * + * @author morin + */ +public class GuixLauncher { + + /** log */ + /*protected static final Log log = + LogFactory.getLog(GuixLauncher.class);*/ + /** original list of files to compile */ + protected final File[] files; + /** original list of classes to compile */ + protected final String[] classNames; + /** Files to be treated while compilation. */ + protected List<File> guixFiles = new ArrayList<File>(); + /** Class names corresponding to the files in the guixFiles list. */ + protected List<String> guixFileClassNames = new ArrayList<String>(); + /** Maps the names of classes being compiled to the compiler instance + * handling the compilation. */ + protected Map<String, GuixCompiler> compilers = + new HashMap<String, GuixCompiler>(); + /** ModelObjects which are root of files */ + protected List<GuixModelObject> rootModelObjects = new ArrayList<GuixModelObject>(); + /** CLassDescriptor met during the compilation */ + protected List<ClassDescriptor> classDescriptors = + new ArrayList<ClassDescriptor>(); + protected int compilerCount; + private File targetDirectory = new File("destination"); + + public GuixLauncher() { + files = new File[]{new File("/home/morin/NetBeansProjects/jaxx-gwt/trunk/jaxx-example/src/main/java/jaxx/demo/JAXXDemo.jaxx")}; + classNames = new String[]{"JAXXDemo"}; + compile(); + } + + /** + * Compiled a set of files. + * + * @return <code>true</code> if compilation succeeds, + * <code>false</code> otherwise + */ + public synchronized boolean compile() { + //reset(); // just to be safe... + compilerCount = 0; + guixFiles.addAll(Arrays.asList(files)); + guixFileClassNames.addAll(Arrays.asList(classNames)); + try { + boolean success = true; + + // pass 1 + /*if (!nextStep(LifeCycle.compile_first_pass, success)) { + return false; + }*/ + boolean compiled; + do { + compiled = false; + assert guixFiles.size() == guixFileClassNames.size(); + // clone it so it can safely be modified while we're iterating + Iterator<File> filesIterator = + new ArrayList<File>(guixFiles).iterator(); + Iterator<String> classNamesIterator = + new ArrayList<String>(guixFileClassNames).iterator(); + + while (filesIterator.hasNext()) { + + File file = filesIterator.next(); + String className = classNamesIterator.next(); + /*if (log.isDebugEnabled()) { + log.debug("compile first pass for " + className); + }*/ + //if (symbolTables.get(file) == null) { + compiled = true; + if (compilers.containsKey(className)) { + //throw new CompilerException("Internal error: " + className + " is already being compiled, attempting to compile it again"); + } + + File destDir = targetDirectory; + if (destDir != null) { + int dotPos = className.lastIndexOf("."); + if (dotPos != -1) { + destDir = new File(destDir, className.substring(0, dotPos).replace('.', File.separatorChar)); + } + if (!destDir.exists() && !destDir.mkdirs()) { + //log.warn("couldn't create directory " + destDir); + System.out.println("couldn't create directory " + destDir); + continue; + } + } else { + //destDir = file.getParentFile(); + } + GuixCompiler compiler = new GuixCompiler( + file.getParentFile(), file, className,this); + //addProfileTime(compiler, currentPass.name() + "_start"); + compilers.put(className, compiler); + GuixModelObject rootModelObject = compiler.compile(); + rootModelObjects.add(rootModelObject); + //addProfileTime(compiler, currentPass.name() + "_end"); + //assert !symbolTables.values().contains(compiler.getSymbolTable()) : "symbolTable is already registered"; + //symbolTables.put(file, compiler.getSymbolTable()); + if (compiler.isFailed()) { + success = false; + } + //} + } + + } while (compiled); + + for(GuixModelObject mo : rootModelObjects) { + //log.trace("class : " + System.out.println("class : " + + mo.getClassDescriptor().getPackageName()+ "." + + mo.getClassDescriptor().getName() + + " extends " + + mo.getClassDescriptor().getSuperClass().getPackageName() + + "." + mo.getClassDescriptor().getSuperClass().getName()); + + } + + /*// pass 2 + if (!nextStep(LifeCycle.compile_second_pass, success)) { + return false; + } + + assert guixFiles.size() == guixFileClassNames.size(); + List<File> jaxxFilesClone = new ArrayList<File>(guixFiles); + for (String className : guixFileClassNames) { + JAXXCompiler compiler = getCompiler(className, "Internal error: could not find compiler for " + className + " during second pass"); + addProfileTime(compiler, currentPass.name() + "_start"); + if (log.isDebugEnabled()) { + log.debug("runInitializers for " + className); + } + if (!compiler.isFailed()) { + compiler.runInitializers(); + } + if (log.isDebugEnabled()) { + log.debug("compile second pass for " + className); + } + compiler.compileSecondPass(); + addProfileTime(compiler, currentPass.name() + "_end"); + if (log.isDebugEnabled()) { + log.debug("done with result [" + !compiler.isFailed() + "] for " + className); + } + if (compiler.isFailed()) { + success = false; + } + } + if (!jaxxFilesClone.equals(guixFiles)) { + throw new AssertionError("Internal error: compilation set altered during pass 2 (was " + jaxxFilesClone + ", modified to " + guixFiles + ")"); + } + + // stylesheet application + if (!nextStep(LifeCycle.stylesheet_pass, success)) { + return false; + } + assert guixFiles.size() == guixFileClassNames.size(); + for (String className : guixFileClassNames) { + JAXXCompiler compiler = getCompiler(className, "Internal error: could not find compiler for " + className + " during stylesheet application"); + addProfileTime(compiler, currentPass.name() + "_start"); + compiler.applyStylesheets(); + addProfileTime(compiler, currentPass.name() + "_end"); + if (compiler.isFailed()) { + success = false; + } + } + + // code generation + if (!nextStep(LifeCycle.generate_pass, success)) { + return false; + } + assert guixFiles.size() == guixFileClassNames.size(); + List<Generator> generators = new ArrayList<Generator>(); + for (Generator generator : ServiceLoader.load(Generator.class)) { + generators.add(generator); + } + for (String className : guixFileClassNames) { + JAXXCompiler compiler = getCompiler(className, "Internal error: could not find compiler for " + className + " during code generation"); + addProfileTime(compiler, currentPass.name() + "_start"); + compiler.generateCode(generators); + addProfileTime(compiler, currentPass.name() + "_end"); + //compiler.generateCode(); + if (compiler.isFailed()) { + success = false; + } + } + + if (options.isProfile()) { + // profile pass (only if succes compile) + if (!nextStep(LifeCycle.profile_pass, success)) { + return false; + } + StringBuilder buffer = profiler.computeProfileReport(); + log.info(buffer.toString()); + } + + return report(success); + + //FIXME : deal better the exception treatment... + } catch (CompilerException e) { + System.err.println(e.getMessage()); + e.printStackTrace(); + return false;*/ + + } catch (Throwable e) { + e.printStackTrace(); + return false; + } finally { + compilerCount = compilers.size(); + //TC - 20081018 only reset when no error was detected + //if (options.isResetAfterCompile() && errorCount == 0) { + // reset(); + //} + return false; + } + } + + public boolean registerClassDescriptor(ClassDescriptor classDescriptor) { + int i = 0; + while ((i < classDescriptors.size()) + && (!classDescriptors.get(i).getName().equals(classDescriptor.getName())) + && (!classDescriptors.get(i).getPackageName().equals(classDescriptor.getPackageName()))) { + i++; + } + //if the ClassDescriptor does not exist + if (i < classDescriptors.size()) { + classDescriptors.add(classDescriptor); + return true; + } + if (classDescriptor.getScript() != null) { + if(classDescriptors.get(i).getScript() == null) + classDescriptors.get(i).setScript(classDescriptor.getScript()); + else if (!classDescriptors.get(i).getScript().equals(classDescriptor.getScript())) + return false; + } + if(classDescriptor.getSuperClass() != null) { + if (classDescriptors.get(i).getSuperClass() == null) + classDescriptors.get(i).setSuperClass(classDescriptor.getSuperClass()); + else if (!classDescriptors.get(i).getSuperClass().equals(classDescriptor.getSuperClass())) + return false; + } + return true; + } +}
participants (1)
-
kmorin@users.labs.libre-entreprise.org