-
Notifications
You must be signed in to change notification settings - Fork 1
/
codeshell
executable file
·344 lines (300 loc) · 10.3 KB
/
codeshell
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
340
341
342
#!/usr/bin/bash
curfile="$0"
curdir=$(realpath "$(dirname $curfile)");
default_template="cpp";
detect_distro() {
if [[ "$OSTYPE" == linux-android* ]]; then
distro="termux"
fi
if [ -z "$distro" ]; then
distro=$(ls /etc | awk 'match($0, "(.+?)[-_](?:release|version)", groups) {if(groups[1] != "os") {print groups[1]}}')
fi
if [ -z "$distro" ]; then
if [ -f "/etc/os-release" ]; then
distro="$(source /etc/os-release && echo $ID)"
elif [ "$OSTYPE" == "darwin" ]; then
distro="darwin"
else
distro="invalid"
fi
fi
}
detect_distro
main="left"
side="right"
echo Distro: $distro
if [[ "$distro" == "termux" ]]; then
main="top"
side="bottom"
fi
tmpdir=""
template=""
buildsys="Ninja"
codeshells_dir="$HOME/codeshells"
template_root=$(realpath "$(dirname $0)/../code-templates")
function move_and_restart {
name="$1"
if [ -d "$codeshells_dir/$name" ]; then
echo "The directory ($codeshells_dir/$name) already exists; use another name.";
exit 1;
fi
echo "Creating $codeshells_dir/$name";
mkdir -p "$codeshells_dir/$name";
echo "Movint to $codeshells_dir/$name";
this_dir=$(realpath .);
mv -T "$this_dir" "$codeshells_dir/$name";
tmux kill-pane -a; # kill all the panes
bash "$curfile" "$name"; # re-run the codeshell
}
function remove_codeshell {
cmd="$1"
name="$2"
if [ "$cmd" = "trash" ]; then
if command -v trash-put >/dev/null; then
cmd="trash-put";
else
echo "Trash is not supported; please install trash-cli package.";
echo " $ sudo pacman -Syu trash-cli";
exit 1;
fi
elif [ "$cmd" = "remove" ]; then
cmd="rm -rf";
else
echo "Unknown command $cmd";
exit 1;
fi
if [ -z "$name" -o "$name" = "." ]; then
directory=$(realpath .);
eval $cmd "$directory";
echo "Removed/Trashed $directory";
tmux kill-pane -a; # kill all the panes
return;
elif [ -d "$codeshells_dir/$name" ]; then
directory="$codeshells_dir/$name";
eval $cmd "$directory";
echo "Removed/Trashed $directory";
tmux kill-pane -a; # kill all the pane
else
echo "The name $name doesn't name a codeshell directory."
exit 1;
fi
}
for i in "$@"; do
case $i in
-t=*|--template=*)
template="${i#*=}"
shift # past argument=value
;;
-n=*|--name=*)
dir="${i#*=}"
shift
;;
-h|--help|help)
echo "Usage: codeshell [template=${default_template}] [name=...]"
echo " codeshell help"
echo " codeshell save NAME # Save the temp codeshell you're in"
echo " codeshell remove [NAME] # Remove the codeshell you're in or the codeshell you named"
echo " codeshell trash [NAME] # Move to trash the codeshell you're in or the codeshell you named"
echo
echo " -n=*|--name=* the name of the project"
echo " -t=*|--template=* the template to use"
echo " -g=*|-G=* the build system to use in cmake"
echo
echo
echo "Flow of usage:"
echo " Imagine you wanna try something real quick; you type:"
echo " $ codeshell c.linux"
echo
echo " Now you do all your testings..."
echo " Now you liked the tests and want to save it, you type:"
echo " $ codeshell save"
echo
echo " Or maybe the tests weren't good, so you want to remove it:"
echo " $ codeshell trash"
echo
echo " Or you if you know the name of your tests already from the get go you type:"
echo " $ codeshell c.linux my.pretty.test"
echo " Or:"
echo " $ codeshell my.pretty.test c.linux"
echo " as long as 'c.linux' is a known template in the '$template_root' directory."
echo
echo " If you're using 'trash' instead of 'remove' then you can use trash-cli to manage them:"
echo " $ trash-list"
echo " $ trash-rm /tmp/..."
echo " $ trash-restore /tmp/..."
echo " $ trash-put /tmp/..."
exit;
;;
-g=*|-G=*)
buildsys="${i#*=}"
;;
save)
shift;
move_and_restart $1;
exit;
;;
ls|list|--list)
shift;
ls "$codeshells_dir" --color=auto --hyperlink=auto $@
exit;
;;
remove)
shift;
remove_codeshell remove $1;
exit;
;;
trash)
shift;
remove_codeshell trash $1;
exit;
;;
*)
if [ -d "$template_root/$i" ]; then
template="$i";
else
dir="$i"
fi
shift;
;;
esac
done
if [ ! -z "$dir" ]; then
tmpdir="$codeshells_dir/$dir"
mkdir -p "$tmpdir"
fi
# if there's no tempalte and no name
if [ -z "${template}" ] && [ ! -z "${tmpdir}" ]; then
if [ -s "${tmpdir}/.template" ]; then
template=$(cat "${tmpdir}/.template")
fi
fi
if [ -z "$template" ]; then
template="${default_template}"
fi
if [ -z "$tmpdir" ]; then
tmpdir=$(mktemp -d --suffix=-codeshell)
fi;
tmpdir=$(realpath ${tmpdir});
template_dir="${template_root}/${template}"
if [ ! -d $template_dir ]; then
echo "The specified template ($template) does not exists. Possible code-templates are:"
dir="$(dirname $0)/../code-templates";
echo "Directory: $dir"
ls "$dir"
exit;
fi;
if [ -z "$TMUX" ]; then
window="code-$((1 + RANDOM % 10000000))"
tmux new -s ${window} -d -c "${tmpdir}"
else
window=$(tmux display-message -p "#S")
fi
if [[ "$main" == "left" ]]; then
tmux split-window -t ${window} -h
else
tmux split-window -t ${window}
fi
tmux select-pane -t ${window}.${side}
tmux resize-pane -t ${window}.${side} -R 18
tmux send-keys -t ${window}.${main} "cd ${tmpdir}" C-m
tmux send-keys -t ${window}.${side} "cd ${tmpdir}" C-m
# copy files if the directory does not exists
if [ `ls -A ${tmpdir} | wc -m` == "0" ]; then
cp -nTr "$template_dir" "${tmpdir}"
fi;
# write config files:
echo "${template}" > "${tmpdir}/.template";
if [ -f "${tmpdir}/.editor" ]; then
EDITOR=$(cat "${tmpdir}/.editor");
elif [ -z "$EDITOR" ]; then
EDITOR="nvim";
fi;
function open_editor() {
local file="$1";
local manifest_file="$2";
local nonblocking=""
local kill_window=0;
case "$EDITOR" in
qtcreator)
nonblocking="setsid"
file=$manifest_file;
;;
aqua|clion1|clion|dataspell|phpstorm|pycharm|studio|webstorm)
file="${tmpdir}";
kill_window=1;
;;
code)
file="${tmpdir}";
;;
esac
if [ "$file" != "nothing" ]; then
tmux send-keys -t ${window}.${main} "${nonblocking} ${EDITOR} ${file}" C-m
fi
if [ $kill_window == 1 ]; then
tmux send-keys -t ${window}.${main} "tmux kill-window" C-m;
fi
}
echo "Editor: ${EDITOR}"
echo "Template: ${template}"
echo "Template directory: ${template_dir}"
echo "Code directory: ${tmpdir}"
# run build commands
if [ -f "${template_dir}/.start" ]; then
start_file="${template_dir}/.start";
main_file=$(cat "${start_file}" | grep "main:" | cut -d" " -f2- | sed 's/^[ \t]*//;s/[ \t]*$//');
manifest_file=$(cat "${start_file}" | grep "manifest:" | cut -d" " -f2- | sed 's/^[ \t]*//;s/[ \t]*$//');
if [ -f "${template_dir}/.watch" ]; then
tmux send-keys -t ${window}.${side} "/bin/bash ${tmpdir}/.watch" C-m
fi
open_editor "$main_file" "$manifest_file";
elif [[ "$template" == asm* ]]; then # make
tmux send-keys -t ${window}.${side} "find . -name '*.asm' | entr -cs 'make -j10 && ./a.out'" C-m
open_editor "main.asm" "main.asm";
elif [ -f ${template_dir}/main.lua ]; then # make
tmux send-keys -t ${window}.${side} "find . -name '*.lua' | entr -cs 'lua main.lua'" C-m
open_editor "main.lua" "main.lua";
elif [ -f ${template_dir}/package.json ]; then # make
tmux send-keys -t ${window}.${side} "find . -name '*.js' -or -name '*.json' | entr -cs 'make && npm start'" C-m
open_editor "index.js" "index.js";
elif [ -f ${template_dir}/Makefile ]; then # make
if [ -f ${template_dir}/main.c ]; then
tmux send-keys -t ${window}.${side} "find . -name '*.c' -or -name '*.h' -or -name 'Makefile' | entr -cs 'make -j10 && ./a.out'" C-m
open_editor "main.c" "main.c";
else
tmux send-keys -t ${window}.${side} "find . -name '*.cpp' -or -name '*.h' -or -name '*.hpp' -or -name 'Makefile' | entr -cs 'make -j10 && ./a.out'" C-m
open_editor "main.cpp" "main.cpp";
fi;
elif [ -f ${template_dir}/CMakeLists.txt ]; then # CMake
buildsyscmd="ninja";
if [ "${buildsys}" == "Ninja" ]; then
buildsyscmd="ninja"
elif [ "${buildsys}" == "Unix Makefiles" ]; then
buildsyscmd="make -j10"
fi
echo "Build system generator: cmake"
echo "Build system: ${buildsys}"
echo "Build system command: ${buildsyscmd}"
tmux send-keys -t ${window}.${side} "mkdir -p \"${tmpdir}/build\"" C-m
tmux send-keys -t ${window}.${side} "cd build/" C-m;
tmux send-keys -t ${window}.${main} "cmake -G \"${buildsys}\" -B${tmpdir}/build -S${tmpdir}" C-m
tmux send-keys -t ${window}.${side} "find .. -name '*.cpp' -or -name '*.h' -or -name '*.hpp' -or -name 'CMakeLists.txt' | entr -cs '${buildsyscmd} && ./a.out'" C-m
open_editor "main.cpp" "CMakeLists.txt";
elif [ -f ${template_dir}/main.py ]; then # Python
chmod +x ${tmpdir}/main.py
tmux send-keys -t ${window}.${side} "find . -name 'main.py' | entr -cs '${tmpdir}/main.py'" C-m
open_editor "main.py" "main.py";
elif [ -f ${template_dir}/main.sh ]; then # Bash
chmod +x ${tmpdir}/main.sh
tmux send-keys -t ${window}.${side} "find . -name 'main.sh' | entr -cs '${tmpdir}/main.sh'" C-m
open_editor "main.sh" "main.sh";
elif [ -f ${template_dir}/Cargo.toml ]; then # Rust
tmux send-keys -t ${window}.${side} "find . -name '*.rs' | entr -cs 'cd ${tmpdir} && cargo build && cargo run'" C-m
open_editor "src/main.rs" "Cargo.toml";
fi;
# tmux split-pane -t ${window}.${side} -v
# tmux send-keys -t ${window}.bottom-right "cd ${tmpdir}" C-m
# tmux send-keys -t ${window}.bottom-right "find ${tmpdir} -name \"*.s\" | entr -cs \"clear; cat ${tmpdir}/main.s\"" C-m
tmux select-pane -t ${window}.${main}
if [ -z "$TMUX" ]; then
tmux attach -t ${window}
fi