Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit 409fd75

Browse files
committed
Added a "pantheon-deploy" command.
1 parent bce5122 commit 409fd75

File tree

2 files changed

+326
-0
lines changed

2 files changed

+326
-0
lines changed

guest/cli/commands/pantheon-deploy

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/env bash
2+
3+
4+
git_url=$(read_config .hosts)
5+
6+
deploy_host="$1"
7+
8+
if [ "" == "${deploy_host}" ]; then
9+
echo -e "\tERROR: You must specify a target host to deploy TO:"
10+
echo -e "\t"
11+
echo -e "\t\tbox deploy {host}"
12+
echo -e "\t"
13+
echo -e "\tWhere {host} is one of:"
14+
echo -e "\t"
15+
index=0
16+
while :; do
17+
host=$(read_config ".hosts | keys[$index]" )
18+
if [ "" == "${host}" ]; then
19+
break
20+
fi
21+
echo -e "\t\t- ${host}"
22+
index=$(( $index+1 ))
23+
done
24+
echo -e "\t"
25+
return
26+
fi
27+
28+
project_name=$(read_config ".project.name" "require")
29+
if [ "" == "${project_name}" ]; then
30+
return
31+
fi
32+
33+
target_domain=$(read_config ".project.target_domain" "require")
34+
if [ "" == "${target_domain}" ]; then
35+
return
36+
fi
37+
38+
selector=".hosts.${deploy_host}.git_url"
39+
git_url=$(read_config "${selector}" "require")
40+
if [ "" == "${git_url}" ]; then
41+
return
42+
fi
43+
44+
echo_if_not_quiet "$*" "^Deploying to the ${target_domain} ${deploy_host} server at Pantheon..."
45+
46+
#
47+
# Create temporary directories to work in
48+
# See Method # 2 Use of $$ variable:
49+
# - http://www.cyberciti.biz/tips/shell-scripting-bash-how-to-create-temporary-random-file-name.html
50+
#
51+
clone_dir="/tmp/deploy-clone.$$.dir"
52+
build_dir="/tmp/deploy-build.$$.dir"
53+
54+
#
55+
# First clone the site from our repo on Pantheon
56+
#
57+
echo_if_not_quiet "$*" "=Cloning from ${deploy_host}'s Git repo..."
58+
git clone "${git_url}" "${clone_dir}"
59+
60+
#
61+
# Clone the pristine Pantheon Upstream
62+
#
63+
echo_if_not_quiet "$*" "&Cloning from the pantheon-systems/WordPress Git repo..."
64+
git clone https://github.com/pantheon-systems/WordPress "${build_dir}"
65+
cd "${build_dir}"
66+
67+
68+
echo_if_not_quiet "$*" "&Cleaning the pantheon-systems/WordPress Git repo..."
69+
#
70+
# Erase the .git that points to Pantheon's pristine Upstream repo
71+
#
72+
rm -rf "${build_dir}/.git"
73+
#
74+
# Rip out it's content directory
75+
#
76+
rm -rf "${build_dir}/wp-content"
77+
78+
79+
echo_if_not_quiet "$*" "=Copying content and vendor directories from dev..."
80+
#
81+
# Replace with our content directory
82+
#
83+
cp -RP "${WPLIB_BOX_CONTENT_DIR}/" "${build_dir}/wp-content"
84+
85+
#
86+
# If there is a /vendor directory, don't forget it
87+
#
88+
cp -RP "${WPLIB_BOX_VENDOR_DIR}/" "${build_dir}/vendor"
89+
90+
#
91+
# Strip .git "submodules"
92+
#
93+
echo_if_not_quiet "$*" "=Stripping Git submodule references..."
94+
find "${build_dir}" -name .git | xargs rm -fr
95+
96+
#
97+
# Remove these. We do not need these on a hosted site.
98+
#
99+
echo_if_not_quiet "$*" "&Removing items to exclude from ${build_dir}/..."
100+
delete_exclude_items "${build_dir}"
101+
102+
#
103+
# Remove anything that is in the "require-dev" category
104+
#
105+
echo_if_not_quiet "$*" "&Removing \"require-dev\" items from ${build_dir}/..."
106+
$WPLIB_BOX_PHPCLI "${WPLIB_BOX_INCLUDES_DIR}/run-command.php" --args remove-composer-require-dev "${build_dir}"
107+
108+
109+
#
110+
# Now get the Pantheon repo's .git dir and make it our own
111+
#
112+
echo_if_not_quiet "$*" "&Copying over .git directory from source..."
113+
mv "${clone_dir}/.git" "${build_dir}"
114+
115+
cd "${build_dir}"
116+
#
117+
# So we can view after the fact
118+
#
119+
#git status
120+
121+
echo_if_not_quiet "$*" "=Staging all files for commit..."
122+
#
123+
# Add any changes to staging
124+
#
125+
quiet_git add --all .
126+
127+
#
128+
# Add any changes to staging
129+
#
130+
quiet_git add -u
131+
132+
#
133+
# So we can view after the fact
134+
#
135+
#git status
136+
137+
echo_if_not_quiet "$*" "=Committing all files to prepare for deployment..."
138+
#
139+
# Merge in any changes
140+
#
141+
quiet_git commit -m "Merging in changes for deployment"
142+
143+
#
144+
# Now do a pull, just in case.
145+
# It should be up to date though.
146+
#
147+
quiet_git pull --commit --no-edit --no-ff --verbose
148+
149+
#
150+
# So we can view after the fact
151+
#
152+
#git status
153+
echo_if_not_quiet "$*" "=Pushing files for ${project_name} to ${deploy_host} at Pantheon..."
154+
#
155+
# Ensure this is already set
156+
#
157+
quiet_git config --global push.default simple
158+
#
159+
# Push our changes back to Panethon
160+
#
161+
quiet_git push
162+
163+
echo_if_not_quiet "$*" "=Cleaning up..."
164+
#
165+
# Clean up
166+
#
167+
rm -rf "${clone_dir}"
168+
rm -rf "${build_dir}"
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
if ( ! isset( $argv[1] ) ) {
4+
echo "ERROR: Root directory not passed to " . __FILE__;
5+
die();
6+
}
7+
remove_composer_require_dev( $argv[1] );
8+
9+
function remove_composer_require_dev( $root_dir ) {
10+
/*
11+
* Look for $composer->{'require-dev'}
12+
*/
13+
do {
14+
15+
$composer_file = $_SERVER['WPLIB_BOX_COMPOSER_FILE'];
16+
17+
if ( ! is_file( $composer_file ) ) {
18+
break;
19+
}
20+
21+
$json = file_get_contents( $composer_file );
22+
23+
$composer = json_decode( $json );
24+
if ( ! $composer ) {
25+
error_die( "*Composer file {$composer_file} is not valid JSON.");
26+
}
27+
28+
if ( ! isset( $composer->{'require-dev'} ) ) {
29+
break;
30+
}
31+
32+
$object_ref = '$composer->extra';
33+
if ( ! isset( $composer->extra ) ) {
34+
error_die( "*'{$object_ref}' is not set in {$composer_file}.");
35+
}
36+
37+
$extra = $composer->extra;
38+
$object_ref .= '->{"installer-paths"}';
39+
if ( ! isset( $extra->{'installer-paths'} ) ) {
40+
error_die( "*'{$object_ref}' is not set in {$composer_file}.");
41+
}
42+
43+
$installer_paths = $extra->{'installer-paths'};
44+
if ( ! is_object( $installer_paths ) ) {
45+
error_die( "*'{$object_ref}' is not an object in {$composer_file}.");
46+
}
47+
48+
$installer_paths = @get_object_vars( $installer_paths );
49+
if ( ! $installer_paths ) {
50+
error_die( "*'{$object_ref}' from {$composer_file} could not be converted into an array.");
51+
}
52+
53+
/*
54+
* Scan through $composer->extra->{'installer-paths'} to find names we can match
55+
*
56+
* $items will look something like this:
57+
*
58+
* Array
59+
* (
60+
* [wpackagist-plugin/wp-redis] => [/vagrant/www/content/mu-plugins/wpackagist-plugin/wp-redis],
61+
* [newclarity/facetwp] => [/vagrant/www/content/mu-plugins/newclarity/facetwp],
62+
* [newclarity/bb-plugin] => [/vagrant/www/content/plugins/newclarity/bb-plugin],
63+
* )
64+
*
65+
*/
66+
$items = array();
67+
foreach( $installer_paths as $installer_path_name => $item_list ) {
68+
preg_match( '#/(mu-plugins|plugins|themes)/#', $installer_path_name, $match );
69+
foreach( $item_list as $item_name ) {
70+
if ( preg_match( '#^type:#', $item_name ) ) {
71+
continue;
72+
}
73+
$items[ $item_name ] = isset( $match[ 1 ] )
74+
? $match[ 1 ]
75+
: null;
76+
}
77+
}
78+
79+
/*
80+
* Look for $composer->config->{'vendor-dir'}
81+
*/
82+
do {
83+
$vendor_dir = null;
84+
if ( ! isset( $composer->config ) ) {
85+
break;
86+
}
87+
$config = $composer->config;
88+
if ( ! isset( $config->{'vendor-dir'} ) ) {
89+
break;
90+
}
91+
$vendor_dir = trim( $config->{'vendor-dir'}, '/' );
92+
$vendor_dir = preg_replace( '#^www/(.+)$#', '$1', $vendor_dir );
93+
$vendor_dir = "{$root_dir}/{$vendor_dir}";
94+
95+
} while ( false );
96+
97+
/**
98+
* Now got back and get the list of require dev items.
99+
*/
100+
$require_dev = (array) $composer->{'require-dev'};
101+
102+
$items_to_delete = array();
103+
foreach( array_keys( $require_dev ) as $dev_only ) {
104+
preg_match( '#^([^/]+)/(.+)$#', $dev_only, $segments );
105+
106+
if ( ! isset( $segments[ 2 ] ) ) {
107+
continue;
108+
}
109+
110+
if ( isset( $items[ $dev_only ] ) ) {
111+
$item = "{$root_dir}/wp-content/{$items[ $dev_only ]}/{$segments[ 2 ]}";
112+
113+
} else if ( preg_match( '#^wpackagist-(plugin|theme)$#', $segments[ 1 ], $type ) ) {
114+
115+
$item = "{$root_dir}/wp-content/{$type[ 1 ]}s/{$segments[ 2 ]}";
116+
117+
} else {
118+
$item = "{$vendor_dir}/{$segments[ 2 ]}";
119+
120+
}
121+
if ( empty( $item ) ) {
122+
continue;
123+
}
124+
125+
if ( '/' === $item ) {
126+
continue;
127+
}
128+
129+
if ( ! is_dir( $item ) ) {
130+
continue;
131+
}
132+
$regex = '#^' . preg_quote( $root_dir ). '(.+)$#';
133+
$content_key = preg_replace( $regex, '$1', $item );
134+
$items_to_delete[ $content_key ] = $item;
135+
136+
}
137+
138+
/**
139+
* Now, finally, delete all them thar files
140+
*/
141+
ksort( $items_to_delete );
142+
foreach( $items_to_delete as $content_key => $item_to_delete ) {
143+
144+
if ( ! file_exists( $item_to_delete ) ) {
145+
/**
146+
* This checks file OR directory
147+
*/
148+
continue;
149+
}
150+
151+
echo_if_not_quiet( "=Deleting {$content_key}..." );
152+
system( "rm -rf " . escapeshellarg( $item_to_delete ) );
153+
154+
}
155+
156+
} while ( false );
157+
158+
}

0 commit comments

Comments
 (0)