Skip to content

Commit 11c9b28

Browse files
committed
Merge pull request composer#125 from bitExpert/feature/single_package_build
Allow to run a satis build for one repo/package
2 parents 5178468 + 9b49181 commit 11c9b28

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

src/Composer/Satis/Command/BuildCommand.php

+66-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Composer\Satis\Command;
1414

15+
use Composer\Package\Loader\ArrayLoader;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Output\OutputInterface;
1718
use Symfony\Component\Console\Input\InputArgument;
@@ -50,6 +51,7 @@ protected function configure()
5051
->setDefinition(array(
5152
new InputArgument('file', InputArgument::OPTIONAL, 'Json file to use', './satis.json'),
5253
new InputArgument('output-dir', InputArgument::OPTIONAL, 'Location where to output built files', null),
54+
new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that should be built, if not provided all packages are.', null),
5355
new InputOption('no-html-output', null, InputOption::VALUE_NONE, 'Turn off HTML view'),
5456
new InputOption('skip-errors', null, InputOption::VALUE_NONE, 'Skip Download or Archive errors'),
5557
))
@@ -101,6 +103,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
101103
{
102104
$verbose = $input->getOption('verbose');
103105
$configFile = $input->getArgument('file');
106+
$packagesFilter = $input->getArgument('packages');
104107
$skipErrors = (bool)$input->getOption('skip-errors');
105108

106109
if (preg_match('{^https?://}i', $configFile)) {
@@ -141,7 +144,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
141144
}
142145

143146
$composer = $this->getApplication()->getComposer(true, $config);
144-
$packages = $this->selectPackages($composer, $output, $verbose, $requireAll, $requireDependencies, $requireDevDependencies, $minimumStability, $skipErrors);
147+
$packages = $this->selectPackages($composer, $output, $verbose, $requireAll, $requireDependencies, $requireDevDependencies, $minimumStability, $skipErrors, $packagesFilter);
145148

146149
if ($htmlView = !$input->getOption('no-html-output')) {
147150
$htmlView = !isset($config['output-html']) || $config['output-html'];
@@ -152,14 +155,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
152155
}
153156

154157
$filenamePrefix = $outputDir.'/include/all';
158+
$filename = $outputDir.'/packages.json';
159+
if(!empty($packagesFilter)) {
160+
// in case of an active package filter we need to load the dumped packages.json and merge the
161+
// updated packages in
162+
$oldPackages = $this->loadDumpedPackages($filename, $packagesFilter);
163+
$packages += $oldPackages;
164+
ksort($packages);
165+
}
166+
155167
$packageFile = $this->dumpPackageIncludeJson($packages, $output, $filenamePrefix);
156168
$packageFileHash = hash_file('sha1', $packageFile);
157169

158170
$includes = array(
159171
'include/all$'.$packageFileHash.'.json' => array( 'sha1'=>$packageFileHash ),
160172
);
161173

162-
$filename = $outputDir.'/packages.json';
163174
$this->dumpPackagesJson($includes, $output, $filename);
164175

165176
if ($htmlView) {
@@ -176,7 +187,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
176187
}
177188
}
178189

179-
private function selectPackages(Composer $composer, OutputInterface $output, $verbose, $requireAll, $requireDependencies, $requireDevDependencies, $minimumStability, $skipErrors)
190+
private function selectPackages(Composer $composer, OutputInterface $output, $verbose, $requireAll, $requireDependencies, $requireDevDependencies, $minimumStability, $skipErrors, array $packagesFilter = array())
180191
{
181192
$selected = array();
182193

@@ -198,6 +209,7 @@ private function selectPackages(Composer $composer, OutputInterface $output, $ve
198209

199210
if ($requireAll) {
200211
$links = array();
212+
$filterForPackages = count($packagesFilter) > 0;
201213

202214
foreach ($repos as $repo) {
203215
// collect links for composer repos with providers
@@ -206,8 +218,18 @@ private function selectPackages(Composer $composer, OutputInterface $output, $ve
206218
$links[] = new Link('__root__', $name, new MultiConstraint(array()), 'requires', '*');
207219
}
208220
} else {
209-
// process other repos directly
210-
foreach ($repo->getPackages() as $package) {
221+
$packages = array();
222+
if($filterForPackages) {
223+
// apply package filter if defined
224+
foreach ($packagesFilter as $filter) {
225+
$packages += $repo->findPackages($filter);
226+
}
227+
} else {
228+
// process other repos directly
229+
$packages = $repo->getPackages();
230+
}
231+
232+
foreach ($packages as $package) {
211233
// skip aliases
212234
if ($package instanceof AliasPackage) {
213235
continue;
@@ -428,6 +450,45 @@ private function dumpWeb(array $packages, OutputInterface $output, PackageInterf
428450
file_put_contents($directory.'/index.html', $content);
429451
}
430452

453+
private function loadDumpedPackages($filename, array $packagesFilter = array())
454+
{
455+
$packages = array();
456+
$repoJson = new JsonFile($filename);
457+
$dirName = dirname($filename);
458+
459+
if ($repoJson->exists()) {
460+
$loader = new ArrayLoader();
461+
$jsonIncludes = $repoJson->read();
462+
$jsonIncludes = isset($jsonIncludes['includes']) && is_array($jsonIncludes['includes'])
463+
? $jsonIncludes['includes']
464+
: array();
465+
466+
foreach ($jsonIncludes as $includeFile => $includeConfig) {
467+
$includeJson = new JsonFile($dirName . '/' . $includeFile);
468+
$jsonPackages = $includeJson->read();
469+
$jsonPackages = isset($jsonPackages['packages']) && is_array($jsonPackages['packages'])
470+
? $jsonPackages['packages']
471+
: array();
472+
473+
foreach ($jsonPackages as $jsonPackage) {
474+
if (is_array($jsonPackage)) {
475+
foreach ($jsonPackage as $jsonVersion) {
476+
if (is_array($jsonVersion)) {
477+
if(isset($jsonVersion['name']) && in_array($jsonVersion['name'], $packagesFilter)) {
478+
continue;
479+
}
480+
$package = $loader->load($jsonVersion);
481+
$packages[$package->getUniqueName()] = $package;
482+
}
483+
}
484+
}
485+
}
486+
}
487+
}
488+
489+
return $packages;
490+
}
491+
431492
private function getMappedPackageList(array $packages)
432493
{
433494
$groupedPackages = $this->groupPackagesByName($packages);

0 commit comments

Comments
 (0)