zozo
zozo

Reputation: 8602

composer - dynamically set parameters variable

I have the following setup:

What I need to do is set a variable in parameters.yml with the timestamp when composer was ran.

For this I tried the following solution:

parameters.yml.dist

   [bla bla bla]
   ran_timestamp: ~


composer.json
   [bla bla bla]
   "scripts": {
       "pre-install-cmd": [
          "export SYMFONY_APP_DATE=$(date +\"%s\")"
       ],
   }
   "extra": {
       "incenteev-parameters": {
          "file": "app/config/parameters.yml",
          "env-map": {              
            "ran_timestamp": "SYMFONY_APP_DATE"
          }
       }
   }

The part where the variable is set inside parameters.yml works fine (the parameter is created with the value from SYMFONY_APP_DATE env variable).

The problem is that the env variable is not updated when composer is ran. Can anyone help me with that pls?

Additional info:

$composer install

export SYMFONY_APP_DATE=$(date +"%s")

Loading composer repositories with package information [bla bla bla]

Upvotes: 5

Views: 4617

Answers (1)

Dmitry Malyshenko
Dmitry Malyshenko

Reputation: 3051

The problem apparently is that you're setting env parameter in child process (which is created for each script), but it's not possible to redefine env parameter for parent process from child (i.e. to set env value for composer itself from one of its scripts)

I think you need to extend \Incenteev\ParameterHandler\ScriptHandler::buildParameters to make it happen.

UPD: I've found a way to make it happen

Define a special block only for build-params in composer.json

"scripts": { "build-params": [ "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters" ],

and than in post-install-cmd block instead of Incenteev\\ParameterHandler\\ScriptHandler::buildParameters make it

"export SYMFONY_APP_DATE=$(date +\"%s\") && composer run-script build-params"

That will create env var and building parameteres in same process

Upvotes: 7

Related Questions