liborm85 / composer-vendor-cleaner

Composer Vendor Cleaner removes unnecessary development files and directories from vendor directory.
MIT License
27 stars 3 forks source link

Run after deploy #6

Closed fballiano closed 2 years ago

fballiano commented 2 years ago

Hi, is there a way to run the plugin after deploy and not before?

liborm85 commented 2 years ago

Plugin run cleanup vendor after package is installed (composer install or require event), no other later event is triggered by composer. How do you run deploy in composer? Any example?

fballiano commented 2 years ago

Sorry for my late answer, we were trying to implement your plugin in OpenMage https://github.com/OpenMage/magento-lts, with this composer.json (the "dev-files" section is just an example):

{
  "name": "openmage/magento-lts",
  "description": "A fork of Magento-1 that is accepting bug fixes (backward compatible, drop in replacement for official Magento)",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "type": "magento-source",
  "require": {
    "php": ">=7.0 <8.2",
    "ext-curl": "*",
    "ext-dom": "*",
    "ext-gd": "*",
    "ext-iconv": "*",
    "ext-json": "*",
    "ext-libxml": "*",
    "ext-mbstring": "*",
    "ext-pdo": "*",
    "ext-simplexml": "*",
    "ext-soap": "*",
    "ext-zlib": "*",
    "colinmollenhour/cache-backend-redis": "^1.14",
    "colinmollenhour/magento-redis-session": "^3.0",
    "liborm85/composer-vendor-cleaner": "^1.7",
    "magento-hackathon/magento-composer-installer": "^3.1 || ^2.1 || ^4.0",
    "phpseclib/mcrypt_compat": "^2.0.3",
    "phpseclib/phpseclib": "^3.0.14",
    "symfony/polyfill-intl-idn": "^1.26"
  },
  "require-dev": {
    "dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
    "friendsofphp/php-cs-fixer": ">=3.4",
    "macopedia/phpstan-magento1": "^1.0.4",
    "magento-ecg/coding-standard": "^4.4",
    "phpcompatibility/php-compatibility": "^9.3",
    "phpmd/phpmd": "^2.12",
    "phpstan/phpstan": "^1.8",
    "squizlabs/php_codesniffer": "^3.7"
  },
  "conflict": {
    "n98/n98_layouthelper": "*"
  },
  "authors": [
    {
      "name": "Lee Saferite",
      "email": "lee.saferite@openmage.org",
      "role": "Maintainer"
    },
    {
      "name": "Daniel Fahlke aka Flyingmana",
      "email": "flyingmana@googlemail.com",
      "role": "Maintainer"
    },
    {
      "name": "David Robinson",
      "email": "david.robinson@aoe.com",
      "role": "Maintainer"
    },
    {
      "name": "Tymoteusz Motylewski",
      "role": "Maintainer"
    },
    {
      "name": "Sven Reichel",
      "email": "github-sr@hotmail.com",
      "role": "Maintainer"
    }
  ],
  "autoload-dev": {
    "psr-4": { "OpenMage\\Tests\\Unit\\": "dev/tests/unit" }
  },
  "extra": {
    "branch-alias": {
      "dev-main": "1.9.4.x-dev"
    },
    "magento-root-dir": ".",
    "magento-deploystrategy": "copy",
    "magento-deploystrategy-dev": "symlink",
    "dev-files": {
      "vendor/colinmollenhour": [
        "*.md"
      ]
    }
  },
  "config": {
    "allow-plugins": {
      "magento-hackathon/magento-composer-installer": true,
      "dealerdirect/phpcodesniffer-composer-installer": true,
      "liborm85/composer-vendor-cleaner": true
    },
    "sort-packages": true
  }
}

But what happens is:

> post-update-cmd: Liborm85\ComposerVendorCleaner\Plugin->cleanup
Composer vendor cleaner: Cleaning vendor binary directory
Composer vendor cleaner: Removed 0 files and 4 (of which 4 are empty) directories from 54 packages

> post-update-cmd: Dealerdirect\Composer\Plugin\Installers\PHPCodeSniffer\Plugin->onDependenciesChangedEvent
Running PHPCodeSniffer Composer Installer
Executing command (/Users/fab/Projects/openmage/vendor/bin): phpcs --config-show
Executing command (/Users/fab/Projects/openmage/vendor/squizlabs/php_codesniffer): '/usr/local/Cellar/php/8.1.9/bin/php' -d allow_url_fopen='1' -d disable_functions='' -d memory_limit='1536M' ./bin/phpcs --config-set installed_paths ../../magento-ecg/coding-standard,../../phpcompatibility/php-compatibility
Executing command (/Users/fab/Projects/openmage/vendor/bin): phpcs --config-show
PHP CodeSniffer Config installed_paths set to ../../magento-ecg/coding-standard,../../phpcompatibility/php-compatibility
Using config file: /Users/fab/Projects/openmage/vendor/squizlabs/php_codesniffer/CodeSniffer.conf

Config value "installed_paths" added successfully

