Skip to content

Instantly share code, notes, and snippets.

@dmulligan
Created June 27, 2013 13:18
Show Gist options
  • Save dmulligan/5876324 to your computer and use it in GitHub Desktop.
Save dmulligan/5876324 to your computer and use it in GitHub Desktop.
Shell script to checkout an SVN repo using Git, build using Maven and import into Eclipse
################################################################################
#!/bin/sh
#
# Author: dave-at-mulligan.ie
# Useful URLs:
# https://gist.github.com/dmulligan/5876324
# http://maymay.net/blog/2009/02/24/how-to-use-git-svn-as-the-only-subversion-client-youll-need/
# http://maymay.net/blog/2008/03/26/howto-use-git-for-personal-development-when-everyone-else-is-using-subversion-part-1/
# http://viget.com/extend/effectively-using-git-with-subversion
# http://www.jukie.net/~bart/blog/svn-branches-in-git
# http://viget.com/extend/a-gaggle-of-git-tips
# The location of Eclipse.
#ECLIPSE_HOME=/c/DEV/apps/java/eclipse-jee-juno-SR1-win32-x86_64
# The Eclipse workspace to use.
WORKSPACE=$PWD
# Project directory to use.
pDir=$(echo $1 | sed -n 's/.*\///p')
################################################################################
# Ensure everything is in place before starting.
echo "################################################################################"
echo "Ensure you have the 'pkg-config support for Eclipse CDT' plugin installed"
echo "################################################################################"
if [ -z $ECLIPSE_HOME ]; then
echo -e "\n*** Error: ECLIPSE_HOME is not set."
exit 1
fi
if [ ! -f $ECLIPSE_HOME/eclipse ]; then
echo -e "\nError: ECLIPSE_HOME is not valid."
exit 1
fi
# Check if the Eclipse workspace is locked!
if [ -f $WORKSPACE/.metadata/.lock ]; then
echo -e "\n*** Error: Eclipse workspace is locked."
exit 1
fi
if [ -z $MVN_HOME ]; then
echo -e "\n*** Error: MVN_HOME is not set."
exit 1
fi
# TODO: A check that ensures the first param is a URL
# TODO: A check to ensure that the first param does not end with /trunk, /branches or /tags
################################################################################
# Clone the SVN repo into a Git repo.
if [ ! -f $pDir ]; then
echo -e "\n*** Cloning $1"
git svn clone -s $1 $pDir
if [ "$?" != "0" ]; then
echo -e "\n*** Error cloning SVN repo: $1"
exit 1
fi
echo -e "\n*** Clearing out the garbage"
git --git-dir=$pDir/.git --work-tree=$pDir gc
else
echo -e "\n*** $pDir already exists"
fi
################################################################################
# Create the project .gitignore file.
if [ ! -f $pDir/.gitignore ]; then
echo -e "\n*** Creating new $pDir/.gitignore"
echo ".gitignore" > $pDir/.gitignore
git --git-dir=$pDir/.git --work-tree=$pDir svn show-ignore >> $pDir/.gitignore
else
echo -e "\n*** $pDir/.gitignore already exists, no changes made."
fi
################################################################################
# If the project contains a pom.xml file, build it and create the Eclipse files.
if [ -f $pDir/pom.xml ]; then
echo -e "\n*** Building project $pDir"
$MVN_HOME/bin/mvn -f $pDir/pom.xml clean install eclipse:eclipse
fi
################################################################################
# If the project contains a .project file, import the project into Eclipse.
# Check if Eclipse workspace is locked!
if [ -f $WORKSPACE/.metadata/.lock ]; then
echo -e "\n*** Error: Eclipse workspace is in use/locked."
exit 1
fi
# Import the parent project into Eclipse.
if [ ! -f $pDir/.project ]; then
echo -e "\n*** Creating a default Eclipse project for $pDir"
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><projectDescription><name>$pDir</name><comment/><projects/><buildSpec/><natures/></projectDescription>" > $pDir/.project
fi
if [ -f $pDir/.project ]; then
echo -e "\n*** Importing Eclipse project into workspace: $pDir"
# eclipse -nosplash -application org.eclipse.cdt.managedbuilder.core.headlessbuild -importAll {[uri:/]/path/to/project}
$ECLIPSE_HOME/eclipse -nosplash -application org.eclipse.cdt.managedbuilder.core.headlessbuild \
-data $WORKSPACE \
-import $pDir \
-vmargs -Dorg.eclipse.cdt.core.console=org.eclipse.cdt.core.systemConsole
fi
# Import each of the project's modules into Eclipse.
for sdir in $(find $pDir -mindepth 1 -maxdepth 1 -type d -print | sort)
do
if [ -f $sdir/.project ]; then
echo -e "\n*** Importing Eclipse project into workspace: $sdir"
$ECLIPSE_HOME/eclipse -nosplash -application org.eclipse.cdt.managedbuilder.core.headlessbuild \
-data $WORKSPACE \
-import $sdir \
-vmargs -Dorg.eclipse.cdt.core.console=org.eclipse.cdt.core.systemConsole
fi
done
# I would have expected the following to be done automatically!
if [ -f $WORKSPACE/.metadata/.lock ]; then
echo -e "\n*** Deleting Eclipse workspace .lock file"
rm $WORKSPACE/.metadata/.lock
fi
echo -e "\n*** Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment