[hibernate-commits] Hibernate SVN: r17800 - in core/branches/gradle: buildSrc and 11 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Oct 19 17:33:36 EDT 2009


Author: steve.ebersole at jboss.com
Date: 2009-10-19 17:33:35 -0400 (Mon, 19 Oct 2009)
New Revision: 17800

Added:
   core/branches/gradle/buildSrc/
   core/branches/gradle/buildSrc/build.gradle
   core/branches/gradle/buildSrc/lib/
   core/branches/gradle/buildSrc/lib/gradle-core-0.8.jar
   core/branches/gradle/buildSrc/lib/gradle-open-api-0.8.jar
   core/branches/gradle/buildSrc/src/
   core/branches/gradle/buildSrc/src/main/
   core/branches/gradle/buildSrc/src/main/java/
   core/branches/gradle/buildSrc/src/main/java/org/
   core/branches/gradle/buildSrc/src/main/java/org/gradle/
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrConvention.java
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrPlugin.java
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrSourceVirtualDirectory.java
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrSourceVirtualDirectoryImpl.java
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrTask.java
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/Grammar.java
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/GrammarFile.java
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/MetadataExtracter.java
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/XRef.java
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/plan/
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/plan/GenerationPlan.java
   core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/plan/GenerationPlanBuilder.java
Modified:
   core/branches/gradle/build.gradle
   core/branches/gradle/hibernate-core/build.gradle
Log:
have test resources defined in src/test/java copied over to test classes dir

Modified: core/branches/gradle/build.gradle
===================================================================
--- core/branches/gradle/build.gradle	2009-10-19 16:48:53 UTC (rev 17799)
+++ core/branches/gradle/build.gradle	2009-10-19 21:33:35 UTC (rev 17800)
@@ -61,8 +61,17 @@
     buildDirName = "target"
 
     // by default, compile to 1.4 compatibility
-    sourceTarget = "1.4"
+    targetCompatibility = "1.4"
     sourceCompatibility = "1.4"
 }
 
-dependsOnChildren()
\ No newline at end of file
+buildscript {
+    dependencies {
+        antlrVersion = '2.7.6'
+        classpath (
+                [group: 'antlr', name: 'antlr', version: antlrVersion]
+        )
+    }
+}
+
+dependsOnChildren()


Property changes on: core/branches/gradle/buildSrc
___________________________________________________________________
Name: svn:ignore
   + .gradle
build
target


Added: core/branches/gradle/buildSrc/build.gradle
===================================================================
--- core/branches/gradle/buildSrc/build.gradle	                        (rev 0)
+++ core/branches/gradle/buildSrc/build.gradle	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,21 @@
+usePlugin "java"
+
+repositories {
+    // todo : what is the best way to handle this for OSS libraries?
+    //      considering too we need to allow for user config (ala repo managers etc)
+    mavenRepo name: "artifactory", urls: "http://localhost:8081/artifactory/repo/"
+}
+
+dependencies {
+    antVersion = '1.6.5'
+    antlrVersion = '2.7.6'
+
+    compile (
+            [group: 'ant', name: 'ant', version: antVersion],
+            [group: 'ant', name: 'ant-antlr', version: antVersion],
+            [group: 'antlr', name: 'antlr', version: antlrVersion],
+            [group: 'org.slf4j', name: 'slf4j-api', version: '1.5.8'],
+            [group: 'org.codehaus.groovy', name: 'groovy-all', version: '1.6.4'],
+            files('lib/gradle-core-0.8.jar', 'lib/gradle-open-api-0.8.jar')
+    )
+}
\ No newline at end of file

Added: core/branches/gradle/buildSrc/lib/gradle-core-0.8.jar
===================================================================
(Binary files differ)


Property changes on: core/branches/gradle/buildSrc/lib/gradle-core-0.8.jar
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: core/branches/gradle/buildSrc/lib/gradle-open-api-0.8.jar
===================================================================
(Binary files differ)


