<project name="grails-bundle"
         xmlns:ivy="antlib:org.apache.ivy.ant"
         xmlns:bundlor="antlib:com.springsource.bundlor.ant">
    <property name="targetDistDirectory" value="${grails.dist}"/>
    <import file="maven.xml"/>
    <import file="resolve.xml"/>

    <target name="-jar-app-files" description="Bundles all the template files for applications and plugins into a JAR files.">
        <!-- Clear the old JAR files out. -->
        <delete>
			<fileset dir="${grails.target}" includes="grails-*-files.jar"/>
		</delete>


        <!-- Files to integrate Grails with IDEs and build systems. -->
        <jar destfile="${grails.target}/grails-integration-files.jar">
            <fileset dir="${basedir}/src/grails/templates/ide-support"/>
            <fileset dir="${basedir}/src/grails/ant"/>
        </jar>
        <!-- Package up the files that are shared by both applications and plugins. -->
        <jar destfile="${grails.target}/grails-shared-files.jar">
            <zipfileset dir="${basedir}/src/war" prefix="web-app">
                <include name="WEB-INF/applicationContext.xml"/>
                <include name="WEB-INF/applicationContext.xml"/>
                <include name="WEB-INF/sitemesh.xml"/>
                <include name="WEB-INF/tld/*.tld"/>
            </zipfileset>
            <fileset dir="${basedir}/src/grails">
                <include name="grails-app/conf/DataSource.groovy"/>
                <include name="grails-app/conf/UrlMappings.groovy"/>
            </fileset>
        </jar>

        <!-- Package up the files that are exclusive to applications. -->
        <jar destfile="${grails.target}/grails-app-files.jar">
            <zipfileset dir="${basedir}/src/war" prefix="web-app">
                <exclude name="WEB-INF/**"/>
            </zipfileset>
            <fileset dir="${basedir}/src/grails">
                <include name="grails-app/**"/>
                <exclude name="grails-app/taglib/**"/>
                <exclude name="grails-app/utils/**"/>
                <exclude name="grails-app/conf/DataSource.groovy"/>
                <exclude name="grails-app/conf/UrlMappings.groovy"/>
            </fileset>
        </jar>

        <!-- Package up the files that are exclusive to plugins. -->
        <jar destfile="${grails.target}/grails-plugin-files.jar">
            <fileset dir="${basedir}/src/grails/templates/plugins"/>
            <fileset dir="${basedir}/src/grails">
                  <include name="grails-app/conf/BuildConfig.groovy"/>
                  <include name="grails-app/views/error.gsp" />
            </fileset>
        </jar>

        <!--Package up files used for generating documentation-->
        <jar basedir="${basedir}/src/grails/docs" destfile="${grails.target}/grails-doc-files.jar" />

    </target>

    <property name="bundlor.version" value="1.0.0.RELEASE" />
    <property name="bundlor.zip" value="${basedir}/bundlor/bundlor-${bundlor.version}.zip" />
    <property name="bundlor.home" location="${basedir}/target/bundlor-${bundlor.version}" />

    <target name="--bundlor-jar" depends="bundlor.init" unless="disable.bundlor">

		<bundlor:bundlor inputPath="${grails.dist}/grails-${bundle.name}-${grails.version}.jar"
                         outputPath="${grails.dist}/grails-${bundle.name}-${grails.version}.jar"
				         bundleVersion="${grails.version}"
				         manifestTemplatePath="${basedir}/maven/grails-${bundle.name}.mf"
                         failOnWarnings="true">
            <propertyset>
                <propertyref prefix="bundlor."/>
                <mapper type="glob" from="bundlor.*" to="*" />
            </propertyset>
		</bundlor:bundlor>
	</target>

	<target name="bundlor.init" unless="disable.bundlor">
		<condition property="bundlor.installed">
			<or>
				<isset property="disable.bundlor" />
				<available type="dir" file="${bundlor.home}"  />
			</or>
    	</condition>
		<antcall target="bundlor.extract" />
		<taskdef resource="com/springsource/bundlor/ant/antlib.xml"
                 uri="antlib:com.springsource.bundlor.ant">
            <classpath id="bundlor.classpath">
                <fileset dir="${bundlor.home}/dist"/>
                <fileset dir="${bundlor.home}/lib"/>
            </classpath>
        </taskdef>
	</target>

    <target name="bundlor.extract" unless="bundlor.installed">
        <unzip src="${bundlor.zip}"
               dest="${grails.target}" />
    </target>
    
    <target name="jar" depends="build,build-scripts,-jar-app-files" description="Build the entire source, run all tests and create the Grails JAR file.">
		<delete>
			<fileset dir="${grails.dist}" includes="*"/>
		</delete>
        
        <jar destfile="${grails.dist}/grails-test-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.build}">
                <include name="META-INF/**" />
                <include name="grails/test/**" />
                <include name="**/groovy/grails/test/**" />
            </fileset>
        </jar>
        <antcall target="--bundlor-jar">
            <param name="bundle.name" value="test" />
        </antcall>

        <jar destfile="${grails.dist}/grails-docs-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.target}" includes="grails-doc-files.jar" />
            <fileset dir="${grails.build}">
                <include name="META-INF/**" />
                <include name="grails/doc/**" />
                <include name="**/groovy/grails/documentation/**" />
            </fileset>
        </jar>
        <antcall target="--bundlor-jar">
            <param name="bundle.name" value="docs" />
        </antcall>


        <jar destfile="${grails.dist}/grails-spring-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.build}">
                <include name="META-INF/**" />
                <include name="grails/spring/**" />
                <include name="**/groovy/grails/commons/spring/**" />
            </fileset>
        </jar>
        <antcall target="--bundlor-jar">
            <param name="bundle.name" value="spring" />
        </antcall>


        <jar destfile="${grails.dist}/grails-bootstrap-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.build}">
                <include name="META-INF/**" />
                <include name="grails/util/BuildSettings*"/>
                <include name="grails/util/GrailsNameUtils*"/>
                <include name="grails/util/Environment*"/>
                <include name="grails/util/Metadata*"/>
                <include name="grails/util/BuildScope*"/>
                <include name="grails/util/GrailsMain*"/>
                <include name="grails/ant/**/*"/>
                <include name="grails/build/**"/>
                <include name="**/groovy/grails/cli/**/*" />
                <include name="**/groovy/grails/resolve/**/*" />
            </fileset>
            <file file="${basedir}/build.properties"/>
        </jar>
        <jar destfile="${grails.dist}/grails-scripts-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.target}/compiled-scripts"/>
            <file file="${basedir}/scripts/log4j.properties"/>            
        </jar>
        <antcall target="--bundlor-jar">
            <param name="bundle.name" value="scripts" />
        </antcall>
        


        <jar destfile="${grails.dist}/grails-gorm-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.build}">
                <include name="META-INF/**" />
                <include name="grails/orm/**" />
                <include name="**/groovy/grails/orm/hibernate/**" />
                <include name="**/groovy/grails/plugins/orm/hibernate/**" />
                <include name="**/groovy/grails/validation/Hibernate**" />
            </fileset>
        </jar>
        <antcall target="--bundlor-jar">
            <param name="bundle.name" value="gorm" />
        </antcall>

        <delete>
            <fileset dir="${grails.build}/META-INF">
                <include name="*.handlers" />
                <include name="*.schemas" />
            </fileset>
        </delete>
        <jar destfile="${grails.dist}/grails-web-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.build}">
                <include name="META-INF/**" />
                <include name="grails/converters/**" />
                <include name="grails/util/**" />
                <include name="grails/web/**" />
                <exclude name="grails/util/Generate**" />
                <include name="**/groovy/grails/web/**" />
                <include name="**/groovy/grails/plugins/web/**" />
                <include name="**/groovy/grails/plugins/converters/**" />

                <include name="**/groovy/grails/webflow/executor/support/GrailsConventionsFlowExecutorArgumentHandler*" />
            </fileset>
        </jar>
        <antcall target="--bundlor-jar">
            <param name="bundle.name" value="web" />
        </antcall>

        <jar destfile="${grails.dist}/grails-webflow-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.build}">
                <include name="META-INF/**" />
                <include name="**/groovy/grails/webflow/**" />
                <include name="**/groovy/grails/plugins/webflow/**" />
            </fileset>
        </jar>
        <antcall target="--bundlor-jar">
            <param name="bundle.name" value="webflow" />
        </antcall>


        <jar destfile="${grails.dist}/grails-crud-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.build}">
                <include name="META-INF/**" />
                <include name="**/groovy/grails/scaffolding/**" />
                <include name="**/groovy/grails/plugins/scaffolding/**" />
            </fileset>
        </jar>
        <antcall target="--bundlor-jar">
            <param name="bundle.name" value="crud" />
        </antcall>

        <jar destfile="${grails.dist}/grails-resources-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.target}" includes="*.jar" excludes="grails-doc-files.jar"/>
            <fileset dir="${basedir}">
                <include name="conf/webdefault.xml"/>
                <include name="src/grails/templates/artifacts/**"/>
                <include name="src/grails/templates/scaffolding/**"/>
                <include name="src/war/WEB-INF/web*.template.xml"/>
                <include name="src/war/WEB-INF/tld/*/*.tld"/>
            </fileset>
        </jar>
        <antcall target="--bundlor-jar">
            <param name="bundle.name" value="resources" />
        </antcall>

    	
    	<mkdir dir="${grails.build}/META-INF/services"/>
    	<copy todir="${grails.build}/META-INF/services">
    		<fileset dir="src/jar/META-INF/services" />
    	</copy>
        <jar destfile="${grails.dist}/grails-core-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
            <fileset dir="${grails.build}">
                <include name="META-INF/**" />
                <include name="grails/ui/**" />
                <include name="grails/util/**" />
                <include name="grails/validation/**" />
                <include name="grails/persistence/**" />
                <include name="**/groovy/grails/aop/framework/**" />
                <include name="**/groovy/grails/beans/factory/**" />
                <include name="**/groovy/grails/commons/**" />
                <include name="**/groovy/grails/context/**" />
                <include name="**/groovy/grails/compiler/**" />
                <include name="**/groovy/grails/orm/support/**" />
                <include name="**/groovy/grails/exceptions/**" />
                <include name="**/groovy/grails/support/**" />
                <include name="**/groovy/grails/test/**" />
                <include name="**/groovy/grails/validation/**" />
                <include name="**/groovy/grails/plugins/**" />
                <exclude name="grails/util/BuildSettings*"/>
                <exclude name="grails/util/GrailsNameUtils*"/>
                <exclude name="grails/util/Environment*"/>
                <exclude name="grails/util/Metadata*"/>
                <exclude name="grails/util/BuildScope*"/>
                <exclude name="grails/util/GrailsMain*"/>
                <exclude name="grails/test/**"/>
                <exclude name="**/groovy/grails/test/**" />
                <exclude name="**/groovy/grails/commons/spring/**" />
                <exclude name="**/groovy/grails/plugins/orm/hibernate**" />
                <exclude name="**/groovy/grails/plugins/web/**" />
                <exclude name="**/groovy/grails/plugins/converters/**" />
                <exclude name="**/groovy/grails/plugins/webflow/**" />
                <exclude name="**/groovy/grails/plugins/scaffolding/**" />
                <exclude name="**/groovy/grails/validation/Hibernate**" />
            </fileset>
        </jar>

		<jar destfile="${grails.dist}/grails-osgi-${grails.version}.jar" manifest="${grails.build}/META-INF/MANIFEST.MF">
			<fileset dir="${grails.dist}">
				<include name="grails-bootstrap-${grails.version}.jar" />
				<include name="grails-core-${grails.version}.jar" />
			</fileset>
		</jar>
        <antcall target="--bundlor-jar">
            <param name="bundle.name" value="osgi" />
        </antcall>

    	<delete dir="${grails.build}/META-INF/services"></delete>
    </target>

    <target name="package-minimal" depends="clean,jar" description="Creates a minimal archive that can be used for integrating a Grails project with an existing build system.">
        <property name="archive" value="grails-${grails.version}-minimal"/>

        <!-- The files to include in the minimal distribution. -->
        <patternset id="files.pkg.minimal">
            <include name="${grails.dist}/*.jar"/>
            <include name="${grails.lib}/*.jar"/>
            <include name="build.properties"/>
            <include name="bin/**/*"/>
            <include name="conf/**/*"/>
            <include name="scripts/**/*"/>
            <include name="src/grails/templates/scaffolding/**/*"/>
            <include name="src/war/WEB-INF/**/*"/>
        </patternset>

        <delete failonerror="true">
            <fileset dir="${grails.dist}">
                <include name="*-minimal.zip"/>
                <include name="*-minimal.tar.gz"/>
            </fileset>
        </delete>

        <!-- Create a ZIP distribution... -->
        <zip basedir="${basedir}" destfile="${grails.dist}/${archive}.zip">
            <patternset refid="files.pkg.minimal"/>
        </zip>

        <!-- ... and a tarball one. -->
        <tar destfile="${grails.dist}/${archive}.tar.gz" compression="gzip" longfile="gnu">
            <tarfileset dir="${basedir}">
                <patternset refid="files.pkg.minimal"/>
            </tarfileset>
        </tar>
    </target>

    <target name="package" depends="jar,javadoc,gdoc" description="Create the release archives for Grails.">
    	<delete failonerror="true">
		    <fileset dir="${grails.dist}">
                <include name="*.zip"/>
                <include name="*.tar.gz"/>
		    </fileset>
		</delete>
        <delete dir="tmp" failonerror="true"/>
        <antcall target="create-binary-release"/>
    </target>

    <target name="-mavenPomDefinitions">
        <property name="pom.source.dir" value="${basedir}/maven"/>
        <property name="pom.target.dir" value="${grails.target}/poms"/>

        <!-- Clear any existing POMs out of the way first -->
        <delete dir="${pom.target.dir}"/>

        <!-- Copy the template POMs over to a target directory, inserting the Grails version -->
        <copy todir="${pom.target.dir}">
            <fileset dir="${pom.source.dir}" includes="*.pom.in"/>
            <filterset>
                <filter token="grails.version" value="${grails.version}"/>
            </filterset>
            <globmapper from="*.in" to="*"/>
        </copy>

        <maven.pom dir="${pom.target.dir}" id="grails.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-bootstrap.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-core.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-docs.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-crud.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-gorm.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-resources.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-scripts.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-spring.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-test.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-web.pom"/>
        <maven.pom dir="${pom.target.dir}" id="grails-webflow.pom"/>
    </target>

    <target name="maven-install"
            depends="jar,-mavenInit"
            description="Installs the Grails artifacts in the local Maven repository.">
        <mavenInstallRepo version="${grails.version}" prefix="grails"/>
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-bootstrap"/>
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-docs"/>        
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-core"/>
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-crud"/>
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-gorm"/>
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-resources"/>
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-scripts"/>
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-spring"/>
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-test"/>
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-web"/>
        <mavenInstallRepoJar version="${grails.version}" prefix="grails-webflow"/>
    </target>

    <target name="maven-deploy"
            depends="jar,-mavenDeployInit"
            description="Installs the Grails artifacts in the appropriate remote Codehaus repository.">
        <mavenDeploy version="${grails.version}" prefix="grails"/>
        <mavenDeploy version="${grails.version}" prefix="grails-bootstrap"/>
        <mavenDeploy version="${grails.version}" prefix="grails-core"/>
        <mavenDeploy version="${grails.version}" prefix="grails-docs"/>
        <mavenDeploy version="${grails.version}" prefix="grails-crud"/>
        <mavenDeploy version="${grails.version}" prefix="grails-gorm"/>
        <mavenDeploy version="${grails.version}" prefix="grails-resources"/>
        <mavenDeploy version="${grails.version}" prefix="grails-scripts"/>
        <mavenDeploy version="${grails.version}" prefix="grails-spring"/>
        <mavenDeploy version="${grails.version}" prefix="grails-test"/>
        <mavenDeploy version="${grails.version}" prefix="grails-web"/>
        <mavenDeploy version="${grails.version}" prefix="grails-webflow"/>
    </target>
</project>
