Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·171 lines (138 loc) · 5.65 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·171 lines (138 loc) · 5.65 KB
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
#!/usr/bin/env bash
#
# WP-CLI Sync installer
#
# Downloads the latest release into wp-content/mu-plugins and writes the loader
# file WordPress needs in order to pick the plugin up from a subdirectory.
#
# Run it from the root of a vanilla WordPress project:
#
# curl -sSL https://raw.githubusercontent.com/jonbp/wp-cli-sync/master/install.sh | bash
#
set -euo pipefail
REPO="jonbp/wp-cli-sync"
PLUGIN_SLUG="wp-cli-sync"
LOADER_FILE="wp-cli-sync-loader.php"
PROJECT_PATH="$PWD"
CONTENT_DIR=""
VERSION="latest"
FORCE=0
# Messages, in the same style as the plugin's own task output
task() { printf '\n\033[34m%s: %s\n\033[0m' "${2:-Task}" "$1"; }
success() { printf '\n\033[32mSuccess: %s\n\033[0m' "$1"; }
warning() { printf '\n\033[33mWarning: %s\n\033[0m' "$1"; }
error() { printf '\n\033[31mError: %s\n\033[0m' "$1" >&2; }
die() { error "$1"; exit 1; }
usage() {
cat <<'USAGE'
WP-CLI Sync installer
Installs the plugin into wp-content/mu-plugins along with the loader file that
WordPress needs in order to load it from a subdirectory.
Usage:
install.sh [options]
Options:
--path=<path> WordPress project root. Defaults to the current directory.
--content-dir=<path> Content directory, if it isn't <project>/wp-content.
Relative paths are resolved against the project root.
--version=<tag> Release to install, such as 1.4.0. Defaults to the latest.
--force Replace the target directory even when it doesn't look
like an existing WP-CLI Sync install.
-h, --help Show this message.
USAGE
}
while [ $# -gt 0 ]; do
case "$1" in
--path=*) PROJECT_PATH="${1#*=}" ;;
--content-dir=*) CONTENT_DIR="${1#*=}" ;;
--version=*) VERSION="${1#*=}" ;;
--force) FORCE=1 ;;
-h|--help) usage; exit 0 ;;
*) error "Unknown option: $1"; usage >&2; exit 1 ;;
esac
shift
done
for dependency in curl tar; do
command -v "$dependency" >/dev/null 2>&1 \
|| die "$dependency is required but wasn't found in PATH."
done
# Locate the project
[ -d "$PROJECT_PATH" ] || die "No such directory: $PROJECT_PATH"
PROJECT_PATH="$(cd "$PROJECT_PATH" && pwd)"
if [ ! -f "$PROJECT_PATH/wp-load.php" ] && [ ! -f "$PROJECT_PATH/wp-settings.php" ]; then
die "$PROJECT_PATH doesn't look like a WordPress root. Run this from the project root, or pass --path."
fi
# Locate the content directory
if [ -z "$CONTENT_DIR" ]; then
CONTENT_DIR="$PROJECT_PATH/wp-content"
if [ -f "$PROJECT_PATH/wp-config.php" ] \
&& grep -qE "define\([[:space:]]*['\"](WP_CONTENT_DIR|WPMU_PLUGIN_DIR)['\"]" "$PROJECT_PATH/wp-config.php"; then
warning "wp-config.php sets a custom content directory. Pass --content-dir if $CONTENT_DIR isn't the right one."
fi
else
case "$CONTENT_DIR" in
/*) ;;
*) CONTENT_DIR="$PROJECT_PATH/$CONTENT_DIR" ;;
esac
fi
[ -d "$CONTENT_DIR" ] || die "No content directory at $CONTENT_DIR."
MU_DIR="$CONTENT_DIR/mu-plugins"
TARGET_DIR="$MU_DIR/$PLUGIN_SLUG"
LOADER_PATH="$MU_DIR/$LOADER_FILE"
if [ -f "$PROJECT_PATH/composer.json" ] && grep -q "$REPO" "$PROJECT_PATH/composer.json"; then
warning "composer.json already requires $REPO, so composer manages this install. Installing here as well will leave the two fighting over the same directory."
fi
# Resolve the version to install
if [ "$VERSION" = "latest" ]; then
task "Looking up the latest release" "Version"
VERSION="$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" \
| sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' \
| head -n1)"
[ -n "$VERSION" ] \
|| die "Couldn't work out the latest release. Pass --version=<tag> to install a specific one."
fi
# Don't clobber anything that isn't ours
plugin_version() {
sed -n 's/^[[:space:]*]*Version:[[:space:]]*\([^[:space:]]*\).*/\1/p' "$1" 2>/dev/null | head -n1
}
if [ -e "$TARGET_DIR" ]; then
if [ -f "$TARGET_DIR/$PLUGIN_SLUG.php" ]; then
installed_version="$(plugin_version "$TARGET_DIR/$PLUGIN_SLUG.php")"
if [ -n "$installed_version" ]; then
task "Replacing version $installed_version" "Update"
else
task "Replacing the existing install" "Update"
fi
elif [ "$FORCE" -eq 1 ]; then
warning "$TARGET_DIR isn't a WP-CLI Sync install, replacing it anyway."
else
die "$TARGET_DIR already exists and isn't a WP-CLI Sync install. Re-run with --force to replace it."
fi
fi
mkdir -p "$MU_DIR"
# Stage inside mu-plugins so the swap is a rename on the same filesystem
TMP_DIR="$(mktemp -d)"
STAGING_DIR="$MU_DIR/.$PLUGIN_SLUG-install-$$"
trap 'rm -rf "$TMP_DIR" "$STAGING_DIR"' EXIT INT TERM
task "Downloading $REPO $VERSION" "Download"
curl -fsSL -o "$TMP_DIR/plugin.tar.gz" "https://codeload.github.com/$REPO/tar.gz/refs/tags/$VERSION" \
|| die "Couldn't download version $VERSION. Check the tag exists at https://github.com/$REPO/releases."
mkdir -p "$STAGING_DIR"
tar -xzf "$TMP_DIR/plugin.tar.gz" -C "$STAGING_DIR" --strip-components=1
[ -f "$STAGING_DIR/$PLUGIN_SLUG.php" ] \
|| die "The downloaded archive doesn't contain $PLUGIN_SLUG.php."
rm -rf "$STAGING_DIR/.github"
task "Installing into $TARGET_DIR" "Install"
rm -rf "$TARGET_DIR"
mv "$STAGING_DIR" "$TARGET_DIR"
# WordPress only loads PHP files sitting directly in mu-plugins, so the plugin
# needs a loader alongside its directory to be picked up
task "Writing $LOADER_FILE" "Loader"
cat > "$LOADER_PATH" <<LOADER
<?php
/*
Plugin Name: WP-CLI Sync Loader
Description: Loads the WP-CLI Sync must-use plugin from its subdirectory.
*/
require_once __DIR__ . '/$PLUGIN_SLUG/$PLUGIN_SLUG.php';
LOADER
success "WP-CLI Sync $VERSION installed. Add the settings to wp-config.php, then run 'wp sync' from $PROJECT_PATH."