Skip to content

Commit b10661c

Browse files
committed
More adjustments for Playground CLI, e.g. blueprint.resolve filter
1 parent 9b72f07 commit b10661c

File tree

5 files changed

+307
-14
lines changed

5 files changed

+307
-14
lines changed

components/Blueprints/DataReference/DataReferenceResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct( Client $client, ?string $tmpRoot = null ) {
5252
$this->tmpRoot = $tmpRoot ?: wp_unix_sys_get_temp_dir();
5353
}
5454

55-
public function setExecutionContext( Filesystem $executionContext ) {
55+
public function setExecutionContext( ?Filesystem $executionContext ) {
5656
$this->executionContext = $executionContext;
5757
}
5858

components/Blueprints/Runner.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,14 @@ private function loadBlueprint() {
300300
$blueprintString = $resolved->getStream()->consume_all();
301301
$this->blueprintExecutionContext = LocalFilesystem::create( dirname( $reference->get_path() ) );
302302
} else {
303+
// For the purposes of Blueprint resolution, the execution context is the
304+
// current working directory. This way, a path such as ./blueprint.json
305+
// will mean "a blueprint.json file in the current working directory" and not
306+
// "a ./blueprint.json path without a point of reference".
307+
$this->assets->setExecutionContext( LocalFilesystem::create( getcwd() ) );
303308
$resolved = $this->assets->resolve( $reference );
309+
$this->assets->setExecutionContext( null );
310+
304311
if ( $resolved instanceof File ) {
305312
$stream = $resolved->getStream();
306313

@@ -388,6 +395,8 @@ private function validateBlueprint(): void {
388395

389396
$this->configuration->getLogger()->debug( 'Final resolved Blueprint: ' . json_encode( $this->blueprintArray, JSON_PRETTY_PRINT ) );
390397

398+
$this->blueprintArray = apply_filters( 'blueprint.resolved', $this->blueprintArray );
399+
391400
// Assert the Blueprint conforms to the latest JSON schema.
392401
$v = new HumanFriendlySchemaValidator(
393402
json_decode( file_get_contents( __DIR__ . '/Versions/Version2/json-schema/schema-v2.json' ), true )
@@ -450,7 +459,7 @@ private function validateBlueprint(): void {
450459
// WordPress Version Constraint
451460
if ( isset( $this->blueprintArray['wordpressVersion'] ) ) {
452461
$wp_version = $this->blueprintArray['wordpressVersion'];
453-
$recommended = null;
462+
$min = $max = $recommended = null;
454463
if ( is_string( $wp_version ) ) {
455464
$this->recommendedWpVersion = $wp_version;
456465
$recommended = WordPressVersion::fromString( $wp_version );
@@ -500,6 +509,14 @@ private function validateBlueprint(): void {
500509
// correctly. The actual version check for WordPress is done in
501510
// NewSiteResolver and ExistingSiteResolver.
502511
}
512+
513+
// Validate the override constraint if it was set
514+
if ( $this->wpVersionConstraint ) {
515+
$wpConstraintErrors = $this->wpVersionConstraint->validate();
516+
if ( ! empty( $wpConstraintErrors ) ) {
517+
throw new BlueprintExecutionException( 'Invalid WordPress version constraint from CLI override: ' . implode( '; ', $wpConstraintErrors ) );
518+
}
519+
}
503520
}
504521

505522
private function createExecutionPlan(): array {
@@ -934,14 +951,6 @@ static function () {
934951

935952
return new WriteFilesStep( $files );
936953

937-
case 'runPHP':
938-
return new RunPHPStep(
939-
$this->createDataReference( [
940-
'filename' => 'run-php.php',
941-
'content' => $data['code'],
942-
] ),
943-
$data['env'] ?? []
944-
);
945954
case 'unzip':
946955
$zipFile = $this->createDataReference( $data['zipFile'], [ ExecutionContextPath::class ] );
947956

components/Blueprints/VersionStrings/WordPressVersion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class WordPressVersion implements Version {
4646
* @return $this|false|null
4747
*/
4848
static public function fromString( string $raw ) {
49-
if(in_array($raw, ['beta','trunk'], true)) {
49+
if(in_array($raw, ['beta','trunk','latest'], true)) {
5050
return null;
5151
}
5252
if(substr($raw, 0, 8) === 'https://') {

components/Blueprints/bin/blueprint.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use WordPress\CLI\CLI;
3737
use WordPress\Blueprints\DataReference\AbsoluteLocalPath;
3838
use WordPress\Blueprints\DataReference\DataReference;
39+
use WordPress\Blueprints\DataReference\ExecutionContextPath;
3940
use WordPress\Blueprints\Exception\BlueprintExecutionException;
4041
use WordPress\Blueprints\Exception\PermissionsException;
4142
use WordPress\Blueprints\Logger\CLILogger;
@@ -255,7 +256,7 @@ function createProgressReporter(): ProgressReporter {
255256
'site-url' => [ 'u', true, null, 'Public site URL (https://example.com)' ],
256257
'site-path' => [ null, true, null, 'Target directory with WordPress install context)' ],
257258
'execution-context' => [ 'x', true, null, 'Source directory with Blueprint context files' ],
258-
'mode' => [ 'm', true, 'create-new-site', 'Execution mode (create|apply)' ],
259+
'mode' => [ 'm', true, 'create-new-site', 'Execution mode (create-new-site|apply-to-existing-site)' ],
259260
'db-engine' => [ 'd', true, 'mysql', 'Database engine (mysql|sqlite)' ],
260261
'db-host' => [ null, true, '127.0.0.1', 'MySQL host' ],
261262
'db-user' => [ null, true, 'root', 'MySQL user' ],
@@ -279,7 +280,7 @@ function createProgressReporter(): ProgressReporter {
279280
] ),
280281
'examples' => [
281282
'php blueprint.php exec my-blueprint.json --site-url https://mysite.test --site-path /var/www/mysite.com',
282-
'php blueprint.php exec my-blueprint.json --execution-context /var/www --site-url https://mysite.test --mode apply --site-path ./site',
283+
'php blueprint.php exec my-blueprint.json --execution-context /var/www --site-url https://mysite.test --mode apply-to-existing-site --site-path ./site',
283284
'php blueprint.php exec my-blueprint.json --site-url https://mysite.test --site-path ./mysite --truncate-new-site-directory',
284285
],
285286
'aliases' => [ 'run' ],
@@ -397,9 +398,10 @@ function cliArgsToRunnerConfiguration( array $positionalArgs, array $options ):
397398
$blueprint_reference = $positionalArgs[0];
398399
$config->setBlueprint( DataReference::create( $blueprint_reference, [
399400
AbsoluteLocalPath::class,
401+
ExecutionContextPath::class,
400402
] ) );
401403
} catch ( InvalidArgumentException $e ) {
402-
throw new InvalidArgumentException( "Invalid Blueprint reference: " . $positionalArgs[0] );
404+
throw new InvalidArgumentException( sprintf( "Invalid Blueprint reference: %s. Hint: paths must start with ./ or /. URLs must start with http:// or https://.", $positionalArgs[0] ) );
403405
}
404406

405407
if ( ! empty( $options['mode'] ) ) {
@@ -409,6 +411,9 @@ function cliArgsToRunnerConfiguration( array $positionalArgs, array $options ):
409411
$config->setExecutionMode( 'create-new-site' );
410412
} elseif ( $mode === 'apply-to-existing-site' ) {
411413
$config->setExecutionMode( 'apply-to-existing-site' );
414+
if(!empty($options['wp'])) {
415+
throw new InvalidArgumentException( "The --wp option cannot be used with --mode=apply-to-existing-site. The WordPress version is whatever the existing site has." );
416+
}
412417
} else {
413418
throw new InvalidArgumentException( "Invalid execution mode: {$mode}. Supported modes are: create-new-site, apply-to-existing-site" );
414419
}

0 commit comments

Comments
 (0)