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

Support additional repository types #169

Draft
wants to merge 6 commits into
base: 0.4.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
/tests/WorkspaceHome
/tests/Test/Updater/fixtures/generated
/.php-cs-fixer.cache
.idea/
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"symfony/finder": "^6.1",
"symfony/yaml": "^6.1",
"twig/twig": "^2.13",
"composer/semver": "^3.3"
"composer/semver": "^3.3",
"czproject/git-php": "^4.2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.2",
Expand Down
56 changes: 54 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ services:
my127\Console\:
resource: '../packages/Console/src/*'

my127\Workspace\Types\Harness\Repository\AggregateRepository: ~
my127\Workspace\Types\Harness\Repository\AggregateRepository:
arguments:
- '@my127\Workspace\Types\Harness\Repository\PackageRepository'
- '@my127\Workspace\Types\Harness\Repository\LocalSyncRepository'
- '@my127\Workspace\Types\Harness\Repository\GithubRepository'
- '@my127\Workspace\Types\Harness\Repository\ArchiveRepository' # this is catch-all and must stay last

my127\Workspace\Types\Harness\Repository\ArchiveRepository: ~
my127\Workspace\Types\Harness\Repository\PackageRepository: ~
my127\Workspace\Types\Harness\Repository\LocalSyncRepository: ~
my127\Workspace\Types\Harness\Repository\GithubRepository: ~

my127\Workspace\Types\Harness\Repository\Repository $packages: '@my127\Workspace\Types\Harness\Repository\AggregateRepository'
my127\Workspace\Types\Harness\Repository\Repository $archiveRepository: '@my127\Workspace\Types\Harness\Repository\ArchiveRepository'
my127\Workspace\Types\Harness\Repository\Repository $packageRepository: '@my127\Workspace\Types\Harness\Repository\PackageRepository'

my127\Workspace\Updater\Updater:
arguments:
Expand Down
24 changes: 12 additions & 12 deletions src/Types/Harness/Repository/AggregateRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@

