Skip to content

Commit

Permalink
add silent mode support, fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Insolita committed Apr 10, 2018
1 parent 2afa2b0 commit a4e01b1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ run `composer dumpautoload` in your project directory

run `unused_scanner /path/to/configuration/file/scanner_config.php`

> **Docker**: run ```docker run -v `pwd`:/app tico/unused-scanner /app/path/to/configuration/file/scanner_config.php```
**For auto-testing**:

Add --silent option for skip progress output and return exit code = 1, when unused packages detected
run `unused_scanner /path/to/configuration/file/scanner_config.php --silent`

**Docker**:

run ```docker run -v `pwd`:/app tico/unused-scanner /app/path/to/configuration/file/scanner_config.php```

wait for result..

Expand Down
34 changes: 23 additions & 11 deletions unused_scanner
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ if (!isset($argv[1])) {
echo 'Missing required argument - path to config' . PHP_EOL;
exit(1);
}
$nextDirectoryCallback = function ($directory){
echo PHP_EOL.' - Scan '.$directory.PHP_EOL;
$silentMode = isset($argv[2]) && in_array($argv[2], ['--silent', '-s']);
$nextDirectoryCallback = function ($directory) use (&$silentMode){
if($silentMode === false){
echo PHP_EOL.' - Scan '.$directory.PHP_EOL;
}
};
$progressCallback = function ($done, $total) {
$width = 60;
$percentage = round(($done * 100) / $total);
$bar = round(($width * $percentage) / 100);
echo sprintf("%s%%[%s>%s]\r", $percentage, str_repeat("=", $bar), str_repeat(" ", $width-$bar));
$progressCallback = function ($done, $total) use(&$silentMode) {
if($silentMode === false) {
$width = 60;
$percentage = round(($done * 100) / $total);
$bar = round(($width * $percentage) / 100);
echo sprintf("%s%%[%s>%s]\r", $percentage, str_repeat("=", $bar), str_repeat(" ", $width - $bar));
}
};
try{
$params = require_once $argv[1];
Expand All @@ -44,15 +49,22 @@ try{
echo ' - search patterns prepared'.PHP_EOL;

$scanResult = (new Scanner($searchMap, $config, new Finder(), $nextDirectoryCallback, $progressCallback))->scan();
$result = array_diff(array_unique(array_values($searchMap)), $scanResult);
$result = array_values(array_diff(array_unique(array_values($searchMap)), $scanResult));
if(empty($result)){
echo PHP_EOL.'Unused dependencies not found!'.PHP_EOL;
exit(0);
}else{
echo PHP_EOL.'Unused dependencies found!'.PHP_EOL;
print_r(array_values($result));
echo PHP_EOL;
array_walk($result, function ($packageName){
echo ' -'.$packageName.PHP_EOL;
});
if($silentMode === true){
exit(1);
}else{
exit(0);
}
}
exit(0);

}catch (\Throwable $e){
echo 'Error! '.$e->getMessage().PHP_EOL;
echo $e->getTraceAsString().PHP_EOL;
Expand Down

0 comments on commit a4e01b1

Please sign in to comment.