-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·129 lines (105 loc) · 4.78 KB
/
run.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
#!/usr/bin/env bash
# https://github.com/MarcinKonowalczyk/run_sh
# Bash script run by a keyboard shortcut, called with the current file path $1
# This is intended as an example, but also contains a bunch of useful path partitions
# Feel free to delete everything in here and make it do whatever you want.
printf "Hello from run script! ^_^\n"
_VERSION="0.2.3" # Version of this script
# The directory of the main project from which this script is running
# https://stackoverflow.com/a/246128/2531987
ROOT_FOLDER="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
ROOT_FOLDER="${ROOT_FOLDER%/*}" # Strip .vscode folder
PROJECT_NAME="${ROOT_FOLDER##*/}" # Project name
FULL_FILE_PATH="$1"
_RELATIVE_FILE_PATH="${FULL_FILE_PATH##*$ROOT_FOLDER/}" # Relative path of the current file
# Split the relative file path into an array
RELATIVE_PATH_PARTS=(${_RELATIVE_FILE_PATH//\// })
DEPTH=${#RELATIVE_PATH_PARTS[@]}
DEPTH=$((DEPTH - 1))
# Couple of useful variables
FILENAME="${RELATIVE_PATH_PARTS[$DEPTH]}"
# If the file has an extension, get it otherwise set it to empty string
EXTENSION="" && [[ "$FILENAME" == *.* ]] && EXTENSION="${FILENAME##*.}"
########################################
GREEN='\033[0;32m';YELLOW='\033[0;33m';RED='\033[0;31m';PURPLE='\033[0;34m';DARK_GRAY='\033[1;30m';NC='\033[0m';
function logo() {
TEXT=(
" ______ __ __ __ __ " " " " ______ __ __ "
"/\\ == \\ /\\ \\/\\ \\ /\\ \"-.\\ \\ " " " " /\\ ___\\ /\\ \\_\\ \\ "
"\\ \\ __< \\ \\ \\_\\ \\\\\\ \\ \\-. \\ " " __" " \\ \\___ \\\\\\ \\ __ \\ "
" \\ \\_\\ \\_\\\\\\ \\_____\\\\\\ \\_\\\\ \\\"\\_\\ " "/\\_\\\\" " \\/\\_____\\\\\\ \\_\\ \\_\\\\"
" \\/_/ /_/ \\/_____/ \\/_/ \\/_/ " "\\/_/" " \\/_____/ \\/_/\\/_/"
)
printf "$PURPLE${TEXT[0]}$DARK_GRAY${TEXT[1]}$PURPLE${TEXT[2]}$NC\n"
printf "$PURPLE${TEXT[3]}$DARK_GRAY${TEXT[4]}$PURPLE${TEXT[5]}$NC\n"
printf "$PURPLE${TEXT[6]}$DARK_GRAY${TEXT[7]}$PURPLE${TEXT[8]}$NC\n"
printf "$PURPLE${TEXT[9]}$DARK_GRAY${TEXT[10]}$PURPLE${TEXT[11]}$NC\n"
printf "$PURPLE${TEXT[12]}$DARK_GRAY${TEXT[13]}$PURPLE${TEXT[14]} ${DARK_GRAY}v${_VERSION}$NC\n"
printf "\n"
}
function info() {
printf "PROJECT_NAME : $GREEN${PROJECT_NAME}$NC # project name (name of the project folder)\n"
printf "RELATIVE_PATH_PARTS : $GREEN${RELATIVE_PATH_PARTS[@]}$NC # relative path of the current file split into an array\n"
printf "DEPTH : $GREEN${DEPTH}$NC # depth of the current file (number of folders deep)\n"
printf "FILENAME : $GREEN${FILENAME}$NC # just the filename (equivalent to RELATIVE_PATH_PARTS[DEPTH])\n"
printf "EXTENSION : $GREEN${EXTENSION}$NC # just the extension of the current file\n"
printf "ROOT_FOLDER : $GREEN${ROOT_FOLDER}$NC # full path to the root folder of the project\n"
printf "FULL_FILE_PATH : $GREEN${FULL_FILE_PATH}$NC # full path of the current file\n"
}
# VERBOSE=true
VERBOSE=false
[ "${RELATIVE_PATH_PARTS[0]}" = ".vscode" ] && [ ${RELATIVE_PATH_PARTS[$DEPTH]} = "run.sh" ] && [ $DEPTH -eq 1 ] && VERBOSE=true
if $VERBOSE; then
logo
info
exit 0
fi
################################################################################
# Do some other stuff here.
_EXAMPLE_MAKEFILE=false
if [ "$_EXAMPLE_MAKEFILE" = true ]; then
# Run the makefile if you're currently editing the makefile
if [ "$FILENAME" = "makefile" ]; then
printf "About to run the makefile!\n"
make
exit 0
fi
fi
_EXAMPLE_PYTEST=false
if [ "$_EXAMPLE_PYTEST" = true ]; then
# Run the test if you're currently editing a python test file
if [ $EXTENSION = "py" ] && [ ${RELATIVE_PATH_PARTS[0]} = "tests" ]; then
printf "Running tests for $FILENAME\n"
pytest -s -x -k ${FILENAME%.*}
exit 0
fi
fi
_EXAMPLE_CMAKE=false
if [ "$_EXAMPLE_CMAKE" = true ]; then
TARGET="goo" # cmake target
if [ ! -d "$ROOT_FOLDER/build" ]; then
# If the build folder doesn't exist, create it and run cmake
(
mkdir "$ROOT_FOLDER/build"
cd "$ROOT_FOLDER/build"
cmake ..
)
fi
make --directory="$ROOT_FOLDER/build/"; OUT=$?;
if [ $OUT == 0 ]; then
printf "Running $TARGET\n";
"$ROOT_FOLDER/build/$TARGET"; OUT=$?;
if [ $OUT == 0 ]; then
printf "Success!\n";
else
printf "$TARGET failed with $OUT\n";
fi
else
printf "make failed with $OUT\n";
printf "maybe delete the content of the build folder, rerun cmake and try again?\n";
fi
exit $OUT
fi
################################################################################
# Got to the end of the script. I guess there's nothing to do.
printf "Nothing to do for $GREEN${FULL_FILE_PATH}$NC\n"