-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwar.sh
executable file
·187 lines (167 loc) · 6.56 KB
/
war.sh
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
#!/bin/bash
#Author: BMO & Antpb
#Version: 0.4.4-alpha
### Not a command ###
assign-opts() {
local OPTIND
while getopts ":aA:b:B:G:D:fm:p:P:r:" opt; do
case $opt in
a)
angular_build=true;;
A)
app_slug=$OPTARG;;
b)
temp_branch=$OPTARG;;
B)
deploy_to_remote_branch=$OPTARG;;
G)
global_composer_path=$OPTARG;;
D)
deploy_from_local_branch=$OPTARG;;
f)
force_push="-f";;
m)
commit_message=$OPTARG;;
p)
prefix_path=$OPTARG;;
P)
angular_prefix=$OPTARG;;
r)
deploy_to_remote_repo=$OPTARG;;
esac
done
### Common Variables ###
current_branch=$(git rev-parse --abbrev-ref HEAD)
br="++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
epoch=$(date +%s)
}
#### Functions ####
## war set <config_key> <config_value>
config-set (){
sed -Ei '' "s/^(${1}=).*/\1\"${2}\"/" ${config_file}
}
## war init
init (){
echo -e "${br}Setting custom variable in Config"
config-set angular_prefix ${angular_prefix}
config-set app_slug ${app_slug}
config-set prefix_path ${prefix_path}
echo -e "${br}Setting up Custom Plugin and Theme"
if [[ -f ${prefix_path}/wp-content/plugins/my-api/my-api.php ]]; then
mv ${prefix_path}/wp-content/plugins/my-api/my-api.php ${prefix_path}/wp-content/plugins/my-api/${app_slug}-api.php
mv ${prefix_path}/wp-content/plugins/my-api ${prefix_path}/wp-content/plugins/${app_slug}-api
fi
if [[ -f ${prefix_path}/wp-content/themes/my-theme/style.css ]]; then
mv ${prefix_path}/wp-content/themes/my-theme ${prefix_path}/wp-content/themes/${app_slug}-theme
fi
echo -e "${br}Installing Composer for Plugin and Theme"
if [[ ! -f ${global_composer_path} ]]; then
echo "${br}No Global Composer Path found, Check wp-cli.config and set accordingly"
exit 1;
fi
find ${prefix_path}/wp-content -maxdepth 3 -iname "composer.json" -type f -execdir php ${global_composer_path} install --no-dev --prefer-source -o \;
if [[ true == ${angular_build} ]]; then
echo -e "${br}Building Angular project : ${app_slug}"
find angular -type f -iname "README.md" -execdir ng new ${app_slug} -is true -it true -p ${angular_prefix} -sg true \;
find angular/${app_slug} -maxdepth 1 -type f -iname ".angular-cli.json" -execdir sed -Ei '' "s/(\"outDir\"\: ).*$/\1\"..\/..\/${prefix_path}\/wp-content\/themes\/${app_slug}-theme\/src\",/" .angular-cli.json \;
echo -e "${br}Updating Themes index.php file"
find ${prefix_path}/wp-content/themes/${app_slug}-theme -maxdepth 1 -type f -iname "index.php" -execdir sed -Ei '' "s/app/${angular_prefix}/g" index.php \;
echo -e "${br}Adding WP Client"
find angular/${app_slug} -maxdepth 1 -type f -iname "package.json" -execdir yarn add @skypress/wp-client@latest \;
fi
}
## war deploy -a (Build Angular) -b <temp deploy branch> -B <remote deploy branch> -c <commit message> -C <composer path> -D <deploy from branch> -f (force push) -p <prefix path> -r <remote name>
deploy (){
if [[ -z ${commit_message} ]]; then
commit_message="Deploy"
fi
echo -e "${br}Saving current work"
git add . --all && git commit -am "${commit_message}"
git checkout ${deploy_from_local_branch}
if [[ ${angular_build} == true ]]; then
echo -e "${br}Building Angular Files"
find angular -maxdepth 2 -iname "package.json" -type f -execdir yarn build \;
echo -e "${br}Commiting build before split"
git add . --all && git commit -am "${commit_message}"
fi
echo -e "${br}Splitting out WordPress into temporary branch"
git subtree split --prefix=${prefix_path} -b ${temp_branch}
git checkout ${temp_branch}
echo -e "${br}Running composer install for plugins and themes"
find wp-content -maxdepth 3 -iname "composer.json" -type f -execdir php ${global_composer_path} install --no-dev --prefer-source -o \;
find wp-content -iname ".git" -type d -execdir rm -rf .git \;
echo -e "${br}Pushing ${temp_branch} to ${deploy_deploy_to_remote_repoemote}:${deploy_to_remote_branch}"
git add . --all && git commit -am "${commit_message}"
git pull -Xtheirs ${deploy_to_remote_repo} ${deploy_to_remote_branch}
git push ${force_push} ${deploy_to_remote_repo} ${temp_branch}:${deploy_to_remote_branch}
echo -e "${br}Cleaning up after ourselves"
git checkout ${current_branch}
git branch -D ${temp_branch}
rm -rf ./wp-content
}
## war deploy
update (){
if [[ -z ${commit_message} ]]; then
commit_message="Post WAR Update"
fi
echo -e "${br}Fetching from WAR Framework Remote"
git fetch ${war_framework_repo} ${war_framework_branch}
echo -e "${br}Pulling in updated files"
git checkout ${war_framework_repo}/${war_framework_branch} -- ${update_include}
git add . --all && git commit -am "${commit_message}"
}
## war check-config
check-config (){
#### CLI Config File ####
config_vars=( "angular_build" "angular_prefix" "app_slug" "commit_message" "deploy_from_local_branch" "deploy_to_remote_branch" "deploy_to_remote_repo" "global_composer_path" "force_push" "prefix_path" "temp_branch" "update_include" "war_framework_repo" "war_framework_branch" )
if [ ! -f ${config_file} ]; then
echo "##### WAR Cli Config #####" > ${config_file}
fi
for v in ${config_vars[*]}; do
if [[ -z $(grep "${v}" ${config_file}) ]]; then
case ${v} in
"angular_build")
echo ${v}'=false' >> ${config_file};;
"angular_prefix")
echo ${v}'="app"' >> ${config_file};;
"app_slug")
echo ${v}'="'$(basename $(pwd))'"' >> ${config_file};;
"commit_message")
echo ${v}'=""' >> ${config_file};;
"deploy_from_local_branch")
echo ${v}'="master"' >> ${config_file};;
"deploy_to_remote_branch")
echo ${v}'="master"' >> ${config_file};;
"deploy_to_remote_repo")
echo ${v}'="prod"' >> ${config_file};;
"global_composer_path")
echo ${v}'="'$(find /usr/local/bin -type f -iname "composer*")'"' >> ${config_file};;
"force_push")
echo ${v}'=""' >> ${config_file};;
"prefix_path")
echo ${v}'="wordpress"' >> ${config_file};;
"temp_branch")
echo ${v}'="temp"' >> ${config_file};;
"update_include")
echo ${v}'="war.sh docker-compose.yml .gitignore wordpress/.gitignore"' >> ${config_file};;
"war_framework_branch")
echo ${v}'="master"' >> ${config_file};;
"war_framework_repo")
echo ${v}'="origin"' >> ${config_file};;
esac
fi
done
}
#### Process Commands ####
if [[ -n ${1} ]]; then
cmd=${1}
shift 1
config_file="./war-cli.config"
check-config
source ${config_file}
assign-opts $*
declare -F ${cmd} &>/dev/null && ${cmd} && exit 0 ||
echo -e "\n\033[1;31m"Looks like you entered the wrong function."\033[1;000m\n\nUsage is: war <function> <optional argument>\n"
else
echo -e "\nUsage is: war <function> <optional argument>\n"
fi