<commit_before><project name="cumulus_ci" default="deployWithoutTest" basedir="." xmlns:sf="antlib:com.salesforce">

    <dirname property="cumulus_ci.basedir" file="${ant.file.cumulus_ci}"/>

    <include file="${cumulus_ci.basedir}/cumulusci.xml" />

    <taskdef 
         resource="net/sf/antcontrib/antlib.xml"
         classpath="${cumulus_ci.basedir}/lib/ant-contrib-1.0b2.jar" />

    <!-- If no propertyfile was passed in command line, attempt to load a build.properties if it exists -->
    <if>
        <and>
            <not>
                <isset property="sf:username" />
            </not>
            <available file="${basedir}/build.properties" />
        </and>
        <then>
            <loadproperties srcFile="${basedir}/build.properties"/>
        </then>
    </if>

    <!-- If there are environment variables set, consider them an override for org credentials -->
    <property environment="env" />

    <if>
      <isset property="env.SF_USERNAME" />
      <then>
        <var name="sf.username" value="${env.SF_USERNAME}" />
      </then>
    </if>
    <if>
      <isset property="env.SF_PASSWORD" />
      <then>
        <var name="sf.password" value="${env.SF_PASSWORD}" />
      </then>
    </if>
    <if>
      <isset property="env.SF_SERVERURL" />
      <then>
        <var name="sf.serverurl" value="${env.SF_SERVERURL}" />
      </then>
    </if>

    <!-- Load up cumulusci.properties file with package specific properties -->
    <loadproperties srcFile="${basedir}/cumulusci.properties"/>

    <!-- Set default values for properties not provided in cumulusci.properties -->
    <property name="cumulusci.package.name.managed" value="${cumulusci.package.name}" />
    <property name="cumulusci.package.installClass" value="" />
    <property name="cumulusci.package.uninstallClass" value="" />
    <property name="cumulusci.package.apiVersion" value="31.0" />
    <property name="cumulusci.maxPoll.test" value="400" />
    <property name="cumulusci.maxPoll.notest" value="200" />
    <property name="cumulusci.maxPoll.managed" value="400" />

    <!-- Setup a blank namespace prefix string.  Managed deployments need to override this property before calling deployUnpackagedPost -->
    <property name="cumulusci.namespace.prefix" value="" />
    
    <!-- Primary Build Targets -->

    <!-- deploy: Run a full deployment including running all tests.  Does not attempt to clean target org or ensure dependent package versions are correct -->
    <target name="deploy">
        <antcall target="deployWithoutTest" />
        <antcall target="runAllTests" />
        <!-- Changed to deploy then test due to suspicion that staging the deployment to run tests was causing a failure with every other build
          <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="src" runAllTests="true" maxPoll="${cumulusci.maxPoll.test}" />
          -->
    </target>

    <!-- deployWithoutTest: Run a full deployment but don't run all tests.  This is useful if you already know tests will pass from previous runs and just want to deploy faster -->
    <target name="deployWithoutTest">
        <antcall target="preDeploy" />
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="src" runAllTests="false" maxPoll="${cumulusci.maxPoll.notest}" />
        <antcall target="postDeploy" />
    </target>

    <!-- deployUnpackagedPre: Deploy the unpackaged/pre subdirectories containing metadata used in builds but not included in the managed package -->
    <target name="deployUnpackagedPre">
        <deployMetadataBundles dir="${basedir}/unpackaged/pre" stagedir="${basedir}/unpackaged_stage" />
    </target>

    <!-- deployUnpackagedPost: Deploy the unpackaged/post subdirectories containing metadata used in builds but not included in the managed package -->
    <target name="deployUnpackagedPost">
        <deployMetadataBundles dir="${basedir}/unpackaged/post" stagedir="${basedir}/unpackaged_stage" />
    </target>

    <!-- deployManagedUnpackagedPre: Deploy the unpackaged/pre subdirectories containing metadata used in builds but not included in the managed package.  Use the managed namespace prefix -->
    <target name="deployManagedUnpackagedPre">
        <deployMetadataBundles dir="${basedir}/unpackaged/pre" stagedir="${basedir}/unpackaged_stage" nsprefix="${cumulusci.package.namespace}__" />
    </target>

    <!-- deployManagedUnpackagedPost: Deploy the unpackaged/post subdirectories containing metadata used in builds but not included in the managed package.  Use the managed namespace prefix -->
    <target name="deployManagedUnpackagedPost">
        <deployMetadataBundles dir="${basedir}/unpackaged/post" stagedir="${basedir}/unpackaged_stage" nsprefix="${cumulusci.package.namespace}__" />
    </target>

    <!-- zipUnpackagedBundles: Populates unpackaged/zipped with non-namespaced versions of all pre and post deployment bundles -->
    <target name="zipUnpackagedBundles">
        <zipMetadataBundles dir="${basedir}/unpackaged/pre" stagedir="${basedir}/unpackaged_stage" zipdir="unpackaged/zipped" />
        <zipMetadataBundles dir="${basedir}/unpackaged/post" stagedir="${basedir}/unpackaged_stage" zipdir="unpackaged/zipped" />
    </target>

    <!-- zipUnpackagedBundles: Populates unpackaged/zipped with namespaced versions of all pre and post deployment bundles -->
    <target name="zipManagedUnpackagedBundles">
        <zipMetadataBundles dir="${basedir}/unpackaged/pre" stagedir="${basedir}/unpackaged_stage" zipdir="unpackaged/namespaced_zipped" nsprefix="${cumulusci.package.namespace}__" />
        <zipMetadataBundles dir="${basedir}/unpackaged/post" stagedir="${basedir}/unpackaged_stage" zipdir="unpackaged/namespaced_zipped" nsprefix="${cumulusci.package.namespace}__" />
    </target>


    <!-- uninstallUnpackagedPre: Uninstall the unpackaged/pre subdirectories containing metadata used in builds but not included in the managed package -->
    <target name="uninstallUnpackagedPre">
        <if>
            <available file="${basedir}/unpackaged/pre/" type="dir" />
            <then>
                <for param="dir">
                    <path>
                        <dirset dir="${basedir}/unpackaged/pre/" includes="*" />
                    </path>
                    <sequential>
                        <echo>Uninstalling unpackaged bundle @{dir}</echo>
                        <delete dir="uninstall" />
                        <buildPackagedDestructiveChanges srcdir="@{dir}" dir="uninstall" package="${cumulusci.package.name}" />
                        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall" runAllTests="false" purgeOnDelete="true" ignoreWarnings="true" maxPoll="${cumulusci.maxPoll.notest}" />
                        <delete dir="uninstall" />
                    </sequential>
                </for>
            </then>
        </if>
    </target>

    <!-- uninstallUnpackagedPost: Uninstall the unpackaged/post subdirectories containing metadata used in builds but not included in the managed package -->
    <target name="uninstallUnpackagedPost">
        <if>
            <available file="${basedir}/unpackaged/post/" type="dir" />
            <then>
                <for param="dir">
                    <path>
                        <dirset dir="${basedir}/unpackaged/post/" includes="*" />
                    </path>
                    <sequential>
                        <echo>Deploying unpackaged bundle @{dir}</echo>
                        <echo>Uninstalling unpackaged bundle @{dir}</echo>
                        <delete dir="uninstall" />
                        <buildPackagedDestructiveChanges srcdir="@{dir}" dir="uninstall" package="${cumulusci.package.name}" />
                        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall" runAllTests="false" purgeOnDelete="true" ignoreWarnings="true" maxPoll="${cumulusci.maxPoll.notest}" />
                        <delete dir="uninstall" />
                    </sequential>
                </for>
            </then>
        </if>
    </target>

    <!-- deployCI: Does a full build including uninstalling previously deployed unpackaged code, updating managed package versions, and then deploying with all tests -->
    <!-- !!!WARNING!!!: DO NOT run this against a real production org as it will delete everything.  It is designed to clear the target org to ensure a clean build test. -->
    <target name="deployCI">
        <antcall target="preDeployCI" />

        <!-- Create the unmanaged package if it doesn't already exist -->
        <antcall target="createUnmanagedPackage" />

        <!-- Uninstall all unpackaged code from the target org.  We have to do this first so we can uninstall and reinstall any managed packages not at the right version -->
        <antcall target="uninstall" />

        <!-- Update any managed packages which are not at the right version -->
        <antcall target="updateRequiredPackages" />

        <!-- Deploy any unpackaged metadata bundles needed before the deployment -->
        <antcall target="deployUnpackagedPre" />

        <!-- Deploy the src directory -->
        <antcall target="deployWithoutTest" />

        <!-- Deploy any unpackaged metadata bundles needed after the deployment -->
        <antcall target="deployUnpackagedPost" />

        <!-- Run all tests -->
        <antcall target="runAllTests" />

        <!-- Uninstall unpackaged post bundles since they may reference code in the package -->
        <antcall target="uninstallUnpackagedPost" />

        <antcall target="postDeployCI" />
    </target>

    <!-- deployCIPackageOrg: Does a full build against the package org.  This differs from deployCI in that it uses destroyStaleMetadata instead of uninstall.
             There are a few assumptions.  We never downgrade a dependent managed package in the packaging org.  This removes the need to completely remove
             all metadata to allow a package downgrade.  In the package org, we can't delete all metadata once a production managed release is cut so this approach is required -->
    <target name="deployCIPackageOrg">
        <antcall target="preDeployCIPackageOrg" />

        <!-- Update any managed packages which are not at the right version -->
        <antcall target="updateRequiredPackages" />

        <!-- Deploy any unpackaged metadata bundles needed before the deployment -->
        <antcall target="deployUnpackagedPre" />

        <!-- Attempt to destroy any stale metadata but continue even if this fails -->
        <trycatch>
            <try>
                <antcall target="destroyStaleMetadata" />
            </try>
            <catch>
                <echo>First run of destroyStaleMetadata failed.  Ignoring for now but it may cause build failures in other targets.</echo>
            </catch>
        </trycatch>

        <!-- Update the package.xml to managed package mode, adding install and uninstall script classes -->
        <antcall target="updatePackageXmlManaged" />

        <!-- Deploy src directory -->
        <antcall target="deployWithoutTest" />

        <!-- Finally, delete any metadata from the org which is not in the repo -->
        <antcall target="destroyStaleMetadata" />

        <!-- Deploy any unpackaged metadata bundles needed after the deployment -->
        <antcall target="deployUnpackagedPost" />

        <!-- Run all tests -->
        <antcall target="runAllTests" />

        <antcall target="postDeployCIPackageOrg" />
    </target>

    <!-- Deploys the latest managed beta release to an org -->
    <!-- !!!WARNING!!!: DO NOT run this against a real production org as it will delete everything.  It is designed to clear the target org to ensure a clean build test. -->
    <target name="deployManagedBeta">
        <antcall target="preDeployManagedBeta" />

        <echo>BUILD_COMMIT = ${env.BUILD_COMMIT}</echo>
        <if>
            <and>
                <isset property="env.BUILD_COMMIT" />
                <not>
                    <equals arg1="env.BUILD_COMMIT" arg2="" />
                </not>
                <isset property="env.PACKAGE_VERSION" />
                <not>
                    <equals arg1="env.PACKAGE_VERSION" arg2="" />
                </not>
            </and>
            <then>
                <!-- If a BUILD_COMMIT and PACKAGE_VERSION are provided in the environment, use them -->
                <property name="managed_beta_tag" value="${env.BUILD_COMMIT}" />
                <property name="version.managed.beta" value="${env.PACKAGE_VERSION}" />
                <echo>Building with environment variables: BUILD_COMMIT=${managed_beta_tag}, PACKAGE_VERSION=${version.managed.beta}</echo>
            </then>
            <else>
                <!-- If no BUILD_COMMIT or PACKAGE_VERSION was provided in environment, call mrbelvedere to fetch latest released version -->
                <!-- Get the latest beta release tag name -->
                <get src="${cumulusci.mrbelvedere.url}/mrbelvedere/repo/${cumulusci.mrbelvedere.repo.owner}/${cumulusci.mrbelvedere.repo.name}/version/beta/tag" dest="${basedir}/managed_beta_tag" />
                <loadfile property="managed_beta_tag" srcfile="${basedir}/managed_beta_tag" />
                <delete file="${basedir}/managed_beta_tag" />

                <!-- Get the latest beta release version number -->
                <get src="${cumulusci.mrbelvedere.url}/mrbelvedere/repo/${cumulusci.mrbelvedere.repo.owner}/${cumulusci.mrbelvedere.repo.name}/version/beta" dest="${basedir}/version_beta" />
                <loadfile property="version.managed.beta" srcfile="${basedir}/version_beta" />
                <delete file="${basedir}/version_beta" />
            </else>
        </if>

        <!-- Fetch the version.properties file for the release from GitHub -->
        <get src="${cumulusci.github.url.raw}/${managed_beta_tag}/version.properties" dest="${basedir}/version.properties.beta" />

        <!-- Since we want to use the versions required by the tag rather than the currently checked out code, unset all version properties and load the version.properties.beta file -->
        <var file="${basedir}/version.properties.beta" />
        <delete file="${basedir}/version.properties.beta" />

        <!-- Update all required packages and uninstall current beta -->
        <antcall target="updateRequiredPackages" />

        <!-- Deploy any unpackaged metadata bundles needed before the deployment -->
        <antcall target="deployManagedUnpackagedPre" />

        <!-- Install the beta packages -->
        <var name="version.managed" value="${version.managed.beta}" />
        <antcall target="updateRequiredPackages" />

        <!-- Deploy any unpackaged metadata bundles needed after the deployment -->
        <antcall target="deployManagedUnpackagedPost" />

        <antcall target="uninstallUnpackagedPost" />

        <antcall target="postDeployManagedBeta" />
    </target>

    <!-- Deploys the latest production managed release to an org -->
    <!-- !!!WARNING!!!: DO NOT run this against a real production org as it will delete everything.  It is designed to clear the target org to ensure a clean build test. -->
    <target name="deployManaged">
        <antcall target="preDeployManaged" />

        <echo>BUILD_COMMIT = ${env.BUILD_COMMIT}</echo>
        <if>
            <and>
                <isset property="env.BUILD_COMMIT" />
                <not>
                    <equals arg1="env.BUILD_COMMIT" arg2="" />
                </not>
                <isset property="env.PACKAGE_VERSION" />
                <not>
                    <equals arg1="env.PACKAGE_VERSION" arg2="" />
                </not>
            </and>
            <then>
                <!-- If a BUILD_COMMIT and PACKAGE_VERSION are provided in the environment, use them -->
                <property name="managed_prod_tag" value="${env.BUILD_COMMIT}" />
                <property name="version.managed.prod" value="${env.PACKAGE_VERSION}" />
                <echo>Building with environment variables: BUILD_COMMIT=${managed_prod_tag}, PACKAGE_VERSION=${version.npsp.managed}</echo>
            </then>
            <else>
                <!-- If no BUILD_COMMIT or PACKAGE_VERSION was provided in environment, call mrbelvedere to fetch latest released version -->
                <!-- Get the latest production release tag name -->
                <get src="${cumulusci.mrbelvedere.url}/mrbelvedere/repo/${cumulusci.mrbelvedere.repo.owner}/${cumulusci.mrbelvedere.repo.name}/version/tag" dest="${basedir}/managed_prod_tag" />
                <loadfile property="managed_prod_tag" srcfile="${basedir}/managed_prod_tag" />
                <delete file="${basedir}/managed_prod_tag" />

                <!-- Get the latest production release version number -->
                <get src="${cumulusci.mrbelvedere.url}/mrbelvedere/repo/${cumulusci.mrbelvedere.repo.owner}/${cumulusci.mrbelvedere.repo.name}/version" dest="${basedir}/version_prod" />
                <loadfile property="version.managed.prod" srcfile="${basedir}/version_prod" />
                <delete file="${basedir}/version_prod" />
            </else>
        </if>

        <!-- Fetch the version.properties file for the release from GitHub -->
        <get src="${cumulusci.github.url.raw}/${managed_prod_tag}/version.properties" dest="${basedir}/version.properties.prod" />

        <!-- Since we want to use the versions required by the tag rather than the currently checked out code, unset all version properties and load the version.properties.prod file -->
        <var file="${basedir}/version.properties.prod" />
        <delete file="${basedir}/version.properties.prod" />

        <!-- Run updateRequiredPackages to install the correct versions of required packages and the main package -->
        <antcall target="updateRequiredPackages" />

        <!-- Deploy any unpackaged metadata bundles needed for builds -->
        <antcall target="deployUnpackagedPre" />
        <antcall target="deployUnpackagedPost" />

        <antcall target="postDeployManaged" />
    </target>

    <!-- Utility Build Targets -->

    <target name="destroyStaleMetadata">
        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />

        <!-- Fetch all metadata in the  package from target org -->
        <retrievePackaged dir="uninstallsrc" package="${cumulusci.package.name.managed}" />

        <!-- Build a destructiveChanges.xml pacckage to delete metadata in org but not in repo -->
        <buildDestroyStaleMetadata srcdir="uninstallsrc" commdir="src" dir="uninstall" />

        <!-- deploy the destructiveChanges.xml package -->
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall" runAllTests="false" purgeOnDelete="true" maxPoll="${cumulusci.maxPoll.notest}" />

        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />
    </target>

    <!-- uninstall: Removes all metadata from the package -->
    <target name="uninstall">
        <antcall target="preUninstall" />
        <antcall target="uninstallUnpackagedPost" />
        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />
        <retrievePackaged dir="uninstallsrc" package="${cumulusci.package.name}" />
        <buildPackagedDestructiveChanges srcdir="uninstallsrc" dir="uninstall" package="${cumulusci.package.name}" />
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall" runAllTests="false" purgeOnDelete="true" maxPoll="${cumulusci.maxPoll.notest}" />
        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />
        <antcall target="uninstallUnpackagedPre" />
        <antcall target="postUninstall" />
    </target>

    <!-- uninstall: Removes all non-standard unpackaged metadata from the org -->
    <target name="uninstallUnpackaged">
        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />
        <retrieveUnpackaged dir="uninstallsrc" />
        <deleteWhitelistedMetadata dir="uninstallsrc/unpackaged" />
        <buildUnpackagedDestructiveChanges srcdir="uninstallsrc" dir="uninstall" />
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall/standard" runAllTests="false" maxPoll="${cumulusci.maxPoll.notest}" />
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall/unpackaged" runAllTests="false" purgeOnDelete="true" maxPoll="${cumulusci.maxPoll.notest}" />
        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />
    </target>

    <!-- Get and echo currently installed vs required versions of all required packages -->
    <macrodef name="getPackageVersions" description="Gets the version for all installed packages">
        <sequential>
            <delete dir="${basedir}/installedPackages"/>
            <mkdir dir="${basedir}/installedPackages"/>
    
            <sf:bulkRetrieve
                username="${sf.username}"
                password="${<PASSWORD>}"
                metadataType="InstalledPackage"
                retrieveTarget="${basedir}/installedPackages"/>
    
            <echo>Required Package Versions:</echo>
            <echo>-------------------------------</echo>
    
            <for list="${required.packages}" delimiter="," param="packageNamespace">
                <sequential>
                    <if>
                        <equals arg1="@{packageNamespace}" arg2="managed" />
                        <then>
                            <getPackageVersion namespace="${cumulusci.package.namespace}" refresh="false" />
                            <propertycopy property="packageVersionInstalled" from="InstalledPackage.${cumulusci.package.namespace}.versionNumber" />
                            <echo>${cumulusci.package.namespace}: ${packageVersionInstalled} (${version.@{packageNamespace}} required)</echo>
                        </then>
                        <else>
                            <getPackageVersion namespace="@{packageNamespace}" refresh="false" />
                            <echo>@{packageNamespace}: ${InstalledPackage.@{packageNamespace}.versionNumber} (${version.@{packageNamespace}} required)</echo>
                        </else>
                    </if>
                </sequential>
            </for>
    
            <delete dir="${basedir}/installedPackages"/>
        </sequential>
    </macrodef>

    <!-- updateDependendentPackages: Ensures all dependent packages are the correct version -->
    <target name="updateRequiredPackages">
        <getPackageVersions />
        <antcall target="preUpdateRequiredPackages" />
        <for list="${required.packages}" delimiter="," param="packageNamespace">
            <sequential>
                <if>
                    <equals arg1="@{packageNamespace}" arg2="managed" />
                    <then>
                        <updatePackageVersion namespace="${cumulusci.package.namespace}" version="${version.@{packageNamespace}}" />
                    </then>
                    <else>
                        <echo>Deploying pre bundles for @{packageNamespace}</echo>
                        <if>
                            <available file="${basedir}/unpackaged/@{packageNamespace}/pre" type="dir" />
                            <then>
                                <deployMetadataBundles dir="${basedir}/unpackaged/pre" stagedir="${basedir}/unpackaged_stage" />
                            </then>
                            <else>
                                <echo>Directory does not exist: "${basedir}/unpackaged/@{packageNamespace}</echo>
                            </else>
                        </if>
                        <updatePackageVersion namespace="@{packageNamespace}" version="${version.@{packageNamespace}}" />
                        <echo>Deploying post bundles for @{packageNamespace}</echo>
                        <if>
                            <available file="${basedir}/unpackaged/@{packageNamespace}/post" type="dir" />
                            <then>
                                <deployMetadataBundles dir="${basedir}/unpackaged/post" stagedir="${basedir}/unpackaged_stage" />
                            </then>
                            <else>
                                <echo>Directory does not exist: "${basedir}/unpackaged/@{packageNamespace}</echo>
                            </else>
                        </if>
                    </else>
                </if>
            </sequential>
        </for>
        <antcall target="postUpdateRequiredPackages" />
    </target>

    <!-- Developer focused build targets -->

    <!-- retrieveUnpackaged: Retrieves all unpackaged metadata from target org into the unpackaged directory -->
    <target name="retrieveUnpackaged">
        <retrieveUnpackaged dir="org_unpackaged" />
    </target>

    <!-- retrievePackaged: Retrieves all metadata from the package in the target org into the packaged directory -->
    <target name="retrievePackaged">
        <retrievePackaged dir="packaged" package="${cumulusci.package.name}" />
    </target>
    
    <!-- retrievePackagedToSrc: Retrieves all metadata from the package in the target org into the src directory --> 
    <target name="retrievePackagedToSrc"> 
        <retrievePackaged dir="src" package="${cumulusci.package.name}" mkdir="false" /> 
    </target>

    <!-- createUnmanagedPackage: Does an empty deployment into an unmanaged package to create it if it does not exist -->
    <target name="createUnmanagedPackage">
        <if>
            <isset property="cumulusci.package.name" />
            <then>
                <delete dir="${basedir}/create_package" />
                <mkdir dir="${basedir}/create_package" />
                <buildPackageXml package="${cumulusci.package.name}" version="${cumulusci.package.apiVersion}" srcDir="create_package" />
                <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="create_package" runAllTests="false" maxPoll="${cumulusci.maxPoll.notest}" />
                <delete dir="${basedir}/create_package" />
            </then>
        </if>
    </target>

    <!-- runAllTests: Uses an empty package manifest to trigger execution of all tests in the target org without deploying any actual code changes -->
    <target name="runAllTests">
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="${cumulus_ci.basedir}/empty" runAllTests="true" maxPoll="${cumulusci.maxPoll.test}" />
    </target>

    <!-- updatePackageXml: Builds a new package.xml based upon the metadata in the src directory -->
    <target name="updatePackageXml">
        <buildPackageXml package="${cumulusci.package.name}" version="${cumulusci.package.apiVersion}" />
    </target>

    <target name="updatePackageXmlManaged">
        <buildPackageXml package="${cumulusci.package.name.managed}" version="${cumulusci.package.apiVersion}" installClass="${cumulusci.package.installClass}" uninstallClass="${cumulusci.package.uninstallClass}" />
    </target>

    <target name="updateMetaXml">
        <for list="${required.packages}" delimiter="," param="packageNamespace">
            <sequential>
                <if>
                    <not><equals arg1="@{packageNamespace}" arg2="managed" /></not>
                    <then>
                        <updateMetaXml dir="${basedir}/src" namespace="@{packageNamespace}" version="${version.@{packageNamespace}}" />
                    </then>
                </if>
            </sequential>
        </for>
        <updateMetaXmlApi dir="${basedir}/src" apiVersion="${cumulusci.package.apiVersion}" />
    </target>

    <!-- Override hook targets: These targets are empty in the core file but exist for allowing projects to hook in their own custom logic without overriding the core targets -->

    <!-- Before uninstall -->
    <target name="preUninstall">
    </target>
    <!-- After uninstall. -->
    <target name="postUninstall">
    </target>

    <!-- Before updateRequiredPackages.  Can be used to uninstall packages in a downgrade scenario if building an extension package -->
    <target name="preUpdateRequiredPackages">
    </target>
    <!-- After updateRequiredPackages. -->
    <target name="postUpdateRequiredPackages">
    </target>

    <!-- Before deploy of src directory -->
    <target name="preDeploy">
    </target>
    <!-- After deploy of src directory -->
    <target name="postDeploy">
    </target>

    <!-- Before deployCI -->
    <target name="preDeployCI">
    </target>
    <!-- After deployCI -->
    <target name="postDeployCI">
    </target>

    <!-- Before deployCIPackageOrg -->
    <target name="preDeployCIPackageOrg">
    </target>
    <!-- After deployCIPackageOrg -->
    <target name="postDeployCIPackageOrg">
    </target>

    <!-- Before deployManaged -->
    <target name="preDeployManaged">
    </target>
    <!-- After deployManaged -->
    <target name="postDeployManaged">
    </target>

    <!-- Before deployManagedBeta -->
    <target name="preDeployManagedBeta">
    </target>
    <!-- After deployManagedBeta -->
    <target name="postDeployManagedBeta">
    </target>

