Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from silpion/feature/remove-commands
Browse files Browse the repository at this point in the history
Added commands for removing src and dist from a composer.lock
  • Loading branch information
h4cc committed Oct 1, 2014
2 parents 1d1bdb9 + 34f7c85 commit b1636dc
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 115 deletions.
19 changes: 15 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,28 @@ php:
- hhvm

before_script:
- composer self-update
- composer self-update || true
- composer install
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then curl -LSs http://box-project.org/installer.php | php; fi;'
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php box.phar -v build; fi;'

script:
# Check Script.
#-- check:dist
- php bin/composer-checker check:dist -p github test/composer.lock | grep "Invalid urls found"
- php bin/composer-checker check:dist -p github -p "jquery.com" test/composer.lock | grep "Invalid urls found"
- php bin/composer-checker check:dist -p github -p "jquery.com" --allow-empty test/composer.lock | grep "All urls valid"
#--- check:src
- php bin/composer-checker check:src -p github test/composer.lock | grep "Invalid urls found"
- php bin/composer-checker check:src -p github -p "jquery.com" test/composer.lock | grep "Invalid urls found"
- php bin/composer-checker check:src -p github -p "jquery.com" -p "googlecode.com" --allow-empty test/composer.lock | grep "All urls valid"
#--- remove:src
- cp -f test/composer.lock test.lock; grep '"source":' test.lock
- cp -f test/composer.lock test.lock; php bin/composer-checker remove:src test.lock && ! grep -q '"source":' test.lock
- cp -f test/composer.lock test.lock; php bin/composer-checker remove:src -e googlecode.com test.lock && grep -q '"source":' test.lock
#--- remove:dist
- cp -f test/composer.lock test.lock; grep '"dist":' test.lock
- cp -f test/composer.lock test.lock; php bin/composer-checker remove:dist test.lock && ! grep -q '"dist":' test.lock
- cp -f test/composer.lock test.lock; php bin/composer-checker remove:dist -e jquery.com test.lock && grep -q '"dist":' test.lock
# Check PHAR.
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php composer-checker.phar check:dist -p github test/composer.lock | grep "Invalid urls found"; fi;'
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php composer-checker.phar check:dist -p github -p "jquery.com" test/composer.lock | grep "Invalid urls found"; fi;'
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php composer-checker.phar check:dist -p github -p "jquery.com" --allow-empty test/composer.lock | grep "All urls valid"; fi;'
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php composer-checker.phar check:dist -p github test/composer.lock | grep "Invalid urls found"; fi;'
108 changes: 108 additions & 0 deletions Command/BaseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Created by PhpStorm.
* User: julius
* Date: 01.10.14
* Time: 11:48
*/

namespace Silpion\ComposerChecker\Command;

use Camspiers\JsonPretty\JsonPretty;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

