Skip to content

Commit 2d28c89

Browse files
committed
Merge branch '1.x' into 2.x
* 1.x: Merge-powered recipe update system
2 parents f7a24c9 + 9763476 commit 2d28c89

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+4141
-283
lines changed

.php_cs.dist renamed to .php-cs-fixer.dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
$finder = PhpCsFixer\Finder::create()->in(__DIR__);
44

5-
return PhpCsFixer\Config::create()
5+
return (new PhpCsFixer\Config())
66
->setFinder($finder)
77
->setRules(array(
88
'@Symfony' => true,

src/Command/InstallRecipesCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313

1414
use Composer\Command\BaseCommand;
1515
use Composer\DependencyResolver\Operation\InstallOperation;
16-
use Composer\Factory;
1716
use Symfony\Component\Console\Exception\RuntimeException;
1817
use Symfony\Component\Console\Input\InputArgument;
1918
use Symfony\Component\Console\Input\InputInterface;
2019
use Symfony\Component\Console\Input\InputOption;
2120
use Symfony\Component\Console\Output\OutputInterface;
2221
use Symfony\Flex\Event\UpdateEvent;
23-
use Symfony\Flex\Lock;
22+
use Symfony\Flex\Flex;
2423

2524
class InstallRecipesCommand extends BaseCommand
2625
{
26+
/** @var Flex */
2727
private $flex;
2828
private $rootDir;
2929

@@ -55,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5555
throw new RuntimeException('Cannot run "sync-recipes --force": git not found.');
5656
}
5757

58-
$symfonyLock = new Lock(getenv('SYMFONY_LOCKFILE') ?: str_replace('composer.json', 'symfony.lock', Factory::getComposerFile()));
58+
$symfonyLock = $this->flex->getLock();
5959
$composer = $this->getComposer();
6060
$locker = $composer->getLocker();
6161
$lockData = $locker->getLockData();

src/Command/RecipesCommand.php

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
use Composer\Command\BaseCommand;
1515
use Composer\Downloader\TransportException;
16-
use Composer\Util\HttpDownloader;
1716
use Symfony\Component\Console\Input\InputArgument;
1817
use Symfony\Component\Console\Input\InputInterface;
1918
use Symfony\Component\Console\Input\InputOption;
2019
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Flex\GithubApi;
2121
use Symfony\Flex\InformationOperation;
2222
use Symfony\Flex\Lock;
2323
use Symfony\Flex\Recipe;
@@ -31,13 +31,13 @@ class RecipesCommand extends BaseCommand
3131
private $flex;
3232

3333
private Lock $symfonyLock;
34-
private HttpDownloader $downloader;
34+
private GithubApi $githubApi;
3535

3636
public function __construct(/* cannot be type-hinted */ $flex, Lock $symfonyLock, HttpDownloader $downloader)
3737
{
3838
$this->flex = $flex;
3939
$this->symfonyLock = $symfonyLock;
40-
$this->downloader = $downloader;
40+
$this->githubApi = new GithubApi($downloader);
4141

4242
parent::__construct();
4343
}
@@ -135,7 +135,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
135135
'',
136136
'Run:',
137137
' * <info>composer recipes vendor/package</info> to see details about a recipe.',
138-
' * <info>composer recipes:install vendor/package --force -v</info> to update that recipe.',
138+
' * <info>composer recipes:update vendor/package</info> to update that recipe.',
139139
'',
140140
]));
141141

@@ -170,13 +170,15 @@ private function displayPackageInformation(Recipe $recipe)
170170
$commitDate = null;
171171
if (null !== $lockRef && null !== $lockRepo) {
172172
try {
173-
list($gitSha, $commitDate) = $this->findRecipeCommitDataFromTreeRef(
173+
$recipeCommitData = $this->githubApi->findRecipeCommitDataFromTreeRef(
174174
$recipe->getName(),
175175
$lockRepo,
176176
$lockBranch ?? '',
177177
$lockVersion,
178178
$lockRef
179179
);
180+
$gitSha = $recipeCommitData ? $recipeCommitData['commit'] : null;
181+
$commitDate = $recipeCommitData ? $recipeCommitData['date'] : null;
180182
} catch (TransportException $exception) {
181183
$io->writeError('Error downloading exact git sha for installed recipe.');
182184
}
@@ -231,7 +233,7 @@ private function displayPackageInformation(Recipe $recipe)
231233
$io->write([
232234
'',
233235
'Update this recipe by running:',
234-
sprintf('<info>composer recipes:install %s --force -v</info>', $recipe->getName()),
236+
sprintf('<info>composer recipes:update %s</info>', $recipe->getName()),
235237
]);
236238
}
237239
}
@@ -323,59 +325,4 @@ private function writeTreeLine($line)
323325

324326
$io->write($line);
325327
}
326-
327-
/**
328-
* Attempts to find the original git sha when the recipe was installed.
329-
*/
330-
private function findRecipeCommitDataFromTreeRef(string $package, string $repo, string $branch, string $version, string $lockRef)
331-
{
332-
// only supports public repository placement
333-
if (0 !== strpos($repo, 'github.com')) {
334-
return [null, null];
335-
}
336-
337-
$parts = explode('/', $repo);
338-
if (3 !== \count($parts)) {
339-
return [null, null];
340-
}
341-
342-
$recipePath = sprintf('%s/%s', $package, $version);
343-
$commitsData = $this->requestGitHubApi(sprintf(
344-
'https://api.github.com/repos/%s/%s/commits?path=%s&sha=%s',
345-
$parts[1],
346-
$parts[2],
347-
$recipePath,
348-
$branch
349-
));
350-
351-
foreach ($commitsData as $commitData) {
352-
// go back the commits one-by-one
353-
$treeUrl = $commitData['commit']['tree']['url'].'?recursive=true';
354-
355-
// fetch the full tree, then look for the tree for the package path
356-
$treeData = $this->requestGitHubApi($treeUrl);
357-
foreach ($treeData['tree'] as $treeItem) {
358-
if ($treeItem['path'] !== $recipePath) {
359-
continue;
360-
}
361-
362-
if ($treeItem['sha'] === $lockRef) {
363-
// shorten for brevity
364-
return [
365-
substr($commitData['sha'], 0, 7),
366-
$commitData['commit']['committer']['date'],
367-
];
368-
}
369-
}
370-
}
371-
372-
return [null, null];
373-
}
374-
375-
private function requestGitHubApi(string $path)
376-
{
377-
$contents = $this->downloader->get($path)->getBody();
378-
379-
return json_decode($contents, true);
380-
}
381328
}

0 commit comments

Comments
 (0)