Index: trunk/tools/code-utils/stylize.php |
— | — | @@ -17,27 +17,48 @@ |
18 | 18 | array_shift( $argv ); |
19 | 19 | |
20 | 20 | if ( count( $argv ) ) { |
21 | | - foreach ( $argv as $dirOrFile ) { |
| 21 | + $args = new Args( $argv ); |
| 22 | + |
| 23 | + if ( $args->flag( 'help' ) ) { |
| 24 | + echo "Usage: php createAndPromote.php [--backup|--help|--ignore=<regextoexclude>] <files/directories> |
| 25 | + backup : Creates a backup of modified files |
| 26 | + help : This message! |
| 27 | + ignore : Regex of files not to stylize e.g. .*\.i18n\.php |
| 28 | + <files/directories> : Files/directories to stylize |
| 29 | +"; |
| 30 | + |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + $ignore = $args->flag( 'ignore' ); |
| 35 | + $backup = $args->flag( 'backup' ); |
| 36 | + |
| 37 | + foreach ( $args->args as $dirOrFile ) { |
22 | 38 | if ( is_dir( $dirOrFile ) ) { |
23 | | - stylize_recursively( $dirOrFile ); |
| 39 | + stylize_recursively( $dirOrFile, $ignore, $backup ); |
24 | 40 | } else { |
25 | | - stylize_file( $dirOrFile ); |
| 41 | + stylize_file( $dirOrFile, $backup ); |
26 | 42 | } |
27 | 43 | } |
28 | 44 | } else { |
29 | 45 | stylize_file( '-' ); |
30 | 46 | } |
31 | 47 | |
32 | | -function stylize_recursively( $dir ) { |
| 48 | +function stylize_recursively( $dir, $ignore = false, $backup = false ) { |
33 | 49 | $dir = trim( $dir, "\/" ); |
34 | 50 | |
35 | 51 | foreach ( glob( "$dir/*" ) as $dirOrFile ) { |
| 52 | + if ( $ignore && preg_match( '/' . $ignore . '$/', $dirOrFile ) ) { |
| 53 | + echo "Ignoring $dirOrFile\n"; |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
36 | 57 | if ( is_dir( $dirOrFile ) ) { // It's a directory, so call this function again. |
37 | | - stylize_recursively( $dirOrFile ); |
| 58 | + stylize_recursively( $dirOrFile, $ignore, $backup ); |
38 | 59 | } elseif ( is_file( $dirOrFile ) ) { // It's a file, so let's stylize it. |
39 | 60 | // Only stylize php and js files, omitting minified js files. |
40 | 61 | if ( preg_match( '/\.(php|php5|js)$/', $dirOrFile ) && !preg_match( '/\.(min\.js)$/', $dirOrFile ) ) { |
41 | | - stylize_file( $dirOrFile, false ); |
| 62 | + stylize_file( $dirOrFile, $backup ); |
42 | 63 | } |
43 | 64 | } |
44 | 65 | } |
— | — | @@ -304,3 +325,76 @@ |
305 | 326 | return preg_replace( "#[\t ]*\n#", "\n", $s ); |
306 | 327 | } |
307 | 328 | } |
| 329 | + |
| 330 | +/** |
| 331 | + * From |
| 332 | + * http://code.google.com/p/tylerhall/source/browse/trunk/class.args.php |
| 333 | + * http://clickontyler.com/blog/2008/11/parse-command-line-arguments-in-php/ - MIT License |
| 334 | + |
| 335 | + Copyright (c) 2008 Tyler Hall |
| 336 | + |
| 337 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 338 | + of this software and associated documentation files (the "Software"), to deal |
| 339 | + in the Software without restriction, including without limitation the rights |
| 340 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 341 | + copies of the Software, and to permit persons to whom the Software is |
| 342 | + furnished to do so, subject to the following conditions: |
| 343 | + |
| 344 | + The above copyright notice and this permission notice shall be included in |
| 345 | + all copies or substantial portions of the Software. |
| 346 | + |
| 347 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 348 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 349 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 350 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 351 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 352 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 353 | + THE SOFTWARE |
| 354 | + */ |
| 355 | +class Args { |
| 356 | + private $flags; |
| 357 | + public $args; |
| 358 | + |
| 359 | + public function __construct( $argv ) { |
| 360 | + $this->flags = array(); |
| 361 | + $this->args = array(); |
| 362 | + |
| 363 | + for ( $i = 0; $i < count( $argv ); $i++ ) { |
| 364 | + $str = $argv[$i]; |
| 365 | + |
| 366 | + // --foo |
| 367 | + if ( strlen( $str ) > 2 && substr( $str, 0, 2 ) == '--' ) { |
| 368 | + $str = substr( $str, 2 ); |
| 369 | + $parts = explode( '=', $str ); |
| 370 | + $this->flags[$parts[0]] = true; |
| 371 | + |
| 372 | + // Does not have an =, so choose the next arg as its value |
| 373 | + if ( count( $parts ) == 1 && isset( $argv[$i + 1] ) && preg_match( '/^--?.+/', $argv[$i + 1] ) == 0 ) { |
| 374 | + $this->flags[$parts[0]] = $argv[$i + 1]; |
| 375 | + } elseif ( count( $parts ) == 2 ) { // Has a =, so pick the second piece |
| 376 | + $this->flags[$parts[0]] = $parts[1]; |
| 377 | + } |
| 378 | + } elseif ( strlen( $str ) == 2 && $str[0] == '-' ) { // -a |
| 379 | + $this->flags[$str[1]] = true; |
| 380 | + if ( isset( $argv[$i + 1] ) && preg_match( '/^--?.+/', $argv[$i + 1] ) == 0 ) |
| 381 | + $this->flags[$str[1]] = $argv[$i + 1]; |
| 382 | + } elseif ( strlen( $str ) > 1 && $str[0] == '-' ) { // -abcdef |
| 383 | + for ( $j = 1; $j < strlen( $str ); $j++ ) |
| 384 | + $this->flags[$str[$j]] = true; |
| 385 | + } |
| 386 | + } |
| 387 | + |
| 388 | + for ( $i = count( $argv ) - 1; $i >= 0; $i-- ) { |
| 389 | + if ( preg_match( '/^--?.+/', $argv[$i] ) == 0 ) |
| 390 | + $this->args[] = $argv[$i]; |
| 391 | + else |
| 392 | + break; |
| 393 | + } |
| 394 | + |
| 395 | + $this->args = array_reverse( $this->args ); |
| 396 | + } |
| 397 | + |
| 398 | + public function flag( $name ) { |
| 399 | + return isset( $this->flags[$name] ) ? $this->flags[$name] : false; |
| 400 | + } |
| 401 | +} |