From d8ef815b2db2eddc98a0813fddd48925034119aa Mon Sep 17 00:00:00 2001 From: lpeidro Date: Tue, 22 Oct 2024 20:16:42 +0200 Subject: [PATCH 1/7] Firs version of instal events --- .gitignore => addons/.gitignore | 0 README.md => addons/README.md | 0 .../clickAndHoverHelper.js | 0 {examples => addons/examples}/backstop.json | 0 .../examples}/bitmaps_reference/.gitignore | 0 {examples => addons/examples}/cookies.json | 0 .../engine_scripts/customScenarioActions.js | 0 .../examples}/engine_scripts/onBefore.js | 0 .../examples}/engine_scripts/onReady.js | 0 loadCookies.js => addons/loadCookies.js | 0 .../loginAndNavigation.js | 0 setup.js => addons/setup.js | 0 .../userAgentByViewport.js | 0 composer.json | 15 ++++- src/Installer.php | 59 +++++++++++++++++++ 15 files changed, 73 insertions(+), 1 deletion(-) rename .gitignore => addons/.gitignore (100%) rename README.md => addons/README.md (100%) rename clickAndHoverHelper.js => addons/clickAndHoverHelper.js (100%) rename {examples => addons/examples}/backstop.json (100%) rename {examples => addons/examples}/bitmaps_reference/.gitignore (100%) rename {examples => addons/examples}/cookies.json (100%) rename {examples => addons/examples}/engine_scripts/customScenarioActions.js (100%) rename {examples => addons/examples}/engine_scripts/onBefore.js (100%) rename {examples => addons/examples}/engine_scripts/onReady.js (100%) rename loadCookies.js => addons/loadCookies.js (100%) rename loginAndNavigation.js => addons/loginAndNavigation.js (100%) rename setup.js => addons/setup.js (100%) rename userAgentByViewport.js => addons/userAgentByViewport.js (100%) create mode 100644 src/Installer.php diff --git a/.gitignore b/addons/.gitignore similarity index 100% rename from .gitignore rename to addons/.gitignore diff --git a/README.md b/addons/README.md similarity index 100% rename from README.md rename to addons/README.md diff --git a/clickAndHoverHelper.js b/addons/clickAndHoverHelper.js similarity index 100% rename from clickAndHoverHelper.js rename to addons/clickAndHoverHelper.js diff --git a/examples/backstop.json b/addons/examples/backstop.json similarity index 100% rename from examples/backstop.json rename to addons/examples/backstop.json diff --git a/examples/bitmaps_reference/.gitignore b/addons/examples/bitmaps_reference/.gitignore similarity index 100% rename from examples/bitmaps_reference/.gitignore rename to addons/examples/bitmaps_reference/.gitignore diff --git a/examples/cookies.json b/addons/examples/cookies.json similarity index 100% rename from examples/cookies.json rename to addons/examples/cookies.json diff --git a/examples/engine_scripts/customScenarioActions.js b/addons/examples/engine_scripts/customScenarioActions.js similarity index 100% rename from examples/engine_scripts/customScenarioActions.js rename to addons/examples/engine_scripts/customScenarioActions.js diff --git a/examples/engine_scripts/onBefore.js b/addons/examples/engine_scripts/onBefore.js similarity index 100% rename from examples/engine_scripts/onBefore.js rename to addons/examples/engine_scripts/onBefore.js diff --git a/examples/engine_scripts/onReady.js b/addons/examples/engine_scripts/onReady.js similarity index 100% rename from examples/engine_scripts/onReady.js rename to addons/examples/engine_scripts/onReady.js diff --git a/loadCookies.js b/addons/loadCookies.js similarity index 100% rename from loadCookies.js rename to addons/loadCookies.js diff --git a/loginAndNavigation.js b/addons/loginAndNavigation.js similarity index 100% rename from loginAndNavigation.js rename to addons/loginAndNavigation.js diff --git a/setup.js b/addons/setup.js similarity index 100% rename from setup.js rename to addons/setup.js diff --git a/userAgentByViewport.js b/addons/userAgentByViewport.js similarity index 100% rename from userAgentByViewport.js rename to addons/userAgentByViewport.js diff --git a/composer.json b/composer.json index 31c443d..765ef04 100644 --- a/composer.json +++ b/composer.json @@ -3,5 +3,18 @@ "description": "Metadrop custom backstopjs scripts", "type": "library", "minimum-stability": "dev", - "require": {} + "require": {}, + "autoload": { + "psr-4": { + "Metadrop\\BackstopjsAddons\\": "src/" + } + }, + "scripts": { + "post-install-cmd": [ + "Metadrop\\BackstopjsAddons\\Installer::MoveFiles" + ], + "post-update-cmd": [ + "Metadrop\\BackstopjsAddons\\Installer::MoveFiles" + ] + } } diff --git a/src/Installer.php b/src/Installer.php new file mode 100644 index 0000000..4f9454e --- /dev/null +++ b/src/Installer.php @@ -0,0 +1,59 @@ +getComposer(); + $package = $event->getOperation()->getPackage(); + + //$extra = $package->getExtra(); + + $source = $composer->getConfig()->get('vendor-dir') . '/metadrop/backstopjs-addons/addons'; + $root = $composer->getConfig()->get('vendor-dir') . '/..'; + $destination = $root . '/tests/backstopjs/common/libraries/backstopjs-addons'; + + // Create the folders if they don't exist of the destination that is a folder + if (!file_exists($destination)) { + mkdir($destination, 0755, TRUE); + } + self::copyFiles($source, $destination); + } + + /** + * Copy files from source to destination. + * + * @param string $source + * The source folder. + * @param string $destination + * The destination folder. + */ + private static function copyFiles($source, $destination) { + $files = scandir($source); + foreach ($files as $file) { + if ($file == '.' || $file == '..') { + continue; + } + if (is_dir($source . '/' . $file)) { + mkdir($destination . '/' . $file); + self::copyFiles($source . '/' . $file, $destination . '/' . $file); + } + else { + copy($source . '/' . $file, $destination . '/' . $file); + } + } + } + +} From 7dcf475799703e8b32dedcb9f86d3684bdb7a4ce Mon Sep 17 00:00:00 2001 From: lpeidro Date: Tue, 22 Oct 2024 20:44:39 +0200 Subject: [PATCH 2/7] Remove unviable method --- src/Installer.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Installer.php b/src/Installer.php index 4f9454e..ac1b35a 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -17,9 +17,6 @@ class Installer { */ public static function MoveFiles(Event $event) { $composer = $event->getComposer(); - $package = $event->getOperation()->getPackage(); - - //$extra = $package->getExtra(); $source = $composer->getConfig()->get('vendor-dir') . '/metadrop/backstopjs-addons/addons'; $root = $composer->getConfig()->get('vendor-dir') . '/..'; From b83e0bff7570ee4277e228f6c74ee175e87e3b0c Mon Sep 17 00:00:00 2001 From: lpeidro Date: Tue, 22 Oct 2024 21:08:49 +0200 Subject: [PATCH 3/7] Improve script to copy libraries --- addons/.gitignore => .gitignore | 0 src/Installer.php | 27 ++++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) rename addons/.gitignore => .gitignore (100%) diff --git a/addons/.gitignore b/.gitignore similarity index 100% rename from addons/.gitignore rename to .gitignore diff --git a/src/Installer.php b/src/Installer.php index ac1b35a..292b0d9 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -22,10 +22,12 @@ public static function MoveFiles(Event $event) { $root = $composer->getConfig()->get('vendor-dir') . '/..'; $destination = $root . '/tests/backstopjs/common/libraries/backstopjs-addons'; - // Create the folders if they don't exist of the destination that is a folder - if (!file_exists($destination)) { + if (is_dir($destination)) { + self::deleteDirectoryContents($source, $destination); + } else { mkdir($destination, 0755, TRUE); } + self::copyFiles($source, $destination); } @@ -53,4 +55,23 @@ private static function copyFiles($source, $destination) { } } -} + /** + * Delete directory content. + * + * @param string $dir + * The destination folder. + */ + private static function deleteDirectoryContents($dir) { + $files = array_diff(scandir($dir), array('.', '..')); + foreach ($files as $file) { + $filePath = $dir . '/' . $file; + if (is_dir($filePath)) { + self::deleteDirectoryContents($filePath); + rmdir($filePath); + } else { + unlink($filePath); + } + } + } + +} \ No newline at end of file From 5ca8b6f1ab56c4c4d022fd6e660d97748a9e5003 Mon Sep 17 00:00:00 2001 From: lpeidro Date: Tue, 22 Oct 2024 21:28:19 +0200 Subject: [PATCH 4/7] Transform the package in a composer plugin --- composer.json | 16 ++++------ src/{Installer.php => InstallerPlugin.php} | 37 +++++++++++++++++++--- 2 files changed, 39 insertions(+), 14 deletions(-) rename src/{Installer.php => InstallerPlugin.php} (67%) diff --git a/composer.json b/composer.json index 765ef04..c1b3288 100644 --- a/composer.json +++ b/composer.json @@ -1,20 +1,16 @@ { "name": "metadrop/backstopjs-addons", "description": "Metadrop custom backstopjs scripts", - "type": "library", - "minimum-stability": "dev", - "require": {}, + "type": "composer-plugin", + "require": { + "composer-plugin-api": "^2.0" + }, "autoload": { "psr-4": { "Metadrop\\BackstopjsAddons\\": "src/" } }, - "scripts": { - "post-install-cmd": [ - "Metadrop\\BackstopjsAddons\\Installer::MoveFiles" - ], - "post-update-cmd": [ - "Metadrop\\BackstopjsAddons\\Installer::MoveFiles" - ] + "extra": { + "class": "Metadrop\\BackstopjsAddons\\InstallerPlugin" } } diff --git a/src/Installer.php b/src/InstallerPlugin.php similarity index 67% rename from src/Installer.php rename to src/InstallerPlugin.php index 292b0d9..d5ebd10 100644 --- a/src/Installer.php +++ b/src/InstallerPlugin.php @@ -2,12 +2,41 @@ namespace Metadrop\BackstopjsAddons; +use Composer\Composer; +use Composer\EventDispatcher\EventSubscriberInterface; +use Composer\IO\IOInterface; +use Composer\Plugin\PluginInterface; use Composer\Script\Event; +use Composer\Script\ScriptEvents; /** * Composer scripts for the BackstopJS Addons during installation. */ -class Installer { +class InstallerPlugin implements PluginInterface, EventSubscriberInterfac { + + public function activate(Composer $composer, IOInterface $io): void + { + // noop + } + + public function deactivate(Composer $composer, IOInterface $io): void + { + // noop + } + + public function uninstall(Composer $composer, IOInterface $io): void + { + // noop + } + + + public static function getSubscribedEvents() + { + return [ + ScriptEvents::POST_INSTALL_CMD => 'MoveFiles', + ScriptEvents::POST_UPDATE_CMD => 'MoveFiles', + ]; + } /** * Move the BackstopJS Addons files to the tests folder. @@ -15,7 +44,7 @@ class Installer { * @param \Composer\Script\Event $event * The Composer event. */ - public static function MoveFiles(Event $event) { + public function MoveFiles(Event $event) { $composer = $event->getComposer(); $source = $composer->getConfig()->get('vendor-dir') . '/metadrop/backstopjs-addons/addons'; @@ -39,7 +68,7 @@ public static function MoveFiles(Event $event) { * @param string $destination * The destination folder. */ - private static function copyFiles($source, $destination) { + private function copyFiles($source, $destination) { $files = scandir($source); foreach ($files as $file) { if ($file == '.' || $file == '..') { @@ -61,7 +90,7 @@ private static function copyFiles($source, $destination) { * @param string $dir * The destination folder. */ - private static function deleteDirectoryContents($dir) { + private function deleteDirectoryContents($dir) { $files = array_diff(scandir($dir), array('.', '..')); foreach ($files as $file) { $filePath = $dir . '/' . $file; From ca7b7aa0ccf6116b4a94a89ca620809dbb9c4ffb Mon Sep 17 00:00:00 2001 From: lpeidro Date: Tue, 22 Oct 2024 21:28:19 +0200 Subject: [PATCH 5/7] Transform the package in a composer plugin --- README.md | 6 ++++ composer.json | 16 ++++------ src/{Installer.php => InstallerPlugin.php} | 37 +++++++++++++++++++--- 3 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 README.md rename src/{Installer.php => InstallerPlugin.php} (67%) diff --git a/README.md b/README.md new file mode 100644 index 0000000..c6170dd --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# BackstopJS Addons +A library that extends and improves the default options provided by [BackstopJS](https://github.com/garris/BackstopJS). + +### Composer + +``composer require --dev metadrop/backstopjs-addons`` diff --git a/composer.json b/composer.json index 765ef04..c1b3288 100644 --- a/composer.json +++ b/composer.json @@ -1,20 +1,16 @@ { "name": "metadrop/backstopjs-addons", "description": "Metadrop custom backstopjs scripts", - "type": "library", - "minimum-stability": "dev", - "require": {}, + "type": "composer-plugin", + "require": { + "composer-plugin-api": "^2.0" + }, "autoload": { "psr-4": { "Metadrop\\BackstopjsAddons\\": "src/" } }, - "scripts": { - "post-install-cmd": [ - "Metadrop\\BackstopjsAddons\\Installer::MoveFiles" - ], - "post-update-cmd": [ - "Metadrop\\BackstopjsAddons\\Installer::MoveFiles" - ] + "extra": { + "class": "Metadrop\\BackstopjsAddons\\InstallerPlugin" } } diff --git a/src/Installer.php b/src/InstallerPlugin.php similarity index 67% rename from src/Installer.php rename to src/InstallerPlugin.php index 292b0d9..c051586 100644 --- a/src/Installer.php +++ b/src/InstallerPlugin.php @@ -2,12 +2,41 @@ namespace Metadrop\BackstopjsAddons; +use Composer\Composer; +use Composer\EventDispatcher\EventSubscriberInterface; +use Composer\IO\IOInterface; +use Composer\Plugin\PluginInterface; use Composer\Script\Event; +use Composer\Script\ScriptEvents; /** * Composer scripts for the BackstopJS Addons during installation. */ -class Installer { +class InstallerPlugin implements PluginInterface, EventSubscriberInterface { + + public function activate(Composer $composer, IOInterface $io): void + { + // noop + } + + public function deactivate(Composer $composer, IOInterface $io): void + { + // noop + } + + public function uninstall(Composer $composer, IOInterface $io): void + { + // noop + } + + + public static function getSubscribedEvents() + { + return [ + ScriptEvents::POST_INSTALL_CMD => 'MoveFiles', + ScriptEvents::POST_UPDATE_CMD => 'MoveFiles', + ]; + } /** * Move the BackstopJS Addons files to the tests folder. @@ -15,7 +44,7 @@ class Installer { * @param \Composer\Script\Event $event * The Composer event. */ - public static function MoveFiles(Event $event) { + public function MoveFiles(Event $event) { $composer = $event->getComposer(); $source = $composer->getConfig()->get('vendor-dir') . '/metadrop/backstopjs-addons/addons'; @@ -39,7 +68,7 @@ public static function MoveFiles(Event $event) { * @param string $destination * The destination folder. */ - private static function copyFiles($source, $destination) { + private function copyFiles($source, $destination) { $files = scandir($source); foreach ($files as $file) { if ($file == '.' || $file == '..') { @@ -61,7 +90,7 @@ private static function copyFiles($source, $destination) { * @param string $dir * The destination folder. */ - private static function deleteDirectoryContents($dir) { + private function deleteDirectoryContents($dir) { $files = array_diff(scandir($dir), array('.', '..')); foreach ($files as $file) { $filePath = $dir . '/' . $file; From f5762c189b3cd2993e58cab4f51ee68c7278890b Mon Sep 17 00:00:00 2001 From: lpeidro Date: Tue, 22 Oct 2024 22:55:39 +0200 Subject: [PATCH 6/7] Fix undesiable removing of library --- src/InstallerPlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/InstallerPlugin.php b/src/InstallerPlugin.php index c051586..3bb8ef8 100644 --- a/src/InstallerPlugin.php +++ b/src/InstallerPlugin.php @@ -52,7 +52,7 @@ public function MoveFiles(Event $event) { $destination = $root . '/tests/backstopjs/common/libraries/backstopjs-addons'; if (is_dir($destination)) { - self::deleteDirectoryContents($source, $destination); + self::deleteDirectoryContents($destination); } else { mkdir($destination, 0755, TRUE); } From 951fd7c1462010d8c17d76bdd1463c0edd3ce95e Mon Sep 17 00:00:00 2001 From: lpeidro Date: Wed, 23 Oct 2024 12:18:16 +0200 Subject: [PATCH 7/7] Added possiblity to overrite the destination --- README.md | 11 +++++++++++ src/InstallerPlugin.php | 9 ++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c6170dd..f31d371 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,14 @@ A library that extends and improves the default options provided by [BackstopJS] ### Composer ``composer require --dev metadrop/backstopjs-addons`` + +You can override the default destination of the library by adding the following parameter to the extra section +of your project's composer.json file: + +```json +"extra": { + "backstop-addons": { + "destinition": "custom/destinition" + } +``` + \ No newline at end of file diff --git a/src/InstallerPlugin.php b/src/InstallerPlugin.php index 3bb8ef8..fe5ccce 100644 --- a/src/InstallerPlugin.php +++ b/src/InstallerPlugin.php @@ -47,9 +47,16 @@ public static function getSubscribedEvents() public function MoveFiles(Event $event) { $composer = $event->getComposer(); + // Source $source = $composer->getConfig()->get('vendor-dir') . '/metadrop/backstopjs-addons/addons'; + + // Destination $root = $composer->getConfig()->get('vendor-dir') . '/..'; - $destination = $root . '/tests/backstopjs/common/libraries/backstopjs-addons'; + + $extra = $composer->getPackage()->getExtra(); + $backstop_addons_extras = $extra['backstop-addons'] ?? []; + $destination = $backstop_addons_extras['destination'] ?? 'tests/backstopjs/common/libraries/backstopjs-addons'; + $destination = "{$root}/{$destination}"; if (is_dir($destination)) { self::deleteDirectoryContents($destination);