forked from django-stars/backend-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
startproject
executable file
·171 lines (136 loc) · 4.98 KB
/
startproject
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
#!/bin/bash
DIALOG=$(which dialog)
COLOR_ERROR='\033[1;91m'
COLOR_LABEL='\033[1;33m'
COLOR_SUBLABEL='\033[0;33m'
COLOR_NC='\033[0m'
HOST="https://be.skeletons.djangostars.com/"
DJANGO_VERSION="2.1"
DOC_VERSION="2.1"
if [[ $DIALOG = "" ]]; then
echo -e "${COLOR_ERROR}dialog not found, please install into your system and run this script again."
echo -e "${COLOR_LABEL}MacOS:\n\t${COLOR_SUBLABEL}brew install dialog"
echo -e "${COLOR_LABEL}Ubuntu Linux:\n\t${COLOR_SUBLABEL}apt install dialog"
echo -e "${COLOR_LABEL}Arch Linux:\n\t${COLOR_SUBLABEL}pacman -S dialog${COLOR_NC}"
exit
fi
DIALOG_TITLE="Django Stars BE Skeleton"
function dlg_python_version () {
local VERSION_OPTIONS=("3.7" "3.7.x"
"3.6" "3.6.x"
"2.7" "2.7.x")
local VERSION_CHOICE=$(dialog --clear \
--title "${DIALOG_TITLE}" \
--menu "Which python version do you need?" \
15 40 4 "${VERSION_OPTIONS[@]}" \
2>&1 > /dev/tty)
# clear
echo ${VERSION_CHOICE}
}
function dlg_project_name () {
local NAME=$(dialog --clear \
--title "${DIALOG_TITLE}" \
--inputbox "Project name" 15 40 "" \
2>&1 > /dev/tty)
# clear
echo ${NAME}
}
function dlg_project_template () {
local TMPL_OPTIONS=("django_drf_celery" "Django + DRF + Celery"
"django" "pure Django"
"django_drf" "Django + DRF"
"django_celery" "Django + Celery")
local TMPL_CHOICE=$(dialog --clear \
--title "${DIALOG_TITLE}" \
--menu "What do you need for your project" \
15 60 4 "${TMPL_OPTIONS[@]}" \
2>&1 > /dev/tty)
# clear
echo ${TMPL_CHOICE}
}
### Ask about future project ###################################################
# === ask Python version
PYVER=$( dlg_python_version )
# === ask project name
#NAME=$( dlg_project_name "hello" )
NAME_SETTED=1
while [[ $NAME_SETTED -ne 0 ]]; do
NAME=$( dlg_project_name ${NAME} )
NAME_SETTED=0
if [ -n "$(python -c "${NAME} = 1" 2>&1)" ]; then
dialog --clear \
--title "Error" \
--msgbox "project name should be a valid python variable" 10 60 \
2>&1 > /dev/tty
NAME_SETTED+=1
fi
if [ -z "$(python -c "import ${NAME}" 2>&1)" ]; then
dialog --clear \
--title "Error" \
--msgbox "python already has module with same name" 10 60 \
2>&1 > /dev/tty
NAME_SETTED+=1
fi
if [ -a "${NAME}" ]; then
dialog --clear \
--title "Error" \
--msgbox "directory with same name already exist" 10 60 \
2>&1 > /dev/tty
NAME_SETTED+=1
fi
done
# === ask for template
TMPL=$( dlg_project_template )
### Get template and create project structure ##################################
echo -e ${COLOR_LABEL}------------------${COLOR_NC}
echo -e ver: ${PYVER} name: ${NAME} template: ${TMPL}
TMP=$(mktemp -d)
curl ${HOST}djangostars_project_template__${TMPL}.tar.gz > ${TMP}/djangostars_project_template__${TMPL}.tar.gz
mkdir ${NAME}
cd ${NAME}
tar xzvf ${TMP}/djangostars_project_template__${TMPL}.tar.gz
rm -rf ${TMP}
### Replace variables in files with real values ################################
find . -type f | xargs sed -i -e "s/{{ project_name }}/${NAME}/g; s/{{ django_version }}/${DJANGO_VERSION}/g; s/{{ doc_version }}/${DOC_VERSION}/g"
mv project_name ${NAME}
sed -i "s/{{ python_version }}/${PYVER}/" Pipfile
clear
### Project information and environment ########################################
read -r -d '' MSG_FINISH << EOF
${NAME} - project sucessfully created
- python: ${PYVER}
- template: ${TMPL}
Avaialable make commands:
* run (default) - start django project
* test - run tests
* celery - start celery with beat
Available features:
* Django
* django-extensions
* pytest
* ipython / ipdb
EOF
case ${TMPL} in
django_drf_celery)
printf -v MSG_FINISH "\n%s\n * Django REST Framework\n * Celery\n" "${MSG_FINISH}"
;;
django_def)
printf -v MSG_FINISH "\n%s\n * Django REST Framwwork" "${MSG_FINISH}"
;;
django_celery)
printf -v MSG_FINISH "\n%s\n * Celery" "${MSG_FINISH}"
;;
esac
if [ -n $(command -v pipenv) ]; then
pipenv --python ${PYVER} install --dev
CONSOLE_MSG_FINISH=${MSG_FINISH}
else
printf -v CONSOLE_MSG_FINISH "%s\n\n>>>> ATTENTION <<<<\nrun ${COLOR_ERROR}pipenv install --dev${COLOR_NC} to create virtualenv and install needed packages" "${MSG_FINISH}"
printf -v MSG_FINISH "%s\n\n>>>> ATTENTION <<<<\nrun 'pipenv install --dev' to create virtualenv and install needed packages" "${MSG_FINISH}"
fi
dialog --clear \
--title "${DIALOG_TITLE}" \
--msgbox "${MSG_FINISH}" 30 70 \
2>&1 > /dev/tty
clear
echo -e "${CONSOLE_MSG_FINISH}"