/** * @dataProvider dataProviderTestDiff * @param $lockOne * @param $lockTwo */ public function testDiff($lockOne, $lockTwo, $expected, $exception = '') { if ($exception) { $this->setExpectedExceptionRegExp($exception); } $helper = new ComposerHelper(); $actual = $helper->diff($lockOne, $lockTwo); $this->assertEquals($expected, $actual); }
/** * @param RepositoryModel $model * @return array */ protected function findVendorsForUpdate(RepositoryModel $model) { $fileSystem = new Filesystem(); $fileSystem->copy($model->getAbsolutePath() . DIRECTORY_SEPARATOR . ComposerHelper::COMPOSER_LOCK, $model->getAbsolutePath() . DIRECTORY_SEPARATOR . ComposerHelper::COMPOSER_LOCK_CONFLICT, true); $model->getProvider()->run('checkout', ['--theirs', ComposerHelper::COMPOSER_LOCK], $this->isDryRun(), false); $theirLock = file_get_contents($model->getAbsolutePath() . DIRECTORY_SEPARATOR . ComposerHelper::COMPOSER_LOCK); $fileSystem->copy($model->getAbsolutePath() . DIRECTORY_SEPARATOR . ComposerHelper::COMPOSER_LOCK_CONFLICT, $model->getAbsolutePath() . DIRECTORY_SEPARATOR . ComposerHelper::COMPOSER_LOCK, true); $model->getProvider()->run('checkout', ['--ours', ComposerHelper::COMPOSER_LOCK], $this->isDryRun(), false); $oursLock = file_get_contents($model->getAbsolutePath() . DIRECTORY_SEPARATOR . ComposerHelper::COMPOSER_LOCK); $fileSystem->rename($model->getAbsolutePath() . DIRECTORY_SEPARATOR . ComposerHelper::COMPOSER_LOCK_CONFLICT, $model->getAbsolutePath() . DIRECTORY_SEPARATOR . ComposerHelper::COMPOSER_LOCK, true); $helper = new ComposerHelper(); $vendorsForUpdate = $helper->diff($theirLock, $oursLock); $composerJson = file_get_contents($model->getAbsolutePath() . DIRECTORY_SEPARATOR . ComposerHelper::COMPOSER_JSON); $vendors = $helper->extractAllVendors($composerJson); $vendorsForUpdate = array_intersect($vendorsForUpdate, $vendors); return $vendorsForUpdate; }
/** * @param RepositoryList $repositories */ public function execute(RepositoryList $repositories) { $this->getLogger()->debug('Run plugin', [get_called_class()]); $model = $repositories->getProjectModel(); if ($model->hasConflicts()) { if (false !== stripos($model->getConflicts(), ComposerHelper::COMPOSER_JSON)) { $this->getSymfonyStyle()->error('You should resolve composer.json conflict first'); $this->stopPropagation(); return; } } $composerFilename = $repositories->getProjectModel()->getAbsolutePath() . DIRECTORY_SEPARATOR . 'composer.json'; $helper = new ComposerHelper(); $composerData = $helper->jsonDecode(file_get_contents($composerFilename)); $data = $repositories->getVendorModels(); $vendorsModels = []; foreach ($data as $model) { $vendorsModels[strtolower($model->getPackageName())] = $model; } $projectBranch = $repositories->getProjectModel()->getBranch(); $updateFlag = false; if (!empty($composerData['require'])) { $changes = $this->changeVersion($composerData['require'], $vendorsModels, $projectBranch); $composerData['require'] = array_merge($composerData['require'], $changes); $updateFlag = $updateFlag || !empty($changes); } if (!empty($composerData['require-dev'])) { $changes = $this->changeVersion($composerData['require-dev'], $vendorsModels, $projectBranch); $composerData['require-dev'] = array_merge($composerData['require-dev'], $changes); $updateFlag = $updateFlag || !empty($changes); } if (!$this->isDryRun()) { $jsonEncodedData = json_encode($composerData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); file_put_contents($composerFilename, $jsonEncodedData); } if ($updateFlag) { $this->getSymfonyStyle()->success('File composer.json updated'); } $this->getLogger()->debug('End plugin', [get_called_class()]); }