How do I use a Git submodule with a Composer loaded library? -


i have zend framework 2 application. contains library code containing business logic , other utilities common other applications created later.

my intention share across projects using composer. question is, how do , streamline development? need make changes , additions library, within other project.

i tried setting vendor/stuff git submodule containing package needed, , referencing in primary composer.json (ref):

"repositories": [     {         "type": "git",         "url": "vendor/stuff"     } ], "require": {     "stuff/library": "master" }, 

composer isn't able load in way. complains package not found, presumably because it's ignoring fact url both local , relative. technically, doesn't need to; vendor/stuff folder initialised separately through git submodule commands.

unfortunately* composer doesn't support git submodules, main aim of composer provide similar inter-project dependency functionality , pointless try replicate submodules in composer.

i have same problem trying solve, of developing library while simultaneously developing application uses library. there couple of ways solve problem using composer.

make symbolic link library directory

this quickest , dirtiest way of doing it. composer update create appropriate directory library in vendors directory, replace symbolic link directory contains library.

obviously not great can accidentally overwrite code may have edited running composer update.

use composer prefer source option

composer has option download source via git clone (--prefer-src) rather downloading zipball (--prefer-dist) default. allows edit source code inside vendors directory , commit through git.

e.g. have project requires amongst other libraries symfony/yaml want fix bug in. is:

  1. composer update - download of dependencies of project.

  2. composer update symfony/yaml --prefer-source - update symfony/yaml directory in vendors directory.

  3. fix bug , commit through git.

use composer local repository

the way ---actually--- used work when i'm developing project , it's requirement in tandem, use composers ability set explicitly set repository use resolve dependencies. e.g. if code in:

/projects/library/ /projects/project/ 

in composer file project add repository entry:

"repositories": [     {         "type": "vcs",         "url": "/projects/library/"     } ] 

running composer update @ git entries in /projects/library/ resolve dependencies on library in preference listed in packagist or other repository.

this mean when want test change in library code need to:

  1. commit it, has git entry.

  2. run composer update in project directory latest version.

but avoid having push commit external repository, means aren't pushing code may not work, , means can work offline git commits don't require internet connection.


although apparently best way of working, it's still kind of dangerous easy accidentally check in version of composer.json has references local directories, break project else.

to avoid made couple of small scripts i) backup real composer.json file, ii) add in local repositories, iii) run composer update iv) restore real composer.json file.

localupdate.sh

cp -f composer.json composer.json.bak php composerlocal.php composer update cp -f composer.json.bak composer.json 

composerlocal.php

<?php  $srcfile = file_get_contents("composer.json"); $hackfile = file_get_contents("composer.local"); $finalstring = str_replace('"localhack",', $hackfile, $srcfile); file_put_contents("composer.json", $finalstring);  ?> 

composer.local

"localhack",  "repositories": [     {         "type": "vcs",         "url": "/projects/library1"     },     {         "type": "vcs",         "url": "/projects/library2"     }    ], 

and place "//": "localhack", somewhere in projects composer.json file. running localupdate.sh safely composer update against local repositories without chance of committing bad version of composer.json.

just clone git

this how tend work now:

i) composer update in project ii) go vendors directory , delete library want developing @ same time. iii) git clone whichever repo developing library in, appropriate vendors directory.

composer understands git repos, won't overwrite git cloned directory(, though seem little confused editing composer.json of library).

doing git clone yourself, gives complete control on gets installed, , allows install repo composer doesn't know about, or untagged version, without having edit composer.json in project.

that's key feature of doing git clone yourself; not touching composer.json of project, it's safe, no possibility of checking in composer.json has been modified use local/custom repos.

  • edit - 6th sept 2014

the validation composer.json files has been tightened , no longer possible have "//": "localhack" entry in file. reason why composer guys not having versioning composer project nuts.

* think git submodules dumb, dumb, dumb implementation 'solve' difficult problem in way causes more problems down road, , composer not supporting them way more 'fortunate' 'unfortunate'. other people use them, , happy them, opinion, man, if you're using composer shouldn't have need submodules.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -