forked from andrewmackrodt/docker-sync-wsl-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp
executable file
·204 lines (173 loc) · 4.98 KB
/
app
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
200
201
202
203
204
#!/bin/bash
# Installs the application dependencies.
install_command () {
local start_exit_code=$(start_command -d)
exec_command sh -c "
composer create-project --prefer-dist laravel/laravel laravel
cd laravel
composer require barryvdh/laravel-debugbar --dev
sudo supervisorctl restart app
"
if [[ "$start_exit_code" -eq 0 ]]; then
docker-compose down
fi
}
#
# DO NOT MODIFY BELOW THIS LINE
#
BASE_PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
IFS=$'\n' read -rd '' -a COMMAND_LIST <<<"$(perl -ne '/^([a-z_-]+)_command ?\(/ and print "$1\n"' "${BASH_SOURCE[0]}")"
# Configures the environment.
configure_command () {
if [[ "$OSTYPE" =~ ^darwin ]]; then
configure_mac
elif [[ "$(uname -r)" =~ Microsoft ]]; then
configure_windows
else
configure_linux
fi
}
# Executes a shell command in a running container, leave arguments empty to enter a new shell.
exec_command () {
if [[ "$1" != "" ]]; then
docker-compose exec app "$@"
else
docker-compose exec app bash -l
fi
}
# Displays this help text.
help_command () {
local project_name=$(cd "$(dirname "${BASH_SOURCE[0]}")" && basename $PWD)
local file_contents=$(<"${BASH_SOURCE[0]}")
local base_name=$(basename "${BASH_SOURCE[0]}")
local command
local text
local help_text
local section
for command in "${COMMAND_LIST[@]}"; do
text=$(echo -e "$file_contents" \
| perl -0777 -ne "/^((?:#[^\n]*\n)+)^${command}_command\ ?\(/m and print \$1;" \
| perl -pe 's/^[#\t ]+|[#\t ]$//g;' -pe 's/[\t\n]| {2,}/ /g;' \
| perl -pe 's/ +$//;' )
if [[ "$text" == "" ]]; then
text="No documentation available."
fi
if [[ "$text" =~ ^\[ ]]; then
section=$(echo "$text" | perl -ne '/^\[([^\n\]]+)\]/ and print $1')
if [[ "$section" != "" ]]; then
text=$(echo "$text" | perl -ne '/\] *(.+)/ and print $1')
help_text="$help_text\n"
help_text="$help_text\n\033[33m$section:\033[0m"
fi
fi
help_text="$help_text\n \033[32m${command/_/:}\033[0m\t$text"
done
{
echo -e "\033[32m[$project_name] $base_name\033[0m"
echo ""
echo -e "\033[33mUsage:\033[0m"
echo " ./$base_name [command=sh] <arguments>"
if [[ "$(echo -e "$help_text" | tail -n+2 | head -n1 | grep $'\t')" != "" ]]; then
echo ""
echo -e "\033[33mCommands:\033[0m"
fi
echo -e "$help_text" | column -t -s $'\t' | perl -pe 's/^ {7}+//' | perl -pe "s/(\033\[33m)/\n\1/"
echo ""
} >&2
}
# Builds the docker containers.
make_command () {
docker-compose build
}
# Runs a one-off command in a self-removing container.
run_command () {
docker-compose run --rm app "$@"
}
# Executes a shell command in a self-removing container, leave arguments empty to enter a new shell.
sh_command () {
if [[ "$1" != "" ]]; then
run_command bash -lc "$@"
else
run_command bash -lll
fi
}
# Starts containers.
start_command () {
# exit early if the service is already started
if `docker-compose exec app hostname >/dev/null 2>&1`; then
return 1
fi
if [[ "$OSTYPE" =~ ^darwin ]] || [[ "$(uname -r)" =~ Microsoft ]]; then
docker-sync start
docker-compose -f docker-compose-dev.yml -f docker-compose.yml up "$@"
else
docker-compose up "$@"
fi
return 0
}
# Stops containers.
stop_command () {
docker-compose down
}
configure_linux() {
echo "Creating .env file"
echo "$(cat <<EOF
BASE_PATH_SRC=.
BASE_PATH_DEST=/opt/project
BUILD_UID=$(id -u)
DOCKER_COMPOSE_ENV_FILENAME=docker-compose.dev.yml
DOCKER_SYNC_STRATEGY=native_linux
DOCKER_SYNC_USERID=$(id -u)
XDEBUG_REMOTE_CONNECT_BACK=0
XDEBUG_REMOTE_HOST=localhost
EOF
)" > "${DOCKER_PATH}/.env"
}
configure_mac() {
echo "Creating .env file"
echo "$(cat <<EOF
BASE_PATH_SRC=.
BASE_PATH_DEST=/opt/project
BUILD_UID=$(id -u)
DOCKER_COMPOSE_ENV_FILENAME=docker-compose.dev.yml
DOCKER_SYNC_STRATEGY=native_osx
DOCKER_SYNC_USERID=$(id -u)
XDEBUG_REMOTE_CONNECT_BACK=0
XDEBUG_REMOTE_HOST=docker.for.mac.localhost
EOF
)" > "$BASE_PATH/.env"
}
configure_windows() {
echo "Creating .env file"
echo "$(cat <<EOF
BASE_PATH_SRC=.
BASE_PATH_DEST=/opt/project
BUILD_UID=$(id -u)
DOCKER_COMPOSE_ENV_FILENAME=docker-compose.dev.yml
DOCKER_SYNC_STRATEGY=unison
DOCKER_SYNC_USERID=$(id -u)
XDEBUG_REMOTE_CONNECT_BACK=0
XDEBUG_REMOTE_HOST=docker.for.win.localhost
EOF
)" > "${BASE_PATH}/.env"
}
#
# Execute the command(s)
#
main () {
local command=${1/:/_}
local args=("${@:2}")
if [[ ! " ${COMMAND_LIST[@]} " =~ " $command " ]]; then
if [[ "$command" != "" ]]; then
command=run
args=("$@")
else
command=help
args=""
fi
fi
"${command}_command" "${args[@]}"
}
pushd "${BASE_PATH}" >/dev/null
main "$@"
popd >/dev/null