Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get and git with any hosting solution #111

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

sebastienheyd
Copy link
Contributor

Hi! I just discover your package today, very interesting!

I modified packager:get and packager:git to be more flexible.

In other words, I created a class that will parse the source url via regular expressions and allow to use more git repository hosting solutions (including personal hosting). So it is therefore no longer necessary to specify whether you want to use github or bitbucket.

As a bonus, I added the recovery of the source url via the packagist api, it is now possible to do packager:get jeroen-g/laravel-package.

Hope you'll enjoy my contribution.

How it works :

See the file: PackageRepository.php

I parse the given URL to check if it is a package name without hosting name and if so I ask packagist to get the associated repo url:

if (preg_match('`^([^/:@]*)/([^/\.]*)$`', $url, $m)) {
    $client = new Client(['verify' => config('packager.curl_verify_cert')]);
    try {
        $response = $client->get(sprintf('https://packagist.org/packages/%s/%s.json', $m[1], $m[2]));
        $json = json_decode($response->getBody()->getContents());
        $this->origin = $json->package->repository;
    } catch (GuzzleHttp\Exception\ClientException $e) {
        throw new RuntimeException('Package not found on packagist');
    }
}

If given URL is a full URL, I get the pieces:

$regex = [
    '`^https?://([^/]*)/([^/]*)/([^./]*).*$`',
    '`^git@([^:]*):([^/]*)/([^.]*)\.git$`'
];

foreach ($regex as $rx) {
    if (preg_match($rx, $this->origin, $m)) {
        $this->host = $m[1];
        $this->vendor = $m[2];
        $this->name = $m[3];
        return $this;
    }
}

To get the zip url, I have an array of hosting solutions to retrieve the zip path:

$urls = [
    'github.com'    => 'https://:host/:vendor/:name/archive/:branch.zip',
    'gitlab.com'    => 'https://:host/:vendor/:name/-/archive/:branch/:name-:branch.zip',
    'bitbucket.org' => 'https://:host/:vendor/:name/get/:branch.zip',
];

To be more flexible, I added an empty array in the configuration file, this array will be merged with the existent one:

/*
 * For other public repositories. Enter the host and the associated url template
 * to allow zip files to be downloaded.
 */
'repositories' => [
    //'personal.repo.com' => 'https://:host/:vendor/:name/archive/:branch.zip',
]

Tests

I added tests to check if the parser correctly works, see IntegratedTest.php#L10

Small fixes

I had to add a micro delay to display the progress bar correctly in the ProgressBar.php#L47 file.

I had to set an more important timeout for process (5 minutes) in file Conveyor.php#L173, my composer.json is heavy and when testing it fails because composer has not enough time to finish. Maybe this can be an configuration option?

@Jeroen-G
Copy link
Owner

I'll take a good look at this, it sounds very exciting. I also, absolutely, have to applaud you for the extensive description and including tests. Amazing 👍

@sebastienheyd
Copy link
Contributor Author

I forgot, I also added the possibility to define another path to the packages folder in the configuration file: packager.php#L7.

I did this because I use Docker containers and the path I specify must be relative. This because the path in the container is not necessarily the same as the path on the host machine.

So it is possible to change the default path or define a PACKAGES_PATH environment variable in the .env file to change the path per environment.

This can be useful if you share on multiple projects a folder with all your packages

@Jeroen-G
Copy link
Owner

Jeroen-G commented Oct 9, 2020

I still need to test this with GitLab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants