Skip to content

Commit 47026a2

Browse files
committed
Move version changes to make-phar.php
- make-phar.php gains a new optional flag --store-version which defaults to false. The script will change the contents of VERSION only if this is true. This is false when building temporary Phars with custom versions for tests. - Because of that there is a new function set_file_contents() in make-phar.php This is similar to add_file(), but the contents of the new file are passed as an argument, instead of reading them via file_get_contents() - Behat test for building a temporary Phar with a fake version Also ensure that VERSION is not modified
1 parent 80978c4 commit 47026a2

6 files changed

Lines changed: 75 additions & 13 deletions

File tree

features/bootstrap/FeatureContext.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ public static function afterSuite( SuiteEvent $event ) {
7878
}
7979
}
8080

81+
/**
82+
* @BeforeScenario
83+
*/
84+
public function beforeScenario( $event ) {
85+
$this->variables['SRC_DIR'] = realpath( __DIR__ . '/../..' );
86+
}
87+
8188
/**
8289
* @AfterScenario
8390
*/
@@ -138,18 +145,19 @@ public function create_run_dir() {
138145
}
139146
}
140147

141-
public function build_phar( $version ) {
142-
$this->variables['TRUE_VERSION'] = file_get_contents( './VERSION' );
148+
public function build_phar( $version = 'same' ) {
143149
$this->variables['PHAR_PATH'] = $this->variables['RUN_DIR'] . '/' . uniqid( "wp-cli-build-", TRUE ) . '.phar';
144150

145151
Process::create(
146-
Utils\esc_cmd( 'php -dphar.readonly=0 %s %2$s --version=%s && chmod +x %2$s', __DIR__ . '/../../utils/make-phar.php', $this->variables['PHAR_PATH'], $version ),
152+
Utils\esc_cmd(
153+
'php -dphar.readonly=0 %1$s %2$s --version=%3$s && chmod +x %2$s',
154+
__DIR__ . '/../../utils/make-phar.php',
155+
$this->variables['PHAR_PATH'],
156+
$version
157+
),
147158
null,
148159
self::get_process_env_variables()
149160
)->run_check();
150-
151-
file_put_contents( './VERSION', $this->variables['TRUE_VERSION'] );
152-
unset( $this->variables['TRUE_VERSION'] );
153161
}
154162

155163
private function set_cache_dir() {

features/cli.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
Feature: `wp cli` tasks
22

3+
Scenario: Ability to set a custom version when building
4+
Given an empty directory
5+
And save the {SRC_DIR}/VERSION file as {TRUE_VERSION}
6+
And a new Phar with version "1.2.3"
7+
8+
When I run `{PHAR_PATH} cli version`
9+
Then STDOUT should be:
10+
"""
11+
WP-CLI 1.2.3
12+
"""
13+
And the {SRC_DIR}/VERSION file should be:
14+
"""
15+
{TRUE_VERSION}
16+
"""
17+
318
Scenario: Check for updates
419
Given an empty directory
520
And a new Phar with version "0.0.0"

features/steps/given.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,21 @@ function ( $world, $stream, $output_filter, $key ) {
123123
function ( $world, $version ) {
124124
$world->build_phar( $version );
125125
}
126+
);
127+
128+
$steps->Given( '/^save the (.+) file ([\'].+[^\'])?as \{(\w+)\}$/',
129+
function ( $world, $filepath, $output_filter, $key ) {
130+
$full_file = file_get_contents( $world->replace_variables( $filepath ) );
131+
132+
if ( $output_filter ) {
133+
$output_filter = '/' . trim( str_replace( '%s', '(.+[^\b])', $output_filter ), "' " ) . '/';
134+
if ( false !== preg_match( $output_filter, $full_file, $matches ) )
135+
$output = array_pop( $matches );
136+
else
137+
$output = '';
138+
} else {
139+
$output = $full_file;
140+
}
141+
$world->variables[ $key ] = trim( $output, "\n" );
142+
}
126143
);

utils/make-phar-spec.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
'desc' => 'New package version',
1414
),
1515

16+
'store-version' => array(
17+
'runtime' => '',
18+
'file' => '<bool>',
19+
'default' => false,
20+
'desc' => 'If true the contents of ./VERSION will be set to the value passed to --version',
21+
),
22+
1623
'quiet' => array(
1724
'runtime' => '',
1825
'file' => '<bool>',

utils/make-phar.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,25 @@
1212
list( $args, $assoc_args, $runtime_config ) = $configurator->parse_args( array_slice( $GLOBALS['argv'], 1 ) );
1313

1414
if ( ! isset( $args[0] ) || empty( $args[0] ) ) {
15-
echo "usage: php -dphar.readonly=0 $argv[0] <path> [--quiet] [--version=same|patch|minor|major|x.y.z]\n";
15+
echo "usage: php -dphar.readonly=0 $argv[0] <path> [--quiet] [--version=same|patch|minor|major|x.y.z] [--store-version]\n";
1616
exit(1);
1717
}
1818

1919
define( 'DEST_PATH', $args[0] );
2020

2121
define( 'BE_QUIET', isset( $runtime_config['quiet'] ) && $runtime_config['quiet'] );
2222

23+
$current_version = file_get_contents( './VERSION' );
24+
2325
if ( isset( $runtime_config['version'] ) ) {
24-
$current_version = file_get_contents( './VERSION' );
25-
$new_version = $runtime_config['version'];
26+
$new_version = $runtime_config['version'];
27+
$new_version = Utils\increment_version( $current_version, $new_version );
28+
29+
if ( isset( $runtime_config['store-version'] ) && $runtime_config['store-version'] ) {
30+
file_put_contents( './VERSION', $new_version );
31+
}
2632

27-
file_put_contents( './VERSION', utils\increment_version( $current_version, $new_version ) );
33+
$current_version = $new_version;
2834
}
2935

3036
function add_file( $phar, $path ) {
@@ -36,6 +42,15 @@ function add_file( $phar, $path ) {
3642
$phar[ $key ] = file_get_contents( $path );
3743
}
3844

45+
function set_file_contents( $phar, $path, $content ) {
46+
$key = str_replace( './', '', $path );
47+
48+
if ( !BE_QUIET )
49+
echo "$key - $path\n";
50+
51+
$phar[ $key ] = $content;
52+
}
53+
3954
$phar = new Phar( DEST_PATH, 0, 'wp-cli.phar' );
4055

4156
$phar->startBuffering();
@@ -81,7 +96,8 @@ function add_file( $phar, $path ) {
8196
add_file( $phar, './vendor/autoload.php' );
8297
add_file( $phar, './utils/get-package-require-from-composer.php' );
8398
add_file( $phar, './vendor/rmccue/requests/library/Requests/Transport/cacert.pem' );
84-
add_file( $phar, './VERSION' );
99+
100+
set_file_contents( $phar, './VERSION', $current_version );
85101

86102
$phar->setStub( <<<EOB
87103
#!/usr/bin/env php
@@ -96,4 +112,3 @@ function add_file( $phar, $path ) {
96112
$phar->stopBuffering();
97113

98114
echo "Generated " . DEST_PATH . "\n";
99-

utils/update-phar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ packages_repo=../wp-cli-packages
1212
fname="phar/wp-cli.phar"
1313

1414
# generate archive
15-
php -dphar.readonly=0 ./utils/make-phar.php $packages_repo/$fname --quiet --version=$version
15+
php -dphar.readonly=0 ./utils/make-phar.php $packages_repo/$fname --quiet --version=$version --store-version
1616

1717
cd $packages_repo
1818

0 commit comments

Comments
 (0)