--- /dev/null
+<?php
+
+if ( !isset( $argv[1] ) ) {
+ echo "usage: php -dphar.readonly=0 $argv[0] <path> [--quiet]\n";
+ exit(1);
+}
+
+define( 'DEST_PATH', $argv[1] );
+
+define( 'BE_QUIET', in_array( '--quiet', $argv ) );
+
+function get_iterator( $dir ) {
+ return new \RecursiveIteratorIterator(
+ new \RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS )
+ );
+}
+
+function add_file( $phar, $path ) {
+ $key = str_replace( './', '', $path );
+
+ if ( !BE_QUIET )
+ echo "$key - $path\n";
+
+ $phar[ $key ] = file_get_contents( $path );
+}
+
+$phar = new Phar( DEST_PATH, 0, 'wp-cli.phar' );
+
+$phar->startBuffering();
+
+// php files
+foreach ( get_iterator( './php' ) as $path ) {
+ if ( !preg_match( '/\.php$/', $path ) )
+ continue;
+
+ add_file( $phar, $path );
+}
+
+// non-php files
+$additional_dirs = array(
+ './templates',
+);
+
+foreach ( $additional_dirs as $dir ) {
+ foreach ( get_iterator( $dir ) as $path ) {
+ add_file( $phar, $path );
+ }
+}
+
+// dependencies
+$ignored_paths = array(
+ '/.git',
+);
+
+$vendor_dirs = array(
+ './vendor/mustache',
+ './vendor/rmccue',
+ './vendor/wp-cli',
+ './vendor/composer',
+);
+
+foreach ( $vendor_dirs as $vendor_dir ) {
+ foreach ( get_iterator( $vendor_dir ) as $path ) {
+ foreach ( $ignored_paths as $ignore ) {
+ if ( strpos( $path, $ignore ) )
+ continue 2;
+ }
+
+ add_file( $phar, $path );
+ }
+}
+
+add_file( $phar, './vendor/autoload.php' );
+
+$phar->setStub( <<<EOB
+#!/usr/bin/env php
+<?php
+Phar::mapPhar();
+include 'phar://wp-cli.phar/php/boot-phar.php';
+__HALT_COMPILER();
+?>
+EOB
+);
+
+$phar->stopBuffering();
+
+echo "Generated " . DEST_PATH . "\n";
+
--- /dev/null
+#!/usr/bin/env bash
+
+set -ex
+
+current_rev=$(git rev-parse HEAD)
+current_rev=${current_rev:0:10}
+
+packages_repo=../wp-cli-packages
+
+fname="phar/wp-cli.phar"
+
+# generate archive
+php -dphar.readonly=0 ./utils/make-phar.php $packages_repo/$fname --quiet
+
+cd $packages_repo
+
+# smoke test
+php $fname --version
+
+# check which wp-cli commit the previous Phar archive was based on
+# can't use the md5 hash, since it will be different each time the
+# archive is generated
+new_commit_subj="update wp-cli.phar to wp-cli/wp-cli@$current_rev"
+
+current_commit_subj=$(git show -s --pretty=format:%s HEAD)
+
+if [ "$new_commit_subj" = "$current_commit_subj" ]; then
+ echo "already at latest revision"
+ exit 1
+fi
+
+# generate md5 checksum
+if [ command -v md5sum > /dev/null ]
+then
+ md5hash=$(md5sum $fname)
+else
+ md5hash=$(md5 -r $fname)
+fi
+
+echo $md5hash | cut -d ' ' -f 1 > $fname.md5
+
+git add $fname $fname.md5
+
+git commit -m "$new_commit_subj"
+
+git push