Composer windows compatible scripts Composer windows compatible scripts windows windows

Composer windows compatible scripts


Just use scripts without specifying vendor/bin path, it will be done by composer automatically, see the Note section in https://getcomposer.org/doc/articles/scripts.md#writing-custom-commands

"scripts": {    "document": "apigen generate -s ./src -d ./docs",    "codecept": "codecept",    "test": "codecept run",    "lint": "phpcs --standard=PSR2 src",    "bootstrap": "composer install && composer codecept bootstrap"}


I was thinking at the same problem, there may be multiple solutions.

Option 1, no shell script, pure PHP only

The shell script interpreter differs between your platforms, and it's quite hard to make something "universal" enough.

But you may use only pure PHP. Composer documents this quite well. Your PHP should be able to adapt easily things for the Windows case or for the POSIX case.

However you may not like wrapping external programs call and shell commands inside a PHP.

Option 2, run the scripts from a Linux VM

You would for example launch the Linux VM defined by a Vagrant file, and then run composer trough "vagrant ssh".

Pros:

  • Same shell for everyone

  • Defining correctly a Vagrant environment may simplify a lot of local tests by developers, removing the well-known "this works on my machine". Every developer has the "same" local environment.

Cons:

  • Some initial setup (including the two-side sync of the project folder with appropriate permissions), lot of examples online may help.

  • Running a VM locally requires enough RAM.

Option 3, use another shell than cmd on Windows

Don't use the standard shell to launch composer. There are plenty of other options, including:

  • cygwin (I wouldn't use it if I may avoid however)

  • git bash (something like MINGW32 that may be installed easily with the git client, using git is not mandatory)


I've created a composer package, which converts slashes to backslashes, if executed on Windows OS.

In your composer.json you just need to add:

{  "require": {    "instituteweb/composer-scripts": "^1.0",  },  "scripts": {    "test": [      "\\InstituteWeb\\ComposerScripts\\ImprovedScriptExecution::apply",      "vendor/bin/whatever"    ]  }}

So when you perform composer run test, it will work on Unix and Windows OS.

How it works:

The apply method in script queue, stops propagation of the original "test" command. Instead, it creates and executes a new command called "_test" (prepends underscore) which contains the original commands, just with converted slashes.

Just commands after the apply method call are affected.

Link to package: https://packagist.org/packages/instituteweb/composer-scripts