Property changes on: core/branches/gradle/buildSrc/lib/gradle-open-api-0.8.jar
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrConvention.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrConvention.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrConvention.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,36 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class AntlrConvention {
+    private boolean trace;
+    private boolean traceLexer;
+    private boolean traceParser;
+    private boolean traceTreeWalker;
+}

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrPlugin.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrPlugin.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrPlugin.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,117 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr;
+
+import java.io.File;
+
+import org.gradle.api.Action;
+import org.gradle.api.Plugin;
+import org.gradle.api.Project;
+import org.gradle.api.artifacts.Configuration;
+import org.gradle.api.internal.DynamicObjectAware;
+import org.gradle.api.internal.IConventionAware;
+import org.gradle.api.internal.project.ProjectInternal;
+import org.gradle.api.internal.tasks.DefaultSourceSet;
+import org.gradle.api.plugins.Convention;
+import org.gradle.api.plugins.JavaPlugin;
+import static org.gradle.api.plugins.JavaPlugin.COMPILE_CONFIGURATION_NAME;
+import org.gradle.api.plugins.JavaPluginConvention;
+import org.gradle.api.plugins.ProjectPluginsContainer;
+import org.gradle.api.tasks.ConventionValue;
+import org.gradle.api.tasks.SourceSet;
+
+
+/**
+ * A {@link Plugin} which extends the {@link JavaPlugin} to provide support for compiling and documenting Groovy
+ * source files.
+ *
+ * @author Steve Ebersole
+ */
+public class AntlrPlugin implements Plugin {
+	public static final String ANTLR_CONFIGURATION_NAME = "antlr";
+
+
+    public void use(final Project project, ProjectPluginsContainer projectPluginsHandler) {
+        projectPluginsHandler.usePlugin( JavaPlugin.class, project );
+
+		// For now, just expect the user to have specified a configuration named
+		// 'antlr' for
+//        Configuration antlrConfiguration = project.getConfigurations()
+//				.getByName( ANTLR_CONFIGURATION_NAME );
+//		assert antlrConfiguration != null
+//				: "You must specify a configuration named '" + ANTLR_CONFIGURATION_NAME + "' with the ant-antlr and antlr jars";
+//        project.getConfigurations().getByName( COMPILE_CONFIGURATION_NAME ).extendsFrom( antlrConfiguration );
+
+        final ProjectInternal projectInternal = (ProjectInternal) project;
+		project.getConvention().getPlugin( JavaPluginConvention.class ).getSourceSets().allObjects(
+				new Action<SourceSet>() {
+					public void execute(SourceSet sourceSet) {
+						// for each source set we will:
+						// 		1) Add a new 'antlr' virtual directory mapping
+						final AntlrSourceVirtualDirectoryImpl antlrDirectoryDelegate = new AntlrSourceVirtualDirectoryImpl(
+								( (DefaultSourceSet) sourceSet ).getDisplayName(),
+								projectInternal.getFileResolver()
+						);
+						( (DynamicObjectAware) sourceSet ).getConvention().getPlugins().put(
+								AntlrSourceVirtualDirectory.NAME,
+								antlrDirectoryDelegate
+						);
+						final String srcDir = String.format( "src/%s/antlr", sourceSet.getName() );
+						antlrDirectoryDelegate.getAntlr().srcDir( srcDir );
+						sourceSet.getAllSource().add( antlrDirectoryDelegate.getAntlr() );
+
+						//		2) create an AntlrTask for this sourceSet following the gradle
+						//			naming conventions via call to sourceSet.getCompileTaskName
+						final String taskName = sourceSet.getCompileTaskName( "antlr" );
+						AntlrTask antlrTask = project.getTasks().add( taskName, AntlrTask.class );
+						antlrTask.setDescription(
+								String.format( "Processes the %s Antlr grammars.", sourceSet.getName() )
+						);
+						antlrTask.conventionMapping(
+								"defaultSource",
+								new ConventionValue() {
+									public Object getValue(Convention convention, IConventionAware conventionAwareObject) {
+										return antlrDirectoryDelegate.getAntlr();
+									}
+								}
+						);
+
+						//		3) Set up the Antlr output directory (adding to javac inputs!)
+						final String outputDirectoryName = String.format(
+								"%s/generated-src/antlr/%s",
+								project.getBuildDir(),
+								sourceSet.getName()
+						);
+						final File outputDirectory = new File( outputDirectoryName );
+						antlrTask.setOutputDirectory( outputDirectory );
+						sourceSet.getJava().srcDir( outputDirectory );
+
+						//		4) register fact that antlr should be run before compiling
+						project.getTasks().getByName( sourceSet.getCompileTaskName( "java" ) )
+								.dependsOn( taskName );
+					}
+				}
+		);
+    }
+}

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrSourceVirtualDirectory.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrSourceVirtualDirectory.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrSourceVirtualDirectory.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,61 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr;
+
+import org.gradle.api.file.SourceDirectorySet;
+import org.gradle.api.file.FileTree;
+import groovy.lang.Closure;
+
+/**
+ * Virtual directory mapping handler for 'antlr'.
+ *
+ * @author Steve Ebersole
+ */
+public interface AntlrSourceVirtualDirectory {
+	public static final String NAME = "antlr";
+
+	/**
+	 * All Antlr source for this source set.
+	 *
+	 * @return The Antlr source.  Never returns null.
+	 */
+	public SourceDirectorySet getAntlr();
+
+	/**
+	 * Configures the Antlr source for this set. The given closure is used to configure the {@code SourceDirectorySet}
+	 * (see {@link #getAntlr}) which contains the Antlr source.
+	 *
+	 * @param configureClosure The closure to use to configure the Antlr source.
+	 *
+	 * @return this
+	 */
+	public AntlrSourceVirtualDirectory antlr(Closure configureClosure);
+
+	/**
+	 * All Antlr source for this source set.
+	 *
+	 * @return The Antlr source. Never returns null.
+	 */
+	public FileTree getAllAntlr();
+}

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrSourceVirtualDirectoryImpl.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrSourceVirtualDirectoryImpl.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrSourceVirtualDirectoryImpl.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,71 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr;
+
+import org.gradle.api.file.SourceDirectorySet;
+import org.gradle.api.internal.file.UnionFileTree;
+import org.gradle.api.internal.file.DefaultSourceDirectorySet;
+import org.gradle.api.internal.file.FileResolver;
+import org.gradle.api.tasks.util.PatternFilterable;
+import org.gradle.api.tasks.util.PatternSet;
+import org.gradle.util.ConfigureUtil;
+import groovy.lang.Closure;
+
+/**
+ * Acts as a delegate to the {@link org.gradle.api.internal.DynamicObjectAware} portion of
+ * Gradle's {@link org.gradle.api.tasks.SourceSet} in order to add Antlr-relate directory
+ * mapping to the source sets.
+ *
+ * @author Steve Ebersole
+ */
+public class AntlrSourceVirtualDirectoryImpl implements AntlrSourceVirtualDirectory {
+	private final SourceDirectorySet antlr;
+    private final UnionFileTree allAntlr;
+    private final PatternFilterable antlrPatterns = new PatternSet();
+
+	public AntlrSourceVirtualDirectoryImpl(String parentDisplayName, FileResolver fileResolver) {
+		final String displayName = String.format( "%s Antlr source", parentDisplayName );
+		antlr = new DefaultSourceDirectorySet( displayName, fileResolver );
+		antlr.getFilter().include( "**/*.g" );
+        antlrPatterns.include( "**/*.g" );
+        allAntlr = new UnionFileTree( displayName, antlr.matching( antlrPatterns ) );
+	}
+
+	public SourceDirectorySet getAntlr() {
+		return antlr;
+	}
+
+	public AntlrSourceVirtualDirectory antlr(Closure configureClosure) {
+        ConfigureUtil.configure( configureClosure, getAntlr() );
+        return this;
+    }
+
+	public UnionFileTree getAllAntlr() {
+		return allAntlr;
+	}
+
+	public PatternFilterable getAntlrSourcePatterns() {
+		return antlrPatterns;
+	}
+}

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrTask.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrTask.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/AntlrTask.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,124 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr;
+
+import java.io.File;
+import java.util.List;
+
+import org.gradle.api.tasks.SourceTask;
+import org.gradle.plugin.antlr.metadata.XRef;
+import org.gradle.plugin.antlr.metadata.MetadataExtracter;
+import org.gradle.plugin.antlr.plan.GenerationPlanBuilder;
+import org.gradle.plugin.antlr.plan.GenerationPlan;
+import org.apache.tools.ant.taskdefs.optional.ANTLR;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class AntlrTask extends SourceTask {
+	private static final Logger log = LoggerFactory.getLogger( AntlrTask.class );
+
+    private boolean trace;
+    private boolean traceLexer;
+    private boolean traceParser;
+    private boolean traceTreeWalker;
+
+	private File outputDirectory;
+
+	public boolean isTrace() {
+		return trace;
+	}
+
+	public void setTrace(boolean trace) {
+		this.trace = trace;
+	}
+
+	public boolean isTraceLexer() {
+		return traceLexer;
+	}
+
+	public void setTraceLexer(boolean traceLexer) {
+		this.traceLexer = traceLexer;
+	}
+
+	public boolean isTraceParser() {
+		return traceParser;
+	}
+
+	public void setTraceParser(boolean traceParser) {
+		this.traceParser = traceParser;
+	}
+
+	public boolean isTraceTreeWalker() {
+		return traceTreeWalker;
+	}
+
+	public void setTraceTreeWalker(boolean traceTreeWalker) {
+		this.traceTreeWalker = traceTreeWalker;
+	}
+
+	public File getOutputDirectory() {
+		return outputDirectory;
+	}
+
+	public void setOutputDirectory(File outputDirectory) {
+		this.outputDirectory = outputDirectory;
+	}
+
+	@org.gradle.api.tasks.TaskAction
+	public void generate() {
+		//noinspection ResultOfMethodCallIgnored
+		outputDirectory.mkdirs();
+
+		// Determine the grammar files and the proper ordering amongst them
+		XRef xref = new MetadataExtracter().extractMetadata( getSource() );
+		List<GenerationPlan> generationPlans = new GenerationPlanBuilder( outputDirectory ).buildGenerationPlans( xref );
+
+		for ( GenerationPlan generationPlan : generationPlans ) {
+			if ( ! generationPlan.isOutOfDate() ) {
+				log.info( "grammar [" + generationPlan.getId() + "] was up-to-date; skipping" );
+				continue;
+			}
+
+			log.info( "performing grammar generation [" + generationPlan.getId() + "]" );
+
+			//noinspection ResultOfMethodCallIgnored
+			generationPlan.getGenerationDirectory().mkdirs();
+
+			ANTLR antlr = new ANTLR();
+			antlr.setTrace( trace );
+			antlr.setTraceLexer( traceLexer );
+			antlr.setTraceParser( traceParser );
+			antlr.setTraceTreeWalker( traceTreeWalker );
+			antlr.setOutputdirectory( generationPlan.getGenerationDirectory() );
+			antlr.setTarget( generationPlan.getSource() );
+
+			antlr.execute();
+		}
+	}
+}

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/Grammar.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/Grammar.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/Grammar.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,100 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr.metadata;
+
+import java.io.File;
+
+/**
+ * Models a grammar defined within an Antlr grammar file.
+ *
+ * @author Steve Ebersole
+ */
+public class Grammar {
+	private final GrammarFile grammarFile;
+	private String className;
+    private String superGrammarName;
+	private String importVocab;
+	private String exportVocab;
+
+	public Grammar(GrammarFile grammarFile) {
+		this.grammarFile = grammarFile;
+		grammarFile.addGrammar( this );
+	}
+
+	public GrammarFile getGrammarFile() {
+		return grammarFile;
+	}
+
+	public String getClassName() {
+		return className;
+	}
+
+	public void setClassName(String className) {
+		this.className = className;
+	}
+
+	public String getSuperGrammarName() {
+		return superGrammarName;
+	}
+
+	public void setSuperGrammarName(String superGrammarName) {
+		this.superGrammarName = superGrammarName;
+	}
+
+	public String getImportVocab() {
+		return importVocab;
+	}
+
+	public void setImportVocab(String importVocab) {
+		this.importVocab = importVocab;
+	}
+
+	public String getExportVocab() {
+		return exportVocab;
+	}
+
+	public void setExportVocab(String exportVocab) {
+		this.exportVocab = exportVocab;
+	}
+
+	public String getPackageName() {
+		return getGrammarFile().getPackageName();
+	}
+
+	public String determineGeneratedParserPath() {
+		if ( isEmpty( getPackageName() ) ) {
+			return getClassName() + ".java";
+		}
+		else {
+			return getPackageName().replace( '.', File.separatorChar )
+					+ File.separatorChar
+					+ getClassName()
+					+ ".java";
+		}
+	}
+
+	private boolean isEmpty(String packageName) {
+		return packageName == null || packageName.trim().length() == 0;
+	}
+}

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/GrammarFile.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/GrammarFile.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/GrammarFile.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,68 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr.metadata;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class GrammarFile {
+	private final String fileName;
+	private final String[] glibs;
+	private String packageName;
+	private List<Grammar> grammars = new ArrayList<Grammar>();
+
+	public GrammarFile(String fileName, String[] glibs) {
+		this.fileName = fileName;
+		this.glibs = glibs != null ? glibs : new String[0];
+	}
+
+	public String getFileName() {
+		return fileName;
+	}
+
+	public String[] getGlibs() {
+		return glibs;
+	}
+
+	public String getPackageName() {
+		return packageName;
+	}
+
+	void setPackageName(String packageName) {
+		this.packageName = packageName;
+	}
+
+	void addGrammar(Grammar grammar) {
+		grammars.add( grammar );
+	}
+
+	public List<Grammar> getGrammars() {
+		return grammars;
+	}
+}

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/MetadataExtracter.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/MetadataExtracter.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/MetadataExtracter.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,194 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr.metadata;
+
+import java.io.File;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.lang.reflect.Method;
+
+import org.gradle.api.file.FileTree;
+import antlr.collections.impl.IndexedVector;
+
+/**
+ * Preprocess an Antlr grammar file to that dependencies between grammars
+ * can be properly determined so that they can be passed to the Antlr {@link antlr.Tool}
+ * in proper order.
+ *
+ * @author Steve Ebersole
+ */
+public class MetadataExtracter {
+
+	public XRef extractMetadata(FileTree source) {
+		antlr.Tool tool = new antlr.Tool();
+		antlr.preprocessor.Hierarchy hierarchy = new antlr.preprocessor.Hierarchy( tool );
+
+		final ArrayList<GrammarFile> grammarFiles = new ArrayList<GrammarFile>();
+		for ( File grammarFileFile : source.getFiles() ) {
+			final String grammarFilePath = grammarFileFile.getPath();
+			GrammarFile grammarFile = new GrammarFile(
+					grammarFilePath,
+					null // todo : account for glibs
+			);
+
+			// :(  antlr.preprocessor.GrammarFile's only access to package is through a protected field :(
+			try {
+				BufferedReader in = new BufferedReader( new FileReader( grammarFileFile ) );
+				try {
+					String line;
+					while ( ( line = in.readLine() ) != null ) {
+						line = line.trim();
+						if ( line.startsWith( "package" ) && line.endsWith( ";" ) ) {
+							grammarFile.setPackageName( line.substring( 8, line.length() -1 ) );
+							break;
+						}
+					}
+				}
+				finally {
+					try {
+						in.close();
+					}
+					catch ( IOException ignore ) {
+					}
+				}
+			}
+			catch ( IOException e ) {
+				e.printStackTrace();
+			}
+
+			grammarFiles.add( grammarFile );
+
+			try {
+				hierarchy.readGrammarFile( grammarFilePath );
+			}
+			catch ( FileNotFoundException e ) {
+				// should never happen here
+				throw new IllegalStateException( "Received FileNotFoundException on already read file", e );
+			}
+		}
+
+		XRef xref = new XRef( hierarchy );
+		for ( GrammarFile grammarFile : grammarFiles ) {
+			final String grammarFilePath = grammarFile.getFileName();
+			try {
+				antlr.preprocessor.GrammarFile antlrToolGrammarFile = hierarchy.getFile( grammarFilePath );
+				intrepretMetadata( grammarFile, antlrToolGrammarFile );
+				xref.addGrammarFile( grammarFile );
+			}
+			catch ( Throwable t ) {
+				throw new IllegalStateException( "Unable to build grammar metadata", t );
+			}
+		}
+
+		return xref;
+	}
+
+	private void intrepretMetadata(GrammarFile grammarFile, antlr.preprocessor.GrammarFile antlrToolGrammarFile) {
+		final Enumeration antlrGrammars = antlrToolGrammarFile.getGrammars().elements();
+		while ( antlrGrammars.hasMoreElements() ) {
+			Grammar grammar = new Grammar( grammarFile );
+			intrepret( grammar, antlrGrammars.nextElement() );
+		}
+	}
+
+
+
+	private void intrepret(Grammar grammar, Object antlrGrammarDef) {
+		// antlr.preprocessor.Grammar is defined as package-protected, ugh...
+		final Class antlrGrammarClass = loadAntlrClass( "antlr.preprocessor.Grammar" );
+		final Class antlrOptionClass = loadAntlrClass( "antlr.preprocessor.Option" );
+
+		try {
+			final Method getNameMethod = antlrGrammarClass.getDeclaredMethod( "getName", NO_ARG_SIGNATURE );
+			getNameMethod.setAccessible( true );
+			final String name = ( String ) getNameMethod.invoke( antlrGrammarDef, NO_ARGS );
+			grammar.setClassName( name );
+
+			final Method getSuperGrammarNameMethod = antlrGrammarClass.getMethod( "getSuperGrammarName", NO_ARG_SIGNATURE );
+			getSuperGrammarNameMethod.setAccessible( true );
+			final String superGrammarName = ( String ) getSuperGrammarNameMethod.invoke( antlrGrammarDef, NO_ARGS );
+			grammar.setSuperGrammarName( superGrammarName );
+
+			Method getOptionsMethod = antlrGrammarClass.getMethod( "getOptions", NO_ARG_SIGNATURE );
+			getOptionsMethod.setAccessible( true );
+			IndexedVector options = ( IndexedVector ) getOptionsMethod.invoke( antlrGrammarDef, NO_ARGS );
+
+			Method getRHSMethod = antlrOptionClass.getMethod( "getRHS", NO_ARG_SIGNATURE );
+			getRHSMethod.setAccessible( true );
+
+			Object importVocabOption = options.getElement( "importVocab" );
+			if ( importVocabOption != null ) {
+				String importVocab = ( String ) getRHSMethod.invoke( importVocabOption, NO_ARGS );
+				if ( importVocab != null ) {
+					importVocab = importVocab.trim();
+					if ( importVocab.endsWith( ";" ) ) {
+						importVocab = importVocab.substring( 0, importVocab.length() - 1 );
+					}
+					grammar.setImportVocab( importVocab );
+				}
+			}
+
+			Object exportVocabOption = options.getElement( "exportVocab" );
+			if  ( exportVocabOption != null ) {
+				String exportVocab = ( String ) getRHSMethod.invoke( exportVocabOption, NO_ARGS );
+				if ( exportVocab != null ) {
+					exportVocab = exportVocab.trim();
+					if ( exportVocab.endsWith( ";" ) ) {
+						exportVocab = exportVocab.substring( 0, exportVocab.length() -1 );
+					}
+				}
+				grammar.setExportVocab( exportVocab );
+			}
+		}
+		catch ( Throwable t ) {
+			throw new IllegalStateException( "Error accessing  Antlr grammar metadata", t );
+		}
+	}
+
+	public static final Class[] NO_ARG_SIGNATURE = new Class[0];
+	public static final Object[] NO_ARGS = new Object[0];
+
+	private Class loadAntlrClass(String className) {
+		try {
+			return Class.forName( className, true, ANTLR_CLASSLOADER );
+		}
+		catch ( ClassNotFoundException e ) {
+			throw new IllegalStateException( "Unable to locate Antlr class [" + className + "]", e );
+		}
+	}
+
+	private static ClassLoader ANTLR_CLASSLOADER = determineAntlrClassLoader();
+
+	private static ClassLoader determineAntlrClassLoader() {
+		ClassLoader cl = Thread.currentThread().getContextClassLoader();
+		if ( cl == null ) {
+			cl = MetadataExtracter.class.getClassLoader();
+		}
+		return cl;
+	}
+}

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/XRef.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/XRef.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/metadata/XRef.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,89 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr.metadata;
+
+import java.util.LinkedHashMap;
+import java.util.HashMap;
+import java.util.Iterator;
+
+import antlr.preprocessor.Hierarchy;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class XRef {
+	private final Hierarchy antlrHierarchy;
+
+//	private LinkedHashMap<String, GrammarFile> filesById = new LinkedHashMap<String, GrammarFile>();
+	private LinkedHashMap<String, GrammarFile> filesByPath = new LinkedHashMap<String, GrammarFile>();
+	private HashMap<String, GrammarFile> filesByExportVocab = new HashMap<String, GrammarFile>();
+	private HashMap<String, GrammarFile> filesByClassName = new HashMap<String, GrammarFile>();
+
+	public XRef(Hierarchy antlrHierarchy) {
+		this.antlrHierarchy = antlrHierarchy;
+	}
+
+	public Object getAntlrHierarchy() {
+		return antlrHierarchy;
+	}
+
+	void addGrammarFile(GrammarFile grammarFile) {
+//		filesById.put( grammarFile.getId(), grammarFile );
+		filesByPath.put( grammarFile.getFileName(), grammarFile );
+		for ( Grammar grammar : grammarFile.getGrammars() ) {
+			filesByClassName.put( grammar.getClassName(), grammarFile );
+			if ( grammar.getExportVocab() != null ) {
+				GrammarFile old = filesByExportVocab.put( grammar.getExportVocab(), grammarFile );
+				if ( old != null && old != grammarFile ) {
+					System.out.println( "[WARNING] : multiple grammars defined the same exportVocab : " + grammar.getExportVocab() );
+				}
+			}
+		}
+	}
+
+//	public Iterator iterateGrammarFiles() {
+//		return filesById.values().iterator();
+//	}
+//
+//	public GrammarFile getGrammarFileById(String id) {
+//		return filesById.get( id );
+//	}
+	public Iterator<GrammarFile> iterateGrammarFiles() {
+		return filesByPath.values().iterator();
+	}
+
+	public GrammarFile getGrammarFileByPath(String path) {
+		return filesByPath.get( path );
+	}
+
+	public GrammarFile getGrammarFileByClassName(String className) {
+		return filesByClassName.get( className );
+	}
+
+	public GrammarFile getGrammarFileByExportVocab(String exportVocab) {
+		return filesByExportVocab.get( exportVocab );
+	}
+}

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/plan/GenerationPlan.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/plan/GenerationPlan.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/plan/GenerationPlan.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,90 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr.plan;
+
+import java.io.File;
+import java.util.LinkedHashSet;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class GenerationPlan {
+	private final String id;
+	private final File source;
+	private final File generationDirectory;
+
+	private File importVocabTokenTypesDirectory;
+	private boolean outOfDate;
+
+	private LinkedHashSet<String> collectedSuperGrammarIds = new LinkedHashSet<String>();
+
+	GenerationPlan(String id, File source, File generationDirectory, String[] glibIds) {
+		this.id = id;
+		this.source = source;
+		this.generationDirectory = generationDirectory;
+		if ( glibIds != null ) {
+			for ( String glibId : glibIds ) {
+				addSuperGrammarId( glibId );
+			}
+		}
+	}
+
+	public String getId() {
+		return id;
+	}
+
+	public File getSource() {
+		return source;
+	}
+
+	public File getGenerationDirectory() {
+		return generationDirectory;
+	}
+
+	void addSuperGrammarId(String id) {
+		collectedSuperGrammarIds.add( id );
+	}
+
+	public LinkedHashSet<String> getCollectedSuperGrammarIds() {
+		return collectedSuperGrammarIds;
+	}
+
+	public File getImportVocabTokenTypesDirectory() {
+		return importVocabTokenTypesDirectory;
+	}
+
+	void setImportVocabTokenTypesDirectory(File importVocabTokenTypesDirectory) {
+		this.importVocabTokenTypesDirectory = importVocabTokenTypesDirectory;
+	}
+
+	public boolean isOutOfDate() {
+		return outOfDate;
+	}
+
+	void markOutOfDate() {
+		this.outOfDate = true;
+	}
+}

