-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkws
executable file
·160 lines (132 loc) · 4.3 KB
/
mkws
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
#
# Create a new Project development workspace
#
function GTWScleanup {
if [ -n "${MKWS_CLEANUP}" ]; then
rm -rf "${GTWS_WSPATH}"
fi
}
# Standard functions
GTWS_LOC=$(readlink -f $(dirname "$0"))
source ${GTWS_LOC}/gtws.sh
if [ -f "${PWD}/.gtwsrc" ]; then
load_rc "${PWD}"
else
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="Create a new development workspace"
USAGE="[-h |--help] [-s <submodule-base> | --submodule-base=<submodule-base>] [-p <project> | --project=<project>] [-v <version> | --version=<version>] [-V | --verbose] <name>"
LONGUSAGE="\t-h, --help\n\t\tPrint this help message
\t-s <submodule-base>, --submodule-base=<submodule-base>\n\t\tBase dir for submodules (default: project origin)
\t-p <project>, --project=<project>\n\t\tProject (default: ${PROJECT:-none})
\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 hVs:p:v: --long help,verbose,submodule-base:,project:,version: -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage
fi
eval set -- "$ARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
-s|--submodule-base) SUBMODULE_BASE=$2 ; shift 2 ;;
-p|--project) PROJECT=$2 ; shift 2 ;;
-v|--version) VERSION=$2 ; shift 2 ;;
-V|--verbose) export GTWS_VERBOSE=yes; shift ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
is_gtws && usage "Cannot create workspace in an existing workspace"
# Remaining arguments are in $1, $2, etc. as normal
if [ -z "${1}" ]; then
usage "Must give a workspace name"
fi
if [ -z "${PROJECT}" ]; then
usage "Must give a project"
fi
if [ -z "${GTWS_PROJECT}" ]; then
# We weren't in a project directory; go there
BASE="${GTWS_BASE_SRCDIR:-${HOME}/src}/${PROJECT}/${VERSION}"
cd "${BASE}" || die "Couldn't cd to project dir ${BASE}"
elif [ "${PROJECT}" != "${GTWS_PROJECT}" ]; then
die "In wrong project dir: expected ${PROJECT} got ${GTWS_PROJECT}"
fi
GTWS_WSPATH=$(readlink -f "${1}")
GTWS_WSNAME=$(basename "${GTWS_WSPATH}")
# Target directory check
if [ -d "${GTWS_WSPATH}" ]; then
die "Workspace ${GTWS_WSPATH} already exists"
fi
# From here on out, clean up on failure
MKWS_CLEANUP="yes"
# Set up the environment
mkdir -p "${GTWS_WSPATH}" || die "Failed to mkdir ${GTWS_WSPATH}"
cd "${GTWS_WSPATH}"
cp "${GTWS_LOC}"/gtwsrc "${GTWS_WSPATH}"/.gtwsrc
source "${GTWS_WSPATH}"/.gtwsrc
# Origin
O="${GTWS_ORIGIN:=${HOME}/origin}"
if [ -z "${SUBMODULE_BASE}" ]; then
gtws_smopvn "${GTWS_SUBMODULE_ORIGIN:-${O}}" "${PROJECT}" "${VERSION}" "${GTWS_WSNAME}" SUBMODULE_BASE || die "Could not get SUBMODULE_BASE"
fi
HAS_CLONE=$(declare -F | grep "\<gtws_project_clone\>")
if [ -z "${HAS_CLONE}" ]; then
gtws_opvn "${O}" "${PROJECT}" "${VERSION}" "${GTWS_WSNAME}" opv
# Workspace contents check
repos=${GTWS_PROJECT_REPOS}
if [ -z "${repos}" ]; then
for i in "${opv}"/*; do
if is_git_repo "$i"; then
repos="$(basename $i) $repos"
fi
done
fi
for repo in ${repos}; do
rpath="${opv}/${repo}"
if ! is_git_repo "$rpath"; then
rpath="${opv}/${repo}.git"
if ! is_git_repo "$rpath"; then
die "${rpath} in origin ${opv} is not git"
fi
fi
done
if [ -z "${repos}" ]; then
die "No git repos in origin ${O}/${PROJECT}/${VERSION}"
fi
export GTWS_PROJECT_REPOS=${repos}
debug_print "new repos ${GTWS_PROJECT_REPOS}"
fi
# Make workspace
echo "Making workspace for ${VERSION}:${TARGET} in ${GTWS_WSPATH}"
# Made directory above
touch "${GTWS_WSPATH}/.sdirs"
if [ -n "${HAS_CLONE}" ]; then
# Project provided a clone
gtws_project_clone "${O}" "${PROJECT}" "${VERSION}" "${GTWS_WSNAME}" "${SUBMODULE_BASE}" || die "clone failed"
else
# Default clone
gtws_project_clone_default "${O}" "${PROJECT}" "${VERSION}" "${GTWS_WSNAME}" "${SUBMODULE_BASE}" || die "clone failed"
fi
gtws_project_setup "${GTWS_WSNAME}" "${O}" "${PROJECT}" "${VERSION}" || die "setup failed"
echo "Done"