ffee400d53631fa1f004081ee2360d868a60cc17
[fnp-django-template.git] / bootstrap.sh
1 #
2 # Use as: 
3 #
4 #   source <(curl https://py.mdrn.pl/django)
5 #
6
7
8 # Make it a function, so that it works with `source`
9 start_project() {
10
11 local DJANGO_REQ='Django>=1.7,<1.8'
12 local DJANGO_ROOT='src'
13 local PYPI='https://py.mdrn.pl:8443/simple'
14 local DEFAULT_PYTHON="`which python3.4`"
15
16 if [ -z "$DEFAULT_PYTHON" ]; then
17     DEFAULT_PYTHON="`which python3`"
18 fi
19 if [ -z "$DEFAULT_PYTHON" ]; then
20     DEFAULT_PYTHON="`which python`"
21 fi
22
23 local PROJECT=$PROJECT
24 if [ -z "$PROJECT" ]; then
25     PROJECT="$1"
26 fi
27
28 if [ -z "$FNP_PROJECT_TEMPLATE" ]; then
29     local FNP_PROJECT_TEMPLATE='https://git.mdrn.pl/fnp-django-template.git/snapshot/HEAD.tar.gz'
30 fi
31
32 local VIRTUALENVWRAPPER_PATHS="
33     /etc/bash_completion.d/virtualenvwrapper
34     /usr/bin/virtualenvwrapper.sh
35     /usr/local/bin/virtualenvwrapper.sh
36 "
37
38 # Colorful output.
39 local strong='\e[0;32m'
40 local error='\e[1;31m'
41 local normal='\e[0m'
42
43 local PYTHON="$PYTHON"
44 if [ -z "$PYTHON" ]; then
45     echo "Defaulting to $DEFAULT_PYTHON."
46     PYTHON="$DEFAULT_PYTHON"
47 fi
48
49
50 echo "Create new Django project."
51 while [ -z "$PROJECT" ]
52 do
53     echo "Name of the project:"
54     read PROJECT
55 done
56 echo -e "Project: ${strong}${PROJECT}${normal}"
57
58 if ! type mkvirtualenv > /dev/null; then
59     for venv in $VIRTUALENVWRAPPER_PATHS
60     do
61         if [ -e "$venv" ]
62         then
63             local VIRTUALENVWRAPPER="$venv"
64             break
65         fi
66     done
67     if [ "$VIRTUALENVWRAPPER" ]
68     then
69         echo "virtualenvwrapper found at $VIRTUALENVWRAPPER."
70         source "$VIRTUALENVWRAPPER"
71     else
72         echo -e "${error}ERROR: virtualenvwrapper not found. Tried locations:${normal}"
73         echo "$VIRTUALENVWRAPPER_PATHS"
74         echo -e "${error}Install virtualenvwrapper or add the correct path to this script.${normal}"
75         echo "Aborting."
76         return
77     fi
78 fi
79
80 echo -e "${strong}Creating virtualenv: $PROJECT $PYTHON...${normal}"
81 mkvirtualenv "$PROJECT" --python "$PYTHON"
82 echo -e "${strong}Upgrading pip...${normal}"
83 pip install -i "$PYPI" -U pip
84 echo -e "${strong}Installing Django...${normal}"
85 pip install -i "$PYPI" "$DJANGO_REQ"
86 pip install -i "$PYPI" --pre django-startproject-plus
87
88 echo -e "${strong}Downloading project template...${normal}"
89 if [[ "$FNP_PROJECT_TEMPLATE" =~ ^(https?|ftp):// ]]; then
90     local PROJECT_TMPDIR=`mktemp -d fnp-django-template-XXX`
91     curl "$FNP_PROJECT_TEMPLATE" | tar xzf - -C "$PROJECT_TMPDIR"
92     local TEMPLATE=("$PROJECT_TMPDIR"/fnp-django-template-*/src)
93 else
94     local TEMPLATE="$FNP_PROJECT_TEMPLATE"
95 fi
96
97 echo -e "${strong}Starting the project...${normal}"
98 django-startproject.py \
99     --template "$TEMPLATE" \
100     --name NOTICE \
101     --extra_context='{"year": "`date +%Y`"}' \
102     "$PROJECT"
103
104 if [ -n "$PROJECT_TMPDIR" ]; then
105     rm -rf "$PROJECT_TMPDIR"
106 fi
107
108 cd "$PROJECT"
109
110 chmod +x "$DJANGO_ROOT"/manage.py
111 mv _gitignore .gitignore
112
113 echo -e "${strong}Installing requirements...${normal}"
114 pip install -i "$PYPI" -r requirements.txt
115 echo -e "${strong}Installing developer requirements...${normal}"
116 pip install -i "$PYPI" -r requirements-dev.txt
117 echo -e "${strong}Running syncdb...${normal}"
118 "$DJANGO_ROOT"/manage.py syncdb --noinput
119
120 echo -e "${strong}Starting new git repository...${normal}"
121 git init
122
123 echo -e "${strong}What next?${normal}"
124 echo " * Work on your app, commit to git."
125 echo " * Review fabfile, use fab for deployment."
126
127
128 }
129 start_project
130
131 # The following is just for displaying it as a webpage:<!--
132 #--><style>body{white-space:pre;color:#ddd}</style><h1 style="color:#000;text-align:center;position:fixed;top:0;bottom:0;left:0;right:0;white-space:normal">source &lt;(curl <span id="location"></span>)</h1><script>document.getElementById('location').innerHTML=window.location;</script>
133