Home

Advertisement

Nov. 18th, 2007

Mirror

The more complex it becomes the simpler it is...

The following shell script is the evolution of my previous scripting attempt. It combines "HTML Tidy" with the necessary Jar making facilities for what I do with Anathema and Yushan.

This script itself has gone through many different revisions to get to this point and still is not yet "complete". I would like to add the ability to find the plug-in directory and to store it as a configuration file. I have attempted that through a find for "plugin-fragment.xml" but it seems that TextWrangler keeps file of that name as a pointer to recently used files and it's the first one in the list. Oh, well a task for another day.

anathemaTool.sh


#!/bin/bash

######################
## global constants ##
######################

anathemaLog=$HOME/Library/Logs/anathemaTool.log ; # Sets default $USER log location in OS X.
anathemaHome=$HOME/Documents/Development/Anathema ; # Sets default repository for plugin directories.
tidyConfiguration=$( mktemp /tmp/anathema.tidy.XXXXXX ) ; # Sets tidy configuration file name.

####################
## make main menu ##
####################

function makeMainMenu {
clear ;
echo "\n" ;
echo "\n" ;
echo "###########################" ;
echo "A.) About Anathema Tool..." ;
echo "L.) License Information..." ;
echo "1.) Tidy up your XML." ;
echo "2.) Create a JAR file." ;
echo "Q.) Quit Anathema Tool" ;
echo "###########################" ;
echo "\n" ;
echo "\n" ;
read -p "Which option do you need? (A, L, 1, 2 or Q): " selectOption ;
case "$selectOption" in
"a" | "A" )
echo "Displaying information \"About\" this script." >> $anathemaLog ;
aboutAnathemaTool ;
;;
"l" | "L" )
echo "Displaying \"Licensing\" information about this script." >> $anathemaLog ;
licenseInformationText ;
;;
"1" )
echo "Starting Tidy function.\n" >> $anathemaLog ;
tidyUpXML ;
;;
"2" )
echo "Starting Jar making function.\n" >> $anathemaLog ;
makePluginJar ;
;;
* )
echo "Removing Tidy configuration: $tidyConfiguration ." >> $anathemaLog ;
rm -f $tidyConfiguration ;
echo "Exiting $0 ." >> $anathemaLog ;
exit 0;
;;
esac
}

############################
## why i made this script ##
############################

function aboutAnathemaTool {
echo "This tool is designed to facilitate my work creating plug-ins for Anathema. It takes\na series of tasks that used to take 10+ minutes with 2 different operating systems to\na minute (or less) and just one operating system. \n\nThis tool is designed to work on OS X 10.5 \"Leopard\" without needing to have any \nadditional software installed. It may also work on other systems with some small \nmodifications, like the installation of GNU's \"ClassPath\" or Sun's \"Java Development \nKit\" programs. \n\nFeel free to modify this script to add any capabilities that you require.\n\nPress \"Q\" to continue." | less ;
makeMainMenu ;
}

#####################################
## display license text for script ##
#####################################

function licenseInformationText {
echo "anathemaTool.sh - An plug-in creation tool for Anathema. \nCopyright (C) 2007 Paul R. Adams (pauladams@hotmail.com)\nThis program is free software; you can redistribute it and/or modify \nit under the terms of the GNU General Public License as published by \nthe Free Software Foundation; either version 2 of the License, or \n(at your option) any later version. \n\nThis program is distributed in the hope that it will be useful, \nbut WITHOUT ANY WARRANTY; without even the implied warranty of \nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the \nGNU General Public License for more details. \n\nYou should have received a copy of the GNU General Public License \nalong with this program; if not, write to the Free Software \nFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n\nPress \"Q\" to continue." | less ;
makeMainMenu ;
}

######################################
## test existence of directory name ##
######################################

function testDirectoryExistence {
if [ -z $pluginDirectory ] ; then
read -p "Please enter the directory containing the plug-in files: " pluginDirectory ;
fi
echo "Searching for plug-in directory: $anathemaHome/$pluginDirectory ." >> $anathemaLog ;
if [ -d $pluginDirectory ] ; then
echo "Directory \"$pluginDirectory\" has been found. Returning control to calling function.\n" >> $anathemaLog ;
return 0 ;
else
local noDirectory ;
echo "Directory \"$pluginDirectory\" does not exist. " >> $anathemaLog ;
read -p "The \"$pluginDirectory\" directory does not exist. Try again? (y/n) " noDirectory ;
if [ $( echo $noDirectory | tr Y y ) == "y" ] ; then
read -p "Please re-enter the directory containing the plug-in files: " pluginDirectory ;
testDirectoryExistence ;
else
pluginDirectory="" ;
return 1 ;
fi
fi
}

