BenMorel
BenMorel

Reputation: 36642

How to ignore directories with Composer?

I'd like to release a PHP library and submit it on Packagist to have it installable via Composer.

My library has the following structure:

lib/
tests/
composer.json
README.md

Basically, whenever I include this library in a project's composer.json, I'd like everything to be copied with the exception of the tests directory, which is cumbersome and is only needed when developing the library itself. It's just a waste of space otherwise (especially when packaging the project for production).

Is it possible to exclude this directory from the library's composer.json?

Upvotes: 39

Views: 23491

Answers (4)

AndreyP
AndreyP

Reputation: 2698

This can be automated with post-update-cmd the composer.json file within the scripts key:

"scripts": {
    "post-update-cmd": [
        "rm -rf vendor/aura/intl/tests vendor/cakephp/cakephp/tests"
    ],
},

Or use pattern to remove directories:

"scripts": {
    "post-update-cmd": [
        "find vendor/ -type d -regextype posix-extended -iregex '.*/(doc|docs|example|examples|test|tests|tmp)' -print -exec rm -r {} +"
    ],
},

Upvotes: 2

Sven
Sven

Reputation: 70933

It's possible to control the archive creation by adding exclude patterns in the composer.json file within the archive key. See https://getcomposer.org/doc/04-schema.md#archive for details.

The example given (cited from above URL):

{
    "archive": {
        "exclude": ["/foo/bar", "baz", "/*.test", "!/foo/bar/baz"]
    }
}

The example will include /dir/foo/bar/file, /foo/bar/baz, /file.php, /foo/my.test but it will exclude /foo/bar/any, /foo/baz, and /my.test.

That way you have about the same control that .gitattributes would give you, without having to use Git or affecting any processes that require different settings in said file.

Upvotes: 25

Motin
Motin

Reputation: 5063

This has been possible since Nov 11, 2015 with https://getcomposer.org/doc/04-schema.md#exclude-files-from-classmaps

Source: https://github.com/composer/composer/issues/4456#issuecomment-155825777

EDIT: Misinterpretation. The above only lets the autoloader ignore the specified paths, it does not actually prevent them from being copied to the filesystem upon install.

Upvotes: 2

Wouter J
Wouter J

Reputation: 41954

This is not possible in Composer. However, there are some ways to do it:

  • When you run the update or install command with --prefer-dist, Composer tries to download the archive on github. You can remove the test directory from the archives by putting this in a .gitattributes file in the root directory of your project:

    Tests/ export-ignore
    
  • Composer will only use the tags on github. Just temporary remove the tests directory when creating a tag will also do the trick.

Upvotes: 42

Related Questions