Last active
September 12, 2017 05:33
-
-
Save eghojansu/1de33f233eb575b51b04510cb8bb95f6 to your computer and use it in GitHub Desktop.
A Shell Script to "ZIP" deployment version of your code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Zipping Git Directory | |
# Usage: | |
# __THIS_FILE__ [ project_name ] [ version ] | |
# | |
# project_name -- default to directory name | |
# version -- default to latest version | |
# | |
# Timer from (http://www.linuxjournal.com/content/use-date-command-measure-elapsed-time) | |
# | |
# Elapsed time. Usage: | |
# | |
# t=$(timer) | |
# ... # do something | |
# printf 'Elapsed time: %s\n' $(timer $t) | |
# ===> Elapsed time: 0:01:12 | |
# | |
# | |
##################################################################### | |
# If called with no arguments a new timer is returned. | |
# If called with arguments the first is used as a timer | |
# value and the elapsed time is returned in the form HH:MM:SS. | |
# | |
function timer() | |
{ | |
if [[ $# -eq 0 ]]; then | |
echo $(date '+%s') | |
else | |
local stime=$1 | |
etime=$(date '+%s') | |
if [[ -z "$stime" ]]; then stime=$etime; fi | |
dt=$((etime - stime)) | |
ds=$((dt % 60)) | |
dm=$(((dt / 60) % 60)) | |
dh=$((dt / 3600)) | |
printf '%d:%02d:%02d' $dh $dm $ds | |
fi | |
} | |
# current dir name | |
CD=${PWD##*/} | |
CF=${0##*/} | |
# default version | |
defaultVersion='0.1.0' | |
# Destination path | |
destinationPath='/home/fal/Temp' | |
# clear console | |
clear | |
# Get project name, default to current directory name | |
if [ -z "$1" ] ; then | |
projectName=$CD | |
else | |
projectName=$1 | |
fi | |
echo "Current directory is $PWD" | |
echo " zipping this directory as '$projectName'..." | |
# using version | |
if [ -z "$2" ] ; then | |
# finding latest version | |
version=$(git tag --list | tail -1) | |
if [ -z "$version" ] ; then | |
# using default version | |
needToCheckout=false | |
version=$defaultVersion | |
else | |
# using latest version | |
needToCheckout=true | |
checkTag=$(echo $version | grep -oP '.*?(?=\-)') | |
if [ $(git tag --list "$checkTag") ]; then | |
# found stable version (no alpha/beta suffix) | |
version=$checkTag | |
fi | |
fi | |
else | |
# check given version | |
if [ ! $(git tag --list "$2") ]; then | |
# given version invalid | |
echo "Error: Given version was not exists" | |
exit | |
fi | |
# using given version | |
version=$2 | |
needToCheckout=true | |
fi | |
# Zip location | |
destination=$destinationPath'/'$projectName'-'$version'.zip' | |
echo " zipping to '$destination'" | |
# Check file | |
if [ -f $destination ] ; then | |
rm -f $destination | |
echo ' (zip file was exists and has been deleted)' | |
fi | |
# current branch | |
currentBranch=$(git branch --list | grep -e '^*' | cut -d' ' -f 2) | |
echo " current branch is $currentBranch" | |
if $needToCheckout; then | |
# Checking out version | |
echo " checking out $version" | |
git checkout -q $version | |
else | |
echo ' using current branch' | |
fi | |
# cd to parent | |
pushd .. > /dev/null | |
# zip it | |
t=$(timer) | |
printf ' zipping...' | |
# Rules (please modify if rules below was not satisfy your needs) | |
zip \ | |
-wsqr9 \ | |
-b /tmp \ | |
-x \ | |
"*/.idea/**" \ | |
"*/.git/**" \ | |
"*/tests/**" \ | |
"*/node_modules/**" \ | |
"*/bower_components/**" \ | |
"*/tests/**" \ | |
"*/var/cache/**" \ | |
"*/var/logs/**" \ | |
"*/var/sessions/**" \ | |
"*/gulpfile.js" \ | |
"*/*.sublime-*" \ | |
"*/*.lock" \ | |
"*/*.dist" \ | |
"*/package.json" \ | |
"*/bower.json" \ | |
"*/ftpsync.setting" \ | |
"*/$CF" \ | |
@ \ | |
$destination $CD'/' | |
printf 'done (elapsed time: %s)\n' $(timer $t) | |
# cd to starting dir | |
popd > /dev/null | |
if $needToCheckout; then | |
# Revert previous checkout | |
echo " checking out $currentBranch" | |
git checkout -q $currentBranch | |
fi | |
echo 'job done' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment