├── cleanup ├── composer.json └── readme.md /cleanup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | $value) { 8 | $options[$key[0]] = $value; 9 | } 10 | 11 | echo 'Cleanup v' . $version . PHP_EOL . PHP_EOL; 12 | 13 | if (isset($options['h'])) { 14 | echo "\e[0;33mUsage:\e[0m" . PHP_EOL; 15 | echo ' command [options] [arguments]' . PHP_EOL . PHP_EOL; 16 | 17 | echo "\e[0;33mOptions:\e[0m" . PHP_EOL; 18 | echo " \e[0;32m-h, --help \e[0m Display this help message" . PHP_EOL; 19 | echo " \e[0;32m-i, --include \e[0m Add patterns for common files, comma separated" . PHP_EOL; 20 | echo " \e[0;32m-e, --exclude \e[0m Remove patterns for common files, comma separated" . PHP_EOL; 21 | echo " \e[0;32m-v, --verbose \e[0m Increase the verbosity of messages" . PHP_EOL; 22 | echo " \e[0;32m-p, --path \e[0m Find on specific path" . PHP_EOL; 23 | echo " \e[0;32m-d, --dry \e[0m Run without actual remove" . PHP_EOL . PHP_EOL; 24 | 25 | echo "\e[0;33mExample:\e[0m" . PHP_EOL; 26 | echo ' ./vendor/bin/cleanup -v --path symfony --include *.zip,*.rar --exclude doc,docs,test' . PHP_EOL; 27 | return; 28 | } 29 | 30 | // Default patterns for common files 31 | $patterns = [ 32 | '.git', 33 | '.github', 34 | 'test', 35 | 'tests', 36 | 'travis', 37 | 'demo', 38 | 'example', 39 | 'examples', 40 | 'doc', 41 | 'docs', 42 | 'license', 43 | 'changelog*', 44 | 'changes*', 45 | 'faq*', 46 | 'contributing*', 47 | 'history*', 48 | 'upgrading*', 49 | 'upgrade*', 50 | 'readme*', 51 | '{,.}*.yml', 52 | '*.md', 53 | '*.xml', 54 | '*.txt', 55 | '*.dist', 56 | '*.neon', 57 | '.php_cs*', 58 | '.scrutinizer', 59 | '.gitignore', 60 | '.gitattributes', 61 | '.editorconfig', 62 | '.phpstorm.meta.php', 63 | 'dockerfile', 64 | 'composer.lock', 65 | ]; 66 | 67 | $dirname = dirname(__DIR__, 2); 68 | 69 | if (isset($options['p'])) { 70 | $dirname .= '/' . $options['p']; 71 | 72 | if (! file_exists($dirname)) { 73 | echo 'Directory not found!' . PHP_EOL; 74 | return; 75 | } 76 | } 77 | 78 | if (isset($options['i'])) { 79 | $patterns = array_merge($patterns, explode(',', $options['i'])); 80 | } 81 | 82 | if (isset($options['e'])) { 83 | $patterns = array_diff($patterns, explode(',', $options['e'])); 84 | } 85 | 86 | /** 87 | * Recursively traverses the directory tree 88 | * 89 | * @param string $dirname 90 | * @return array 91 | */ 92 | function expandTree($dirname) 93 | { 94 | $directories = []; 95 | $files = array_diff(scandir($dirname), ['.', '..']); 96 | foreach($files as $file) { 97 | $directory = $dirname . '/' . $file; 98 | if(is_dir($directory)) { 99 | $directories[] = $directory; 100 | $directories = array_merge($directories, expandTree($directory)); 101 | } 102 | } 103 | 104 | return $directories; 105 | } 106 | 107 | /** 108 | * Recursively deletes the directory 109 | * 110 | * @param string $dirname 111 | * @return bool 112 | */ 113 | function delTree($dirname) 114 | { 115 | $files = array_diff(scandir($dirname), ['.', '..']); 116 | foreach ($files as $file) { 117 | is_dir($dirname . '/' . $file) ? delTree($dirname . '/' . $file) : unlink($dirname . '/' . $file); 118 | } 119 | 120 | return rmdir($dirname); 121 | } 122 | 123 | /** 124 | * Prepare word 125 | * 126 | * @param string $matches 127 | * @return string 128 | */ 129 | function prepareWord($matches) 130 | { 131 | return '[' . strtolower($matches[1]) . strtoupper($matches[1]) . ']'; 132 | } 133 | 134 | $objects = 0; 135 | $directories = expandTree($dirname); 136 | 137 | foreach ($directories as $directory) { 138 | foreach ($patterns as $pattern) { 139 | 140 | $casePattern = preg_replace_callback('/([a-z])/i', 'prepareWord', $pattern); 141 | 142 | foreach (glob($directory . '/' . $casePattern, GLOB_BRACE) as $file) { 143 | 144 | if (isset($options['v']) || isset($options['d'])) { 145 | echo $file . PHP_EOL; 146 | } else { 147 | echo '.'; 148 | } 149 | 150 | if (! isset($options['d'])) { 151 | if (is_dir($file)) { 152 | delTree($file); 153 | } else { 154 | unlink($file); 155 | } 156 | } 157 | 158 | usleep(3000); 159 | $objects++; 160 | } 161 | } 162 | } 163 | 164 | if ($objects) { 165 | if (isset($options['d'])) { 166 | echo PHP_EOL . $objects . ' object(s) will be deleted!' . PHP_EOL; 167 | } else { 168 | echo PHP_EOL . $objects . ' object(s) successfully deleted!' . PHP_EOL; 169 | } 170 | } else { 171 | echo 'No objects found for deletion!' . PHP_EOL; 172 | } 173 | 174 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "visavi/cleanup", 3 | "description": "Cleaning vendor directory", 4 | "keywords": ["composer", "vendor", "clean"], 5 | "homepage": "http://visavi.net", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Alexander Grigorev", 10 | "email": "admin@visavi.net", 11 | "homepage": "http://visavi.net" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.4.0" 16 | }, 17 | "minimum-stability": "stable", 18 | "bin": ["cleanup"] 19 | } 20 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Cleanup 2 | ========= 3 | 4 | [![Latest Stable Version](https://poser.pugx.org/visavi/cleanup/v/stable)](https://packagist.org/packages/visavi/cleanup) 5 | [![Total Downloads](https://poser.pugx.org/visavi/cleanup/downloads)](https://packagist.org/packages/visavi/cleanup) 6 | [![Latest Unstable Version](https://poser.pugx.org/visavi/cleanup/v/unstable)](https://packagist.org/packages/visavi/cleanup) 7 | [![License](https://poser.pugx.org/visavi/cleanup/license)](https://packagist.org/packages/visavi/cleanup) 8 | 9 | ## Cleaning composer vendor directory 10 | 11 | It cleans up any tests, descriptions, documentation, examples, etc. 12 | 13 | * .git 14 | * .github 15 | * test 16 | * tests 17 | * travis 18 | * demo 19 | * example 20 | * examples 21 | * doc 22 | * docs 23 | * license 24 | * changelog* 25 | * changes* 26 | * faq* 27 | * contributing* 28 | * history* 29 | * upgrading* 30 | * upgrade* 31 | * readme* 32 | * {,.}*.yml 33 | * *.md 34 | * *.xml 35 | * *.txt 36 | * *.dist 37 | * *.neon 38 | * .php_cs* 39 | * .scrutinizer 40 | * .gitignore 41 | * .gitattributes 42 | * .editorconfig 43 | * .phpstorm.meta.php 44 | * dockerfile 45 | * composer.lock 46 | 47 | ## Installing 48 | 49 | ``` 50 | composer require visavi/cleanup 51 | ``` 52 | 53 | ## Run 54 | ``` 55 | ./vendor/bin/cleanup 56 | ``` 57 | 58 | ## Option 59 | --help (-h) - Display help message 60 | 61 | --include (-i) - include new rules pattern 62 | 63 | --exclude (-e) - excludes from the pattern rule 64 | 65 | --verbose (-v) - Increase the verbosity of messages 66 | 67 | --path (-p) - Find on specific path 68 | 69 | --dry (-d) - Run without actual remove 70 | 71 | *the list of arguments must be passed by a comma* 72 | 73 | ## Example 74 | ``` 75 | ./vendor/bin/cleanup -v --path symfony --include *.zip,*.rar --exclude doc,docs,test 76 | ``` 77 | 78 | ## License 79 | 80 | The class is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) 81 | --------------------------------------------------------------------------------