Skip to content

Commit b9c757b

Browse files
committed
wp-cli fixes
1 parent 5e0aed0 commit b9c757b

2 files changed

Lines changed: 131 additions & 5 deletions

File tree

scripts/install/tools/wordpress/wp-cli.sh

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,72 @@ source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-common.
1919
# Start Main Script
2020

2121
# WP-CLI
22-
cd /usr/local/src
22+
cd /usr/local/src || { echo "Error: Failed to change directory to /usr/local/src" >&2; exit 1; }
2323
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
2424
chmod +x wp-cli.phar
2525
sudo mv wp-cli.phar /usr/local/bin/wp
26+
WP_CLI_BIN="/usr/local/bin/wp"
27+
28+
configure_wp_cli_package_fetching() {
29+
export COMPOSER_ALLOW_SUPERUSER=1
30+
export COMPOSER_HOME="${COMPOSER_HOME:-/tmp/enginescript-composer}"
31+
mkdir -p "${COMPOSER_HOME}" || { echo "Error: Failed to create Composer home at ${COMPOSER_HOME}" >&2; exit 1; }
32+
33+
# shellcheck disable=SC2016
34+
php -r '
35+
$composerHome = rtrim($argv[1] ?? "", "/");
36+
if ($composerHome === "") {
37+
fwrite(STDERR, "Error: Composer home was not provided\n");
38+
exit(1);
39+
}
40+
$file = $composerHome . "/config.json";
41+
$config = [];
42+
if (is_file($file)) {
43+
$contents = file_get_contents($file);
44+
if ($contents !== false && trim($contents) !== "") {
45+
$decoded = json_decode($contents, true);
46+
if (!is_array($decoded)) {
47+
fwrite(STDERR, "Error: Invalid Composer config JSON at {$file}\n");
48+
exit(1);
49+
}
50+
$config = $decoded;
51+
}
52+
}
53+
if (!isset($config["config"]) || !is_array($config["config"])) {
54+
$config["config"] = [];
55+
}
56+
$config["config"]["github-protocols"] = ["https"];
57+
$config["config"]["use-github-api"] = false;
58+
$json = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
59+
if ($json === false || file_put_contents($file, $json . PHP_EOL) === false) {
60+
fwrite(STDERR, "Error: Failed to write Composer config at {$file}\n");
61+
exit(1);
62+
}
63+
' "${COMPOSER_HOME}" || exit 1
64+
65+
local git_config_count=0
66+
if [[ "${GIT_CONFIG_COUNT:-}" =~ ^[0-9]+$ ]]; then
67+
git_config_count="${GIT_CONFIG_COUNT}"
68+
fi
69+
70+
export GIT_CONFIG_COUNT=$((git_config_count + 2))
71+
export "GIT_CONFIG_KEY_${git_config_count}=url.https://github.com/.insteadOf"
72+
export "GIT_CONFIG_VALUE_${git_config_count}=git@github.com:"
73+
74+
local next_git_config=$((git_config_count + 1))
75+
export "GIT_CONFIG_KEY_${next_git_config}=url.https://github.com/.insteadOf"
76+
export "GIT_CONFIG_VALUE_${next_git_config}=ssh://git@github.com/"
77+
}
78+
79+
install_wp_cli_package() {
80+
local package="$1"
81+
82+
if ! "${WP_CLI_BIN}" package install "${package}" --allow-root; then
83+
echo "Error: Failed to install WP-CLI package ${package}." >&2
84+
exit 1
85+
fi
86+
}
87+
2688
mkdir -p /tmp/wp-cli-phar
2789
chown -R www-data:www-data /tmp/wp-cli-phar
2890
chmod 775 /tmp/wp-cli-phar
@@ -31,8 +93,9 @@ chown -R www-data:www-data ~/.wp-cli/cache
3193
chmod 775 ~/.wp-cli/cache
3294

3395
# Install WP-CLI Extensions
34-
wp package install pantheon-systems/wp_launch_check:@stable --allow-root
35-
wp package install wp-cli/doctor-command:@stable --allow-root
96+
configure_wp_cli_package_fetching
97+
install_wp_cli_package "pantheon-systems/wp_launch_check:@stable"
98+
install_wp_cli_package "wp-cli/doctor-command:2.3.1"
3699

