-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGKomander.sh
339 lines (276 loc) · 8.69 KB
/
GKomander.sh
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env bash
# It is highly recommended to create a "GK" alias.
# alias gk=". GKomander.sh"
# CONSTANTS
GK_VERSION=0.4.0
GK_COPYRIGHT="(c)2019 jwillp"
# Set home
GK_HOME="${GK_HOME:-$HOME/.gk}"
GK_PROJ_DIR="$GK_HOME/projects" # dir where the list of projects are stored
# Colors
export BLACK='\033[0;30m'
export DARK_GRAY='\033[1;30m'
export RED='\033[0;31m'
export LIGHT_RED='\033[1;31m'
export GREEN='\033[0;32m'
export LIGHT_GREEN='\033[1;32m'
export ORANGE='\033[0;33m'
export YELLOW='\033[1;33m'
export BLUE='\033[0;34m'
export LIGHT_BLUE='\033[1;34m'
export PURPLE='\033[0;35m'
export LIGHT_PURPLE='\033[1;35m'
export CYAN='\033[0;36m'
export LIGHT_CYAN='\033[1;36m'
export LIGHT_GRAY='\033[0;37m'
export WHITE='\033[1;37m'
export NC='\033[0m' # No Color
export RED='\033[0;31m'
## So echo uses colors and backslashes escape
alias echo="echo -e"
# Load autocompletion
if [[ -e $GK_HOME/GKomander-completion.sh ]]; then
source $GK_HOME/GKomander-completion.sh
fi
# Make sure GK_HOME directory exists
if [[ ! -d $GK_HOME ]]; then
mkdir $GK_HOME
mkdir -p $GK_HOME/templates
fi
if [[ ! -d $GK_PROJ_DIR ]]; then
mkdir $GK_PROJ_DIR
fi
function gk_return_error() {
# Print error message and return error code
if [ "$2" ]; then
echo "$2"
fi
if [ "$1" ]; then
return "$1"
else
return 1
fi
}
# Test if gk is sourced. It must not be ran in a subshell
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
SOURCE="${YELLOW}GKomander MUST be sourced - not run in a subshell.\ni.e. '. ./gk'\n${NC}"
gk_return_error 1 "$(printf "$SOURCE")"
exit
fi
# Displays help for Goat Komander
function gk_help() {
echo ""
echo "${LIGHT_GREEN}Goat Komander${NC} v"$GK_VERSION
echo ""
gk_usage
echo ""
echo "Options:"
echo " active Display name of the current project"
echo " switch_project|switch|load|<project name> Sets the current project to <project name>"
echo " -l,list List projects"
echo " new-project|new Creates a new project in the current directory"
echo " import|import-project|-i Imports an already existing project (cwd)"
echo " remove|rm Deletes a project and the current directory"
echo " update Updates gk to latest version"
echo " quit Quits the current Goat Komander session"
echo " -h --help Display this information"
echo " -v --version Display version info"
echo " "
echo "Please report bugs and issues at https://github.com/jwillp/GKomander"
}
# usage
function gk_usage() {
echo "usage: GoatKomander <option> [<args>] ..."
}
# Version information
function gk_version() {
echo "${LIGHT_GREEN}Goat Komander${NC} $GK_VERSION"
echo "$GK_COPYRIGHT"
}
# Change prompt to display project name
function gk_prompt_start() {
export OLD_PS1=$PS1
export PS1="${PS1::-3} [$1]$ "
}
# REset the path to its normal value
function gk_prompt_finish() {
export PS1=$OLD_PS1 #$OLD_PS1
}
# Returns the path of a project
function gk_get_proj_path() {
echo "$HOME/"`cat $GK_PROJ_DIR/$1`
}
# Removes a project
function gk_remove() {
if [[ $GK_ACTIVE_PROJECT ]]; then
gk_quit
fi
echo "Deleting $1 ..."
rm -rf gk_get_proj_path $1
rm $GK_PROJ_DIR/$1
echo "${LIGHT_GREEN}Project $1 deleted${NC}"
}
# Loads a project (should exist)
function gk_load() {
if [[ $GK_ACTIVE_PROJECT ]]; then
gk_quit
fi
projPath=`gk_get_proj_path $1`
cd $projPath
# Name of current project
export GK_ACTIVE_PROJECT=$1
# Home directory of current project
export GK_ACTIVE_HOME=$projPath
gk_prompt_start $1
source $projPath/gk_start.sh
# Create useful default aliases
# Get back home alias
alias hcd="cd $projPath"
alias cddoc="cd $projPath/doc"
alias cdsr="cd $projPath/src"
alias getcd="alias | grep -i cd"
}
# Quits gkomander
function gk_quit() {
echo "Quitting $GK_ACTIVE_PROJECT ... "
projPath=`gk_get_proj_path $GK_ACTIVE_PROJECT`
gk_prompt_finish
source $projPath/gk_stop.sh
export GK_ACTIVE_PROJECT=""
}
# Active project
function gk_active_project() {
echo "Active Project: "$GK_ACTIVE_PROJECT
}
# Change the active project for something else
function gk_switch_project() {
gk_load $1
}
# List available projects
function gk_list() {
projList=`ls -1p $GK_PROJ_DIR | grep -v /`
if [[ $projList == "" ]]; then
echo ""
echo "There are no projects"
echo "Create one using: GoatKomander new <project_name>"
else
# echo "Projects:"
# echo ""
ls -1p $GK_PROJ_DIR | grep -v /
fi
}
function gk_new_project() {
if [[ "$1" ]]; then
homeProj="$GK_PROJ_DIR/$1"
echo "HOME PROJ $homeProj"
if [[ ! -e $homeProj ]]; then
echo "Creating new project : $1 ..."
# Create project in current directory
projDir=`pwd`"/$1"
mkdir $projDir
# Create begin and quit scripts
# Use template script
template="${2:-default}.sh"
# Create start and stop scripts
if [[ ! -d "$projDir/gk_start.sh" ]]; then
printf "#!/usr/bin/env bash\n\n# This script will run when STARTING the project \"%s\"\n# Here you might want to cd into your project directory, activate virtualenvs, etc.\n\n# The currently active project is available via \$GK_ACTIVE_PROJECT\n\n" > "$projDir/gk_start.sh"
fi
if [[ ! -d "$projDir/gk_stop.sh" ]]; then
printf "#!/usr/bin/env bash\n\n# This script will run when STOPPING the project \"%s\"\n# Here you might want to deactivate virtualenvs, clean up temporary files, etc.\n\n# The currently active project is available via \$GK_ACTIVE_PROJECT\n\n" > "$projDir/gk_stop.sh"
fi
# Execute template script
$GK_HOME/templates/$template $1 $projDir
# Add project to local projects
touch $homeProj
relPath=`realpath --relative-to=$HOME $projDir`
echo $relPath > $homeProj
# Create default directory structure according to template
# TODO
echo "${LIGHT_GREEN}Project \"$1\" created successfully${NC}"
gk_load "$1"
else
echo "Goat Komander: Project already exists, please choose another name"
fi
else
echo "Goat Komander: You must specify a project name"
return 1
fi
}
# Imports an already existing project
# imports the current directory as project with name $1
function gk_import_project() {
if [[ "$1" ]]; then
homeProj="$GK_PROJ_DIR/$1"
if [[ ! -e $homeProj ]]; then
# Add project to local projects
touch $homeProj
relPath=`realpath --relative-to=$HOME .`
echo $relPath > $homeProj
echo "${LIGHT_GREEN}Project \"$1\" created successfully${NC}"
else
echo "Goat Komander: Project already exists, please choose another name"
fi
else
echo "Goat Komander: You must specify a project name"
return 1
fi
}
function gk_update() {
old_pwd=`pwd`
cd $GK_HOME
git pull
cd $old_pwd
echo "${LIGHT_GREEN}GKomander update done${NC}"
}
# Main procedure
case "$1" in
active)
gk_active_project
;;
switch_project|switch|load)
gk_switch_project ${@:2}
;;
list|-l)
gk_list
;;
new|new-project)
gk_new_project ${@:2}
;;
import|import-project| -i)
gk_import_project ${@:2}
;;
rm|remove)
if [[ -e $GK_PROJ_DIR/$2 ]]; then # dir where the list of projects are storeden
gk_remove ${@:2}
else
echo "Goat Komander: Deletion of project failed. The project $2 does not exist"
return 1
fi
;;
update)
gk_update
;;
quit)
gk_quit
;;
-h|--help)
gk_help
;;
-v|--version)
gk_version
;;
*) # Anything else
if [ -z "$1" ]; then
gk_help
else
# Find project
if [[ -e $GK_PROJ_DIR/$1 ]]; then
gk_load $1
else
# If no project found
echo "Goat Komander: '$1' is not a command. See GoatKomander --help for help"
return 1
fi
fi
;;
esac