Index: trunk/tools/code-utils/stylize.php |
— | — | @@ -3,23 +3,46 @@ |
4 | 4 | /** |
5 | 5 | * A PHP code beautifier aimed at adding lots of spaces to files that lack them, |
6 | 6 | * in keeping with MediaWiki's spacey site style. |
| 7 | + * |
| 8 | + * @author tstarling |
| 9 | + * @author Jeroen De Dauw |
7 | 10 | */ |
8 | 11 | |
9 | 12 | |
10 | 13 | if ( php_sapi_name() != 'cli' ) { |
11 | | - print "This script must be run from the command line\n"; |
| 14 | + echo "This script must be run from the command line\n"; |
12 | 15 | exit( 1 ); |
13 | 16 | } |
| 17 | + |
14 | 18 | array_shift( $argv ); |
| 19 | + |
15 | 20 | if ( count( $argv ) ) { |
16 | | - foreach ( $argv as $filename ) { |
17 | | - stylize_file( $filename ); |
| 21 | + foreach ( $argv as $dirOrFile ) { |
| 22 | + if ( is_dir( $dirOrFile ) ) { |
| 23 | + stylize_recursivly( $dirOrFile ); |
| 24 | + } else { |
| 25 | + stylize_file( $dirOrFile ); |
| 26 | + } |
18 | 27 | } |
19 | 28 | } else { |
20 | 29 | stylize_file( '-' ); |
21 | 30 | } |
22 | 31 | |
23 | | -function stylize_file( $filename ) { |
| 32 | +function stylize_recursivly( $dir ) { |
| 33 | + foreach ( glob("$dir/*") as $dirOrFile ) { |
| 34 | + if ( is_dir( $dirOrFile ) ) { // It's a directory, so call this function again. |
| 35 | + stylize_recursivly( $dirOrFile ); |
| 36 | + } elseif ( is_file( $dirOrFile ) ) { // It's a file, so let's stylize it. |
| 37 | + // Only stylize php and js files, omitting minified js files. |
| 38 | + if ( preg_match( '/\.(php|php5|js)$/', $dirOrFile ) && !preg_match( '/\.(min\.js)$/', $dirOrFile ) ) { |
| 39 | + stylize_file( $dirOrFile, false ); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function stylize_file( $filename, $backup = true ) { |
| 46 | + echo "Stylizing file $filename\n"; |
24 | 47 | if ( $filename == '-' ) { |
25 | 48 | $s = file_get_contents( '/dev/stdin' ); |
26 | 49 | if ( $s === false ) { |
— | — | @@ -35,7 +58,7 @@ |
36 | 59 | } |
37 | 60 | $stylizer = new Stylizer( $s ); |
38 | 61 | $s = $stylizer->stylize(); |
39 | | - rename( $filename, "$filename~" ); |
| 62 | + if ( $backup ) rename( $filename, "$filename~" ); |
40 | 63 | file_put_contents( $filename, $s ); |
41 | 64 | } |
42 | 65 | } |