37100
echo ""
38101
echo ""

scripts/update/wp-cli-update.sh

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,72 @@ source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-common.
2121
#----------------------------------------------------------------------------
2222

2323
# Update WP-CLI
24+
WP_CLI_BIN="/usr/local/bin/wp"
25+
if [[ ! -x "${WP_CLI_BIN}" ]]; then
26+
if command -v wp >/dev/null 2>&1; then
27+
WP_CLI_BIN="$(command -v wp)"
28+
else
29+
echo "WP-CLI not found. Expected executable at /usr/local/bin/wp." | tee -a /tmp/enginescript_install_errors.log >&2
30+
exit 1
31+
fi
32+
fi
33+
34+
configure_wp_cli_package_fetching() {
35+
export COMPOSER_ALLOW_SUPERUSER=1
36+
export COMPOSER_HOME="${COMPOSER_HOME:-/tmp/enginescript-composer}"
37+
mkdir -p "${COMPOSER_HOME}" || { echo "Error: Failed to create Composer home at ${COMPOSER_HOME}" >&2; exit 1; }
38+
39+
# shellcheck disable=SC2016
40+
php -r '
41+
$composerHome = rtrim($argv[1] ?? "", "/");
42+
if ($composerHome === "") {
43+
fwrite(STDERR, "Error: Composer home was not provided\n");
44+
exit(1);
45+
}
46+
$file = $composerHome . "/config.json";
47+
$config = [];
48+
if (is_file($file)) {
49+
$contents = file_get_contents($file);
50+
if ($contents !== false && trim($contents) !== "") {
51+
$decoded = json_decode($contents, true);
52+
if (!is_array($decoded)) {
53+
fwrite(STDERR, "Error: Invalid Composer config JSON at {$file}\n");
54+
exit(1);
55+
}
56+
$config = $decoded;
57+
}
58+
}
59+
if (!isset($config["config"]) || !is_array($config["config"])) {
60+
$config["config"] = [];
61+
}
62+
$config["config"]["github-protocols"] = ["https"];
63+
$config["config"]["use-github-api"] = false;
64+
$json = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
65+
if ($json === false || file_put_contents($file, $json . PHP_EOL) === false) {
66+
fwrite(STDERR, "Error: Failed to write Composer config at {$file}\n");
67+
exit(1);
68+
}
69+
' "${COMPOSER_HOME}" || exit 1
70+
71+
local git_config_count=0
72+
if [[ "${GIT_CONFIG_COUNT:-}" =~ ^[0-9]+$ ]]; then
73+
git_config_count="${GIT_CONFIG_COUNT}"
74+
fi
75+
76+
export GIT_CONFIG_COUNT=$((git_config_count + 2))
77+
export "GIT_CONFIG_KEY_${git_config_count}=url.https://github.com/.insteadOf"
78+
export "GIT_CONFIG_VALUE_${git_config_count}=git@github.com:"
79+
80+
local next_git_config=$((git_config_count + 1))
81+
export "GIT_CONFIG_KEY_${next_git_config}=url.https://github.com/.insteadOf"
82+
export "GIT_CONFIG_VALUE_${next_git_config}=ssh://git@github.com/"
83+
}
84+
85+
configure_wp_cli_package_fetching
86+
2487
# The intent is to fail when either command fails
25-
if ! wp cli update --stable --allow-root --yes 2>> /tmp/enginescript_install_errors.log \
26-
|| ! wp package update --allow-root --yes 2>> /tmp/enginescript_install_errors.log; then
88+
if ! "${WP_CLI_BIN}" cli update --stable --allow-root --yes 2>> /tmp/enginescript_install_errors.log \
89+
|| ! "${WP_CLI_BIN}" package update --allow-root --yes 2>> /tmp/enginescript_install_errors.log; then
2790
echo "WP-CLI update failed. See /tmp/enginescript_install_errors.log for details." >&2
2891
exit 1
2992
fi

0 commit comments

Comments
 (0)