Added: core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/plan/GenerationPlanBuilder.java
===================================================================
--- core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/plan/GenerationPlanBuilder.java	                        (rev 0)
+++ core/branches/gradle/buildSrc/src/main/java/org/gradle/plugin/antlr/plan/GenerationPlanBuilder.java	2009-10-19 21:33:35 UTC (rev 17800)
@@ -0,0 +1,193 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA\
+ */
+package org.gradle.plugin.antlr.plan;
+
+import java.io.File;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Iterator;
+import java.util.ArrayList;
+
+import org.gradle.plugin.antlr.metadata.XRef;
+import org.gradle.plugin.antlr.metadata.GrammarFile;
+import org.gradle.plugin.antlr.metadata.Grammar;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class GenerationPlanBuilder {
+	private static final Logger log = LoggerFactory.getLogger( GenerationPlanBuilder.class );
+
+	private final LinkedHashMap<String,GenerationPlan> generationPlans = new LinkedHashMap<String,GenerationPlan>();
+	private final File outputDirectory;
+
+	private XRef metadataXRef;
+
+	public GenerationPlanBuilder(File outputDirectory) {
+		this.outputDirectory = outputDirectory;
+	}
+
+	public synchronized List<GenerationPlan> buildGenerationPlans(XRef metadataXRef) {
+		this.metadataXRef = metadataXRef;
+
+		Iterator<GrammarFile> grammarFiles = metadataXRef.iterateGrammarFiles();
+		while ( grammarFiles.hasNext() ) {
+			final GrammarFile grammarFile = grammarFiles.next();
+			// NOTE : loacteOrBuildGenerationPlan populates the generationPlans map
+			loacteOrBuildGenerationPlan( grammarFile );
+		}
+
+		return new ArrayList<GenerationPlan>( generationPlans.values() );
+	}
+
+	private GenerationPlan loacteOrBuildGenerationPlan(GrammarFile grammarFile) {
+		GenerationPlan generationPlan = generationPlans.get( grammarFile.getFileName() );
+		if ( generationPlan == null ) {
+			generationPlan = buildGenerationPlan( grammarFile );
+		}
+		return generationPlan;
+	}
+
+	private GenerationPlan buildGenerationPlan(GrammarFile grammarFile) {
+		File generationDirectory = isEmpty( grammarFile.getPackageName() )
+				? outputDirectory
+				: new File( outputDirectory, grammarFile.getPackageName().replace( '.', File.separatorChar ) );
+
+		GenerationPlan generationPlan = new GenerationPlan(
+				grammarFile.getFileName(),
+				new File( grammarFile.getFileName() ),
+				generationDirectory,
+				grammarFile.getGlibs()
+		);
+
+		File leastRecentGrammarOutput = locateLeastRecentlyModifiedOutputFile( generationDirectory );
+
+		// see if the grammar is out-of-date by way super-grammars from user defined glib options
+		for ( int i = 0; i < grammarFile.getGlibs().length; i++ ) {
+			final GrammarFile superGrammarGrammarFile =  metadataXRef.getGrammarFileByPath( grammarFile.getGlibs()[i] );
+			final GenerationPlan superGrammarGenerationPlan = loacteOrBuildGenerationPlan( superGrammarGrammarFile );
+			if ( superGrammarGenerationPlan.isOutOfDate() ) {
+				generationPlan.markOutOfDate();
+			}
+			else if ( superGrammarGenerationPlan.getSource().lastModified() > leastRecentGrammarOutput.lastModified() ) {
+				generationPlan.markOutOfDate();
+			}
+		}
+
+		for ( Grammar grammar : grammarFile.getGrammars() ) {
+			final File generatedParserFile = new File(
+					outputDirectory,
+					grammar.determineGeneratedParserPath()
+			);
+
+			if ( !generatedParserFile.exists() ) {
+				generationPlan.markOutOfDate();
+			}
+			else if ( generatedParserFile.lastModified() < generationPlan.getSource().lastModified() ) {
+				generationPlan.markOutOfDate();
+			}
+
+			// see if the grammar if out-of-date by way of its super-grammar(s) as gleaned from parsing the grammar file
+			if ( isNotEmpty( grammar.getSuperGrammarName() ) ) {
+				final GrammarFile superGrammarGrammarFile = metadataXRef.getGrammarFileByClassName( grammar.getSuperGrammarName() );
+				if ( superGrammarGrammarFile != null ) {
+					final GenerationPlan superGrammarGenerationPlan = loacteOrBuildGenerationPlan(
+							superGrammarGrammarFile
+					);
+					generationPlan.addSuperGrammarId( superGrammarGenerationPlan.getId() );
+					if ( superGrammarGenerationPlan.isOutOfDate() ) {
+						generationPlan.markOutOfDate();
+					}
+					else if ( superGrammarGenerationPlan.getSource()
+							.lastModified() > generatedParserFile.lastModified() ) {
+						generationPlan.markOutOfDate();
+					}
+				}
+			}
+
+			// see if the grammar if out-of-date by way of its importVocab
+			if ( isNotEmpty( grammar.getImportVocab() ) ) {
+				final GrammarFile importVocabGrammarFile = metadataXRef.getGrammarFileByExportVocab( grammar.getImportVocab() );
+				if ( importVocabGrammarFile == null ) {
+					log.warn( "unable to locate grammar exporting specifcied import vocab [" + grammar.getImportVocab() + "]" );
+				}
+				else if ( importVocabGrammarFile.getFileName().equals( grammarFile.getFileName() ) ) {
+
+				}
+				else {
+					final GenerationPlan importVocabGrammarGenerationPlan = loacteOrBuildGenerationPlan(
+							importVocabGrammarFile
+					);
+					generationPlan.setImportVocabTokenTypesDirectory(
+							importVocabGrammarGenerationPlan.getGenerationDirectory()
+					);
+					if ( importVocabGrammarGenerationPlan.isOutOfDate() ) {
+						generationPlan.markOutOfDate();
+					}
+					else if ( importVocabGrammarGenerationPlan.getSource()
+							.lastModified() > generatedParserFile.lastModified() ) {
+						generationPlan.markOutOfDate();
+					}
+				}
+			}
+		}
+
+		generationPlans.put( generationPlan.getId(), generationPlan );
+		return generationPlan;
+	}
+
+	private boolean isEmpty(String string) {
+		return string == null || string.trim().length() == 0;
+	}
+
+	private boolean isNotEmpty(String string) {
+		return ! isEmpty( string );
+	}
+
+	private static File locateLeastRecentlyModifiedOutputFile(File directory) {
+		if ( !directory.exists() ) {
+			return null;
+		}
+
+		File[] contents = directory.listFiles();
+		if ( contents.length == 0 ) {
+			return null;
+		}
+
+		File oldest = contents[0];
+		for ( int i = 1; i < contents.length; i++ ) {
+			if ( contents[i].lastModified() < oldest.lastModified() ) {
+				oldest = contents[i];
+			}
+		}
+
+		return oldest;
+	}
+
+
+}