> post-update-cmd: MagentoHackathon\Composer\Magento\Plugin->onNewCodeEvent
start magento module deploy via moduleManager
Start magento deploy for colinmollenhour/cache-backend-redis
Start magento deploy for colinmollenhour/cache-backend-redis

In DeploystrategyAbstract.php line 382:

  [ErrorException]                                                                                     
  Source /Users/fab/Projects/openmage/vendor/colinmollenhour/cache-backend-redis/lib/* does not exist  

Exception trace:
  at /Users/fab/Projects/openmage/vendor/magento-hackathon/magento-composer-installer/src/MagentoHackathon/Composer/Magento/Deploystrategy/DeploystrategyAbstract.php:382
 MagentoHackathon\Composer\Magento\Deploystrategy\DeploystrategyAbstract->create() at /Users/fab/Projects/openmage/vendor/magento-hackathon/magento-composer-installer/src/MagentoHackathon/Composer/Magento/Deploystrategy/DeploystrategyAbstract.php:103
 MagentoHackathon\Composer\Magento\Deploystrategy\DeploystrategyAbstract->deploy() at /Users/fab/Projects/openmage/vendor/magento-hackathon/magento-composer-installer/src/MagentoHackathon/Composer/Magento/ModuleManager.php:107
 MagentoHackathon\Composer\Magento\ModuleManager->doInstalls() at /Users/fab/Projects/openmage/vendor/magento-hackathon/magento-composer-installer/src/MagentoHackathon/Composer/Magento/ModuleManager.php:82
 MagentoHackathon\Composer\Magento\ModuleManager->updateInstalledPackages() at /Users/fab/Projects/openmage/vendor/magento-hackathon/magento-composer-installer/src/MagentoHackathon/Composer/Magento/Plugin.php:273
 MagentoHackathon\Composer\Magento\Plugin->onNewCodeEvent() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php:200
 Composer\EventDispatcher\EventDispatcher->doDispatch() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php:123
 Composer\EventDispatcher\EventDispatcher->dispatchScript() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/src/Composer/Installer.php:374
 Composer\Installer->run() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/src/Composer/Command/InstallCommand.php:144
 Composer\Command\InstallCommand->execute() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/vendor/symfony/console/Command/Command.php:298
 Symfony\Component\Console\Command\Command->run() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/vendor/symfony/console/Application.php:1024
 Symfony\Component\Console\Application->doRunCommand() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/vendor/symfony/console/Application.php:299
 Symfony\Component\Console\Application->doRun() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/src/Composer/Console/Application.php:343
 Composer\Console\Application->doRun() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/vendor/symfony/console/Application.php:171
 Symfony\Component\Console\Application->run() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/src/Composer/Console/Application.php:138
 Composer\Console\Application->run() at phar:///usr/local/Cellar/composer/2.4.1/bin/composer/bin/composer:88
 require() at /usr/local/Cellar/composer/2.4.1/bin/composer:29

So it gave me the idea that the plugin was not running as the last possible action.

Thank you!

liborm85 commented 2 years ago

This error is not related to execution order plugin. Plugin by default remove all empty directories, which means directory /vendor/colinmollenhour/cache-backend-redis/lib/ is removed and therefore this error is returned.

In README is described how to disable remove empty directories feature. Simply you can add config to composer.json:

"config": {
    "dev-files": {
        "remove-empty-dirs": false
    }
}

and problem is resolved.

fballiano commented 2 years ago

Thanks! I changed the composer.json to:

{
  "name": "openmage/magento-lts",
  "description": "A fork of Magento-1 that is accepting bug fixes (backward compatible, drop in replacement for official Magento)",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "type": "magento-source",
  "require": {
    "php": ">=7.0 <8.2",
    "ext-curl": "*",
    "ext-dom": "*",
    "ext-gd": "*",
    "ext-iconv": "*",
    "ext-json": "*",
    "ext-libxml": "*",
    "ext-mbstring": "*",
    "ext-pdo": "*",
    "ext-simplexml": "*",
    "ext-soap": "*",
    "ext-zlib": "*",
    "magento-hackathon/magento-composer-installer": "^3.1 || ^2.1 || ^4.0",
    "phpseclib/phpseclib": "^3.0.14",
    "phpseclib/mcrypt_compat": "^2.0.3",
    "symfony/polyfill-intl-idn": "^1.26",
    "colinmollenhour/cache-backend-redis": "^1.14",
    "colinmollenhour/magento-redis-session": "^3.0",
    "liborm85/composer-vendor-cleaner": "^1.7"
  },
  "require-dev": {
    "dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
    "friendsofphp/php-cs-fixer": ">=3.4",
    "macopedia/phpstan-magento1": "^1.0.4",
    "magento-ecg/coding-standard": "^4.4",
    "phpcompatibility/php-compatibility": "^9.3",
    "phpmd/phpmd": "^2.12",
    "phpstan/phpstan": "^1.8",
    "squizlabs/php_codesniffer": "^3.7"
  },
  "conflict": {
    "n98/n98_layouthelper": "*"
  },
  "authors": [
    {
      "name": "Lee Saferite",
      "email": "lee.saferite@openmage.org",
      "role": "Maintainer"
    },
    {
      "name": "Daniel Fahlke aka Flyingmana",
      "email": "flyingmana@googlemail.com",
      "role": "Maintainer"
    },
    {
      "name": "David Robinson",
      "email": "david.robinson@aoe.com",
      "role": "Maintainer"
    },
    {
      "name": "Tymoteusz Motylewski",
      "role": "Maintainer"
    },
    {
      "name": "Sven Reichel",
      "email": "github-sr@hotmail.com",
      "role": "Maintainer"
    }
  ],
  "autoload-dev": {
    "psr-4": { "OpenMage\\Tests\\Unit\\": "dev/tests/unit" }
  },
  "extra": {
    "branch-alias": {
      "dev-main": "1.9.4.x-dev"
    },
    "magento-root-dir": ".",
    "magento-deploystrategy": "copy",
    "magento-deploystrategy-dev": "symlink",
    "dev-files": {
      "remove-empty-dirs": false,
      "/": [
        "*.md"
      ]
    }
  },
  "config": {
    "allow-plugins": {
      "magento-hackathon/magento-composer-installer": true,
      "dealerdirect/phpcodesniffer-composer-installer": true,
      "liborm85/composer-vendor-cleaner": true
    },
    "sort-packages": true
  }
}

but it still generates the error:

Composer vendor cleaner: Cleaning vendor binary directory
Composer vendor cleaner: Removed 91 files and 4 (of which 4 are empty) directories from 54 packages

PHP CodeSniffer Config installed_paths set to ../../magento-ecg/coding-standard,../../phpcompatibility/php-compatibility

In DeploystrategyAbstract.php line 382:

  Source /Users/fab/Projects/openmage/vendor/colinmollenhour/cache-backend-redis/lib/* does not exist  

It seems it's still removing the empty dirs :-(

liborm85 commented 2 years ago

remove-empty-dirs is property for config not for extra. This is correct definition:

{
  "name": "openmage/magento-lts",
  "description": "A fork of Magento-1 that is accepting bug fixes (backward compatible, drop in replacement for official Magento)",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "type": "magento-source",
  "require": {
    "php": ">=7.0 <8.2",
    "ext-curl": "*",
    "ext-dom": "*",
    "ext-gd": "*",
    "ext-iconv": "*",
    "ext-json": "*",
    "ext-libxml": "*",
    "ext-mbstring": "*",
    "ext-pdo": "*",
    "ext-simplexml": "*",
    "ext-soap": "*",
    "ext-zlib": "*",
    "magento-hackathon/magento-composer-installer": "^3.1 || ^2.1 || ^4.0",
    "phpseclib/phpseclib": "^3.0.14",
    "phpseclib/mcrypt_compat": "^2.0.3",
    "symfony/polyfill-intl-idn": "^1.26",
    "colinmollenhour/cache-backend-redis": "^1.14",
    "colinmollenhour/magento-redis-session": "^3.0",
    "liborm85/composer-vendor-cleaner": "^1.7"
  },
  "require-dev": {
    "dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
    "friendsofphp/php-cs-fixer": ">=3.4",
    "macopedia/phpstan-magento1": "^1.0.4",
    "magento-ecg/coding-standard": "^4.4",
    "phpcompatibility/php-compatibility": "^9.3",
    "phpmd/phpmd": "^2.12",
    "phpstan/phpstan": "^1.8",
    "squizlabs/php_codesniffer": "^3.7"
  },
  "conflict": {
    "n98/n98_layouthelper": "*"
  },
  "authors": [
    {
      "name": "Lee Saferite",
      "email": "lee.saferite@openmage.org",
      "role": "Maintainer"
    },
    {
      "name": "Daniel Fahlke aka Flyingmana",
      "email": "flyingmana@googlemail.com",
      "role": "Maintainer"
    },
    {
      "name": "David Robinson",
      "email": "david.robinson@aoe.com",
      "role": "Maintainer"
    },
    {
      "name": "Tymoteusz Motylewski",
      "role": "Maintainer"
    },
    {
      "name": "Sven Reichel",
      "email": "github-sr@hotmail.com",
      "role": "Maintainer"
    }
  ],
  "autoload-dev": {
    "psr-4": { "OpenMage\\Tests\\Unit\\": "dev/tests/unit" }
  },
  "extra": {
    "branch-alias": {
      "dev-main": "1.9.4.x-dev"
    },
    "magento-root-dir": ".",
    "magento-deploystrategy": "copy",
    "magento-deploystrategy-dev": "symlink",
    "dev-files": {
      "/": [
        "*.md"
      ]
    }
  },
  "config": {
    "allow-plugins": {
      "magento-hackathon/magento-composer-installer": true,
      "dealerdirect/phpcodesniffer-composer-installer": true,
      "liborm85/composer-vendor-cleaner": true
    },
    "sort-packages": true,
    "dev-files": {
      "remove-empty-dirs": false
    }
  }
}
fballiano commented 2 years ago

Thank you, it works now