Skip to content

Commit

Permalink
added case insensitive search
Browse files Browse the repository at this point in the history
  • Loading branch information
visavi committed Oct 23, 2017
1 parent 17c3445 commit 0684443
Showing 1 changed file with 64 additions and 34 deletions.
98 changes: 64 additions & 34 deletions cleanup
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ if (isset($options['exclude'])) {
}

if (isset($options['h']) || isset($options['help'])) {
echo "Cleaning composer vendor directory".PHP_EOL.PHP_EOL;
echo "Cleaning composer vendor directory" . PHP_EOL . PHP_EOL;

echo "\e[0;33mUsage:\e[0m".PHP_EOL;
echo " command [options] [arguments]".PHP_EOL.PHP_EOL;
echo "\e[0;33mUsage:\e[0m" . PHP_EOL;
echo " command [options] [arguments]" . PHP_EOL . PHP_EOL;

echo "\e[0;33mOptions:\e[0m".PHP_EOL;
echo " \e[0;32m-h, --help \e[0m Display this help message".PHP_EOL;
echo " \e[0;32m-i, --include \e[0m Add patterns for common files, comma separated".PHP_EOL;
echo " \e[0;32m-e, --exclude \e[0m Remove patterns for common files, comma separated".PHP_EOL.PHP_EOL;
echo "\e[0;33mOptions:\e[0m" . PHP_EOL;
echo " \e[0;32m-h, --help \e[0m Display this help message" . PHP_EOL;
echo " \e[0;32m-i, --include \e[0m Add patterns for common files, comma separated" . PHP_EOL;
echo " \e[0;32m-e, --exclude \e[0m Remove patterns for common files, comma separated" . PHP_EOL . PHP_EOL;

echo "\e[0;33mExample:\e[0m".PHP_EOL;
echo " ./vendor/bin/cleanup --include *.lock,*.txt --exclude doc,docs,test".PHP_EOL;
echo "\e[0;33mExample:\e[0m" . PHP_EOL;
echo " ./vendor/bin/cleanup --include *.lock,*.txt --exclude doc,docs,test" . PHP_EOL;
return;
}

Expand All @@ -32,36 +32,34 @@ $patterns = [
'.github',
'test',
'tests',
'Tests',
'travis',
'demo',
'example',
'examples',
'doc',
'docs',
'README*',
'LICENSE*',
'CHANGELOG*',
'CHANGES*',
'FAQ*',
'CONTRIBUTING*',
'HISTORY*',
'UPGRADING*',
'UPGRADE*',
'license*',
'changelog*',
'changes*',
'faq*',
'contributing*',
'history*',
'upgrading*',
'upgrade*',
'package*',
'readme*',
'Dockerfile',
'.scrutinizer',
'phpunit.xml*',
'phpunit.php',
'phpunit',
'phpunit*',
'*.yml',
'*.md',
'*.xml',
'.php_cs*',
'.scrutinizer',
'.gitignore',
'.gitattributes',
'.editorconfig',
'dockerfile',
'composer.json',
'composer.lock',
];
Expand All @@ -73,44 +71,76 @@ if (isset($options['i'])) {
if (isset($options['e'])) {
$patterns = array_diff($patterns, explode(',', $options['e']));
}

function expandTree($dir) {
/**
* Recursively traverses the directory tree
*
* @param string $dir
* @return array
*/
function expandTree($dir)
{
$directories = [];
$files = array_diff(scandir($dir), ['.', '..']);
foreach($files as $file) {
$directory = $dir.'/'.$file;
$directory = $dir . '/' . $file;
if(is_dir($directory)) {
$directories[] = $directory;
$directories = array_merge($directories, expandTree($directory));
}
}

return $directories;
}


function delTree($dir) {
/**
* Recursively deletes the directory
*
* @param string $dir
* @return bool
*/
function delTree($dir)
{
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
(is_dir($dir.'/'.$file)) ? delTree($dir.'/'.$file) : unlink($dir.'/'.$file);
(is_dir($dir . '/' . $file)) ? delTree($dir . '/' . $file) : unlink($dir . '/' . $file);
}

return rmdir($dir);
}

/**
* Prepare word
*
* @param string $matches
* @return string
*/
function prepareWord($matches)
{
return '[' . strtolower($matches[1]) . strtoupper($matches[1]) . ']';
}

$directories = expandTree(dirname(dirname(__DIR__)));

foreach ($patterns as $pattern) {
foreach ($directories as $directory) {
foreach (glob($directory .'/'. $pattern) as $file) {
foreach ($directories as $directory) {
foreach ($patterns as $pattern) {

$casePattern = preg_replace_callback('/([a-z])/i', 'prepareWord', $pattern);

foreach (glob($directory . '/' . $casePattern) as $file) {

if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
continue;
}

echo 'delete: '.$file.PHP_EOL;
echo 'delete: ' . $file . PHP_EOL;

if (is_dir($file)) {
delTree($file);
} else {
unlink($file);
unlink($file);
}
}
}
}

echo 'success'.PHP_EOL;
echo 'success' . PHP_EOL;

0 comments on commit 0684443

Please sign in to comment.