Installing A Custom Composer Package from GitLab - php

I've created a simple test wordpress plugin. The plugin consists of a single php file (kreplach.php) and a composer.json:
kreplach.php
<?php
/*
Plugin Name: kreplach
Plugin URI: http://gitlab.example.com/sales/kreplach
Description: just a test
Author: Foo Bartok
Version: 1.0
Author URI: http://example.com
*/
?>
composer.json
{
"license": "MIT",
"name": "sales/kreplach",
"type": "wordpress-plugin",
"description": "just a test",
"authors": [
{
"name": "Foo Bartok",
"email": "foo#example.com",
"homepage": "example.com"
}
],
"require": {
"composer/installers": "*"
}
}
On my dev server I have the following composer.json
Server's composer.json
{
"repositories": [
{
"type": "composer",
"url": "https://wpackagist.org"
},
{
"type": "vcs",
"url": "git#gitlab.example.com:sales/kreplach.git"
}
],
"require": {
"php": ">=5.4",
"wpackagist-plugin/akismet": "*",
"wpackagist-plugin/contact-form-7": "*",
"wpackagist-plugin/wordpress-importer": "*",
"sales/kreplach": "master",
"johnpbloch/wordpress": "4.*",
"composer/installers": "*"
},
"extra": {
"wordpress-install-dir": "wp"
}
}
What I think SHOULD happen:
Composer looks through the git repo for composer.json
Composer matches the name "sales/kreplach" found in the build host's composer.json
Composer copies the contents of the master branch into wp-content/plugins/kreplach on my build host.
My fake plug-in does nothing, as designed.
What actually happens:
Bitter, bitter failure.
Loading composer repositories with package information Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested package sales/kreplach could not be found in any version, there may be a typo in the package name.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.
Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.
I have tested cloning the sales/kreplach repo onto the same host I'm attempting to install the plugin on.
To make sure that composer is actually reading the composer.json file from the git repo, I introduced a typo (yeah, totally on purpose, like) which threw a "hey, this json file is broken, Foam Head" error.
My version of gitlab is omnibus edition 8.6.4 (installed today).
I have successfully done this same sort of trick with pip/requirements.txt to install custom python modules, so I'm not unused to following directions. Am I missing a step, or some sort of non-obvious (to me at least) nomenclature?

Update 2021, 5 years later, since GitLab 13.3 (Aug. 2020), GitLab (even in its free edition) has a Package Registry, establishing GitLab as a private repository.
You can publish in it Composer packages, and, with GitLab 13.11 (April 2021):
Use Composer v2 with the GitLab Package Registry
You use Composer to publish, share, and download your PHP dependencies to your GitLab Project. Six months ago, a new major version (v2) of Composer was launched with a variety of changes, including significant performance improvements, architectural updates, and runtime features.
You can read more about the changes here.
Until recently, you couldn’t take advantage of these improvements because the GitLab registry didn’t support Composer v2.
This prevented some of you from using the GitLab registry at all.
As an MVC, we focused on adding support for the mandatory parameter metadata-URL. We added a new endpoint GET group/:id/-/packages/composer/p2/:package_name, which returns the metadata for all packages in your repository.
When Composer looks for a package, it replaces %package% with the package name and fetches that URL.
This means we’ve added a new endpoint GET group/:id/-/packages/composer/p2/:package_name which will return the metadata for all packages in your repository.
Please note that there are two parameters considered optional.
We have issues open to add support for the providers-api and list-api parameters. We hope to prioritize them in an upcoming milestone.
See Documentation and Issue.
And (still GitLab 13.11, April 2021):
Download Composer dependencies from version control
You have two options when downloading Composer dependencies: source or dist. For stable versions, Composer uses dist by default and downloads the dependency as a zip file.
However, you can also download it directly from version control.
If --prefer-source is enabled, Composer downloads your dependency as a Git clone instead of as a packaged zip file.
This is useful if you want to make a bug fix for a project and get a local Git clone of the dependency directly.
Until recently, you could not use the prefer-source and related preferred-install commands and configurations when downloading Composer dependencies.
This prevented many of you from using the GitLab Package Registry for your Composer dependencies.
We are happy to announce that you can now download your Composer dependencies from source.
Do so by simply adding the prefer-source option to your install command like this: composer update --prefer-source.
See Documentation and Issue.

I've not used gitlab but have used this method quite a bit with Bitbucket and GitHub.
You need to specify "sales/kreplach": "dev-master" in the server's composer.json - note that the branch name must be prefixed with "dev-".
Apparently Composer has special support for GitHub and BitBucket, which may not exist for Gitlab...so you may need to specify git as the repository type rather than vcs.
Good luck!
Refs:
https://lornajane.net/posts/2014/use-a-github-branch-as-a-composer-dependency
https://getcomposer.org/doc/05-repositories.md#vcs
https://getcomposer.org/doc/05-repositories.md#git-alternatives

Related

How I can specify a branch of my library's repo generated by git-flow as composer requirement

