From 252c1c421d33429dfae93f5af728f455a5482f77 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Fri, 15 Mar 2024 10:32:28 -0400 Subject: [PATCH] Add add_exclusion/remove_exclusion method to rsync installation manager (#517) * Switching to symfony/css-selector package * Switch to symfony here too * Add add_exclusion/remove_exclusion methods and remove .buddy-tests/.composer from default exclusions * Break out default to a standalone function for reuse * PHPCS fix * Rector fix --- .../testing/class-installation-manager.php | 2 + .../concerns/trait-rsync-installation.php | 73 +++++++++++++++---- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/mantle/testing/class-installation-manager.php b/src/mantle/testing/class-installation-manager.php index b0bfb618..7d5d2d67 100644 --- a/src/mantle/testing/class-installation-manager.php +++ b/src/mantle/testing/class-installation-manager.php @@ -47,6 +47,8 @@ class Installation_Manager { * configure the installation. */ public function __construct() { + $this->with_default_exclusions(); + if ( Utils::env_bool( 'MANTLE_INSTALL_VIP_MU_PLUGINS', false ) ) { $this->with_vip_mu_plugins(); } diff --git a/src/mantle/testing/concerns/trait-rsync-installation.php b/src/mantle/testing/concerns/trait-rsync-installation.php index 1b929cbb..3a3ca8e1 100644 --- a/src/mantle/testing/concerns/trait-rsync-installation.php +++ b/src/mantle/testing/concerns/trait-rsync-installation.php @@ -60,17 +60,26 @@ trait Rsync_Installation { * * @var string[] */ - protected array $rsync_exclusions = [ - '.buddy-tests', - '.buddy', - '.composer', - '.git', - '.npm', - '.phpcs', - '.turbo', - 'node_modules', - 'phpstan.neon', - ]; + protected array $rsync_exclusions = []; + + /** + * Add the default set of exclusions to the list of exclusions to be used when rsyncing the codebase. + */ + public function with_default_exclusions(): static { + return $this->exclusions( + [ + '.buddy', + '.git', + '.github', + '.npm', + '.phpcs', + '.turbo', + '.phpunit.result.cache', + 'node_modules', + 'phpstan.neon', + ] + ); + } /** * Rsync the code base to be located under a valid WordPress installation. @@ -151,7 +160,7 @@ public function with_vip_mu_plugins( bool $install = true ): static { return $this; } - $this->rsync_exclusions[] = 'mu-plugins'; + $this->add_exclusion( 'mu-plugins' ); putenv( 'MANTLE_INSTALL_VIP_MU_PLUGINS=' . ( $install ? '1' : '0' ) ); @@ -178,7 +187,7 @@ public function with_object_cache( bool $install = true ): static { return $this; } - $this->rsync_exclusions[] = 'object-cache.php'; + $this->add_exclusion( 'object-cache.php' ); putenv( 'MANTLE_INSTALL_OBJECT_CACHE=' . ( $install ? '1' : '0' ) ); @@ -201,8 +210,12 @@ public function with_sqlite( bool $install = true ): static { putenv( 'MANTLE_USE_SQLITE=' . ( $install ? '1' : '0' ) ); putenv( 'WP_SKIP_DB_CREATE=1' ); - $this->rsync_exclusions[] = 'db.php'; - $this->rsync_exclusions[] = 'sqlite-database-integration'; + $this->exclusions( + [ + 'db.php', + 'sqlite-database-integration', + ] + ); return $this; } @@ -271,7 +284,35 @@ public function maybe_rsync_theme( string $name = null, string $from = null ): s * @param bool $merge Whether to merge the exclusions with the default exclusions. */ public function exclusions( array $exclusions, bool $merge = true ): static { - $this->rsync_exclusions = $merge ? array_merge( $this->rsync_exclusions, $exclusions ) : $exclusions; + $this->rsync_exclusions = collect( $merge ? $this->rsync_exclusions : [] ) + ->merge( $exclusions ) + ->unique() + ->values() + ->all(); + + return $this; + } + + /** + * Add an exclusion to the list of exclusions to be used when rsyncing the codebase. + * + * @param string $exclusion Exclusion to add to the list of exclusions. + */ + public function add_exclusion( string $exclusion ): static { + return $this->exclusions( [ $exclusion ], true ); + } + + /** + * Remove an exclusion from the list of exclusions to be used when rsyncing the codebase. + * + * @param string $exclusion Exclusion to remove from the list of exclusions. + */ + public function remove_exclusion( string $exclusion ): static { + $this->rsync_exclusions = collect( $this->rsync_exclusions ) + ->filter( fn ( $item ) => $item !== $exclusion ) + ->unique() + ->values() + ->all(); return $this; }