trellis_install_wp_cli_via_composer

trellis_install_wp_cli_via_composer

Ansible Role GitHub tag (latest SemVer) Ansible Role CircleCI Ansible Quality Score GitHub License Hire Itineris Twitter Follow @itineris_ltd Twitter Follow @TangRufus

Install WP-CLI via composer on Trellis servers.

Goal

The Problem

[Since WP-CLI v2.0.0,] the most problematic set of the dependencies, the hard requirement on an old version of Symfony, is gone. The only Symfony component we still have (yet) is symfony/finder, as there’s no upper version limit for that one.

-- https://make.wordpress.org/cli/2018/08/08/wp-cli-v2-0-0-release-notes/

However, the phar bundles with WP-CLI's dependencies. Those dependencies' always being loaded from the phar. As a result, their versions are locked.

For example: WP-CLI v2.4.0 phar bundles with symfony/process v2.8.5 (as a dependency of symfony/finder). Assume we have my-awesome-command which requires symfony/process:5.0.0. $ wp package install my-awesome-command installs symfony/process v5.0.0 as expected. However, symfony/process v2.8.5 (from the WP-CLI phar) is always used; the newer version of symfony/process (which required by my-awesome-command) is being ignored. Thus, my-awesome-command fails when trying to use symfony/process.

$ wp shell

wp> $reflector = new \ReflectionClass('Symfony\Component\Process\Process');
=> object(ReflectionClass)#2801 (1) {
  ["name"]=>
  string(33) "Symfony\Component\Process\Process"
}
wp> echo $reflector->getFileName(); // Note that it is loaded form the WP-CLI phar.
phar:///usr/bin/wp/vendor/symfony/process/Process.php

This problem affects symfony/finder and its dependencies.

The Solution

Installing WP-CLI via composer resolves the problem.

$ wp shell

wp> $reflector = new \ReflectionClass('Symfony\Component\Process\Process');
=> object(ReflectionClass)#2801 (1) {
  ["name"]=>
  string(33) "Symfony\Component\Process\Process"
}
wp> echo $reflector->getFileName(); // Note that it is loaded form composer's vendor folder.
/home/web/.composer/vendor/symfony/process/Process.php

Role Variables

# Composer packages to be removed before installing WP-CLI
# Default: []
wp_cli_composer_global_remove_packages:
  - wp-cli/wp-cli-bundle
  - psy/psysh

# Composer packages to be installed
# Default: "wp-cli/wp-cli-bundle:{{ wp_cli_version }}"
wp_cli_composer_global_require_packages:
  - "wp-cli/wp-cli:2.4.0"
  - "wp-cli/package-command:^2"
  - "psy/psysh:^0.9.12"
  - "xxx/yyy:'^1.2.3 || ^2.2.3'"

# WP-CLI package to be installed
# Default: []
# Taken form https://github.com/roots/trellis/blob/4425669bab0665f0c9aed92c80eb9b8c54f63e85/roles/wp-cli/defaults/main.yml#L10
wp_cli_packages:
  - "typisttech/image-optimize-command:@stable"
  - "[email protected]:Yoast/wp-cli-faker.git"

# WP-CLI path
# Default: /usr/bin/wp
# Taken form https://github.com/roots/trellis/blob/4425669bab0665f0c9aed92c80eb9b8c54f63e85/roles/wp-cli/defaults/main.yml#L3
wp_cli_bin_path: /usr/bin/wp

# WP-CLI bash completion path
# Default: /etc/bash_completion.d/wp-completion.bash
# Taken form https://github.com/roots/trellis/blob/4425669bab0665f0c9aed92c80eb9b8c54f63e85/roles/wp-cli/defaults/main.yml#L9
wp_cli_completion_path: /etc/bash_completion.d/wp-completion.bash

Requirements

  • Trellis v1.3.0 or later
  • Ansible v2.7.0 or later
  • Python v3.7.6 or later

Installation

  1. Add itinerisltd.trellis_install_wp_cli_via_composer to galaxy.yml

      # galaxy.yml
    
    + - src: itinerisltd.trellis_install_wp_cli_via_composer
    + version: XXX.YYY.ZZZ # Check for latest version!
    
  2. Replace wp-cli role with itinerisltd.trellis_install_wp_cli_via_composer

      # server.yml
    
    - - { role: wp-cli, tags: [wp-cli] }
    + - { role: itinerisltd.trellis_install_wp_cli_via_composer, tags: [wp-cli] }
    
  3. Install galaxy roles

    trellis galaxy install
    # Alternatively
    ansible-galaxy install -r galaxy.yml --force
    
  4. Re-provision

    trellis provision production
    # Alternatively
    ansible-playbook server.yml -e env=production
    

FAQ

How to install certain commands only instead of the whole WP-CLI bundle?

By default, the whole WP-CLI bundle (wp-cli/wp-cli-bundle) installed. If you want to keep your servers lean, install command packages selectively:

wp_cli_composer_global_remove_packages:
  - wp-cli/wp-cli-bundle

wp_cli_composer_global_require_packages:
  # Required: WP-CLI framework
  - "wp-cli/wp-cli:^2.4"
  # Only install commands you need:
  - "wp-cli/core-command:^2"
  - "wp-cli/cron-command:^2"
  - "wp-cli/db-command:^2"
  - "wp-cli/package-command:^2"

What to do when composer couldn't install packages because of conflicting version constraints?

Double check existing composer packages. Then, change version constraints via role variables. You might need to uninstall some packages.

These commands are your friends:

composer global show
cat $(composer config --global --absolute home)/composer.json

wp package list
cat $(wp package path)/composer.json

How to verify WP-CLI is installed via composer?

# Bad: Installed via Trellis, i.e: the phar
$ wp cli info
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
# Good: Installed via this role, i.e: composer
$ wp cli info
WP-CLI root dir:	/home/web/.composer/vendor/wp-cli/wp-cli

Is it idempotent or deterministic?

No.

Specific exact package versions might help but you will need to manage them manually.

It looks awesome. Where can I find more goodies like this?

Where can I give :star::star::star::star::star: reviews?

Thanks! Glad you like it. It's important to let my boss knows somebody is using this project. Since this is not hosted on wordpress.org, please consider:

Testing

ansible-playbook -i 'localhost,' --syntax-check tests/test.yml

Feedback

Please provide feedback! We want to make this library useful in as many projects as possible. Please submit an issue and point out what you do and don't like, or fork the project and make suggestions. No issue is too small.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

trellis_install_wp_cli_via_composer is a Itineris Limited project created by Tang Rufus.

Full list of contributors can be found here.

License

trellis_install_wp_cli_via_composer is released under the MIT License.

About

Install WP-CLI via composer on Trellis servers

Install
ansible-galaxy install ItinerisLtd/trellis_install_wp_cli_via_composer
GitHub repository
License
mit
Downloads
224
Owner
We’re a full-service digital marketing agency that partners with ambitious clients to help them grow