0

Sorry for the title, I really can't explain better. I'm learning shell programming.

I'm trying to detect if Composer has a script named "post-install-cmd". To do that, one can call composer run-script --list (documentation). So i pipe that command to grep -q inside a conditional.

When I run this inside a Docker layer (distribution is Debian-based), i only get an error when calling composer run-script --list inside the conditional:

RUN set -eux; \
    if [ -f composer.json ]; then \
        composer dump-autoload --no-dev --classmap-authoritative; \
        # This is working fine, a list is returned (see log)
        composer run-script --list; \
        # This gives me an error (Unable to write output)
        if composer run-script --list | grep -q post-install-cmd; then \
            echo "Script was found!"; \
        fi; \
    fi

Log:

#41 [myproject php_prod  9/10] RUN set -eux;     if [ -f composer.json ]; then         composer dump-autoload --no-dev --classmap-authoritative;         composer run-script --list;         if composer run-script --list | grep -q post-install-cmd; then             echo "Script was found!";         fi;     fi
#41 0.441 + [ -f composer.json ]
#41 0.445 + composer dump-autoload --no-dev --classmap-authoritative
#41 0.835 Generating optimized autoload files (authoritative)
#41 0.878 Generated optimized autoload files (authoritative) containing 40 classes
#41 0.892 + composer run-script --list
#41 1.164 scripts:
#41 1.168   auto-scripts      Runs the auto-scripts script as defined in composer.json.  
#41 1.169   post-install-cmd                                                             
#41 1.169   post-update-cmd                                                              
#41 1.183 + grep -q post-install-cmd
#41 1.188 + composer run-script --list
#41 1.468 scripts:
#41 1.471 
#41 1.479                                                           
#41 1.480   [Symfony\Component\Console\Exception\RuntimeException]  
#41 1.481   Unable to write output.                                 
#41 1.481                                                           
#41 1.481 
#41 1.482 run-script [--timeout TIMEOUT] [--dev] [--no-dev] [-l|--list] [--] [<script>] [<args>]...
#41 1.482 
#41 1.495 + echo Script was found!
#41 1.497 Script was found!
#41 DONE 1.6s
gremo
  • 339
  • 1
  • 4
  • 20

2 Answers2

1

You can try disabling the usage of ANSI escape codes for output formatting by adding the --no-ansi option to the composer run-script --list command. This can occasionally fix output-related problems in terminal environments that don't support them.

RUN set -eux; \
    if [ -f composer.json ]; then \
        composer dump-autoload --no-dev --classmap-authoritative; \
        composer run-script --list; \
        if composer run-script --list --no-ansi | grep -q post-install-cmd; then \
            echo "Script was found!"; \
        fi; \
    fi

Or Alternatively:

docker run -t my_image
Hawshemi
  • 302
  • 5
0

I also got the command to work in a build step by running:

echo "$(composer show)" | grep myvendor/mypackage

Speech marks " is to avoid field splitting (puts the command all in on one line, which is not what you want). Credit: https://unix.stackexchange.com/a/170726/177624

More info in field splitting @see https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05