|
| 1 | +<? |
| 2 | +/** |
| 3 | + * tool for command line |
| 4 | + */ |
| 5 | +class tool { |
| 6 | + |
| 7 | + var $argc = '', |
| 8 | + $argv = '', |
| 9 | + $sort = null, |
| 10 | + $in = null, |
| 11 | + $out = null; |
| 12 | + |
| 13 | +/** |
| 14 | + * print man |
| 15 | + */ |
| 16 | +function man(){ |
| 17 | +?> |
| 18 | + |
| 19 | +CSSComb 2.14 (build 57f634a-1307080853) Command line tool for resort CSS code. |
| 20 | + |
| 21 | +SYNOPSIS |
| 22 | + $ php <?php echo $this->argv[0]; ?> -s <file with JSON array> -i <path to input css file> -o <path to result css file> |
| 23 | + |
| 24 | +DESCRIPTION |
| 25 | + options: |
| 26 | + -s, --sort-order specify file with custom sort order. File must contain JSON data. |
| 27 | + -i, --input must be directory name or file that needs to be sorted |
| 28 | + -o, --output sort result file. Use output when input is not a directory name, otherwise output param ignored. If output filename does not exist, the file is created. Otherwise, the existing file is overwritten. |
| 29 | + |
| 30 | + --help, -help, -h, -? or no options will print this man. |
| 31 | + |
| 32 | +EXAMPLE |
| 33 | + <?php echo $this->argv[0]; ?> -s my-custom-sort-order.json -i css/style.css -o css/style-resorted.css |
| 34 | + |
| 35 | +EXAMPLE 2 |
| 36 | + <?php echo $this->argv[0]; ?> -i some_directory_name |
| 37 | + |
| 38 | +SEE ALSO |
| 39 | + http://csscomb.com/ |
| 40 | + |
| 41 | +<?php |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * init |
| 46 | + */ |
| 47 | +function init($argc, $argv) { |
| 48 | + $this->argc = $argc; |
| 49 | + $this->argv = $argv; |
| 50 | + |
| 51 | + for ($i = 0; $i < $this->argc; $i++) { |
| 52 | + switch ($this->argv[$i]) { |
| 53 | + case '-s': |
| 54 | + $this->sort = $this->argv[$i+1]; |
| 55 | + echo "sort order from: ".$this->sort."\n"; |
| 56 | + break; |
| 57 | + |
| 58 | + case '-i': |
| 59 | + $this->in = $this->argv[$i+1]; |
| 60 | + break; |
| 61 | + |
| 62 | + case '-o': |
| 63 | + $this->out = $this->argv[$i+1]; |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +function tool($argc, $argv) { |
| 72 | + $this->init($argc, $argv); |
| 73 | + |
| 74 | + if($this->argc == 1 || in_array($this->argv[1], array('--help', '-help', '-h', '-?'))) { |
| 75 | + $this->man(); |
| 76 | + exit(1); |
| 77 | + } |
| 78 | + else { |
| 79 | + |
| 80 | + if($this->in != null) { |
| 81 | + |
| 82 | + if ($this->sort) { |
| 83 | + $sort = file_get_contents($this->sort); |
| 84 | + } else { |
| 85 | + $sort = 'zen'; |
| 86 | + } |
| 87 | + $c = new csscomb('', false, $sort); |
| 88 | + |
| 89 | + if(is_dir($this->in)) { |
| 90 | + $files = $this->getArrayOfCssFilenames($this->in); |
| 91 | + |
| 92 | + foreach($files as $file) { |
| 93 | + echo "Sorting ".$file."...\n"; |
| 94 | + $result = $c->csscomb(file_get_contents($file), false, $sort); |
| 95 | + file_put_contents($file, $result); |
| 96 | + } |
| 97 | + } |
| 98 | + elseif(is_file($this->in) && preg_match('/^text/', mime_content_type($this->in))){ |
| 99 | + echo "Sorting ".$this->in."...\n"; |
| 100 | + $result = $c->csscomb(file_get_contents($this->in), false, $sort); |
| 101 | + if($this->out == null){ |
| 102 | + $this->out = $this->in; |
| 103 | + } |
| 104 | + file_put_contents($this->out, $result); |
| 105 | + } |
| 106 | + elseif(!preg_match('/^text/', mime_content_type($this->in))){ |
| 107 | + echo("Wrong input file mime type."); |
| 108 | + exit(1); |
| 109 | + } |
| 110 | + else{ |
| 111 | + echo("Error with input file."); |
| 112 | + exit(1); |
| 113 | + } |
| 114 | + echo "Done.\n"; |
| 115 | + exit(0); |
| 116 | + |
| 117 | + |
| 118 | + } else { |
| 119 | + echo "No input file\n"; |
| 120 | + $this->man(); |
| 121 | + exit(1); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Возвращает массив с путями до CSS-файлов |
| 128 | + * @param: $dir директория, где искать |
| 129 | + * @param: $extensions массив с расширениями файлов, в которых CSS-код. |
| 130 | + */ |
| 131 | +function getArrayOfCssFilenames($dir, $extensions = Array('.css','.sass','.less','.scss')) { |
| 132 | + $files = Array(); |
| 133 | + |
| 134 | + if($handle = opendir($dir)) { |
| 135 | + while(false !== ($filename = readdir($handle))) { |
| 136 | + if($filename != '.' && $filename != '..') { |
| 137 | + if(is_dir($dir.'/'.$filename)) { |
| 138 | + $files = array_merge($files, $this->getArrayOfCssFilenames($dir.'/'.$filename, $extensions)); |
| 139 | + } |
| 140 | + else { |
| 141 | + $pos = strrpos($filename, '.'); |
| 142 | + $ext = substr($filename, $pos, strlen($filename) - $pos); |
| 143 | + |
| 144 | + if($extensions) { |
| 145 | + if(in_array($ext, $extensions)) { |
| 146 | + $files[] = $dir.'/'.$filename; |
| 147 | + } |
| 148 | + } |
| 149 | + else { |
| 150 | + $files[] = $dir.'/'.$filename; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + closedir($handle); |
| 156 | + } |
| 157 | + return $files; |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +} |
| 163 | + |
| 164 | +$tool = new tool($argc, $argv); |
| 165 | +?> |
0 commit comments