Modified: core/branches/gradle/hibernate-core/build.gradle
===================================================================
--- core/branches/gradle/hibernate-core/build.gradle	2009-10-19 16:48:53 UTC (rev 17799)
+++ core/branches/gradle/hibernate-core/build.gradle	2009-10-19 21:33:35 UTC (rev 17800)
@@ -1,9 +1,11 @@
-configurations {
-    antAntlr {
-        description = 'Classpath used to define and execute the Antlr Ant task.'
-    }
-}
+usePlugin org.gradle.plugin.antlr.AntlrPlugin.class
 
+//configurations {
+//    antAntlr {
+//        description = 'Classpath used to define and execute the Antlr Ant task.'
+//    }
+//}
+
 dependencies {
     antVersion = '1.6.5'
 
@@ -20,72 +22,72 @@
             [group: 'javax.security', name: 'jaas', version: '1.0.01'],
             [group: 'javax.security', name: 'jacc', version: '1.0']
     )
-    antAntlr (
-            [group: 'ant', name: 'ant-antlr', version: antVersion],
-            [group: 'antlr', name: 'antlr', version: antlrVersion]
-    )
+//    antAntlr (
+//            [group: 'ant', name: 'ant-antlr', version: antVersion],
+//            [group: 'antlr', name: 'antlr', version: antlrVersion]
+//    )
 }
 
 manifest.mainAttributes(
     mainClass: 'org.hibernate.Version'
 )
 
