Created
October 25, 2018 06:51
-
-
Save gedex/8b89e4caaf18619e8326c63b52de9515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
define( 'SVN_USERNAME', getenv( 'SVN_USERNAME' ) ); | |
define( 'SVN_PASSWORD', getenv( 'SVN_PASSWORD' ) ); | |
define( 'WORKDIR', sys_get_temp_dir() . '/' . uniqid() ); | |
define( 'WORKDIR_ATOMIC', WORKDIR . '/atomic' ); | |
define( 'WORKDIR_SRC', WORKDIR . '/src' ); | |
const ATOMIC_SVN_URL = 'https://woothemes.svn.beanstalkapp.com/woocommerce-atomic-products'; | |
const PLUGINS_SVN_URL = 'https://woothemes.svn.beanstalkapp.com/woocommerce-products'; | |
const PLUGINS = [ | |
'storefront-powerpack', | |
'woocommerce-product-addons', | |
'woocommerce-shipping-australia-post', | |
'woocommerce-shipping-royalmail', | |
'woocommerce-shipping-ups', | |
'woocommerce-shipping-usps', | |
]; | |
const THEMES_SVN_URL = 'https://woothemes.svn.beanstalkapp.com/woothemes-products'; | |
const THEMES = [ | |
'galleria', | |
'homestore', | |
'bookshop', | |
]; | |
function build_cmd( ...$args ) { | |
$args = array_map( 'escapeshellarg', $args ); | |
return implode( ' ', $args ); | |
} | |
function exec_cmd( $cmd ) { | |
exec( $cmd . ' 2>&1', $output, $exit_status ); | |
if ( 0 !== $exit_status ) { | |
throw new Exception( | |
sprintf( 'cmd %s returns non-zero code: %s', $cmd, implode( PHP_EOL, $output ) ) | |
); | |
} | |
} | |
function svn_checkout( $src, $dst ) { | |
exec_cmd( build_cmd( | |
'svn', | |
'checkout', | |
$src, | |
$dst, | |
'--username', | |
SVN_USERNAME, | |
'--password', | |
SVN_PASSWORD | |
) ); | |
} | |
function svn_add( $path ) { | |
exec_cmd( build_cmd( | |
'svn', | |
'add', | |
'--force', | |
'--parents', | |
$path | |
) ); | |
} | |
function svn_commit( $path, $message ) { | |
exec_cmd( build_cmd( | |
'svn', | |
'ci', | |
$path, | |
'-m', | |
$message, | |
'--username', | |
SVN_USERNAME, | |
'--password', | |
SVN_PASSWORD | |
) ); | |
} | |
function svn_remote_copy( $src_url, $dst_url, $message ) { | |
exec_cmd( build_cmd( | |
'svn', | |
'copy', | |
$src_url, | |
$dst_url, | |
'-m', | |
$message | |
) ); | |
} | |
function unzip( $src, $dst ) { | |
exec_cmd( build_cmd( | |
'unzip', | |
'-o', | |
$src, | |
'-d', | |
$dst | |
) ); | |
} | |
function mv( $src, $dst ) { | |
exec_cmd( build_cmd( | |
'mv', | |
$src, | |
$dst | |
) ); | |
} | |
function latest_version_from_changelog( $changelog_path ) { | |
$changelog = file_get_contents( $changelog_path ); | |
preg_match_all( | |
'/(\d{4}[.\-]\d{1,2}[.\-]\d{1,2})[\w\s\-—]+(?<version>\d+\.\d+(\.\d+)?(\.\d+)?)/', | |
$changelog, | |
$matches | |
); | |
if ( empty( $matches['version'] ) ) { | |
throw new Exception( 'No valid date or version format detected in changelog.txt' ); | |
} | |
$latest_version = ! empty( $matches['version'][0] ) ? $matches['version'][0] : ''; | |
if ( empty( $latest_version ) ) { | |
throw new Exception( 'No latest version number found in changelog.txt' ); | |
} | |
return trim( $latest_version ); | |
} | |
function init() { | |
mkdir( WORKDIR, 0777, true ); | |
// Prepare src dirs in workdir. | |
mkdir( WORKDIR_SRC, 0777, true ); | |
mkdir( WORKDIR_SRC . '/plugins', 0777, true ); | |
mkdir( WORKDIR_SRC . '/themes', 0777, true ); | |
// Prepare sync dirs in workdir. | |
svn_checkout( ATOMIC_SVN_URL, WORKDIR_ATOMIC ); | |
mkdir( WORKDIR_ATOMIC . '/plugins', 0777, true ); | |
mkdir( WORKDIR_ATOMIC . '/themes', 0777, true ); | |
} | |
function import_products( string $type ) { | |
if ( ! in_array( $type, [ 'plugin', 'theme' ] ) ) { | |
throw new Exception( 'Invalid product type' ); | |
} | |
echo 'plugin' === $type | |
? ( 'importing plugins..' . PHP_EOL ) | |
: ( 'importing themes..' . PHP_EOL ); | |
$products = 'plugin' === $type | |
? PLUGINS | |
: THEMES; | |
$base_svn = 'plugin' === $type | |
? PLUGINS_SVN_URL . '/trunk/product-packages/' | |
: THEMES_SVN_URL . '/trunk/theme-packages/'; | |
$base_src = 'plugin' === $type | |
? WORKDIR_SRC . '/plugins/' | |
: WORKDIR_SRC . '/themes/'; | |
$base_atomic = 'plugin' === $type | |
? WORKDIR_ATOMIC . '/plugins/' | |
: WORKDIR_ATOMIC . '/themes/'; | |
$base_atomic_svn = 'plugin' === $type | |
? ATOMIC_SVN_URL . '/plugins/' | |
: ATOMIC_SVN_URL . '/themes/'; | |
foreach ( $products as $product ) { | |
echo "import {$product}.." . PHP_EOL; | |
$src = $base_svn . $product; | |
$dst = $base_src . $product; | |
svn_checkout( $src, $dst ); | |
mkdir( $base_atomic . $product, 0777, true ); | |
mkdir( $base_atomic . $product . '/tags', 0777, true ); | |
$zip_src = $dst . '/' . $product . '.zip'; | |
$extract_dst = $base_atomic . $product . '/tmp/'; | |
unzip( $zip_src, $extract_dst ); | |
mv( $extract_dst . $product, $base_atomic . $product . '/trunk' ); | |
rmdir( $extract_dst ); | |
$latest_version = latest_version_from_changelog( $base_atomic . $product . '/trunk/changelog.txt' ); | |
svn_add( $base_atomic . $product ); | |
svn_commit( $base_atomic, "{$product} {$latest_version}" ); | |
svn_remote_copy( | |
$base_atomic_svn . $product . '/trunk', | |
$base_atomic_svn . $product . '/tags/' . $latest_version, | |
"tag {$product} {$latest_version}" | |
); | |
} | |
echo PHP_EOL; | |
} | |
function main() { | |
echo 'Working dir: ' . WORKDIR . PHP_EOL; | |
echo 'Source dir: ' . WORKDIR_SRC . PHP_EOL; | |
echo 'Atomic dir: ' . WORKDIR_ATOMIC . PHP_EOL; | |
echo PHP_EOL; | |
try { | |
init(); | |
import_products( 'plugin' ); | |
import_products( 'theme' ); | |
echo 'Done!'; | |
} catch ( Exception $e ) { | |
echo $e->getMessage(); | |
exit( 1 ); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment