-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp.sh
executable file
·269 lines (250 loc) · 8.02 KB
/
p.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
#!/usr/bin/env bash
# Copyright (c) 2016 Benito Lopez under the MIT license
# INSTALL:
# Put something like this in your .bashrc:
# . /path/to/p.sh
# And ensure the script is executable
# chmod +x /path/to/p.sh
#
# Optionally:
# set $_P_CMD in .bashrc to change the command
# - default 'p'
# set $_P_DIR in .bashrc to change the projects dir
# - default '~/.projects'
# set $_P_OPEN_FUNC in .bashrc to change the function
# used to open the project with your default editor
# - default '$EDITOR .'
# Print error message and return error code
_p_print_error() {
if [ "$2" ]; then
echo "$2"
fi
if [ "$1" ]; then
return "$1"
else
return 1
fi
}
# Print help screen
_p_print_help() {
echo "Usage: p <option>"
echo ""
echo "Options:"
echo " <project name> Run project(s) configuration script."
echo " add <project name> Add project."
echo " list List all projects."
echo " delete <project name> Delete project(s)."
echo " o <project name> Open project(s) with the default editor."
echo " g <project name> Go to the project(s) directory."
echo " edit <project name> Edit project(s)."
echo " rename <old> <new> Rename project."
echo " -h --help Display this information."
echo " -v --version Display version info."
echo ""
}
# Check if project has a valid name (not reserved)
_p_check_project_name() {
case "$1" in
add|list|delete|edit|rename|o|-h|--help|-v|--version)
echo "$1: Illegal name"
return 1
esac
}
# Check if we have the editor environment variable
_p_check_editor() {
if [ -z "$EDITOR" ]; then
echo "The \$EDITOR environment variable is not set."
echo "You need to manually edit the configuration file in $p_dir."
return 1
fi
}
# Open the configuration file with the default editor
_p_edit_config_file() {
$EDITOR "$p_dir/$1.sh"
}
# Open the project with the default editor
_p_open_with_editor() {
p_path=$(sed -n "/^PROJECT_PATH=\(.*\)$/s//\1/p" $p_dir/$1.sh)
if [ -d "$p_path" ]; then
cd $p_path
# Open the folder - You can override this function with $_P_OPEN_FUNC
${_P_OPEN_FUNC:-$EDITOR .}
else
_p_print_error 1 "Directory does not exist"
return 1
fi
}
# Go to the project directory
_p_go_to_project() {
p_path=$(sed -n "/^PROJECT_PATH=\(.*\)$/s//\1/p" $p_dir/$1.sh)
if [ -d "$p_path" ]; then
# Go to the project directory
cd $p_path
else
_p_print_error 1 "Directory does not exist"
return 1
fi
}
_p() {
VERSION=3.0.0
# Set projects dir (can be changed with the environment variable $_P_DIR)
p_dir="${_P_DIR:-$HOME/.projects}"
# Create .projects dir
if [ ! -d "$p_dir" ]; then
mkdir "$p_dir"
fi
# Initialize empty PROJECTS array
PROJECTS=()
# Get the list of the installed projects
projects_files=($(find $p_dir -iname "*.sh"))
for project in "${projects_files[@]}"; do
name="${project##*/}"
name="${name%.*}"
PROJECTS+=($name)
done
case "$1" in
add)
# Add project
if [ "$2" ]; then
for argument in "${@:2}"; do
if [ -f "$p_dir/$argument.sh" ]; then
_p_print_error 1 "Project $argument already exists"
return 1
else
_p_check_project_name "$argument" || return
printf "#!/usr/bin/env bash\n\n# This script runs when you call the project with \"p %s\"\n\n# DO NOT DELETE THIS LINE\nPROJECT_PATH=$PWD\n\n" "$argument" > "$p_dir/$argument.sh"
echo "Added project $argument"
fi
done
else
_p_print_error 1 "No project name given"
return 1
fi
;;
list)
# List projects
if [ ${#PROJECTS[@]} -eq 0 ]; then
_p_print_error 1 "There are no projects"
return 1
else
for project in "${PROJECTS[@]}"; do
echo $project
done
fi
;;
delete)
# Remove project
if [ "$2" ]; then
for argument in "${@:2}"; do
if [ -f "$p_dir/$argument.sh" ]; then
rm "$p_dir/$argument.sh"
echo "Project $argument deleted"
else
_p_print_error 1 "Project $argument does not exist"
return 1
fi
done
else
_p_print_error 1 "No project name given"
return 1
fi
;;
edit)
# Edit project
if [ "$2" ]; then
for argument in "${@:2}"; do
if [ -f "$p_dir/$argument.sh" ]; then
_p_check_editor || return
_p_edit_config_file $argument
else
_p_print_error 1 "Project $argument does not exist"
return 1
fi
done
else
_p_print_error 1 "No project name given"
return 1
fi
;;
rename)
# Rename project
if [ "$2" ]; then
if [ ! -f "$p_dir/$2.sh" ]; then
_p_print_error 1 "Project $2 does not exist"
return 1
else
if [ "$3" ]; then
if [ -f "$p_dir/$3.sh" ]; then
_p_print_error 1 "Project $3 already exists"
return 1
else
mv "$p_dir/$2.sh" "$p_dir/$3.sh"
echo "Project renamed to $3"
fi
else
_p_print_error 1 "No new name given"
return 1
fi
fi
else
return_error 1 "No name given"
return 1
fi
;;
o)
# Open the project with the default editor
if [ "$2" ]; then
for argument in "${@:2}"; do
if [ -f "$p_dir/$argument.sh" ]; then
_p_check_editor || return
_p_open_with_editor $argument
else
_p_print_error 1 "Project $argument does not exist"
return 1
fi
done
else
_p_print_error 1 "No project name given"
return 1
fi
;;
g)
# Go to the project directory
if [ "$2" ]; then
if [ -f "$p_dir/$2.sh" ]; then
_p_go_to_project $2
else
_p_print_error 1 "Project $2 does not exist"
return 1
fi
else
_p_print_error 1 "No project name given"
return 1
fi
;;
-h|--help)
# Show help screen
_p_print_help
;;
-v|--version)
# Show version
echo "P $VERSION."
;;
*)
# Empty command
if [ -z "$1" ]; then
_p_print_help
# Run the project configuration file
else
for argument in "${@:1}"; do
if [ -f "$p_dir/$argument.sh" ]; then
. $p_dir/$argument.sh
else
_p_print_error 1 "Project $argument does not exist"
fi
done
fi
;;
esac
}
alias ${_P_CMD:-p}='_p 2>&1'