Code Monkey home page Code Monkey logo

composer-substitution-plugin's Introduction

Composer Substitution Plugin

The Composer Substitution plugin replaces placeholders in the scripts section by dynamic values.

It also permits to cache these values during the command execution and adds the ability to escape them with the function of your choice.

Build Status AppVeyor Build Status Latest Stable Version Minimum PHP Version License

Installation

composer require villfa/composer-substitution-plugin

Requirements

  • PHP >= 5.3.2
  • Composer >= 1.0.0

Usage

You need to configure the plugin in the extra section of composer.json.

Here an example:

"extra": {
    "substitution": {
        "enable": true,
        "mapping": {
            "{MY_NAME}": {
                "type": "literal",
                "value": "John Doe",
                "escape": "addslashes"
            },
            "{PHP_VERSION}": {
                "type": "callback",
                "value": "phpversion"
            },
            "{DB_STATUS}": {
                "type": "include",
                "value": "./scripts/db.php",
                "cached": true
            },
            "{HOME}": {
                "type": "env",
                "value": "HOME"
            },
            "{COMPOSER_VERSION}": {
                "type": "constant",
                "value": "Composer\\Composer::VERSION"
            },
            "{NPROC}": {
                "type": "process",
                "value": "nproc"
            }
        }
    }
}

Then you can add the configured placeholders in the scripts section:

"scripts": {
    "welcome": "echo 'Hi {MY_NAME}, the database is {DB_STATUS}.'"
}

And now if you run the command:

$ composer run-script welcome
Hi John Doe, the database is OK.

Configuration

Configuration key Mandatory Type Default value Description
extra.substitution.enable no bool true with Composer 2.2+, false otherwise Disables the plugin when false
extra.substitution.mapping yes object empty object Mapping between placeholders (the keys) and substitution rules (the values). There is no restriction with the placeholders format.
extra.substitution.mapping.*.type yes string n/a Substitution type (see the related section below)
extra.substitution.mapping.*.value yes string n/a Substitution value (depends on the type)
extra.substitution.mapping.*.cached no bool false Indicates whether the value provided after the first substitution must be cached
extra.substitution.mapping.*.escape no string null Escaping function that will receive the substitute value as argument
extra.substitution.priority no integer 0 Plugin's event handler priority (see Composer documentation)
โš ๏ธ From Composer 2.2+ you'll have to configure your composer.json file to allow the plugin to run

Example:

{
    "config": {
        "allow-plugins": {
            "villfa/composer-substitution-plugin": true
        }
    }
}

You can just execute this command:

composer config allow-plugins.villfa/composer-substitution-plugin true

For more details, see https://getcomposer.org/doc/06-config.md#allow-plugins

Substitution types

For each type of substitution the value replacing the placeholder comes from a different source.

  • literal: The value in configuration is used directly.
  • callback: The value is the string returned by a callback.
  • include: The value is the string returned by a PHP file.
  • env: The value is an ENV variable.
  • constant: The value comes from a constant or a class constant.
  • process: The value is the output of the processed command.

Real-life examples

This library defines a Composer script which uses PHP_CodeSniffer this way:

"scripts": {
    "phpcs": "phpcs --standard=PSR12 --parallel=$(nproc) src/ tests/",

Unfortunately it is not cross-platform because of the usage of nproc.

This is solved by the substitution plugin in combination with Linfo (See also the tiny script nproc.php). Here how it is configured:

"extra": {
    "substitution": {
        "enable": true,
        "mapping": {
            "$(nproc)": {
                "cached": true,
                "type": "include",
                "value": "./scripts/nproc.php"
            }
        }
    }
}

So now it also works on Windows without even touching the scripts section.

composer-substitution-plugin's People

Contributors

szepeviktor avatar villfa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

composer-substitution-plugin's Issues

Replace placeholders from cli arguments

Hi,

I'm interested in replacing placeholders with arguments that are passed with the Composer command.

Here is a simple example:

"scripts": {
    "welcome": "echo 'Hi {MY_NAME}, the database is {DB_STATUS}.'"
}
"extra": {
    "substitution": {
        "enable": true,
        "mapping": {
            "{MY_NAME}": {
                "type": "cli-argument",
                "value": "$1"
            },
            "{DB_STATUS}": {
                "type": "cli-argument",
                "value": "$2",
            }
        }
    }
}
$ composer run-script welcome "John Doe" "OK"
Hi John Doe, the database is OK.

Is this a feature that could be added?

"process" substitution executes command immediately after substitution

The command is getting executed before continuing after the substitution. So, if you use the substitution in the middle and have other options after the substitution, the post-substitution options/flags/arguments won't be read.

// composer.json
{
  "scripts": {
    "bash": [
      "docker exec -ti {CONTAINER} bash"
    ]
  },
    "substitution": {
      "enable": true,
      "mapping": {
        "{CONTAINER}": {
          "type": "process",
          "value": "docker ps -aqf ancestor=special-tag"
        }
      }
    }
}

results in

ivr (git)-[1337-ivr-tests]- % composer bash
> docker exec -ti ae055d08efb2
 bash
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.