add cli checks to Php code
[roojs1] / buildSDK / less-bootstrap-compiler.php
1 <?php
2
3 if (php_sapi_name() != 'cli') {
4     die("CLI ONLY");
5 }
6
7 $cwd = getcwd();
8
9 $cc = explode('/', $cwd);
10 if (array_pop($cc) !== 'roojs1') {
11     echo "this should be run in roojs1 directory...\n";
12     exit;
13 }
14
15 ini_set('include_path', 
16     dirname(__FILE__) . '/../:' .
17     dirname(__FILE__) . '/../../pear:' .
18     ini_get('include_path')
19 );
20
21 $rootDir = dirname(__FILE__) . '/../..';
22
23 $files = array(
24     "{$rootDir}/roojs1/less/bootstrap/bootstrap.less" => array(
25         'baseDir' => "{$rootDir}/roojs1/css-bootstrap",
26         'name' => 'bootstrap.css',
27         'minify' => 'bootstrap.min.css',
28         'sourceMapRootpath' => '../less/bootstrap/',
29         'variables' => array("@import 'variables.less';")
30     ),
31     "{$rootDir}/roojs1/less/bootstrap/bootstrap-light.less" => array(
32         'baseDir' => "{$rootDir}/roojs1/css-bootstrap",
33         'name' => 'bootstrap-light.css',
34         'minify' => 'bootstrap-light.min.css',
35         'sourceMapRootpath' => '../less/bootstrap/',
36         'variables' => array("@import 'variables.less';")
37     ),
38     
39     "{$rootDir}/roojs1/less/roojs-bootstrap/roojs-bootstrap.less" => array(
40         'baseDir' => "{$rootDir}/roojs1/css-bootstrap",
41         'name' => 'roojs-bootstrap-debug.css',
42         'minify' => 'roojs-bootstrap.css',
43         'sourceMapRootpath' => '../less/roojs-bootstrap/',
44         'variables' => array("@import '../bootstrap/variables.less';")
45     )
46 );
47
48 foreach ($files as $src => $file){
49     
50     $css = "{$file['baseDir']}/{$file['name']}";
51     
52     if(!file_exists($css)){
53         continue;
54     }
55     
56     $dir = "{$file['baseDir']}/{$file['sourceMapRootpath']}";
57     
58     $variable = "{$dir}variables.less";
59     
60     if(file_exists($variable) && filemtime("{$dir}variables.less") > filemtime($css)){
61         continue;
62     }
63     
64     $isLatest = true;
65     
66     foreach(scandir($dir) as $f) {
67                 
68         if (!strlen($f) || $f[0] == '.') {
69             continue;
70         }
71
72         $less = "{$dir}{$f}";
73         
74         if(filemtime($less) > filemtime($css)){
75             $isLatest = false;
76             break;
77         }
78     }
79     
80     if($isLatest){
81         echo "{$css} already up-to-date \n";
82         unset($files[$src]);
83     }
84     
85 }
86
87 require_once 'HTML/Less.php';
88
89 foreach ($files as $src => $file){
90
91     if(!file_exists($src)){
92         echo "{$less} does not exist...\n";
93         continue;
94     }
95     
96     try {
97         
98         $less = new HTML_Less();
99         
100         $less->setOptions(array(
101             'variables' => $file['variables'],
102             'sourceMap' => true,
103             'sourceMapWriteTo' => "{$file['baseDir']}/{$file['name']}.map",
104             'sourceMapURL' => "{$file['name']}.map",
105             'sourceMapRootpath' => $file['sourceMapRootpath'],
106             'sourceMapBasepath' => dirname(realpath($src))
107         ));
108         
109         $css = "{$file['baseDir']}/{$file['name']}";
110         
111         echo "Compiling - {$src} To {$css}\n";
112         
113         $less->compileFile($src, $css);
114         
115         $min = "{$file['baseDir']}/{$file['minify']}";
116         
117         $less = new HTML_Less();
118         
119         $less->setOptions(array(
120             'compress' => true,
121             'variables' => $file['variables'],
122             'sourceMap' => true,
123             'sourceMapWriteTo' => "{$file['baseDir']}/{$file['minify']}.map",
124             'sourceMapURL' => "{$file['minify']}.map",
125             'sourceMapRootpath' => $file['sourceMapRootpath'],
126             'sourceMapBasepath' => dirname(realpath($src))
127         ));
128         
129         echo "Minifing - {$src} To {$min}\n";
130         
131         $less->compileFile($src, $min);
132         
133     } catch (Exception $ex) {
134         echo "lessphp fatal error: {$ex->getMessage()}\n";
135     }
136 }