-
Notifications
You must be signed in to change notification settings - Fork 0
/
startws
executable file
·126 lines (106 loc) · 3.6 KB
/
startws
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
#
# Start session in a workspace
#
# Standard functions
GTWS_LOC=$(readlink -f $(dirname "$0"))
source ${GTWS_LOC}/gtws.sh
# Allow starting a workspace from inside another
clear_env
if [ -f "${PWD}/.gtwsrc" ]; then
debug_print "startws: loading ${PWD}"
load_rc "${PWD}"
else
debug_print "startws: sourcing Top"
source "${HOME}/.gtwsrc"
fi
if [ -n "${GTWS_PROJECT_VERSION}" ]; then
VERSION="${GTWS_PROJECT_VERSION}"
elif [ -n "${GTWS_DEFAULT_PROJECT_VERSION}" ]; then
VERSION="${GTWS_DEFAULT_PROJECT_VERSION}"
else
VERSION=$(basename "$PWD");
fi
if [ -n "${GTWS_PROJECT}" ]; then
PROJECT="${GTWS_PROJECT}"
elif [ -n "${GTWS_DEFAULT_PROJECT}" ]; then
PROJECT="${GTWS_DEFAULT_PROJECT}"
else
PROJECT=$(basename $(dirname "$PWD"));
fi
# Set usage output
DESCRIPTION="Start a session in a workspace"
USAGE="[-h |--help] [-i |--init] [-c <command> | --command=<command>] [-p <project> | --project=<project>] [-t <tag> | --tag=<tag>] [-v <version> | --version=<version>] [-V | --verbose] <name>"
LONGUSAGE="\t-h, --help\n\t\tPrint this help message
\t-i, --init\n\t\tInit WS tree
\t-c <command>, --command=<command>\n\t\tRun <command> in the workspace and exit
\t-p <project>, --project=<project>\n\t\tProject (default: ${PROJECT:-none})
\t-t <tag>, --tag=<tag>\n\t\tTag for this session
\t-v <version>, --version=<version>\n\t\tProject version (default: ${VERSION})
\t-V, --verbose\n\t\tBe verbose
\t<name>\n\t\tWorkspace name"
# Script name
ME=$(basename $0)
# Parse arguments
ARGS=`getopt -o hVic:p:t:v: --long help,verbose,init,command:,project:,tag:,version: -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage
fi
eval set -- "$ARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
-c|--command) SUBCMD=$2 ; shift 2 ;;
-i|--init) INIT=yes; shift ;;
-p|--project) PROJECT=$2 ; shift 2 ;;
-t|--tag) TAG=$2 ; shift 2 ;;
-v|--version) VERSION=$2 ; shift 2 ;;
-V|--verbose) export GTWS_VERBOSE=yes; shift ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
# Remaining arguments are in $1, $2, etc. as normal
if [ -n "${1}" ]; then
GTWS_WSNAME="${1%/}"
fi
if [ -z "${GTWS_WSNAME}" ]; then
usage "Must be in a workspace or give a workspace name"
fi
if [ -z "${PROJECT}" ]; then
usage "Must be in a project or give a project name"
fi
BASE=${GTWS_BASE_SRCDIR:-${HOME}/src}
if [ -n "${INIT}" ]; then
mkdir -p "${BASE}" || die "Failed to make ${BASE}"
cd "${BASE}" || die "Failed to cd to ${BASE}"
mkdir -p "${PROJECT}/${VERSION}/" || die "Failed to make ${PROJECT}/${VERSION}"
cd "${PROJECT}" || die "Failed to cd to ${PROJECT}"
if [ ! -f ".gtwsrc" ]; then
sudo chown ${USER}: . || die "Couldn't chown ${PROJECT}"
cp "${GTWS_LOC}/examples/gtwsrc.project" .gtwsrc \
|| die "Couldn't copy project gtwsrc"
fi
cd "${VERSION}" || die "Failed to cd to ${VERSION}"
if [ ! -f ".gtwsrc" ]; then
sudo chown ${USER}: . || die "Couldn't chown ${VERSION}"
cp "${GTWS_LOC}/examples/gtwsrc.version" .gtwsrc \
|| die "Couldn't copy version gtwsrc"
fi
fi
if [ ! -f "${BASE}/${PROJECT}/${VERSION}/${GTWS_WSNAME}/.gtwsrc" ]; then
usage "No .gtwsrc for ${PROJECT}/${VERSION}/${GTWS_WSNAME}"
fi
if [ -n "${SUBCMD}" ]; then
CMDLOC="${BASE}/${PROJECT}/${VERSION}/${GTWS_WSNAME}";
echo "${SUBCMD}" > ${CMDLOC}/.gtws_subcmd
COMMAND="-i ${CMDLOC}/.gtws_subcmd"
fi
# Clear env to get a clean one in the new workspace
clear_env
if [ -n "${TAG}" ]; then
export GTWS_TAG=${TAG}
fi
cd "${BASE}/${PROJECT}/${VERSION}/${GTWS_WSNAME}"
debug_print "exec bash --rcfile ${BASE}/${PROJECT}/${VERSION}/${GTWS_WSNAME}/.gtwsrc ${COMMAND}"
exec bash --rcfile ${BASE}/${PROJECT}/${VERSION}/${GTWS_WSNAME}/.gtwsrc ${COMMAND}