class AggregateRepository implements Repository
{
/** @var Repository */
private $packageRepository;
/** @var HandlingRepository[] */
private array $repositories;

/** @var Repository */
private $archiveRepository;

public function __construct(Repository $packageRepository, Repository $archiveRepository)
public function __construct(HandlingRepository ...$repositories)
{
$this->packageRepository = $packageRepository;
$this->archiveRepository = $archiveRepository;
$this->repositories = $repositories;
}

/**
* @throws \Exception
*/
public function get(string $package): Package
{
$parts = parse_url($package);
if ($parts === false || (empty($parts['scheme']) && str_contains($parts['path'], ':'))) {
return $this->packageRepository->get($package);
foreach ($this->repositories as $repository) {
if ($repository->handles($package)) {
return $repository->get($package);
}
}

return $this->archiveRepository->get($package);
throw new \Exception(sprintf('No handler found for URI "%s"', $package));
}
}
7 changes: 6 additions & 1 deletion src/Types/Harness/Repository/ArchiveRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

use my127\Workspace\Types\Harness\Repository\Package\Package;

class ArchiveRepository implements Repository
class ArchiveRepository implements HandlingRepository
{
public function handles(string $uri): bool
{
return true;
}

public function get(string $package): Package
{
return new Package([
Expand Down
33 changes: 33 additions & 0 deletions src/Types/Harness/Repository/GithubRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace my127\Workspace\Types\Harness\Repository;

use my127\Workspace\Types\Harness\Repository\Package\Package;

class GithubRepository implements HandlingRepository
{
// format: github:git@github.com:inviqa/harness-base-php.git:0.4.x
public const PATTERN = '#^github:(?<urn>.+):(?<ref>[^:]+)$#';
public const KEY_REF = 'ref';
public const KEY_URN = 'urn';

public function handles(string $uri): bool
{
return (bool) \preg_match(self::PATTERN, $uri);
}

public function get(string $package): Package
{
if (!\preg_match(self::PATTERN, $package, $matches)) {
throw new \Exception("Package '$package' not matching git URL pattern.");
}

return new Package([
'url' => $matches[self::KEY_URN],
'ref' => $matches[self::KEY_REF],
'git' => true,
]);
}
}
10 changes: 10 additions & 0 deletions src/Types/Harness/Repository/HandlingRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace my127\Workspace\Types\Harness\Repository;

interface HandlingRepository extends Repository
{
public function handles(string $uri): bool;
}
23 changes: 23 additions & 0 deletions src/Types/Harness/Repository/LocalSyncRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace my127\Workspace\Types\Harness\Repository;

use my127\Workspace\Types\Harness\Repository\Package\Package;

class LocalSyncRepository implements HandlingRepository
{
public function handles(string $uri): bool
{
return \str_starts_with($uri, 'sync://');
}

public function get(string $package): Package
{
return new Package([
'url' => rtrim(\substr($package, 6), '/') . '/',
'localsync' => true,
]);
}
}
9 changes: 8 additions & 1 deletion src/Types/Harness/Repository/PackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use my127\Workspace\Types\Harness\Repository\Exception\UnknownPackage;
use my127\Workspace\Types\Harness\Repository\Package\Package;

class PackageRepository implements Repository
class PackageRepository implements HandlingRepository
{
private const HARNESS_PACKAGE_PATTERN = '/^((?P<vendor>[a-z0-9-]+)\/)?(?P<harness>[a-z0-9-]+){1}(:(?P<version>[a-z0-9.-]+))?$/';
private const HARNESS_VERSION_PATTERN = '/^v(?<major>[0-9x]+){1}(\.(?<minor>[0-9x]+))?(.(?<patch>[0-9x]+))?$/';
Expand Down Expand Up @@ -48,6 +48,13 @@ public function __construct(JsonLoader $fileLoader)
$this->fileLoader = $fileLoader;
}

public function handles(string $uri): bool
{
$parts = parse_url($uri);

return $parts === false || (empty($parts['scheme']) && str_contains($parts['path'], ':'));
}

public function get(string $package): Package
{
$this->importPackagesFromSources();
Expand Down
4 changes: 3 additions & 1 deletion src/Types/Workspace/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,16 @@ public function build(Environment $environment, DefinitionCollection $definition
->usage('install')
->option('--step=<step> Step from which to start installer. [default: 1]')
->option('--skip-events If set events will not be triggered.')
->option('--force Force re-download and overwrite (in step=prepare)')
->action(function (Input $input) {
$this->workspace->install($input);
});

$this->application->section('harness download')
->usage('harness download')
->option('--force Force re-download and overwrite')
->action(function (Input $input) {
$this->workspace->run('install --step=download');
$this->workspace->run('install --step=download' . ($input->getOption('force')->value() ? ' --force' : ''));
});

$this->application->section('harness prepare')
Expand Down
27 changes: 23 additions & 4 deletions src/Types/Workspace/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace my127\Workspace\Types\Workspace;

use CzProject\GitPhp\Git;
use my127\Workspace\Types\Crypt\Key;
use my127\Workspace\Types\Harness\Repository\Package\Package;
use my127\Workspace\Types\Harness\Repository\Repository;
use my127\Workspace\Utility\Filesystem;
use my127\Workspace\Utility\TmpNamType;
use Symfony\Component\Yaml\Yaml;

class Creator
Expand Down Expand Up @@ -52,7 +55,7 @@ private function findHarnessLayers(string $harness): array

$harnessLayers = [$harness];

$harnessData = $this->parseYamlMergeStreams($this->downloadAndExtractHarnessYml($package));
$harnessData = $this->parseYamlMergeStreams($this->acquireAndExtractHarnessYml($package));
if (!is_array($harnessData)) {
throw new \Exception('Could not parse the harness\'s harness.yml file');
}
Expand Down Expand Up @@ -89,16 +92,32 @@ private function parseYamlMergeStreams($content): array
return $mergedDocument;
}

private function downloadAndExtractHarnessYml(Package $package): string
private function acquireAndExtractHarnessYml(Package $package): string
{
if ($package->getDist()['localsync'] ?? false) {
return file_get_contents($package->getDist()['url'] . 'harness.yml');
}

if ($package->getDist()['git'] ?? false) {
$packageDirPath = Filesystem::tempname(TmpNamType::PATH);

$git = new Git();
$git->cloneRepository($package->getDist()['url'], $packageDirPath, ['-q', '--depth', '1', '--branch', $package->getDist()['ref']]);

$yaml = file_get_contents($packageDirPath . '/harness.yml');
Filesystem::rrmdir($packageDirPath);

return $yaml;
}

$packageTarball = tempnam(sys_get_temp_dir(), 'my127ws');
file_put_contents($packageTarball, file_get_contents($package->getDist()['url']));

$packageDir = tempnam(sys_get_temp_dir(), 'my127ws');
if ($packageDir === false) {
throw new \Exception('Could not create temporary directory for harness');
}
unlink($packageDir);
\unlink($packageDir);
if (!mkdir($packageDir, 0700)) {
throw new \Exception('Could not create temporary ' . $packageDir . ' directory for harness');
}
Expand All @@ -112,7 +131,7 @@ private function downloadAndExtractHarnessYml(Package $package): string
if (is_dir($packageDir)) {
exec('rm -rf ' . escapeshellarg($packageDir));
}
unlink($packageTarball);
\unlink($packageTarball);

if ($harnessYaml === false) {
throw new \Exception('Could not parse the harness\'s harness.yml file');
Expand Down
Loading
Loading