Showing posts with label composer. Show all posts
Showing posts with label composer. Show all posts

Wednesday, January 10, 2018

Laravel: Automatic Vendor Cleanup Command

When installing composer packages, they come up with lot of useless files and folders such as .git, tests, readme.md and more. When project becomes bigger with many packages, this junk takes considerable disk space. Since I work with Laravel framework most of the time, I created a command that allows me to automatically delete all these junk files whenever I happen to install or update composer packages.

Here is the pattern used by glob function to detect and delete all junk files:

protected $patterns = [
    '.git',
    '.github',
    'test',
    'tests',
    'travis',
    'demo',
    'demos',
    'license',
    'changelog*',
    'contributing*',
    'upgrading*',
    'upgrade*',
    '.idea',
    '.vagrant',
    'readme*',
    '_ide_helper.php',
    '{,.}*.yml',
    '*.yaml',
    '*.md',
    '*.xml',
    '*.log',
    '*.txt',
    '*.dist',
    '*.pdf',
    '*.xls',
    '*.doc',
    '*.docx',
    '*.png',
    '*.gif',
    '*.jpg',
    '*.bmp',
    '*.jpeg',
    '*.ico',
    '.php_cs*',
    '.scrutinizer',
    '.gitignore',
    '.gitattributes',
    '.editorconfig',
    'dockerfile',
    'composer.json',
    'composer.lock',
];

The command itself can be seen here.

To automatically cleanup junk files/folders when installing or updating composer packages in Laravel, @php artisan vendor:cleanup should be added under the post-autoload-dump section in composer.json file. Or we can also trigger it manually anytime by typing php artisan vendor:cleanup --o where --o is optional argument to display verbose output.

If you actually want to be able to use this idea of cleanup of useless files in any PHP project (Laravel or not), you might want to create a composer plugin instead and install it globally that can then work across PHP projects and cleanup useless files from the vendor folder.

If you witness any issue or something missing in the pattern list shown above, please leave your suggestion/improvements in comments. Thanks


Friday, May 1, 2015

Semantic Versioning and Composer

Today any seriously written application/software follows the Semantic Versioning (also called SemVer). It boils down to this (emphasis mine):

  1. MAJOR version when you make incompatible API changes
  2. MINOR version when you add functionality in a backwards-compatible manner
  3. PATCH version when you make backwards-compatible bug fixes

Let's take example of Symfony framework which follows SemVer. As of this writing, its stable version stands at 2.6.6

Symfony Versioning

+-------+---------+
| Name  | Version |
+-------+---------+
| MAJOR | 2       |
+-------+---------+
| MINOR | 6       |
+-------+---------+
| PATCH | 6       |
+-------+---------+

Here, we can see that MINOR and PATCH versions are backwards-compatible. This means if you install Symfony version 2.0.0 or 2.x.x with x being any number, it will be backwards-compatible. Meaning if you had initially installed Symfony version 2.0.0 and now you want to install version 2.6.6, you can do so without worrying it breaking functionality. However when Symfony announces a version starting with 3 as MAJOR version then it will most likely break some of the functionality with all previous version less than 3.x.x. Thanks to SemVer, we can easily find that out now. Not to mention, almost all nicely written frameworks or libraries or software in general now follow SemVer. Please read more about it.

As a PHP developer, composer is something we can't do without these days. If you think you are a PHP developer and haven't started using composer yet, you are not a PHP developer DOT In that case, search on Google on what it is and its tutorials or see its official documentation. In short, it is package manager for PHP that has superseded older methods such as PEAR, etc. It is like NPM for NodeJS or RubyGem for Ruby developers.

Let's say I want to install Symfony via composer, I can specify specific version to use in quite some ways like:

+--------------------------------+----------------------------------------------------------------+
| Version                        | Description                                                    |
+--------------------------------+----------------------------------------------------------------+
| symfony/symfony: "2.6.6"       | Exact 2.6.6 version                                            |
+--------------------------------+----------------------------------------------------------------+
| symfony/symfony: ">= 2.6"      | Any version greater than or equal to 2.6 like 2.6.7, 6.6.9     |
+--------------------------------+----------------------------------------------------------------+
| symfony/symfony: ">= 2.6,<3.0" | Any version greater than or equal to 2.6 but less than 3 Major |
+--------------------------------+----------------------------------------------------------------+
| symfony/symfony: "2.*"         | Any version as long as Major version is 2                      |
+--------------------------------+----------------------------------------------------------------+
| symfony/symfony: "*"           | Latest version                                                 |
+--------------------------------+----------------------------------------------------------------+
| symfony/symfony: "dev-master"  | Latest version from "master" branch                            |
+--------------------------------+----------------------------------------------------------------+
| symfony/symfony: "dev-testing" | Latest version from "testing" branch                           |
+--------------------------------+----------------------------------------------------------------+

We can also use special tilde character ~:

+---------------------------+-----------------------------------------------------------------------+
| Version                   | Description                                                           |
+---------------------------+-----------------------------------------------------------------------+
| symfony/symfony: "~2.6.6" | Any version starting from 2.6.0 to 2.6.x with x being any number      |
+---------------------------+-----------------------------------------------------------------------+
| symfony/symfony: "~2.6"   | Any version starting from 2.0 to 2.x or 2.x.x with x being any number |
+---------------------------+-----------------------------------------------------------------------+
| symfony/symfony: "~2"     | Any Major version greater than or equal to 2 like 2.x or 5.x.x        |
+---------------------------+-----------------------------------------------------------------------+

Once you know how to specify versions in composer, you can easily make sure you are only installing those packages that are backwards-compatible unless you want fresh installation of some package. Similarly, if you have wrote some package, you should choose versioning sensibly so that users of your package don't go incompatible accidentally if you have not made Major changes to your package.


By the way, here is extremely useful Composer cheat sheet, have fun !

Popular Posts