-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_cpp_project.sh
executable file
·123 lines (108 loc) · 3.16 KB
/
create_cpp_project.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
#!/bin/bash
###############################################################################
#File: create_cpp_project.sh
#
#License: MIT
#
#Copyright (C) 2024 Onur Ozuduru
#
#Follow Me!
# github: github.com/onurozuduru
###############################################################################
SCRIPT_DIR=$(dirname "$(realpath "$0")")
TEMPLATE_NAME="TemplateProject"
TEMPLATE_DIR="${SCRIPT_DIR}/TemplateProject"
NEW_PROJECT_PATH=""
NEW_PROJECT_NAME=""
IS_INSTALL=""
INSTALL_DIR="${HOME}/.local/bin"
create_link() {
if [[ -z "${INSTALL_DIR}" ]]; then
echo "Install location is not specified!"
exit 1
fi
if [[ ! -d "${INSTALL_DIR}" ]]; then
echo "Install location does not exist: ${INSTALL_DIR}"
exit 1
fi
if [[ ! -w "${INSTALL_DIR}" ]]; then
echo "Install location, no permission to write: ${INSTALL_DIR}"
exit 1
fi
local target_path
local link_name
target_path=$(realpath "$0")
link_name=$(basename "${target_path}" .sh)
local link_path="${INSTALL_DIR}/${link_name}"
echo "Linking ${target_path}->${link_path}"
ln -s "${target_path}" "${link_path}" && echo "Linked!" || echo "Failed!"
}
create_project() {
if [[ -z "${NEW_PROJECT_PATH}" ]]; then
echo "Project path cannot be empty!"
exit 1
fi
if [[ -d "${NEW_PROJECT_PATH}" ]]; then
echo "Directory already exists: ${NEW_PROJECT_PATH}"
exit 1
fi
NEW_PROJECT_NAME=$(basename "${NEW_PROJECT_PATH}")
if [[ -z "${NEW_PROJECT_NAME}" ]]; then
echo "Project name could not be derived from: ${NEW_PROJECT_PATH}"
exit 1
fi
if [[ ! -d "${TEMPLATE_DIR}" ]]; then
echo "Template does not exist: ${TEMPLATE_DIR}"
exit 1
fi
echo "Creating new project '${NEW_PROJECT_NAME}'"
cp -r "${TEMPLATE_DIR}" "${NEW_PROJECT_PATH}"
sed -i "s/${TEMPLATE_NAME}/${NEW_PROJECT_NAME}/g" "${NEW_PROJECT_PATH}/CMakeLists.txt"
echo "Project created: ${NEW_PROJECT_PATH}"
}
print_help() {
printf 'Usage: %s <NEW_PROJECT_PATH> [ --install | -i ] [ --install-dir | -d <PATH> ] [ --help | -h ]\n' "$0"
printf 'Create new CPP project under <NEW_PROJECT_PATH>.\n'
printf '\t-i,--install\t\tCreate link to this script to use as command. Default location: %s\n' "${INSTALL_DIR}"
printf '\t-d,--install-dir <PATH>\tUse given path for --install instead of default location.\n'
printf '\t-h,--help\t\tDisplay help.\n'
}
### Get params
# -l long options (--help)
# -o short options (-h)
# : options takes argument (--option1 arg1)
# $@ pass all command line parameters.
set -e
params=$(getopt -l "install,install-dir:,help" -o "id:h" -- "$@")
eval set -- "$params"
### Run
while true
do
case $1 in
-h|--help)
print_help
exit 0
;;
-i|--install)
IS_INSTALL="YES"
;;
-d|--install-dir)
shift
INSTALL_DIR="$1"
;;
--)
shift
break;;
*)
print_help
exit 0
;;
esac
shift
done
if [[ -n "${IS_INSTALL}" ]]; then
create_link
exit 0
fi
NEW_PROJECT_PATH="$1"
create_project