16
votes

I'm getting the following error when trying to run a bash script in Composer post install/update hooks:

> post-install.sh
sh: 1: post-install.sh: not found
Script post-install.sh handling the post-install-cmd event returned with an error



  [RuntimeException]
  Error Output: sh: 1: post-install.sh: not found

Original composer.json

Works but it's just annoying to update the post install/update commands to run in two places.

{
  "require": {
    "twbs/bootstrap": "3.3.5"
    ...
    ...
  },
  "scripts": {
    "post-install-cmd": [
      "mkdir -p _libraries",
      "cp -r vendor/twbs/bootstrap/dist _libraries/bootstrap/",
      ...
      ...
    ],
    "post-update-cmd": [
      "mkdir -p _libraries",
      "cp -r vendor/twbs/bootstrap/dist _libraries/bootstrap/",
      ...
      ...
    ]
  }
}

According to the Composer documentation:

A script, in Composer's terms, can either be a PHP callback (defined as a static method) or any command-line executable command.

So my composer.json should be able to work as such:

Wanted composer.json

{
  "require": {
    "twbs/bootstrap": "3.3.5"
    ...
    ...
  },
  "scripts": {
    "post-install-cmd": [
      "post-install.sh"
    ],
    "post-update-cmd": [
      "post-install.sh"
    ]
  }
}

post-install.sh

Executable by everyone (0775) and located in the same directory as the composer.json

#!/bin/bash

mkdir -p _libraries
cp -r vendor/twbs/bootstrap/dist _libraries/bootstrap/
...
...
2
did you make post-install.sh executable? (e.g. chmod 0755 post-install.sh) You say it is, but I just thought I would confirm. Also, how is it being executed by Composer? It is calling it by some php call? - David C. Rankin
@DavidC.Rankin Yes it's executable. The bash script is not being executed by Composer. (I'm not to sure what you're asking here) - grim
How should composer find that script? - hek2mgl
@hek2mgl I wouldn't be asking the question if I knew, but I assume it does so by putting the script in the same directory as composer.phar and composer.json and defining the script under scripts in composer.json... - grim
Does it work when you use sh post-install.sh ? However, I guess it should be even sh vendor/you/yourproject/post-install.sh. Have no test setup by the hand - hek2mgl

2 Answers

21
votes

Other way to achieve single task definition is referencing scripts:

{
  "require": {
    "twbs/bootstrap": "3.3.5"
    ...
  },
  "scripts": {
    "your-cmd": [
      "mkdir -p _libraries",
      "cp -r vendor/twbs/bootstrap/dist _libraries/bootstrap/",
      ...
    ],
    "post-install-cmd": [
      "@your-cmd",
      ...
    ],
    "post-update-cmd": [
      "@your-cmd",
      ...
    ]
  }
}
13
votes

In comments I suggested to use

bash post-install.sh

This seems working.