Skip to content

Allow packs to embed their recipe #924

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

Closed
wants to merge 1 commit into from
Closed
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
62 changes: 62 additions & 0 deletions src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Package\PackageInterface;
use Composer\Util\Http\Response as ComposerResponse;
use Composer\Util\HttpDownloader;
use Composer\Util\Loop;
Expand Down Expand Up @@ -51,6 +52,7 @@ class Downloader
private $caFile;
private $enabled = true;
private $composer;
private $vendorDir;

public function __construct(Composer $composer, IoInterface $io, $rfs)
{
Expand Down Expand Up @@ -94,6 +96,7 @@ public function __construct(Composer $composer, IoInterface $io, $rfs)
$this->cache = new ComposerCache($io, $config->get('cache-repo-dir').'/flex');
$this->sess = bin2hex(random_bytes(16));
$this->composer = $composer;
$this->vendorDir = $config->get('vendor-dir');
}

public function getSessionId(): string
Expand Down Expand Up @@ -155,6 +158,7 @@ public function getRecipes(array $operations): array
$this->conflicts = [];
}

$localRecipes = [];
$data = [];
$urls = [];
$chunk = '';
Expand All @@ -175,6 +179,11 @@ public function getRecipes(array $operations): array
}
}

if ('symfony-pack' === $package->getType() && is_file($this->vendorDir.'/'.$package->getName().'/manifest.json')) {
$localRecipes[$package->getName()] = $package;
continue;
}

$version = $package->getPrettyVersion();
if ($operation instanceof InformationOperation && $operation->getVersion()) {
$version = $operation->getVersion();
Expand Down Expand Up @@ -310,6 +319,14 @@ public function getRecipes(array $operations): array
}
}

foreach ($localRecipes as $name => $package) {
$data['locks'][$name] = [
'version' => $package->getPrettyVersion(),
];

$data['manifests'][$name] = $this->getLocalRecipe($package);
}

return $data;
}

Expand All @@ -328,6 +345,10 @@ public function removeRecipeFromIndex(string $packageName, string $version)
*/
private function get(array $urls, bool $isRecipe = false, int $try = 3): array
{
if (!$urls) {
return [];
}

$responses = [];
$retries = [];
$options = [];
Expand Down Expand Up @@ -481,6 +502,47 @@ private function initialize()
}
}

private function getLocalRecipe(PackageInterface $package): array
{
$name = $package->getName();
$path = $this->vendorDir.'/'.$name;
$manifest = json_decode(file_get_contents($path.'/manifest.json'), true);
if (!\is_array($manifest)) {
throw new \LogicException(sprintf('Invalid recipe manifest in "%s".', $path));
}
$files = [];
$it = new \RecursiveDirectoryIterator($path);
$it->setFlags($it::SKIP_DOTS | $it::FOLLOW_SYMLINKS | $it::UNIX_PATHS);

foreach (new \RecursiveIteratorIterator($it) as $path => $file) {
$file = substr($path, 1 + \strlen($name));
if (is_dir($path) || 'manifest.json' === $file || 'composer.json') {
continue;
}
if ('post-install.txt' === $file) {
$manifest['post-install-output'] = explode("\n", rtrim(str_replace("\r", '', file_get_contents($path)), "\n"));
continue;
}
if ('Makefile' === $file) {
$manifest['makefile'] = explode("\n", rtrim(str_replace("\r", '', file_get_contents($path)), "\n"));
continue;
}
$contents = file_get_contents($path);
$files[$file] = [
'contents' => preg_match('//u', $contents) ? explode("\n", $contents) : base64_encode($contents),
'executable' => is_executable($path),
];
}

return [
'manifest' => $manifest,
'files' => $files,
'package' => $name,
'origin' => sprintf('%s:%s@vendor:manifest.json', $name, $package->getPrettyVersion()),
'is_contrib' => false,
];
}

private static function generateCacheKey(string $url): string
{
$url = preg_replace('{^https://api.github.com/repos/([^/]++/[^/]++)/contents/}', '$1/', $url);
Expand Down
2 changes: 1 addition & 1 deletion src/Recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getFormattedOrigin(): string

public function getURL(): string
{
if (!$this->data['origin']) {
if (empty($this->data['origin'])) {
return '';
}

Expand Down