33# shellcheck source=.settings
44. " ${BASH_SOURCE%/* } /.settings"
55
6+ to-cpp () {
7+ echo " $1 " | sed -e ' s#[^a-zA-Z0-9_]#_#g' -e ' s#__*#_#g' -e ' s#^_##' -e ' s#_$##'
8+ }
9+
610if [ -z " $NAME " ]; then
7- export NAME=" ${PWD##*/ } "
11+ NAME=" ${PWD##*/ } "
12+ fi
13+ NAME=" $( to-cpp " $NAME " ) "
14+
15+ if [ -z " $VERSION " ]; then
16+ if git symbolic-ref --short HEAD > /dev/null 2>&1 ; then
17+ VERSION=" $( git symbolic-ref --short HEAD) "
18+ fi
19+ fi
20+ if [ -z " $VERSION " ]; then
21+ VERSION=" v1"
822fi
9- export VERSION=" ${VERSION :- v1} "
23+ VERSION=" $( to-cpp " $VERSION " ) "
1024
11- if [ " $# " -ne 0 ] || [ ! -d .git ] || \
12- [ " $NAME " != " $( echo " $NAME " | grep -o ' ^[a-zA-Z0-9_]\+' ) " ] ; then
25+ if [ " $# " -ne 0 ]; then
1326 CMD=" ${0##*/ } "
1427 cat << EOF
1528Usage: $CMD
@@ -19,13 +32,20 @@ Options:
1932 NAME=$NAME |...
2033 VERSION=$VERSION |...
2134
22- Initializes a new C++ project with cppsm configuration files or updates an
23- existing project to use the latest configuration files. Run $CMD in the
24- top-level directory of a fresh git project.
35+ Initializes a new project with cppsm configuration files when run in an empty
36+ directory or updates an existing project to use the latest configuration files.
2537EOF
2638 exit 1
2739fi
2840
41+ if [ ! -e .git ] || [ -z " $NAME " ] || [ -z " $VERSION " ]; then
42+ git init
43+ fi
44+
45+ if [ " $( git symbolic-ref --short HEAD) " != " $VERSION " ]; then
46+ git checkout -b " $VERSION "
47+ fi
48+
2949if [ -n " $TRAVIS_BRANCH " ]; then
3050 CLI_BRANCH=" $TRAVIS_BRANCH "
3151else
@@ -43,15 +63,31 @@ if [ ! -d .cppsm ]; then
4363 git submodule " ${GIT_QUIET[@]} " add --branch " $BOILERPLATE_BRANCH " " $BOILERPLATE_URL " .cppsm
4464fi
4565
46- ln -fs .cppsm/.clang-format .clang-format
47- ln -fs .cppsm/.gitignore .gitignore
48- ln -fs .cppsm/.prettierrc .prettierrc
66+ create-parent-dir () {
67+ local FILE=" $1 "
68+ if [[ " $FILE " =~ / ]]; then mkdir -p " ${FILE%/* } " ; fi
69+ }
70+
71+ add-link () {
72+ local FILE=" $1 "
73+ local LINK=" $2 "
74+ if [ ! -e " $LINK " ]; then
75+ create-parent-dir " $LINK "
76+ ln -fs " $FILE " " $LINK "
77+ git add " $LINK "
78+ fi
79+ }
80+
81+ add-link .cppsm/.clang-format .clang-format
82+ add-link .cppsm/.gitignore .gitignore
83+ add-link .cppsm/.prettierrc .prettierrc
4984
5085add-file () {
51- if [[ " $1 " =~ / ]]; then mkdir -p " ${1%/* } " ; fi
52- if [ ! -e " $1 " ]; then
53- cat > " $1 "
54- git add " $1 "
86+ local FILE=" $1 "
87+ if [ ! -e " $FILE " ]; then
88+ create-parent-dir " $FILE "
89+ cat > " $FILE "
90+ git add " $FILE "
5591 fi
5692}
5793
@@ -61,10 +97,4 @@ project($NAME)
6197include(.cppsm/c++17.cmake)
6298EOF
6399
64- cp " $CPPSM /.travis.yml" .travis.yml
65-
66- git add \
67- .clang-format \
68- .gitignore \
69- .prettierrc \
70- .travis.yml
100+ add-file .travis.yml < " $CPPSM /.travis.yml"
0 commit comments