-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp-init-default
More file actions
200 lines (169 loc) · 5.42 KB
/
Copy pathcpp-init-default
File metadata and controls
200 lines (169 loc) · 5.42 KB
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
#!/usr/bin/env bash
#
# This bash script automates the creation of a new C++ CMake project with git version control.
#
# The script expects template files to exist in the parent directory (../!LIBRARY_TEMPLATE_CMakeLists.txt.in, etc.).
set -e # Exit immediately if a command exits with a non-zero status
# Default values
library=false
EXE_NAME=""
PROJECT_DESCRIPTION=""
print_help() {
cat <<EOF
Usage: $0 [options] <project name>
Options:
-h, --help Show this help message and exit
-x, --exe <name> Set the name for the executable if different from the project name (executable only)
-l, --library Create a library rather than an executable (boolean flag)
-d, --description <description> Provide a project description
Examples:
$0 Myexe --exe myexe --description "A sample project"
$0 Mylib --library --description "A library project"
EOF
}
# Parse command line options
PARSED=$(getopt -o hx:ld: --long help,exe:,library,description: -- "$@")
if [[ $? -ne 0 ]]; then
print_help
exit 1
fi
eval set -- "$PARSED"
while true; do
case "$1" in
-h|--help)
print_help
exit 0
;;
-x|--exe)
EXE_NAME="$2"
shift 2
;;
-l|--library)
library=true
shift
;;
-d|--description)
PROJECT_DESCRIPTION="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Unexpected option: $1"
exit 1
;;
esac
done
# --exe and --library are mutually exclusive
if [ "$library" = true ] && [ -n "$EXE_NAME" ]; then
echo "Error: --exe and --library options are mutually exclusive."
print_help
exit 1;
fi
# Check for project name argument
if [ $# -lt 1 ]; then
echo "Error: Project name is required."
print_help
exit 1;
fi
# Check for too many arguments
if [ $# -gt 1 ]; then
echo "Error: Too many arguments provided."
print_help
exit 1;
fi
PROJECT_NAME="${!#}"
# Set EXE_NAME to project name if not provided
if [ -z "$EXE_NAME" ]; then
EXE_NAME="$PROJECT_NAME"
fi
echo "*** Creating C++ CMake project ..."
if [ -d "$PROJECT_NAME" ]; then
echo "Error: Directory '$PROJECT_NAME' already exists"
exit 1
fi
mkdir "$PROJECT_NAME"
cd "$PROJECT_NAME"
echo
echo "*** Creating empty git repo ..."
git init
git commit --allow-empty -m "New repo"
echo
echo "*** Creating .gitignore ..."
cat > .gitignore << 'EOF'
# Visual Studio artifacts
out/
.vs/
CMakeSettings.json
CMakePresets.json
# Araxis Merge artifacts
*.orig
EOF
git add .gitignore
git commit -m "Added .gitignore"
echo
echo "*** Creating MIT license ..."
cat > LICENSE << EOF
MIT License
Copyright (c) $(date +%Y) John J. Bolton
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
EOF
git add LICENSE
git commit -m "Added MIT License"
echo
echo "*** Creating default README.md ..."
echo "# $PROJECT_NAME" > README.md
git add README.md
git commit -m "Added default README.md"
echo "*** Creating default CMakeLists.txt ..."
if [ "$library" = true ]; then
if [ ! -f '../!LIBRARY_TEMPLATE_CMakeLists.txt.in' ]; then
echo 'Error: Template file not found: ../!LIBRARY_TEMPLATE_CMakeLists.txt.in'
exit 1
fi
sed -e "s/@PROJECT_NAME@/$PROJECT_NAME/g" \
-e "s/@PROJECT_DESCRIPTION@/$PROJECT_DESCRIPTION/g" \
../!LIBRARY_TEMPLATE_CMakeLists.txt.in > CMakeLists.txt
else
if [ ! -f '../!EXE_TEMPLATE_CMakeLists.txt.in' ]; then
echo 'Error: Template file not found: ../!EXE_TEMPLATE_CMakeLists.txt.in'
exit 1
fi
sed -e "s/@PROJECT_NAME@/$PROJECT_NAME/g" \
-e "s/@EXECUTABLE_NAME@/$EXE_NAME/g" \
-e "s/@PROJECT_DESCRIPTION@/$PROJECT_DESCRIPTION/g" \
../!EXE_TEMPLATE_CMakeLists.txt.in > CMakeLists.txt
fi
git add CMakeLists.txt
git commit -m "Added default CMakeLists.txt"
echo "*** Creating miscellaneous and placeholder files ..."
# Note placeholder source files are not committed here; left for user to replace with real code
if [ "$library" = true ]; then
if [ ! -f '../!LIBRARY_TEMPLATE_Doxyfile.in' ]; then
echo 'Error: Template file not found: ../!LIBRARY_TEMPLATE_Doxyfile.in'
exit 1
fi
mkdir -p include/${PROJECT_NAME}
touch include/${PROJECT_NAME}/${PROJECT_NAME}.h ${PROJECT_NAME}.cpp
cp ../!LIBRARY_TEMPLATE_Doxyfile.in Doxyfile.in
else
touch main.cpp
fi
echo
echo "*** C++ CMake project '$PROJECT_NAME' created"