forked from WordPress/meta-environment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper-functions.sh
174 lines (148 loc) · 5.97 KB
/
helper-functions.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
#!/bin/bash
# Clone the Meta repository
#
# We create a full repository rather than using Git submodules, because having to learn to use submodules would
# create an extra barrier to entry for new contributors.
#
# $1 - the base folder
function wme_clone_meta_repository {
local REPOSITORY_DIR="$1/meta-repository"
if [ -d $REPOSITORY_DIR ]; then
return 0
fi
git clone git://meta.git.wordpress.org/ $REPOSITORY_DIR
git -C $REPOSITORY_DIR config diff.noprefix true
}
# Symlink each site's public_html folder to the corresponding Meta repository
#
# The Git mirror of the Meta repository has all sites in a single repo, just like the SVN repo. Git doesn't support
# cloning a sub-tree of a repository, though. The best solution is to just have a single copy of the entire repo,
# and then symlink each site's public_html folder.
#
# $1 - the site's root directory
# $2 - the name of the site's folder in the Meta Environment
# $3 - the name of the site's folder in the Meta repository
function wme_symlink_public_dir {
PUBLIC_DIR_PATH="$1/meta-repository/$3/public_html/"
cd "$1/$2"
ln -rs $PUBLIC_DIR_PATH public_html
mkdir -p $PUBLIC_DIR_PATH
}
# Symlink each site's log folder to the default log folder.
#
# $1 - the site's root directory
# $2 - the name of the site's folder in the Meta Environment
# $3 - the name of the site's folder in the Meta repository
function wme_symlink_logs_dir {
LOGS_DIR_PATH="$1/$2/logs"
cd "$1/meta-repository/$3"
ln -rs $LOGS_DIR_PATH logs
mkdir -p $LOGS_DIR_PATH
}
# Add entries to a .gitignore file
#
# todo use .git/info/exclude instead, that's more appropriate for this situation
#
# $1 - the site's web root
function wme_create_gitignore {
# Ignore the .gitignore file itself
echo "/.gitignore" >> $1/.gitignore
for i in "${IGNORED_FILES[@]}"
do :
echo "$i" >> $1/.gitignore
done
}
# Download the global WordPress.org header into the given directory.
#
# This is a workaround because the header isn't open-sourced yet
#
# $1 - the absolute path to the folder where the header should be placed
function wme_pull_wporg_global_header {
curl -so $1/header.php https://wordpress.org/header.php
sed -i "s/<\/head>/\n<?php\nif ( function_exists( 'gp_head' ) ) {\n\tgp_head();\n} else {\n\twp_head();\n}\n?>\n\n&/" $1/header.php
sed -i "s/<body id=\"wordpress-org\"/<body id=\"wordpress-org\" <?php if ( function_exists( 'body_class' ) ) { body_class(); } ?>/" $1/header.php
# Replace the links to point locally
# Match: //wordpress.org -> //wordpressorg.test
sed -i -e 's/\/\/wordpress.org/\/\/wordpressorg.test/g' $1/header.php
# Match: make.wordpress.org -> make.wordpressorg.test
sed -i -e 's/make.wordpress.org/make.wordpressorg.test/g' $1/header.php
}
# Download the global WordPress.org footer into the given directory.
#
# This is a workaround because the footer isn't open-sourced yet
#
# $1 - the absolute path to the folder where the footer should be placed
function wme_pull_wporg_global_footer {
curl -so $1/footer.php https://wordpress.org/footer.php
sed -i "s/<\/body>/\n<?php\nif ( function_exists( 'gp_footer' ) ) {\n\tgp_footer();\n} else {\n\twp_footer();\n}\n?>\n\n&/" $1/footer.php
# Replace the links to point locally
# Match: //wordpress.org -> //wordpressorg.test
sed -i -e 's/\/\/wordpress.org/\/\/wordpressorg.test/g' $1/footer.php
# Match: .wordpress.org -> .wordpressorg.test
sed -i -e 's/.wordpress.org/.wordpressorg.test/g' $1/footer.php
}
# Create log stubs
#
# $1 - the absolute path to the log folder
function wme_create_logs {
echo "Creating log files in ${1}"
mkdir -p $1
touch $1/nginx-access.log
touch $1/nginx-error.log
touch $1/php-error.log
}
# Import a MySQL database
#
# $1 - the name of the database
# $2 - the absolute path to the folder where the $1.sql file is stored
function wme_import_database {
echo "Creating database ${1}"
mysql -u root --password=root -e "CREATE DATABASE IF NOT EXISTS $1;"
mysql -u root --password=root -e "GRANT ALL PRIVILEGES ON $1.* TO wp@localhost IDENTIFIED BY 'wp';"
echo "Importing database ${2}/${1}.sql"
mysql -u root --password=root $1 < "${2}/${1}.sql"
echo "Finished database import operations for ${1}"
}
# Warn users that we moved their cheese during the upgrade from SVN to Git
#
# See https://github.com/WordPress/meta-environment/issues/13
#
# $1 - the path to the site's public_html folder
function wme_svn_git_migration {
if [[ ! -e $1 || -L $1 ]]; then
return 0
fi
echo -e "\n\nWARNING: The Meta Environment now uses Git to track files in the Meta sites.\n"
echo "Your current public_html folder will be backed up to public_html-old-svn-backup, and a new Git-based public_html folder will be provisioned."
echo "If you're working on any unfinished patches, please copy them from the backup folder."
echo -e "For help contributing with Git, see https://make.wordpress.org/meta/handbook/documentation/contributing-with-git/\n"
mv $1 "$1-old-svn-backup"
MIGRATED_TO_GIT=true
}
# Copy of VVV's noroot()
#
# We can't just use VVV's version because it's not available when manually running the provision scripts during
# development.
function wme_noroot() {
sudo -EH -u "vagrant" "$@";
}
# Whether a site should be provisioned.
#
# $1 - domain of the site
function wme_provision_site {
WME_SITE_ESCAPED=`echo ${1} | sed 's/\./\\\\./g'`
WME_PROVISION_SITE=`get_config_value "provision_site.${WME_SITE_ESCAPED}" 'true'`
echo ${WME_PROVISION_SITE,,}
}
# Loads mo/mo files from translate.wordpress.org.
#
# $1 - Slug of GlotPress locale
# $2 - Slug of GlotPress project
# $3 - File path without the PO/MO extension
function wme_download_pomo {
local GPLOCALE=$1
local GPPROJECT=$2
local OUTPUT=$3
curl -sfg -o $OUTPUT.po "https://translate.wordpress.org/projects/$GPPROJECT/$GPLOCALE/default/export-translations?filters[status]=current&format=po" || echo "Error downloading ${GPPROJECT}-${GPLOCALE}.po"
curl -sfg -o $OUTPUT.mo "https://translate.wordpress.org/projects/$GPPROJECT/$GPLOCALE/default/export-translations?filters[status]=current&format=mo" || echo "Error downloading ${GPPROJECT}-${GPLOCALE}.mo"
}