-antlrSourceDirName = "src/main/antlr"
-antlrJavaTargetDirName = "$buildDir/generated-src/antlr"
+//antlrSourceDirName = "src/main/antlr"
+//antlrJavaTargetDirName = "$buildDir/generated-src/antlr"
+//
+//antlrJavaTargetDir = new File( antlrJavaTargetDirName )
+//
+//task antlrInit << {
+//    antlrJavaTargetDir.mkdirs()
+//}
+//
+//task antlr(dependsOn: antlrInit) << {
+//    ant.taskdef(
+//            name: 'antlrtask',
+//            classname: 'org.apache.tools.ant.taskdefs.optional.ANTLR',
+//            classpath: configurations.antAntlr.asPath
+//    )
+//
+//    outputDir = new File( "$antlrJavaTargetDirName/org/hibernate/hql/antlr" )
+//    outputDir.mkdirs()
+//
+//    ant.antlrtask(
+//            target: "${antlrSourceDirName}/hql.g",
+//            outputdirectory: outputDir
+//    )
+//
+//    ant.antlrtask(
+//            target: "${antlrSourceDirName}/hql-sql.g",
+//            outputdirectory: outputDir,
+//            tracetreewalker: true
+//    )
+//
+//    ant.antlrtask(
+//            target: "${antlrSourceDirName}/sql-gen.g",
+//            outputdirectory: outputDir,
+//            tracetreewalker: true
+//    )
+//
+//    outputDir = new File( "$antlrJavaTargetDirName/org/hibernate/sql/ordering/antlr" )
+//    outputDir.mkdirs()
+//
+//    ant.antlrtask(
+//            target: "${antlrSourceDirName}/order-by.g",
+//            outputdirectory: outputDir
+//    )
+//
+//    ant.antlrtask(
+//            target: "${antlrSourceDirName}/order-by-render.g",
+//            outputdirectory: outputDir,
+//            tracetreewalker: true
+//    )
+//
+//}
+//
+//def antlrGeneratedSourceFiles = files( antlrJavaTargetDir ) {
+//    builtBy 'antlr'
+//}
+//
+//compileJava.dependsOn antlrGeneratedSourceFiles.builtBy
+//sourceSets.main.java.srcDir( antlrGeneratedSourceFiles )
 
