Index: LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/core/participation/TemplateCompilerParticipant.java diff -u /dev/null LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/core/participation/TemplateCompilerParticipant.java:1.1 --- /dev/null Wed Jun 20 12:40:40 2007 +++ LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/core/participation/TemplateCompilerParticipant.java Wed Jun 20 12:40:35 2007 @@ -0,0 +1,302 @@ +/* *##% + * Copyright (C) 2007 Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.eclipse.generator.core.participation; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.codelutin.processor.Processor; +import org.codelutin.processor.filters.Filter; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.compiler.BuildContext; +import org.eclipse.jdt.core.compiler.CompilationParticipant; +import org.eclipse.jdt.core.compiler.ReconcileContext; + +/** + * TemplateCompilerParticipant.java + * + * @author chatellier + * @version $Revision: 1.1 $ + * + * @deprecated Essai non concluant de compilation en utilisant lutinprocessor + * + * Last update : $Date: 2007/06/20 12:40:35 $ + * By : $Author: chatellier $ + */ +public class TemplateCompilerParticipant extends CompilationParticipant { + + /** + * Filters + * TODO a externaliser (configuration) + */ + protected static String[] filters = { + "org.codelutin.processor.filters.GeneratorTemplatesFilter" + }; + + /** + * src-build folder + * TODO a externalise configuration + */ + protected final String relativeGeneratedSourcesDirectory = "target" + File.separator + "src-build"; + + /** + * Current project + */ + protected IJavaProject project; + + /** + * Lutin processor + */ + private Processor processor; + + /** + * Consructor. + * + * Initialise processor + */ + public TemplateCompilerParticipant() { + processor = new Processor(getFilters()); + } + + /* (non-Javadoc) + * @see org.eclipse.jdt.core.compiler.CompilationParticipant#aboutToBuild(org.eclipse.jdt.core.IJavaProject) + */ + @Override + public int aboutToBuild(IJavaProject project) { + // about to build project + // generate generated sources directory + try { + new File(project.getCorrespondingResource().getLocation() + + File.separator + relativeGeneratedSourcesDirectory).mkdirs(); + } catch (JavaModelException e1) { + e1.printStackTrace(); + } + + // save project + this.project = project; + + System.err.println("aboutToBuild project"); + try { + System.out.println(" project Output " + project.getOutputLocation()); + //System.out.println(" project Output " + project.); + } catch (JavaModelException e) { + e.printStackTrace(); + } + + return super.aboutToBuild(project); + } + + /* (non-Javadoc) + * @see org.eclipse.jdt.core.compiler.CompilationParticipant#buildStarting(org.eclipse.jdt.core.compiler.BuildContext[], boolean) + */ + @Override + public void buildStarting(BuildContext[] files, boolean isBatch) { + + System.out.println("buildStarting isBatch : " + isBatch); + + // pour tous les fichier a recompiler + for(BuildContext file : files) { + String inputFileName = getInputFileName(file); + String outputFileName = getOutputFileName(file); + + //System.out.println(" inputFileName : " + inputFileName); + //System.out.println(" outputFileName : " + outputFileName); + + // creation du repertoire pour le fichier destination + new File(outputFileName).getParentFile().mkdirs(); + try { + processor.process(new FileReader(inputFileName), + new FileWriter(outputFileName)); + } catch (IOException e) { + // TODO add eclipse error + } + + IFile genfile = project.getProject().getFile(File.separator + relativeGeneratedSourcesDirectory + + File.separator + fullPathToFilePath(file.getFile().getFullPath().toOSString())); + + IFile delfile = project.getProject().getFile(File.separator + "target" + + File.separator + (fullPathToFilePath(file.getFile().getFullPath().toOSString())).replaceFirst("\\.java", ".class")); + System.out.println("final : " + genfile.getLocation().toOSString()); + System.out.println("final (class): " + delfile.getLocation().toOSString()); + file.recordDeletedGeneratedFiles(new IFile[]{delfile}); + file.recordAddedGeneratedFiles(new IFile[]{genfile}); + } + + + // blha blahh blah + // If we're building, types can be generated, so we + // want to run this as an atomic workspace operation + RunnableCompiler runnable = + new RunnableCompiler(project,files); + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + try { + workspace.run(runnable, project.getResource(), IWorkspace.AVOID_UPDATE, null); + } + catch (CoreException ce) { + // TODO log + } + + // end blah blah + + super.buildStarting(files, isBatch); + } + + /** + * Retourne le chemin relatif depuis / + * @param file + * @return le chmein absolu + */ + private String getInputFileName(BuildContext file) { + return file.getFile().getLocation().toOSString(); + } + + /** + * Retourne output file name depuis / + * en y incluant le dossier de generation temporaire + * + * @param file + * @return + */ + private String getOutputFileName(BuildContext file) { + + // result + String outputFileName = null; + + try { + // chemin comprenant le nom du projet + // ex : /topia/src/java/org/codelutin/topia/generator/UnGenerator.java + String relativePath = file.getFile().getFullPath().toOSString(); + + relativePath = fullPathToFilePath(relativePath); + + // ici on a /org/codelutin/topia/generator/UnGenerator.java + + // on renvoie + // le chemin absolut du projet / dossier de generation / ce qu'on a deja + // ex : /home/moi/projet/topia/target/src-build/org/codelutin/topia/generator/UnGenerator.java + + outputFileName = project.getCorrespondingResource().getLocation() + + File.separator + relativeGeneratedSourcesDirectory // TODO dans une config + + relativePath; + } catch (JavaModelException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return outputFileName; + } + + private String fullPathToFilePath(String fullpath) { + // explore toutes les entrees du buildpath du projet + // qui sont des sources (pas des jar) => CPE_SOURCE + // ca renvoie par exemple : + // /topia/src/java + // /topia/src/test + // Si on detecte ce motif en debut de path, on le supprime + try { + for(IClasspathEntry entry : project.getRawClasspath()) { + if(entry.getEntryKind() == IClasspathEntry.CPE_SOURCE){ + String theEntry = entry.getPath().toOSString(); + //System.out.println(" entry " + theEntry); + //System.out.println(" relativePath " + fullpath); + if(fullpath.startsWith(theEntry)) { + fullpath = fullpath.substring(theEntry.length()); + } + } + } + } catch (JavaModelException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return fullpath; + } + + /* (non-Javadoc) + * @see org.eclipse.jdt.core.compiler.CompilationParticipant#cleanStarting(org.eclipse.jdt.core.IJavaProject) + */ + @Override + public void cleanStarting(IJavaProject project) { + + // about to build project + // generate generated sources directory + try { + //TOD clean + new File(project.getCorrespondingResource().getLocation() + + File.separator + relativeGeneratedSourcesDirectory).delete(); + } catch (JavaModelException e1) { + e1.printStackTrace(); + } + + + + super.cleanStarting(project); + } + + /* (non-Javadoc) + * @see org.eclipse.jdt.core.compiler.CompilationParticipant#isActive(org.eclipse.jdt.core.IJavaProject) + */ + @Override + public boolean isActive(IJavaProject project) { + // TODO config, parametrable + return true; + } + + /* (non-Javadoc) + * @see org.eclipse.jdt.core.compiler.CompilationParticipant#reconcile(org.eclipse.jdt.core.compiler.ReconcileContext) + */ + @Override + public void reconcile(ReconcileContext context) { + // do nothing by default + //super.reconcile(context); + } + + /** + * Convert filter names into filter instance + * + * TODO a deplacer et pas a effectuer a chaque compilation + * @return les instances de filter + */ + protected Filter[] getFilters() { + + List result = new ArrayList(); + + for (String filterName : filters) { + try { + result.add((Filter) Class.forName(filterName).newInstance()); + } catch (Exception e) { + // TODO add eclipse errors + } + } + + // Conversion de la List en tableau + Filter[] param = new Filter[result.size()]; + result.toArray(param); + return param; + } +} Index: LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/core/participation/RunnableCompiler.java diff -u /dev/null LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/core/participation/RunnableCompiler.java:1.1 --- /dev/null Wed Jun 20 12:40:40 2007 +++ LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/core/participation/RunnableCompiler.java Wed Jun 20 12:40:35 2007 @@ -0,0 +1,135 @@ +/* *##% + * Copyright (C) 2007 Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.eclipse.generator.core.participation; + +import org.eclipse.core.resources.IWorkspaceRunnable; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.BindingKey; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.compiler.BuildContext; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.ASTRequestor; +import org.eclipse.jdt.core.dom.IBinding; + +/** + * RunnableCompiler + * + * @author chatellier + * @version $Revision: 1.1 $ + * + * @deprecated Essai non concluant de compilation en utilisant lutinprocessor + * + * Last update : $Date: 2007/06/20 12:40:35 $ + * By : $Author: chatellier $ + */ +public class RunnableCompiler implements IWorkspaceRunnable { + + protected BuildContext[] filesToBuild; + + protected IJavaProject project; + + /** + * Constructor. + * + * @param filesToBuild files to build + */ + public RunnableCompiler(IJavaProject project, BuildContext[] filesToBuild) { + this.project = project; + this.filesToBuild = filesToBuild; + } + + /* (non-Javadoc) + * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor) + */ + public void run(IProgressMonitor monitor) throws CoreException { + System.out.println("run run run..."); + build(); + } + + /** + * + */ + private void build() { + // Construct build environment, this invokes the build inside a callback + // in order to keep open the DOM AST pipeline + /*BuildEnv.newBuildEnv( + _filesWithAnnotation, + _filesWithoutAnnotation, + _aptProject.getJavaProject(), + buildCallback);*/ + + createASTs(filesToBuild); + } + + void createASTs(BuildContext[] cpResults){ + final int len = cpResults.length; + final ICompilationUnit[] units = new ICompilationUnit[len]; + for( int i=0; i