6

We are using subversion to commit all our meta data changes from our sandboxes. When we deploy to a testing environment, we want to get only the changed meta data which we can get from svn and dynamically create a package.xml file which would only have the changed metadata components and folders. Does anybody have an ant script which creates a package.xml dynamically from subversion? Thanks Buyan

  • Are you looking for windows script or unix based ? – Mohith Shrivastava Dec 16 '13 at 15:27
  • @MohithKumar a windows script would work for us. – buyan kumar Dec 16 '13 at 15:28
  • Sorry i love unix and fan of bash so windows script i dont have – Mohith Shrivastava Dec 16 '13 at 16:14
  • @MohithKumar Someone could always use something like cygwin to run the bash scripts on windows, so if you have something bash specific it could still be useful. – Peter Knolle Dec 16 '13 at 17:21
  • 1
    @PeterKnolle posted the bash script – Mohith Shrivastava Dec 17 '13 at 4:06
2
    #/usr/bin/env bash
# -bname build name
# -wspace build workspace dir
 # -lcommit builds last commit

#read command line args
while getopts b:w:l: option
 do
    case "${option}"
    in
            b) BNAME=${OPTARG};;
            w) WSPACE=${OPTARG};;
            l) LCOMMIT=${OPTARG};;
       esac
       done

echo Build Name: $BNAME
echo Workspace: $WSPACE
echo Last Commit: $LCOMMIT

#change working directory to the dir for this build
echo Changing working directory to /var/lib/jenkins/workspace
cd /var/lib/jenkins/workspace

  # check for the existence of an existing commit file for this
  # project
  SCRIPTFILE='/var/lib/jenkins/workspace/lastcommit/'$BNAME'.txt'
  echo Searching for script file $SCRIPTFILE...
  if [ -e $SCRIPTFILE ]
   then
    PREVRSA=$(<$SCRIPTFILE) &&
    echo Found previous SHA $PREVRSA

    # Backup existing Package.xml
    cd $WSPACE/src
    echo changing directoy to $WSPACE
    cp package.xml{,.bak} &&
    echo Backing up package.xml to package.xml.bak &&
    read -d '' NEWPKGXML <<EOF
  <?xml version="1.0" encoding="UTF-8"?>
  <Package>
   </Package>
 EOF      
    echo ===PKGXML===
    echo $NEWPKGXML
    echo Creating new package.xml
    echo $NEWPKGXML > $WSPACE/src/package.xml

    for CFILE in `git diff-tree --no-commit-id --name-only -r $LCOMMIT $PREVRSA`
    do
            echo Analyzing file `basename $CFILE`

            case "$CFILE"
            in
                    *.cls*) TYPENAME="ApexClass";;
                    *.page*) TYPENAME="ApexPage";;
                    *.component*) TYPENAME="ApexComponent";;
                    *.trigger*) TYPENAME="ApexTrigger";;
                    *.app*) TYPENAME="CustomApplication";;
                    *.labels*) TYPENAME="CustomLabels";;
                    *.object*) TYPENAME="CustomObject";;
                    *.tab*) TYPENAME="CustomTab";;
                    *.resource*) TYPENAME="StaticResource";;
                    *.workflow*) TYPENAME="Workflow";;
                    *.remoteSite*) TYPENAME="RemoteSiteSettings";;
                    *.pagelayout*) TYPENAME="Layout";;
                    *) TYPENAME="UNKNOWN TYPE";;
            esac

            if [[ "$TYPENAME" != "UNKNOWN TYPE" ]]
            then
                    ENTITY=$(basename "$CFILE")
                    ENTITY="${ENTITY%.*}"
                    echo ENTITY NAME: $ENTITY

                    if grep -Fq "$TYPENAME" $WSPACE/src/package.xml
                    then
                            echo Generating new member for $ENTITY
                            xmlstarlet ed -L -s "/Package/types[name='$TYPENAME']" -t elem -n members -v "$ENTITY" $WSPACE/src/package.xml
                    else
                            echo Generating new $TYPENAME type
                            xmlstarlet ed -L -s /Package -t elem -n types -v "" $WSPACE/src/package.xml
                            xmlstarlet ed -L -s '/Package/types[not(*)]' -t elem -n name -v "$TYPENAME" $WSPACE/src/package.xml
                            echo Generating new member for $ENTITY
                            xmlstarlet ed -L -s "/Package/types[name='$TYPENAME']" -t elem -n members -v "$ENTITY" $WSPACE/src/package.xml
                    fi
            else
                    echo ERROR: UNKNOWN FILE TYPE $CFILE
            fi
            echo ====UPDATED PACKAGE.XML====
            cat $WSPACE/src/package.xml
    done
    echo Saving last RSA Commit
    echo $LCOMMIT > /var/lib/jenkins/workspace/lastcommit/$BNAME.txt
    echo Cleaning up Package.xml
    xmlstarlet ed -L -s /Package -t elem -n version -v "27.0" $WSPACE/src/package.xml
    xmlstarlet ed -L -i /Package -t attr -n xmlns -v "http://soap.sforce.com/2006/04/metadata" $WSPACE/src/package.xml

    echo ====FINAL PACKAGE.XML=====
    cat $WSPACE/src/package.xml
 else
    echo No RSA found, default Package.xml will be used
    echo Creating new list commit file
    echo $LCOMMIT > /var/lib/jenkins/workspace/lastcommit/$BNAME.txt
fi

The above is the simple bash script that works .

  • Thx for the script. I will test this and let you know. – buyan kumar Dec 17 '13 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.