In my team's projects need a common library where I place common logic in it. Therefore, according to teams flow I cloned the library repo (assuming hosted upon myteam/mylibrary.git repo in github):
git clone git#github.com:myteam/mylibrary.git
And in order to apply my changes on the library I run the following command cocktail:
git flow feature start mychanges
# Developing changeds
git commit -m "Applying my changed"
git push origin feature/mychanges
Then I need to test the library changes on each project that will consume the library's change. Therefore I tried the to change the following on composer.json:
{
"require":{
...,
'myteam/mylibrary':'dev-feature/mychanges'
},
"repositories": [
{
"type": "vcs",
"url": "git#github.com:myteam/mylibrary.git"
}
]
}
And then I run the following command:
composer update myteam/mylibrary
But I got the following error:
Problem 1
- The requested package myteam/mylibrary could not be found in any version, there may be a typo in the package name.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.
- It's a private package and you forgot to add a custom repository to find it
Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.
Do you know how I can fix that?
Same result I get if I change my composer.json into:
{
"require":{
...,
'myteam/mylibrary':'dev-master'
},
"repositories": [
{
"type": "vcs",
"url": "git#github.com:myteam/mylibrary.git"
}
]
}

Composer throws error "Could not find package with stability stable."

I have tried to publish a project with composer. The project resides at github, and are published through packagist.org.
But when I try to create my project with composer it fails with this error message:
"Could not find package madskullcreations/simplicity with stability
stable."
I use the following command:
composer create-project madskullcreations/simplicity
The composer.json contains this:
{
"name": "madskullcreations/simplicity",
"description": "Website made simple!",
"homepage": "https://madskullcreations.com",
"type": "project",
"license": "GPL-3.0-or-later",
"minimum-stability": "dev",
"require": {
"php": ">=5.6",
"cakephp/cakephp": "3.5.*"
}
}
My repository contains just one file for testing. What is wrong? I tried to remove the dependencies, the entire "require"-block, but no real change...
Beginner as I am, I don't even know where I would define the packages "stability", can't find anything at github or at packagist.
Please help me get this started!
Working solution:
I eventually got it working with the help from Flying, see his answer further down. Since I think it is a wee bit complicated to get composer up'n working, I try to put the steps I did to get it working here:
Create a repository at github.
Create a composer.json with your depencencies. Check it in.
Release it. There are a "Releases" link somewhere, use it and give the release a name.
Now, to skip the packagist.org step during your testing, follow these steps. It is not good style to publish a non-working solution (like I did) on packagist.org, and it's no fun at all to do all the steps necessary ten times over.
Create a local folder somewhere, and create a new composer.json file there.
Put something like this in it:
{
"require": {
"madskullcreations/simplicity":"dev-master#dev"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/madskullcreations/simplicity"
}
]
}
Run the following command in your new folder:
composer create-project
It should now download and install your project.
And, read the error messages given by composer and make sure you understand them. They are useful. My headache was a missing PHP-extension (intl) and that I assumed it was using php version 7.1, while it actually listened to my requirement in the composer.json file, and used v5.6. (I have several php-versions installed in iis, but my fuzzy head did not consider that.)
Packages stability requirement is defined into minimum-stability setting of composer.json of your project, not a composer.json of the external package.
Your madskullcreations/simplicity package have no releases defined so the only branch that is available in it - is dev-master "release" (it can be seen at the right side of package page on Packagist). This "release" have "dev" stability level.
Because of above if you're requiring this package into your project without either setting minimum-stability: dev or without specifying stability requirement for a package as
"require": {
"madskullcreations/simplicity":"dev-master#dev"
}
(notice #dev into version requirement) it is correct behavior of Composer to complain about lack of compatible releases.
Also it is generally bad practice to publish your test packages into public registry like Packagist. Instead you should use direct repository specification into your composer.json as explained here. In your case it will be:
"repositories": [
{
"type": "vcs",
"url": "https://github.com/madskullcreations/simplicity"
}
]
After specifying direct repository reference - it will be safe to remove your test package from Packagist unless you're really want to share it with open source community.

Composer cannot find the package for new require within Yii2 composer.json

Ok, I'm using Yii2 and I am trying to add a new requirement/library to the project. Said library can be found here: https://github.com/cyphix333/SBBCodeParser
It is a forked project with an added composer.json.
I tried adding it as a requirement in the projects main composer file, ie:
"require": {
//..........
"samclarke/sbb-code-parser": "*"
},
Then I ran:
composer update
It just complained that it couldn't find the package or any version of it.
Then I removed that line and tried:
require samclarke/sbb-code-parser
I have the files already in my Yii vendor folder located at: #app/vendor/samclarke/sbb-code-parser
I'm pretty new to composer and am not sure what I'm doing wrong or how composer actually is supposed to know where to get the files from based on the package name.
The package samclarke/sbb-code-parser can be found at Packagist.
https://packagist.org/packages/samclarke/sbb-code-parser
By default Composer tries to resolve a stableset of packages.
But this packages doesn't provide a stable version (version tag), yet - only the dev-master version exists. Composer can not resolve it.
In order to require it, you need to lower your minimum-stability for this package to development.
You might do this for one package explicitly:
"require": {
"samclarke/sbb-code-parser": "dev-master#dev"
},
Or for all packages by setting:
"minimum-stability": "dev"
The package cyphix333/SBBCodeParser is not on Packagist.
It's a private fork. If you want exactly this code. You might add a repositories section to your composer.json and add repo with url and type with vcs there. Then Composer can load the package from the VCS repository over at Github. This is described here: https://getcomposer.org/doc/05-repositories.md#loading-a-package-from-a-vcs-repository
That would work like this:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/cyphix333/SBBCodeParser"
}
],
"require": {
"samclarke/sbb-code-parser": "dev-master"
}
}
But unfortunately, the fork is also not well maintained and doesn't contain a valid composer.json file.
[Composer\Repository\InvalidRepositoryException]
No valid composer.json was found in any branch or tag of https://github.com/cyphix333/SBBCodeParser, could not load a package from it.
This needs to be fixed first....

