How can I require Gulp in a composer.json file? The composer (php>=7.0) should install gulp and gulp-sass (and probably npm) for a developer who has not installed them. The possible solution should be the code to add in a composer.json file. I want to know how developers handle this situation. How do they create a composer-ready development project?
Asset Packagist might be the solution : https://asset-packagist.org/site/about
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
"require": {
"npm-asset/gulp": "^4.0.2",
"npm-asset/gulp-sass": "^4.1.0"
}
NPM has it's own file packages.json to hold dependencies. When you install a new project you have to run npm install and all packages will be installed on the system in the node_modules folder. But first you have to install npm in your own. Every system has it's own package manager to install npm and most people download it from the website to get the latest version.
I think it's not really common to run everything from your composer file. I think it's possible but not really common.
https://nodesource.com/blog/an-absolute-beginners-guide-to-using-npm/
Related
While refactoring legacy code, I try to work on a composer package while at the same time work on a project which uses this package.
Composer allows me to add local path repositories for retrieving the package in-development, and symlinking this package into my project.
<composer.json excerpt>
"repositories": [
{
"type": "path",
"url": "../my-package/",
"options": {
"symlink": true
}
}
],
...
"require": {
"my/package": "#dev"
}
I then do
composer update my/package --prefer-source
Which symlinks just fine. However, when building my project on a CI server, I want the project to retrieve the package from a remote git repository, this is why I added the vcs section to my composer.json.
{
"type": "vcs",
"url": "git#bitbucket.org:my/package.git"
}
However, during building via composer install it still tries to retrieve the package locally, which is not available on the CI server of course. I guess because my composer.lock explicitly says that the package is retrieved from a path.
How can I make it work smoothly, both locally and on the CI server? I seem to lack a decent workflow.
What I tried so far:
adding my/package again as a dev-package, but apparently the composer.json will remove if from the no-dev packages automatically then. Also, I do not know how to tell composer to use the path repository for the dev requirement, and the vcs repository for the no-dev requirement.
After tons of hours searching for a suitable workflow, I found out one. Hope it can help you.
Since repository-dev (like require-dev for repository) doesn't exist and will not exist soon (see this), we need to make two composer.json files. Let's say we call the second one composer-dev.json. I think you should commit both and having both up-to-date. To tell composer to use composer-dev.json, you need to put COMPOSER=composer-dev.json in front of every composer command. To illustrate, see this :
composer.json
{
"repositories":[
{
"type": "vcs",
"url": "{repo}"
}
],
"require": {
"vendor/package": "{version}",
}
}
composer-dev.json
{
"repositories":[
{
"type": "path",
"url": "path/to/your/package",
"options": {
"symlink": true
}
}
],
"require": {
"vendor/package" : "{version}",
}
}
As you can see, the composer.json contains the 'vcs' repository and the composer-dev.json contains the 'path' repository.
To initialise your application and start developing :
COMPOSER=composer-dev.json composer update
Composer created the vendor directory and symlink your package folder to vendor/package. It also created a composer-dev.lock file which you should commit for deployment.
To install new package :
COMPOSER=composer-dev.json composer require vendor/package
Remember that composer.json have to be up-to-date, so you have to put all new lines in it.
To build your application :
COMPOSER=composer-dev.json composer install
Which should throw you :
[RuntimeException]
Source path "path/to/your/package" is not found for package vendor/package
Now you can run :
composer update --no-dev vendor/package
or if you need specific version :
composer update --no-dev vendor/package:{version}
Notice that there is no COMPOSER=composer-dev.json in front of the last command since we are using composer.json in order to use our vcs repository. This last command will also install all missing packages.
I hope it was usefull !
How do I install a composer package with dev dependencies?
So for example:
When I have this package:
https://github.com/spatie/laravel-demo-mode
And I run:
composer require spatie/laravel-demo-mode
The tests folder is not installed?!
In short you don't (ever). If you want to contribute, you need to set up new Laravel project and set up composer to autoload your version of package, either from your fork or from somewhere on your disk. And when you do that you are just using your version of the package (again without the possibility to run packages's tests).
In order to run tests of the package you need to change directory to the root of the package and install its dependencies ($ composer install), after you've done that you may run $ phpunit.
What I am usually doing in when I want to contribute is:
have empty Laravel project ready (unversioned)
have packages folder in root
in that packages folder I usually do $ git clone <repo fork> (It may be more than one package at once)
in case I want to run package's tests I do $ composer install and $ phpunit (your IDE may squeak at you about duplicate definitions but you may ignore it)
improve and test the code of package in packages folder
test your changes "live" on Laravel project right-away
composer.json may look like:
...
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/",
"Vendor\\Package\\": "packages/path-to-src/"
}
},
...
You may find useful this example repo I use https://github.com/Kyslik/column-sortable-example to demonstrate how to work with package I maintain. Mainly take a look at folder structure and composer.json.
There may be better ways on how to do contributing but I have only found (and developed) this one.
If I have small changes in mind (like typo, or just return type or whatever is "small") I do change package within vendor folder, and see if it breaks anything; after that I fork package source and edit the change right in the Github's web interface. To clean up just run $ composer install/update from root folder of your Laravel project.
I know that I can use composer update vendor/package but here's my case.
Composer is very slow when updating, I have around 6 packages installed and one local vcs package being loaded from a local folder. When I run composer update even for that specific local package, composer connects to Packagist to look for other updates and this process is very slow, I don't know if it's my computer or my internet. Is there a way I can tell composer to just update the package from local folder when I run composer update local/package without contacting Packagist and running through all the heavy json files it downloads?
Note:
I know how to load a local composer package. It's loading perfectly, it's just that I'm looking for a way to tell composer just to load the local package without contacting Packagist.
"repositories": [
{
"type": "vcs",
"url": "../local/package"
}
],
My problem is that it's slow to contact Packagist. Running composer update local/package -vvv Shows that it still downloads json files from packagist even if it's told to update just local/package.
There are multiple ways of speeding the Composer fetch up:
define a custom repo, which points to a local path and install with --prefer-source
"repositories": [
{
"type":"vcs",
"url":"/path/to/your/local/package/packageA"
}
],
"require":{
"package/packageA" : "dev-master"
}
Follow-up trick: if you exactly know the type of the repo, then specify it!
In other words: do not use "type":"vcs" if you can specify "type":"git" or "type":"svn". Composer will skip running through all the repo adapters to figure the correct one out.
you could setup Satis and define only the required packages for your project and their dependencies ("require-dependencies": "true"). This acts as a package proxy in front of packagist. You would simply fetch from the local Satis mirror/proxy.
Give this a try, to disable the default Packagist repository:
{
"repositories": [
{
"packagist": false
}
]
}
When I run
$ composer.phar install
where do the packages that get installed come from?
I understand that Packagist is the default repository for PHP packages, and that lacking a different package in composer.json, this is where composer will look for packages.
However, what I'm not clear on is how Composer and Packagist interact.
Does Composer download files directly from packagist.org
Or does Composer get a git/svn/hg repository link from packagist and download the files from the repository directly?
Or something else?
It depends on the contents of your composer.json file.
For example, if your composer.json contained simply
{
"require": {
"phpunit/phpunit": "3.8.*#dev"
}
}
then composer searches packagist, and finds phpunit here:
https://packagist.org/packages/phpunit/phpunit
which tells composer to load phpunit from here:
https://github.com/sebastianbergmann/phpunit.git
If instead your composer.json contained
{
"repositories": [
{
"type": "vcs",
"url": "http://github.com/sebastianbergmann/phpunit"
}
],
"require": {
"phpunit/phpunit": "3.8.*#dev"
}
}
then composer will not look to packagist, but go directly to github to download the repo.
The packages registered on Packagist are usually the "authoritative" version of the package (not a fork), but I have found several instances where this is NOT the case, so you should check it to be sure you are pulling the package you expect.
Packagist.org offers users to register their software there by pointing Packagist to read their composer.json file that is published somewhere on the web.
The usual case would be some of the common open source hosters like github, which makes it really easy because composer can deal with such a git repo right away. You could however host your own git or svn or hg repository, or even just publish ready-made ZIP or TGZ files for each software version.
Composer downloads directly from the source, e.g. Packagist only knows those source and tells your composer instance where to go. It does this by downloading a bunch of json files from Packagist.org that have all the infos. This is way easier than to find out where the libraries that you want are hosted and add this info as a repository entry into your local composer.json file. :)
I see there is already a question but it did not answer the question
How can I install a composer package into the /src dir?
How can I install a bundle in the /src directory?
Reason I would like to do this is for development and deployment, so
I don't have to check in Symfony's base code into my subversion repo
I could use Composer to deploy
Looking over the Composer docs some more I did come across this:
http://getcomposer.org/doc/04-schema.md#config
vendor-dir: Defaults to vendor. You can install dependencies into a
different directory if you want to.
Could I set this at a Bundle level? or is this for the overall install?
https://github.com/composer/composer/blob/master/res/composer-schema.json
I know this is late, but in case anyone is searching for an answer that I painstakingly (hours and hours) found: vendor-dir
The documentation says:
By setting this var you can make composer install the dependencies into a directory other than vendor
Example:
{
"config": {
"vendor-dir": "website/password/vendor/"
}
}
From this doc and this doc
Again, hope to save anyone else a couple hours.
{
"extra": {
"installer-paths": {
"sites/example.com/modules/{$name}": ["vendor/package"]
}
}
}
Read more.
If you find composer's custom installers too complex or rigid, and you can plan what types of systems you will be deploying to, you might consider using post-install scripts.
Here's an example that creates a symlink from a package installed under vendors to the location where it might be expected:
"scripts": {
"post-install-cmd": [
"test -d vendor/foo/bar && ln -s ../vendor/foo/bar lib/bar"
]
}
This will create a symlink at lib/bar/ pointing to vendor/foo/bar/.
I have implemented this composer plugin to install packages into user (custom) defined folders you can just include it in your composer.json, follow the example and tell me if you have more questions :)
https://github.com/mnsami/composer-custom-directory-installer
composer-custom-directory-installer
A composer plugin, to install differenty types of composer packages in custom directories outside the default composer default installation path which is in the vendor folder.
This is not another composer-installer library for supporting non-composer package types i.e. application .. etc. This is only to add the flexability of installing composer packages outside the vendor folder. This package only supports composer package types,
https://getcomposer.org/doc/04-schema.md#type
The type of the package. It defaults to library.
Package types are used for custom installation logic. If you have a package that needs some special logic, you can define a custom type. This could be a symfony-bundle, a wordpress-plugin or a typo3-module. These types will all be specific to certain projects, and they will need to provide an installer capable of installing packages of that type.
How to use
Include the composer plugin into your composer.json require section::
"require":{
"php": ">=5.3",
"mnsami/composer-custom-directory-installer": "1.1.*",
"monolog/monolog": "*"
}
In the extra section define the custom directory you want to the package to be installed in::
"extra":{
"installer-paths":{
"./monolog/": ["monolog/monolog"]
}
by adding the installer-paths part, you are telling composer to install the monolog package inside the monolog folder in your root directory.
As an added new feature, we have added more flexibility in defining your download directory same like the composer/installers, in other words you can use variables like {$vendor} and {$name} in your installer-path section:
"extra": {
"installer-paths": {
"./customlibs/{$vendor}/db/{$name}": ["doctrine/orm"]
}
}
the above will manage to install the doctrine/orm package in the root folder of your project, under customlibs.
Note
Composer type: project is not supported in this installer, as packages with type project only make sense to be used with application shells like symfony/framework-standard-edition, to be required by another package.