abstract class BaseCommand extends Command
{
/**
* @var OutputInterface
*/
protected $output;

/**
* @var InputInterface
*/
protected $input;

protected function execute(InputInterface $input, OutputInterface $output) {
$this->input = $input;
$this->output = $output;
}

/**
* Verbose output helper.
*
* @param $message
* @param $level
*/
protected function verbose($message, $level)
{
if ($this->output->getVerbosity() >= $level) {
$this->output->write($message);
}
}

protected function printTable($rows, $title = ' --- Invalid urls found --- ', array $headers = array('Package', 'Src-URL'))
{
/** @var \Symfony\Component\Console\Helper\TableHelper $table */
$table = $this->getApplication()->getHelperSet()->get('table');
$table->setHeaders($headers)->setRows($rows);

$this->output->writeln('<error>'.$title.'</error>');
$table->render($this->output);
}

/**
* Will return true, if the given url matches at least one of the given patterns.
*
* @param $url
* @param array $patterns
* @return bool
*/
protected function doesUrlMatchToAtLeastOnePattern($url, array $patterns)
{
foreach ($patterns as $pattern) {
$regex = '|' . $pattern . '|';
$this->verbose(
"Checking url '" . $url . "' with regex '" . $regex . "' -> ",
OutputInterface::VERBOSITY_VERBOSE
);
if (preg_match($regex, $url)) {
$this->verbose("MATCHED\n", OutputInterface::VERBOSITY_VERBOSE);
return true;
} else {
$this->verbose("NOT matched\n", OutputInterface::VERBOSITY_VERBOSE);
}
}
return false;
}

protected function readJsonFromFile($path)
{
$content = @file_get_contents($path);
if (!$content) {
throw new \Exception("File not found or empty.");
}

$json = @json_decode($content);
if (!is_object($json) || json_last_error() != JSON_ERROR_NONE) {
throw new \Exception("Invalid JSON in file.");
}

return $json;
}

protected function updateJsonContentInFile($path, \stdClass $content)
{
array_unshift($content->_readme, '------------------------------------------------------------------');
array_unshift($content->_readme, 'ATTENTION: This file has been changed by silpion/composer-checker.');
array_unshift($content->_readme, '------------------------------------------------------------------');

// Write new content to same file
$jsonPretty = new JsonPretty();
file_put_contents($path, $jsonPretty->prettify($content));

$this->output->writeln('<info>Updated file.</info>');
}
}
69 changes: 13 additions & 56 deletions Command/CheckDistCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @author Julius Beckmann <[email protected]>
*/
class CheckDistCommand extends Command
class CheckDistCommand extends BaseCommand
{
protected function configure()
{
Expand All @@ -41,6 +41,8 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$patterns = $input->getOption('url-pattern');
if (!$patterns) {
$output->writeln('<error>Need at least one url-pattern.</error>');
Expand All @@ -52,17 +54,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$patterns[] = '^$';
}

$content = @file_get_contents($input->getArgument('file'));
if (!$content) {
$output->writeln('<error>File not found.</error>');

return 1;
}

$json = @json_decode($content);
if (!is_object($json) || json_last_error() != JSON_ERROR_NONE) {
$output->writeln('<error>Invalid JSON in file.</error>');

try {
$json = $this->readJsonFromFile($input->getArgument('file'));
}catch(\Exception $e) {
$output->writeln('<error>'.$e->getMessage().'</error>');
return 1;
}

Expand All @@ -73,12 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$rows[] = array($package, $url);
}

/** @var \Symfony\Component\Console\Helper\TableHelper $table */
$table = $this->getApplication()->getHelperSet()->get('table');
$table->setHeaders(array('Package', 'Dist-URL'))->setRows($rows);

$output->writeln('<error> --- Invalid urls found --- </error>');
$table->render($output);
$this->printTable($rows);

return 1;
}
Expand All @@ -87,63 +77,30 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* Will return a array of invalid packages and their dist-urls determined by the given patterns.
* A dist-url is invalid if NONE of the given patterns has matched.
* Will return a array of invalid packages and their urls determined by the given patterns.
* A url is invalid if NONE of the given patterns has matched.
*
* @param \stdClass $json
* @param array $patterns
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return array
*/
protected function searchUrlPatterns(\stdClass $json, array $patterns, OutputInterface $output)
protected function searchUrlPatterns(\stdClass $json, array $patterns)
{
$errors = array();
foreach ($json->packages as $package) {
if (!isset($package->dist)) {
$this->verbose($output, 'Dist not found in "' . $package->name . "\"\n", OutputInterface::VERBOSITY_VERBOSE);
$this->verbose('Dist not found in "' . $package->name . "\"\n", OutputInterface::VERBOSITY_VERBOSE);
$url = '';
}else{
$url = $package->dist->url;
}

$matched = false;

foreach ($patterns as $pattern) {
$regex = '|' . $pattern . '|';
$this->verbose(
$output,
"Checking dist url '" . $url . "' with regex '" . $regex . "' -> ",
OutputInterface::VERBOSITY_VERBOSE
);
if (preg_match($regex, $url)) {
$this->verbose($output, "MATCHED\n", OutputInterface::VERBOSITY_VERBOSE);
$matched = true;
break;
} else {
$this->verbose($output, "NOT matched\n", OutputInterface::VERBOSITY_VERBOSE);
}
}

if (!$matched) {
if (!$this->doesUrlMatchToAtLeastOnePattern($url, $patterns)) {
$errors[$package->name] = $url;
}
}

return $errors;
}

/**
* Verbose output helper.
*
* @param OutputInterface $output
* @param $message
* @param $level
*/
private function verbose(OutputInterface $output, $message, $level)
{
if ($output->getVerbosity() >= $level) {
$output->write($message);
}
}
}

65 changes: 11 additions & 54 deletions Command/CheckSrcCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @author Julius Beckmann <[email protected]>
*/
class CheckSrcCommand extends Command
class CheckSrcCommand extends BaseCommand
{
protected function configure()
{
Expand All @@ -41,6 +41,8 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$patterns = $input->getOption('url-pattern');
if (!$patterns) {
$output->writeln('<error>Need at least one url-pattern.</error>');
Expand All @@ -52,17 +54,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$patterns[] = '^$';
}

$content = @file_get_contents($input->getArgument('file'));
if (!$content) {
$output->writeln('<error>File not found.</error>');

return 1;
}

$json = @json_decode($content);
if (!is_object($json) || json_last_error() != JSON_ERROR_NONE) {
$output->writeln('<error>Invalid JSON in file.</error>');

try {
$json = $this->readJsonFromFile($input->getArgument('file'));
}catch(\Exception $e) {
$output->writeln('<error>'.$e->getMessage().'</error>');
return 1;
}

Expand All @@ -73,12 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$rows[] = array($package, $url);
}

/** @var \Symfony\Component\Console\Helper\TableHelper $table */
$table = $this->getApplication()->getHelperSet()->get('table');
$table->setHeaders(array('Package', 'Src-URL'))->setRows($rows);

$output->writeln('<error> --- Invalid urls found --- </error>');
$table->render($output);
$this->printTable($rows);

return 1;
}
Expand All @@ -92,58 +82,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
*
* @param \stdClass $json
* @param array $patterns
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return array
*/
protected function searchUrlPatterns(\stdClass $json, array $patterns, OutputInterface $output)
protected function searchUrlPatterns(\stdClass $json, array $patterns)
{
$errors = array();
foreach ($json->packages as $package) {
if (!isset($package->source)) {
$this->verbose($output, 'Source not found in "' . $package->name . "\"\n", OutputInterface::VERBOSITY_VERBOSE);
$this->verbose('Source not found in "' . $package->name . "\"\n", OutputInterface::VERBOSITY_VERBOSE);
$url = '';
}else{
$url = $package->source->url;
}

$matched = false;

foreach ($patterns as $pattern) {
$regex = '|' . $pattern . '|';
$this->verbose(
$output,
"Checking src url '" . $url . "' with regex '" . $regex . "' -> ",
OutputInterface::VERBOSITY_VERBOSE
);
if (preg_match($regex, $url)) {
$this->verbose($output, "MATCHED\n", OutputInterface::VERBOSITY_VERBOSE);
$matched = true;
break;
} else {
$this->verbose($output, "NOT matched\n", OutputInterface::VERBOSITY_VERBOSE);
}
}

if (!$matched) {
if (!$this->doesUrlMatchToAtLeastOnePattern($url, $patterns)) {
$errors[$package->name] = $url;
}
}

return $errors;
}

/**
* Verbose output helper.
*
* @param OutputInterface $output
* @param $message
* @param $level
*/
private function verbose(OutputInterface $output, $message, $level)
{
if ($output->getVerbosity() >= $level) {
$output->write($message);
}
}
}

Loading

0 comments on commit b1636dc

Please sign in to comment.