##############################
## tidy xml nested function ##
##############################

function tidyUpXML {
function compareTidyConfiguration {
local existingConfiguration ;
existingConfiguration="$( cat $tidyConfiguration )" ;
echo "Comparing Tidy configuration in file $tidyConfiguration to defaults." >> $anathemaLog ;
if [ "$existingConfiguration" == "$(echo "indent: auto\nindent-spaces: 5\nlanguage: en-US\nnewline: CRLF\noutput-encoding: utf8\nwrap: 0\nadd-xml-decl: yes\ninput-encoding: utf8\ninput-xml: yes\noutput-xml: yes")" ] ; then
echo "Tidy configuration match confirmed, running Tidy." >> $anathemaLog ;
runXMLTidy ;
else
echo "Tidy configuration does not match or is blank, must replace file. " >> $anathemaLog ;
writeTidyConfiguration ;
fi
}
function writeTidyConfiguration {
echo "Writing Tidy configuration file to: $( basename $tidyConfiguration ) ." >> $anathemaLog ;
echo "indent: auto\nindent-spaces: 5\nlanguage: en-US\nnewline: CRLF\noutput-encoding: utf8\nwrap: 0\nadd-xml-decl: yes\ninput-encoding: utf8\ninput-xml: yes\noutput-xml: yes" > $tidyConfiguration ;
runXMLTidy ;
}
function runXMLTidy {
echo "#### XML Tidy ####\n" >> $anathemaLog ;
tidy -m -config $tidyConfiguration $( find $pluginDirectory -type f -name *.xml ) >> $anathemaLog 2>&1 ;
echo "\n##################" >> $anathemaLog ;
}

testDirectoryExistence ;
if [ ! -z "$pluginDirectory" ] ; then
compareTidyConfiguration ;
fi
pluginDirectory="" ;
echo "Returning to main menu.\n" >> $anathemaLog ;
makeMainMenu ;
}

################################
## jar making nested function ##
################################

function makePluginJar {
function testJarExistence {
if [ ! -f $pluginDirectory.jar ] ; then
makeJarFile ;
else
echo "Existing plug-in JAR detected at: $anathemaHome/$pluginDirectory.jar ." >> $anathemaLog ;
local overwriteJar ;
read -p "\"$pluginDirectory.jar\" already exists. Overwrite? (y/n) " overwriteJar ;
if [ $( echo $overwriteJar | tr Y y ) == "y" ] ; then
echo "Approval given to overwrite: $pluginDirectory.jar ." >> $anathemaLog ;
rm -f $pluginDirectory.jar ;
testJarExistence ;
else
echo "Denied permission to overwrite: $pluginDirectory.jar ." >> $anathemaLog ;
return 1 ;
fi
fi
}
function makeJarFile {
temporaryDirectory=$( mktemp -d /tmp/plugin.XXXXXX ) ;
echo "Copying plug-in to temporary directory: $temporaryDirectory " >> $anathemaLog ;
cp -R $pluginDirectory $temporaryDirectory ;
echo "Removing SubVersioN meta-data from: $( basename $temporaryDirectory)." >> $anathemaLog ;
rm -rf $( find $temporaryDirectory -type d -name .svn ) ;
echo "Removing OS X meta-data from: $( basename $temporaryDirectory)." >> $anathemaLog ;
rm -f $( find $temporaryDirectory -type f -name .DS_Store ) ;
echo "Creating JAR and moving to: $anathemaHome/$pluginDirectory.jar ." >> $anathemaLog ;
$( cd $temporaryDirectory ; cd $pluginDirectory; jar cf $anathemaHome/$pluginDirectory.jar * ) ;
echo "Removing temporary directory: $temporaryDirectory" >> $anathemaLog ;
rm -rdf $temporaryDirectory ;
open -g $anathemaHome ;
}

testDirectoryExistence ;
if [ ! -z "$pluginDirectory" ] ; then
testJarExistence ;
fi
pluginDirectory=""
echo "Returning to main menu.\n" >> $anathemaLog ;
makeMainMenu ;
}

echo "" > $anathemaLog ;
cd $anathemaHome ;
echo "Starting main menu." >> $anathemaLog ;
makeMainMenu ;


Now to post it to Yushan.

UPDATE: Fixed typo with makeMainMenu (Quite -> Quit).

Advertisement

Customize