UPCOMING: Magento 2 Bootcamp of four days in The Netherlands (April 29th - May 2nd)
background

October 4, 2019

CLI access to Magento Marketplace details

Yireo Blog Post

So, I wanted to easily see which version of the Magento Framework belongs to which major version of the Magento distribution. For instance, Magento version 2.3.2 ships with the package magento/framework version 102.0.2. How to put a script together that finds this version information automatically? Let's use jq for that.

Using the proper versions in composer

When releasing my Magento extensions, I always make sure that my modules have their dependencies mapped in both module.xml and composer.json. And because every Magento extension is always using specific features from the Magento Framework, the package magento/framework should be listed there as a dependency too.

For instance, if my module is using ViewModels, I should know that these ViewModels were introduced in Magento 2.2.0 and actually improved in Magento 2.2.1. And Magento 2.2.1 ships with magento/framework version 101.0.1. All minor and patch versions after 101.0.1 include this new feature, so does major 102. A nice way of summarizing this in semantic versioning would be ^101.0.1|^101.1|^102.0.

When dealing with versions this way, knowing which composer package was shipped with which Magento main release is vital.

Fetching composer information

All of this information is contained within the composer packages of Magento. While the source of Magento can be found on GitHub, composer packages are released via the Magento Marketplace (https://repo.magento.com) so that we can collect all information using a single CURL request. The Magento Marketplace is protected by a HTTP Basic Authentication login, which we can add to a curl command like the following:

USERNAME=your-marketplace-username
PASSWORD=your-marketplace-password
curl --basic --user $USERNAME:$PASSWORD https://repo.magento.com/packages.json > magento-packages.json

The file magento-packages.json now contains a huge JSON blob with information on Magento core packages and third party extensions (that you purchased, free or commercial). Let's play with it.

Meet jq

jq is a light-weight JSON processor that you can use to collect information from the command-line. For instance, we can pipe the downloaded JSON to jq to list all of the available packages:

cat magento-packages.json | jq '.packages | keys'

Or, how many packages are contained within the Magento Marketplace by default:

cat magento-packages.json | jq '.packages | keys' | wc

Which is 299 minus 2 (the brackets of the keys array) is 297.

Which Magento version matches which package?

We can now also use jq to search for specific information within the JSON structure. For instance, the following oneliner fetches the specific package magento/product-community-edition version 2.2.9 and returns the value of the requirement magento/framework:

cat magento-packages.json | jq '.packages."magento/product-community-edition"."2.2.9".require."magento/framework"'

Answer: 101.0.9.

More

Obviously, much more is possible with jq. It supports regular expressions, simple if statements, different parsers. It definitely is a handy tool when working with composer JSON blobs, Magento Marketplace being one of them.

Posted on October 4, 2019

About the author

Author Jisse Reitsma

Jisse Reitsma is the founder of Yireo, extension developer, developer trainer and 3x Magento Master. His passion is for technology and open source. And he loves talking as well.

Sponsor Yireo

Looking for a training in-house?

Let's get to it!

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.

Do not miss out on what we say

This will be the most interesting spam you have ever read

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.