-antlrJavaTargetDir = new File( antlrJavaTargetDirName )
-
-task antlrInit << {
-    antlrJavaTargetDir.mkdirs()
-}
-
-task antlr(dependsOn: antlrInit) << {
-    ant.taskdef(
-            name: 'antlrtask',
-            classname: 'org.apache.tools.ant.taskdefs.optional.ANTLR',
-            classpath: configurations.antAntlr.asPath
-    )
-
-    outputDir = new File( "$antlrJavaTargetDirName/org/hibernate/hql/antlr" )
-    outputDir.mkdirs()
-
-    ant.antlrtask(
-            target: "${antlrSourceDirName}/hql.g",
-            outputdirectory: outputDir
-    )
-
-    ant.antlrtask(
-            target: "${antlrSourceDirName}/hql-sql.g",
-            outputdirectory: outputDir,
-            tracetreewalker: true
-    )
-
-    ant.antlrtask(
-            target: "${antlrSourceDirName}/sql-gen.g",
-            outputdirectory: outputDir,
-            tracetreewalker: true
-    )
-
-    outputDir = new File( "$antlrJavaTargetDirName/org/hibernate/sql/ordering/antlr" )
-    outputDir.mkdirs()
-
-    ant.antlrtask(
-            target: "${antlrSourceDirName}/order-by.g",
-            outputdirectory: outputDir
-    )
-
-    ant.antlrtask(
-            target: "${antlrSourceDirName}/order-by-render.g",
-            outputdirectory: outputDir,
-            tracetreewalker: true
-    )
-
-}
-
-def antlrGeneratedSourceFiles = files( antlrJavaTargetDir ) {
-    builtBy 'antlr'
-}
-
-compileJava.dependsOn antlrGeneratedSourceFiles.builtBy
-sourceSets.main.java.srcDir( antlrGeneratedSourceFiles )
-



More information about the hibernate-commits mailing list