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 #3 from silpion/feature/check_source
Browse files Browse the repository at this point in the history
Added check:src command. Added --allow-empty flags.
  • Loading branch information
h4cc committed Jun 25, 2014
2 parents ca17b8e + f67dd35 commit 1d1bdb9
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 14 deletions.
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ before_script:

script:
# Check Script.
- php bin/composer-checker check:dist -p github composer.lock | grep "All urls valid."
- php bin/composer-checker check:dist -p giub composer.lock | grep "Invalid urls found"
- 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 PHAR.
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php composer-checker.phar check:dist -p github composer.lock | grep "All urls valid."; fi;'
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php composer-checker.phar check:dist -p giub composer.lock | grep "Invalid urls found"; 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;'
- 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;'
23 changes: 15 additions & 8 deletions Command/CheckDistCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ protected function configure()
->setDescription('Matching the dist urls in a composer.lock file against some patterns.')
->addArgument('file', InputArgument::REQUIRED, 'composer.lock file to check')
->addOption(
'url-pattern',
'url-pattern',
'p',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Regex-Patterns for dist-urls.'
);
)
->addOption('allow-empty', null, InputOption::VALUE_NONE, 'Will allow empty dist urls.');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -47,6 +48,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

if($input->getOption('allow-empty')) {
$patterns[] = '^$';
}

$content = @file_get_contents($input->getArgument('file'));
if (!$content) {
$output->writeln('<error>File not found.</error>');
Expand Down Expand Up @@ -95,20 +100,22 @@ protected function searchUrlPatterns(\stdClass $json, array $patterns, OutputInt
$errors = array();
foreach ($json->packages as $package) {
if (!isset($package->dist)) {
$output->writeln('Dist not found in "' . $package->name . '"');
$this->verbose($output, 'Dist not found in "' . $package->name . "\"\n", OutputInterface::VERBOSITY_VERBOSE);
$url = '';
}else{
$url = $package->dist->url;
}
$dist = $package->dist;

$matched = false;

foreach ($patterns as $pattern) {
$regex = '|' . $pattern . '|';
$this->verbose(
$output,
"Checking dist url '" . $dist->url . "' with regex '" . $regex . "' -> ",
"Checking dist url '" . $url . "' with regex '" . $regex . "' -> ",
OutputInterface::VERBOSITY_VERBOSE
);
if (preg_match($regex, $dist->url)) {
if (preg_match($regex, $url)) {
$this->verbose($output, "MATCHED\n", OutputInterface::VERBOSITY_VERBOSE);
$matched = true;
break;
Expand All @@ -118,7 +125,7 @@ protected function searchUrlPatterns(\stdClass $json, array $patterns, OutputInt
}

if (!$matched) {
$errors[$package->name] = $dist->url;
$errors[$package->name] = $url;
}
}

Expand Down
149 changes: 149 additions & 0 deletions Command/CheckSrcCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

/*
* This file is part of the Silpion/ComposerChecker package.
*
* (c) Julius Beckmann <beckmann@silpion.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Silpion\ComposerChecker\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command for checking the src urls.
*
* @author Julius Beckmann <beckmann@silpion.de>
*/
class CheckSrcCommand extends Command
{
protected function configure()
{
$this
->setName('check:src')
->setDescription('Matching the src urls in a composer.lock file against some patterns.')
->addArgument('file', InputArgument::REQUIRED, 'composer.lock file to check')
->addOption(
'url-pattern',
'p',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Regex-Patterns for src-urls.'
)
->addOption('allow-empty', null, InputOption::VALUE_NONE, 'Will allow empty src urls.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$patterns = $input->getOption('url-pattern');
if (!$patterns) {
$output->writeln('<error>Need at least one url-pattern.</error>');

return 1;
}

if($input->getOption('allow-empty')) {
$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>');

return 1;
}

$errors = $this->searchUrlPatterns($json, $patterns, $output);
if ($errors) {
$rows = array();
foreach ($errors as $package => $url) {
$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);

return 1;
}

$output->writeln('<info>All urls valid.</info>');
}

/**
* 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)
{
$errors = array();
foreach ($json->packages as $package) {
if (!isset($package->source)) {
$this->verbose($output, '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) {
$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);
}
}
}

15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Usage
list Lists commands
check
check:dist Matching the dist urls in a composer.lock file against some patterns.
check:src Matching the src urls in a composer.lock file against some patterns.


Check: Dist-Urls
Expand Down Expand Up @@ -43,6 +44,20 @@ It is also possible to enforce to use only "https" dist-urls with a pattern like

$ php bin/composer-checker check:dist -p "^https://" composer.lock

Allowing empty or missing dist urls can be done with the `--allow-empty` switch.


Check: Source-Urls
---------------------

Parallel to the dist urls, the source urls can be checked too.

$ php bin/composer-checker check:src -p "git@git.example.com/foo.git" composer.lock


Allowing empty or missing source urls can be done with the `--allow-empty` switch.


LICENSE
-------

Expand Down
4 changes: 3 additions & 1 deletion bin/composer-checker
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ require_once call_user_func(function() {
});

use Silpion\ComposerChecker\Command\CheckDistCommand;
use Silpion\ComposerChecker\Command\CheckSrcCommand;
use Symfony\Component\Console\Application;

$application = new Application();
$application->setName('Composer Checker');
$application->setVersion('1.0');
$application->setVersion('0.1');
$application->add(new CheckDistCommand());
$application->add(new CheckSrcCommand());
$application->run();
35 changes: 35 additions & 0 deletions test/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"require": {
"symfony/console": "*",
"smarty/smarty": "*",
"javascript/jquery": "*"
},
"repositories": [
{
"type": "package",
"package": {
"name": "smarty/smarty",
"version": "3.1.7",
"source": {
"url": "http://smarty-php.googlecode.com/svn/",
"type": "svn",
"reference": "tags/Smarty_3_1_7/distribution/"
},
"autoload": {
"classmap": ["libs/"]
}
}
},
{
"type": "package",
"package": {
"name": "javascript/jquery",
"version": "1.7.2",
"dist": {
"url": "http://code.jquery.com/jquery-1.7.2.js",
"type": "file"
}
}
}
]
}
Loading

0 comments on commit 1d1bdb9

Please sign in to comment.