Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort packages without adding/updating dependencies

Tags:

composer-php

On an existing project with a long list of packages and various feature branches where new dependencies are being added I want to mitigate and minimize merge conflicts by adding dependencies in alphabetical order.

To get this cleaned up, though, I'd like to be able to run the --sort-packages functionality on its own -- without adding or updating anything -- as just a single commit that cleans up the existing packages, and then add "sort-packages" : "true" to the "config" section of the composer.json file to ensure all new packages are added in alphabetical order going forward.

Is it possible to sort the packages listed in a messy composer.json file using composer's --sort-packages option on the CLI without actually adding or updating any dependencies?

The only workaround I've found so far is to run composer update some/package --sort-packages against a package that you're sure wont update because it is already at the latest version. This is not ideal.

like image 830
JamesWilson Avatar asked Oct 04 '16 03:10

JamesWilson


3 Answers

You can "re-require" a package you've already required. In my case, it's Symfony 3.4, so I did:

composer require symfony/symfony:3.4.*

If you don't have "sort-packages": true in your composer.json, you can do:

composer require --sort-packages symfony/symfony:3.4.*

From what I can tell, only the require command has the option for sorting packages so it seems you need to require a package for sorting to be applied.

like image 65
Darryl Hein Avatar answered Nov 01 '22 02:11

Darryl Hein


I know I'm (really) late! But perhaps my answer can help others in the future ;-)

If you run

composer config sort-packages true

this will add the following in your composer.json file:

    "config": {
        "sort-packages": true
    },

The next time you do a composer require (or update) it will automatically sort the whole list.

like image 32
Stefan Korfitz Avatar answered Nov 01 '22 00:11

Stefan Korfitz


First of all, +1 for using "sort-packages": true, for all the reasons you describe.

It is a bad idea to edit composer.lock directly, but I do not think the same applies to composer.json. I would edit the files in Vim, select everything inside the "require" and "require-dev" sections (one at a time) and :sort. Plus some fiddling to make sure that every line except the last has a comma.

like image 1
benjifisher Avatar answered Nov 01 '22 00:11

benjifisher