How to move composer dependency from require-dev to require?

I have two projects on my git repo server. The first one is a library I wrote, let's call it foo/lib-bar. The second is an application which uses that library foo/app-bar. Currently the library is in development version, so the composer.json file of the library looks like this:
{
"name": "foo/lib-bar",
"description": "Bar library",
"version": "1.0.0-dev",
"type": "library",
"require": {
"php": ">=5.4",
"ext-posix": "*"
}
}
The application uses this library, so it contains the necessary requirement:
{
"name": "foo/app-bar",
"description": "Bar application",
"version": "0.5.0-dev",
"type": "application",
"repositories": [
{
"type": "vcs",
"url": "ssh://user#git.example.com/lib-foo"
}
],
"require-dev": {
"foo/lib-bar": ">=1.0.0-dev",
},
"require": {
"php": ">=5.5.3"
}
}
And everything is smooth up to this point: both composer install and composer update run as expected, install the dependency and I can see it in vendor/
Now, the docs says that
require#
Lists packages required by this package. The package will not be installed unless those requirements can be met.
And for the steps which lead to the issue in question:
So ok, my library is ready to be deployed and come out of the development phase. It also happens to be required in production version of my application. I remove the dev suffix from the composer.json file of my library, commit and push the file, and am ready to update the application.
With the application's composer.json I move the library dependency from require-dev section to require and remove the dev suffix (everything is copy-pasted, so there is no typo - I've checked and rechecked again):
"require-dev": {},
"require": {
"php": ">=5.5.3",
"foo/lib-bar": ">=1.0.0"
}
Now I run composer update and I get:
$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for foo/lib-bar >=1.0.0 -> satisfiable by foo/lib-bar[dev-master].
- Removal request for foo/lib-bar == 9999999-dev
I assumed it did not find the new version, so I deleted the old library manually:
$ rm composer.lock
$ rm -rf vendor/foo/
and tried to install it from scratch
$ composer install
but this time it gives me:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested package foo/lib-bar could not be found in any version, there may be a typo in the package name.
So it seems like require-dev does work, but require doesn't. Any suggestions on what may have gone wrong here?
require-dev is not the place to develop dependencies. It is meant for software that is used only in development, like PHPUnit, Mockery etc., or for dependencies that are useful by themselves, but in this case only used for development, like the client library for the service a software package is about (to make some real requests in a test scenario).
So your library shouldn't have been "require-dev" from the beginning.
Another thing is: Composer will deduct the version if use appropriate branches and tags, i.e. a branch named "1.0.x" in your repository will be detected as being the development branch for all 1.0 versions, and any requirements for such versions could possibly be satisfied by this branch - provided you allow for development versions either by setting "minimum-stability": "dev" (which would allow development versions for ALL software - rather unintended), or when you require version "1.0.0#dev" for your software.
The one thing that might currently break your composer setup is that you mention a version in the librarys composer.json explicitly, and this is a development version. Have you removed that version indicator? You should remove it, because life is easier if Composer can detect the versions from the tags in a repository, not by having them explicitly mentioned in composer.json.
Finally please make sure that when using Git you tag the commit with appropriate version. The required version should correspond to the git tagged version.

Composer not installing or updating package

I create a package and pushed it to github, My composer.json for that package looks like such:
{
"name": "adam.balan/AisisAjax",
"description": "This is a component for AisisCore, that is - it should be installed to the Components section of the web based framework for wordpress.",
"license": "GPL v3",
"authors": [
{
"name": "Adam Kyle Balan",
"email": "AdamKyleBalan#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
}
}
It all works fine for me - for using composer install the first time, how ever if I make a change and push it to the repository and run composer install (or even delete the vendor folder and run the command again to do a fresh install) I get a version that reflects the first commit I ever did to this repo. In other words my chanegs do not show up in whats being downloaded.
I have read about caching issues with composer and went to C:\Users\<user>\AppData\Local\Composer and deleted all the files in there - same issue.
What gives?
If you do not use a defined version that is tagged in the repository, but a branch you are developing on, Composer detects which commit was downloaded and will always download that exact commit if you only "install".
If you want Composer to update any dependency, you have to call composer update.
Note that Composer creates a lock file that contains this info, because the usual case is that someone wants to restore the exact combination of dependencies that were used when the software was composed. If you develop actively on the software, simply update your dependencies more often. :)

Categories