</project>

<commit_msg>Fix directory paths in pre/post managed bundles<commit_after><project name="cumulus_ci" default="deployWithoutTest" basedir="." xmlns:sf="antlib:com.salesforce">

    <dirname property="cumulus_ci.basedir" file="${ant.file.cumulus_ci}"/>

    <include file="${cumulus_ci.basedir}/cumulusci.xml" />

    <taskdef 
         resource="net/sf/antcontrib/antlib.xml"
         classpath="${cumulus_ci.basedir}/lib/ant-contrib-1.0b2.jar" />

    <!-- If no propertyfile was passed in command line, attempt to load a build.properties if it exists -->
    <if>
        <and>
            <not>
                <isset property="sf:username" />
            </not>
            <available file="${basedir}/build.properties" />
        </and>
        <then>
            <loadproperties srcFile="${basedir}/build.properties"/>
        </then>
    </if>

    <!-- If there are environment variables set, consider them an override for org credentials -->
    <property environment="env" />

    <if>
      <isset property="env.SF_USERNAME" />
      <then>
        <var name="sf.username" value="${env.SF_USERNAME}" />
      </then>
    </if>
    <if>
      <isset property="env.SF_PASSWORD" />
      <then>
        <var name="sf.password" value="${env.SF_PASSWORD}" />
      </then>
    </if>
    <if>
      <isset property="env.SF_SERVERURL" />
      <then>
        <var name="sf.serverurl" value="${env.SF_SERVERURL}" />
      </then>
    </if>

    <!-- Load up cumulusci.properties file with package specific properties -->
    <loadproperties srcFile="${basedir}/cumulusci.properties"/>

    <!-- Set default values for properties not provided in cumulusci.properties -->
    <property name="cumulusci.package.name.managed" value="${cumulusci.package.name}" />
    <property name="cumulusci.package.installClass" value="" />
    <property name="cumulusci.package.uninstallClass" value="" />
    <property name="cumulusci.package.apiVersion" value="31.0" />
    <property name="cumulusci.maxPoll.test" value="400" />
    <property name="cumulusci.maxPoll.notest" value="200" />
    <property name="cumulusci.maxPoll.managed" value="400" />

    <!-- Setup a blank namespace prefix string.  Managed deployments need to override this property before calling deployUnpackagedPost -->
    <property name="cumulusci.namespace.prefix" value="" />
    
    <!-- Primary Build Targets -->

    <!-- deploy: Run a full deployment including running all tests.  Does not attempt to clean target org or ensure dependent package versions are correct -->
    <target name="deploy">
        <antcall target="deployWithoutTest" />
        <antcall target="runAllTests" />
        <!-- Changed to deploy then test due to suspicion that staging the deployment to run tests was causing a failure with every other build
          <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="src" runAllTests="true" maxPoll="${cumulusci.maxPoll.test}" />
          -->
    </target>

    <!-- deployWithoutTest: Run a full deployment but don't run all tests.  This is useful if you already know tests will pass from previous runs and just want to deploy faster -->
    <target name="deployWithoutTest">
        <antcall target="preDeploy" />
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="src" runAllTests="false" maxPoll="${cumulusci.maxPoll.notest}" />
        <antcall target="postDeploy" />
    </target>

    <!-- deployUnpackagedPre: Deploy the unpackaged/pre subdirectories containing metadata used in builds but not included in the managed package -->
    <target name="deployUnpackagedPre">
        <deployMetadataBundles dir="${basedir}/unpackaged/pre" stagedir="${basedir}/unpackaged_stage" />
    </target>

    <!-- deployUnpackagedPost: Deploy the unpackaged/post subdirectories containing metadata used in builds but not included in the managed package -->
    <target name="deployUnpackagedPost">
        <deployMetadataBundles dir="${basedir}/unpackaged/post" stagedir="${basedir}/unpackaged_stage" />
    </target>

    <!-- deployManagedUnpackagedPre: Deploy the unpackaged/pre subdirectories containing metadata used in builds but not included in the managed package.  Use the managed namespace prefix -->
    <target name="deployManagedUnpackagedPre">
        <deployMetadataBundles dir="${basedir}/unpackaged/pre" stagedir="${basedir}/unpackaged_stage" nsprefix="${cumulusci.package.namespace}__" />
    </target>

    <!-- deployManagedUnpackagedPost: Deploy the unpackaged/post subdirectories containing metadata used in builds but not included in the managed package.  Use the managed namespace prefix -->
    <target name="deployManagedUnpackagedPost">
        <deployMetadataBundles dir="${basedir}/unpackaged/post" stagedir="${basedir}/unpackaged_stage" nsprefix="${cumulusci.package.namespace}__" />
    </target>

    <!-- zipUnpackagedBundles: Populates unpackaged/zipped with non-namespaced versions of all pre and post deployment bundles -->
    <target name="zipUnpackagedBundles">
        <zipMetadataBundles dir="${basedir}/unpackaged/pre" stagedir="${basedir}/unpackaged_stage" zipdir="unpackaged/zipped" />
        <zipMetadataBundles dir="${basedir}/unpackaged/post" stagedir="${basedir}/unpackaged_stage" zipdir="unpackaged/zipped" />
    </target>

    <!-- zipUnpackagedBundles: Populates unpackaged/zipped with namespaced versions of all pre and post deployment bundles -->
    <target name="zipManagedUnpackagedBundles">
        <zipMetadataBundles dir="${basedir}/unpackaged/pre" stagedir="${basedir}/unpackaged_stage" zipdir="unpackaged/namespaced_zipped" nsprefix="${cumulusci.package.namespace}__" />
        <zipMetadataBundles dir="${basedir}/unpackaged/post" stagedir="${basedir}/unpackaged_stage" zipdir="unpackaged/namespaced_zipped" nsprefix="${cumulusci.package.namespace}__" />
    </target>


    <!-- uninstallUnpackagedPre: Uninstall the unpackaged/pre subdirectories containing metadata used in builds but not included in the managed package -->
    <target name="uninstallUnpackagedPre">
        <if>
            <available file="${basedir}/unpackaged/pre/" type="dir" />
            <then>
                <for param="dir">
                    <path>
                        <dirset dir="${basedir}/unpackaged/pre/" includes="*" />
                    </path>
                    <sequential>
                        <echo>Uninstalling unpackaged bundle @{dir}</echo>
                        <delete dir="uninstall" />
                        <buildPackagedDestructiveChanges srcdir="@{dir}" dir="uninstall" package="${cumulusci.package.name}" />
                        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall" runAllTests="false" purgeOnDelete="true" ignoreWarnings="true" maxPoll="${cumulusci.maxPoll.notest}" />
                        <delete dir="uninstall" />
                    </sequential>
                </for>
            </then>
        </if>
    </target>

    <!-- uninstallUnpackagedPost: Uninstall the unpackaged/post subdirectories containing metadata used in builds but not included in the managed package -->
    <target name="uninstallUnpackagedPost">
        <if>
            <available file="${basedir}/unpackaged/post/" type="dir" />
            <then>
                <for param="dir">
                    <path>
                        <dirset dir="${basedir}/unpackaged/post/" includes="*" />
                    </path>
                    <sequential>
                        <echo>Deploying unpackaged bundle @{dir}</echo>
                        <echo>Uninstalling unpackaged bundle @{dir}</echo>
                        <delete dir="uninstall" />
                        <buildPackagedDestructiveChanges srcdir="@{dir}" dir="uninstall" package="${cumulusci.package.name}" />
                        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall" runAllTests="false" purgeOnDelete="true" ignoreWarnings="true" maxPoll="${cumulusci.maxPoll.notest}" />
                        <delete dir="uninstall" />
                    </sequential>
                </for>
            </then>
        </if>
    </target>

    <!-- deployCI: Does a full build including uninstalling previously deployed unpackaged code, updating managed package versions, and then deploying with all tests -->
    <!-- !!!WARNING!!!: DO NOT run this against a real production org as it will delete everything.  It is designed to clear the target org to ensure a clean build test. -->
    <target name="deployCI">
        <antcall target="preDeployCI" />

        <!-- Create the unmanaged package if it doesn't already exist -->
        <antcall target="createUnmanagedPackage" />

        <!-- Uninstall all unpackaged code from the target org.  We have to do this first so we can uninstall and reinstall any managed packages not at the right version -->
        <antcall target="uninstall" />

        <!-- Update any managed packages which are not at the right version -->
        <antcall target="updateRequiredPackages" />

        <!-- Deploy any unpackaged metadata bundles needed before the deployment -->
        <antcall target="deployUnpackagedPre" />

        <!-- Deploy the src directory -->
        <antcall target="deployWithoutTest" />

        <!-- Deploy any unpackaged metadata bundles needed after the deployment -->
        <antcall target="deployUnpackagedPost" />

        <!-- Run all tests -->
        <antcall target="runAllTests" />

        <!-- Uninstall unpackaged post bundles since they may reference code in the package -->
        <antcall target="uninstallUnpackagedPost" />

        <antcall target="postDeployCI" />
    </target>

    <!-- deployCIPackageOrg: Does a full build against the package org.  This differs from deployCI in that it uses destroyStaleMetadata instead of uninstall.
             There are a few assumptions.  We never downgrade a dependent managed package in the packaging org.  This removes the need to completely remove
             all metadata to allow a package downgrade.  In the package org, we can't delete all metadata once a production managed release is cut so this approach is required -->
    <target name="deployCIPackageOrg">
        <antcall target="preDeployCIPackageOrg" />

        <!-- Update any managed packages which are not at the right version -->
        <antcall target="updateRequiredPackages" />

        <!-- Deploy any unpackaged metadata bundles needed before the deployment -->
        <antcall target="deployUnpackagedPre" />

        <!-- Attempt to destroy any stale metadata but continue even if this fails -->
        <trycatch>
            <try>
                <antcall target="destroyStaleMetadata" />
            </try>
            <catch>
                <echo>First run of destroyStaleMetadata failed.  Ignoring for now but it may cause build failures in other targets.</echo>
            </catch>
        </trycatch>

        <!-- Update the package.xml to managed package mode, adding install and uninstall script classes -->
        <antcall target="updatePackageXmlManaged" />

        <!-- Deploy src directory -->
        <antcall target="deployWithoutTest" />

        <!-- Finally, delete any metadata from the org which is not in the repo -->
        <antcall target="destroyStaleMetadata" />

        <!-- Deploy any unpackaged metadata bundles needed after the deployment -->
        <antcall target="deployUnpackagedPost" />

        <!-- Run all tests -->
        <antcall target="runAllTests" />

        <antcall target="postDeployCIPackageOrg" />
    </target>

    <!-- Deploys the latest managed beta release to an org -->
    <!-- !!!WARNING!!!: DO NOT run this against a real production org as it will delete everything.  It is designed to clear the target org to ensure a clean build test. -->
    <target name="deployManagedBeta">
        <antcall target="preDeployManagedBeta" />

        <echo>BUILD_COMMIT = ${env.BUILD_COMMIT}</echo>
        <if>
            <and>
                <isset property="env.BUILD_COMMIT" />
                <not>
                    <equals arg1="env.BUILD_COMMIT" arg2="" />
                </not>
                <isset property="env.PACKAGE_VERSION" />
                <not>
                    <equals arg1="env.PACKAGE_VERSION" arg2="" />
                </not>
            </and>
            <then>
                <!-- If a BUILD_COMMIT and PACKAGE_VERSION are provided in the environment, use them -->
                <property name="managed_beta_tag" value="${env.BUILD_COMMIT}" />
                <property name="version.managed.beta" value="${env.PACKAGE_VERSION}" />
                <echo>Building with environment variables: BUILD_COMMIT=${managed_beta_tag}, PACKAGE_VERSION=${version.managed.beta}</echo>
            </then>
            <else>
                <!-- If no BUILD_COMMIT or PACKAGE_VERSION was provided in environment, call mrbelvedere to fetch latest released version -->
                <!-- Get the latest beta release tag name -->
                <get src="${cumulusci.mrbelvedere.url}/mrbelvedere/repo/${cumulusci.mrbelvedere.repo.owner}/${cumulusci.mrbelvedere.repo.name}/version/beta/tag" dest="${basedir}/managed_beta_tag" />
                <loadfile property="managed_beta_tag" srcfile="${basedir}/managed_beta_tag" />
                <delete file="${basedir}/managed_beta_tag" />

                <!-- Get the latest beta release version number -->
                <get src="${cumulusci.mrbelvedere.url}/mrbelvedere/repo/${cumulusci.mrbelvedere.repo.owner}/${cumulusci.mrbelvedere.repo.name}/version/beta" dest="${basedir}/version_beta" />
                <loadfile property="version.managed.beta" srcfile="${basedir}/version_beta" />
                <delete file="${basedir}/version_beta" />
            </else>
        </if>

        <!-- Fetch the version.properties file for the release from GitHub -->
        <get src="${cumulusci.github.url.raw}/${managed_beta_tag}/version.properties" dest="${basedir}/version.properties.beta" />

        <!-- Since we want to use the versions required by the tag rather than the currently checked out code, unset all version properties and load the version.properties.beta file -->
        <var file="${basedir}/version.properties.beta" />
        <delete file="${basedir}/version.properties.beta" />

        <!-- Update all required packages and uninstall current beta -->
        <antcall target="updateRequiredPackages" />

        <!-- Deploy any unpackaged metadata bundles needed before the deployment -->
        <antcall target="deployManagedUnpackagedPre" />

        <!-- Install the beta packages -->
        <var name="version.managed" value="${version.managed.beta}" />
        <antcall target="updateRequiredPackages" />

        <!-- Deploy any unpackaged metadata bundles needed after the deployment -->
        <antcall target="deployManagedUnpackagedPost" />

        <antcall target="uninstallUnpackagedPost" />

        <antcall target="postDeployManagedBeta" />
    </target>

    <!-- Deploys the latest production managed release to an org -->
    <!-- !!!WARNING!!!: DO NOT run this against a real production org as it will delete everything.  It is designed to clear the target org to ensure a clean build test. -->
    <target name="deployManaged">
        <antcall target="preDeployManaged" />

        <echo>BUILD_COMMIT = ${env.BUILD_COMMIT}</echo>
        <if>
            <and>
                <isset property="env.BUILD_COMMIT" />
                <not>
                    <equals arg1="env.BUILD_COMMIT" arg2="" />
                </not>
                <isset property="env.PACKAGE_VERSION" />
                <not>
                    <equals arg1="env.PACKAGE_VERSION" arg2="" />
                </not>
            </and>
            <then>
                <!-- If a BUILD_COMMIT and PACKAGE_VERSION are provided in the environment, use them -->
                <property name="managed_prod_tag" value="${env.BUILD_COMMIT}" />
                <property name="version.managed.prod" value="${env.PACKAGE_VERSION}" />
                <echo>Building with environment variables: BUILD_COMMIT=${managed_prod_tag}, PACKAGE_VERSION=${version.npsp.managed}</echo>
            </then>
            <else>
                <!-- If no BUILD_COMMIT or PACKAGE_VERSION was provided in environment, call mrbelvedere to fetch latest released version -->
                <!-- Get the latest production release tag name -->
                <get src="${cumulusci.mrbelvedere.url}/mrbelvedere/repo/${cumulusci.mrbelvedere.repo.owner}/${cumulusci.mrbelvedere.repo.name}/version/tag" dest="${basedir}/managed_prod_tag" />
                <loadfile property="managed_prod_tag" srcfile="${basedir}/managed_prod_tag" />
                <delete file="${basedir}/managed_prod_tag" />

                <!-- Get the latest production release version number -->
                <get src="${cumulusci.mrbelvedere.url}/mrbelvedere/repo/${cumulusci.mrbelvedere.repo.owner}/${cumulusci.mrbelvedere.repo.name}/version" dest="${basedir}/version_prod" />
                <loadfile property="version.managed.prod" srcfile="${basedir}/version_prod" />
                <delete file="${basedir}/version_prod" />
            </else>
        </if>

        <!-- Fetch the version.properties file for the release from GitHub -->
        <get src="${cumulusci.github.url.raw}/${managed_prod_tag}/version.properties" dest="${basedir}/version.properties.prod" />

        <!-- Since we want to use the versions required by the tag rather than the currently checked out code, unset all version properties and load the version.properties.prod file -->
        <var file="${basedir}/version.properties.prod" />
        <delete file="${basedir}/version.properties.prod" />

        <!-- Run updateRequiredPackages to install the correct versions of required packages and the main package -->
        <antcall target="updateRequiredPackages" />

        <!-- Deploy any unpackaged metadata bundles needed for builds -->
        <antcall target="deployUnpackagedPre" />
        <antcall target="deployUnpackagedPost" />

        <antcall target="postDeployManaged" />
    </target>

    <!-- Utility Build Targets -->

    <target name="destroyStaleMetadata">
        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />

        <!-- Fetch all metadata in the  package from target org -->
        <retrievePackaged dir="uninstallsrc" package="${cumulusci.package.name.managed}" />

        <!-- Build a destructiveChanges.xml pacckage to delete metadata in org but not in repo -->
        <buildDestroyStaleMetadata srcdir="uninstallsrc" commdir="src" dir="uninstall" />

        <!-- deploy the destructiveChanges.xml package -->
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall" runAllTests="false" purgeOnDelete="true" maxPoll="${cumulusci.maxPoll.notest}" />

        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />
    </target>

    <!-- uninstall: Removes all metadata from the package -->
    <target name="uninstall">
        <antcall target="preUninstall" />
        <antcall target="uninstallUnpackagedPost" />
        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />
        <retrievePackaged dir="uninstallsrc" package="${cumulusci.package.name}" />
        <buildPackagedDestructiveChanges srcdir="uninstallsrc" dir="uninstall" package="${cumulusci.package.name}" />
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall" runAllTests="false" purgeOnDelete="true" maxPoll="${cumulusci.maxPoll.notest}" />
        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />
        <antcall target="uninstallUnpackagedPre" />
        <antcall target="postUninstall" />
    </target>

    <!-- uninstall: Removes all non-standard unpackaged metadata from the org -->
    <target name="uninstallUnpackaged">
        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />
        <retrieveUnpackaged dir="uninstallsrc" />
        <deleteWhitelistedMetadata dir="uninstallsrc/unpackaged" />
        <buildUnpackagedDestructiveChanges srcdir="uninstallsrc" dir="uninstall" />
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall/standard" runAllTests="false" maxPoll="${cumulusci.maxPoll.notest}" />
        <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="uninstall/unpackaged" runAllTests="false" purgeOnDelete="true" maxPoll="${cumulusci.maxPoll.notest}" />
        <delete dir="uninstallsrc" />
        <delete dir="uninstall" />
    </target>

    <!-- Get and echo currently installed vs required versions of all required packages -->
    <macrodef name="getPackageVersions" description="Gets the version for all installed packages">
        <sequential>
            <delete dir="${basedir}/installedPackages"/>
            <mkdir dir="${basedir}/installedPackages"/>
    
            <sf:bulkRetrieve
                username="${sf.username}"
                password="${<PASSWORD>}"
                metadataType="InstalledPackage"
                retrieveTarget="${basedir}/installedPackages"/>
    
            <echo>Required Package Versions:</echo>
            <echo>-------------------------------</echo>
    
            <for list="${required.packages}" delimiter="," param="packageNamespace">
                <sequential>
                    <if>
                        <equals arg1="@{packageNamespace}" arg2="managed" />
                        <then>
                            <getPackageVersion namespace="${cumulusci.package.namespace}" refresh="false" />
                            <propertycopy property="packageVersionInstalled" from="InstalledPackage.${cumulusci.package.namespace}.versionNumber" />
                            <echo>${cumulusci.package.namespace}: ${packageVersionInstalled} (${version.@{packageNamespace}} required)</echo>
                        </then>
                        <else>
                            <getPackageVersion namespace="@{packageNamespace}" refresh="false" />
                            <echo>@{packageNamespace}: ${InstalledPackage.@{packageNamespace}.versionNumber} (${version.@{packageNamespace}} required)</echo>
                        </else>
                    </if>
                </sequential>
            </for>
    
            <delete dir="${basedir}/installedPackages"/>
        </sequential>
    </macrodef>

    <!-- updateDependendentPackages: Ensures all dependent packages are the correct version -->
    <target name="updateRequiredPackages">
        <getPackageVersions />
        <antcall target="preUpdateRequiredPackages" />
        <for list="${required.packages}" delimiter="," param="packageNamespace">
            <sequential>
                <if>
                    <equals arg1="@{packageNamespace}" arg2="managed" />
                    <then>
                        <updatePackageVersion namespace="${cumulusci.package.namespace}" version="${version.@{packageNamespace}}" />
                    </then>
                    <else>
                        <echo>Deploying pre bundles for @{packageNamespace}</echo>
                        <if>
                            <available file="${basedir}/unpackaged/@{packageNamespace}/pre" type="dir" />
                            <then>
                                <deployMetadataBundles dir="${basedir}/unpackaged/@{packageNamespace}/pre" stagedir="${basedir}/unpackaged_stage" />
                            </then>
                            <else>
                                <echo>No pre bundles found for @{packageNamespace} at ${basedir}/unpackaged/@{packageNamespace}/pre</echo>
                            </else>
                        </if>
                        <!--<updatePackageVersion namespace="@{packageNamespace}" version="${version.@{packageNamespace}}" />-->
                        <echo>Deploying post bundles for @{packageNamespace}</echo>
                        <if>
                            <available file="${basedir}/unpackaged/@{packageNamespace}/post" type="dir" />
                            <then>
                                <deployMetadataBundles dir="${basedir}/unpackaged/@{packageNamespace}/post" stagedir="${basedir}/unpackaged_stage" />
                            </then>
                            <else>
                                <echo>No post bundles found for @{packageNamespace} at ${basedir}/unpackaged/@{packageNamespace}/post</echo>
                            </else>
                        </if>
                    </else>
                </if>
            </sequential>
        </for>
        <antcall target="postUpdateRequiredPackages" />
    </target>

    <!-- Developer focused build targets -->

    <!-- retrieveUnpackaged: Retrieves all unpackaged metadata from target org into the unpackaged directory -->
    <target name="retrieveUnpackaged">
        <retrieveUnpackaged dir="org_unpackaged" />
    </target>

    <!-- retrievePackaged: Retrieves all metadata from the package in the target org into the packaged directory -->
    <target name="retrievePackaged">
        <retrievePackaged dir="packaged" package="${cumulusci.package.name}" />
    </target>
    
    <!-- retrievePackagedToSrc: Retrieves all metadata from the package in the target org into the src directory --> 
    <target name="retrievePackagedToSrc"> 
        <retrievePackaged dir="src" package="${cumulusci.package.name}" mkdir="false" /> 
    </target>

    <!-- createUnmanagedPackage: Does an empty deployment into an unmanaged package to create it if it does not exist -->
    <target name="createUnmanagedPackage">
        <if>
            <isset property="cumulusci.package.name" />
            <then>
                <delete dir="${basedir}/create_package" />
                <mkdir dir="${basedir}/create_package" />
                <buildPackageXml package="${cumulusci.package.name}" version="${cumulusci.package.apiVersion}" srcDir="create_package" />
                <sf:deploy username="${sf.username}" password="${<PASSWORD>}" serverurl="${sf.serverurl}" deployRoot="create_package" runAllTests="false" maxPoll="${cumulusci.maxPoll.notest}" />
                <delete dir="${basedir}/create_package" />
            </then>
        </if>
    </target>

    <!-- runAllTests: Uses an empty package manifest to trigger execution of all tests in the target org without deploying any actual code changes -->
    <target name="runAllTests">
        <sf:deploy username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" deployRoot="${cumulus_ci.basedir}/empty" runAllTests="true" maxPoll="${cumulusci.maxPoll.test}" />
    </target>

    <!-- updatePackageXml: Builds a new package.xml based upon the metadata in the src directory -->
    <target name="updatePackageXml">
        <buildPackageXml package="${cumulusci.package.name}" version="${cumulusci.package.apiVersion}" />
    </target>

    <target name="updatePackageXmlManaged">
        <buildPackageXml package="${cumulusci.package.name.managed}" version="${cumulusci.package.apiVersion}" installClass="${cumulusci.package.installClass}" uninstallClass="${cumulusci.package.uninstallClass}" />
    </target>

    <target name="updateMetaXml">
        <for list="${required.packages}" delimiter="," param="packageNamespace">
            <sequential>
                <if>
                    <not><equals arg1="@{packageNamespace}" arg2="managed" /></not>
                    <then>
                        <updateMetaXml dir="${basedir}/src" namespace="@{packageNamespace}" version="${version.@{packageNamespace}}" />
                    </then>
                </if>
            </sequential>
        </for>
        <updateMetaXmlApi dir="${basedir}/src" apiVersion="${cumulusci.package.apiVersion}" />
    </target>

    <!-- Override hook targets: These targets are empty in the core file but exist for allowing projects to hook in their own custom logic without overriding the core targets -->

    <!-- Before uninstall -->
    <target name="preUninstall">
    </target>
    <!-- After uninstall. -->
    <target name="postUninstall">
    </target>

    <!-- Before updateRequiredPackages.  Can be used to uninstall packages in a downgrade scenario if building an extension package -->
    <target name="preUpdateRequiredPackages">
    </target>
    <!-- After updateRequiredPackages. -->
    <target name="postUpdateRequiredPackages">
    </target>

    <!-- Before deploy of src directory -->
    <target name="preDeploy">
    </target>
    <!-- After deploy of src directory -->
    <target name="postDeploy">
    </target>

    <!-- Before deployCI -->
    <target name="preDeployCI">
    </target>
    <!-- After deployCI -->
    <target name="postDeployCI">
    </target>

    <!-- Before deployCIPackageOrg -->
    <target name="preDeployCIPackageOrg">
    </target>
    <!-- After deployCIPackageOrg -->
    <target name="postDeployCIPackageOrg">
    </target>

    <!-- Before deployManaged -->
    <target name="preDeployManaged">
    </target>
    <!-- After deployManaged -->
    <target name="postDeployManaged">
    </target>

    <!-- Before deployManagedBeta -->
    <target name="preDeployManagedBeta">
    </target>
    <!-- After deployManagedBeta -->
    <target name="postDeployManagedBeta">
    </target>

</project>


{'fraction_non_alphanumeric': 0.1340280817535944, 'fraction_numerical': 0.0009764638540018183, 'mean_word_length': 2.5141099213157427, 'pattern_counts': {'":': 0, '<': 1011, '<?xml version=': 0, '>': 1011, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2589158', 'n_tokens_mistral': 15869, 'n_tokens_neox': 15062, 'n_words': 3836}