add cli checks to Php code
[roojs1] / buildSDK / scss-bootstrap.php
1 <?php
2 if (php_sapi_name() != 'cli') {
3     die("CLI ONLY");
4 }
5 $cwd = getcwd();
6
7 $cc = explode('/', $cwd);
8 if (array_pop($cc) !== 'roojs1') {
9     echo "this should be run in roojs1 directory...\n";
10     exit;
11 }
12
13 ini_set('include_path', 
14     dirname(__FILE__) . '/../:' .
15     dirname(__FILE__) . '/../../pear:' .
16     ini_get('include_path')
17 );
18
19 $rootDir = dirname(__FILE__) . '/../..';
20
21 $files = array(
22     "bootstrap.scss" => array(
23         "scssDir" => "{$rootDir}/roojs1/scss/bootstrap",
24         'baseDir' => "{$rootDir}/roojs1/css-bootstrap4",
25         'name' => 'bootstrap.css',
26         'minify' => 'bootstrap.min.css',
27         'sourceMapRootpath' => '../scss/bootstrap/',
28         'variables' =>  ""
29     ),
30     
31     "roojs-bootstrap.scss" => array(
32         "scssDir" => "{$rootDir}/roojs1/scss/roojs-bootstrap",
33         'baseDir' => "{$rootDir}/roojs1/css-bootstrap4",
34         'name' => 'roojs-bootstrap-debug.css',
35         'minify' => 'roojs-bootstrap.css',
36         'sourceMapRootpath' => '../scss/roojs-bootstrap/',
37         'variables' => "@import '../bootstrap/functions';\n@import '../bootstrap/variables';"
38     ),
39     
40     // this is a test to see if we can get this theme to build..
41     // note our classic roojs-bootstrap will need a rebuild as well as it uses different variables.
42     "sb-admin-2.scss" => array(
43         "scssDir" => "{$rootDir}/roojs1/scss/startbootstrap-sb-admin-2",
44         'baseDir' => "{$rootDir}/roojs1/css-bootstrap4",
45         'name' => 'sb-admin-2.css',
46         'minify' => 'sb-admin-2.min.css',
47         'sourceMapRootpath' => '../scss/startbootstrap-sb-admin-2/',
48         'variables' =>  ""
49     ),
50     
51      
52 );
53 require_once 'System.php';
54 $sassc = System::which("sassc");
55 if (empty($sassc)) {
56     die("INSTALL sassc");
57 }
58 foreach($files as $file => $f) {
59     
60     $tmpFile = tempnam("/tmp/", "scss");
61     file_put_contents($tmpFile, "{$f['variables']}\n@import \"{$file}\";\n");
62     echo file_get_contents($tmpFile);
63     
64     
65     
66     $cmd = "{$sassc}  --sourcemap=auto -I {$f['scssDir']} $tmpFile {$f['baseDir']}/{$f['name']}";
67     echo "$cmd\n";
68     echo `$cmd`;
69     $cmd = "{$sassc} --style=compressed --sourcemap=auto -I {$f['scssDir']} $tmpFile {$f['baseDir']}/{$f['minify']}";
70     echo "$cmd\n";
71     echo `$cmd`;
72 }
73 exit;
74
75
76
77 /*
78 foreach ($files as $src => $file){
79     
80     $css = "{$file['baseDir']}/{$file['name']}";
81     
82     if(!file_exists($css)){
83         continue;
84     }
85     
86     $dir = "{$file['baseDir']}/{$file['sourceMapRootpath']}";
87     
88     $variable = "{$dir}variables.less";
89     
90     if(file_exists($variable) && filemtime("{$dir}variables.less") > filemtime($css)){
91         continue;
92     }
93     
94     $isLatest = true;
95     
96     foreach(scandir($dir) as $f) {
97                 
98         if (!strlen($f) || $f[0] == '.') {
99             continue;
100         }
101
102         $less = "{$dir}{$f}";
103         
104         if(filemtime($less) > filemtime($css)){
105             $isLatest = false;
106             break;
107         }
108     }
109     
110     if($isLatest){
111         echo "{$css} already up-to-date \n";
112         unset($files[$src]);
113     }
114     
115 }
116 */
117 require_once 'HTML/Scss.php';
118
119 foreach ($files as $src => $file){
120     
121     if(!file_exists($file['scssDir'].'/'. $src)){
122         echo "{$file['scssDir']}/{$src} does not exist...\n";
123         continue;
124     }
125     
126     try {
127         
128         $scss = new HTML_Scss();
129          
130         $scss->setSourceMap(HTML_Scss::SOURCE_MAP_FILE);
131         $scss->setSourceMapOptions(array(
132                 'sourceRoot' => $file['sourceMapRootpath'],
133         
134                 // an optional name of the generated code that this source map is associated with.
135                 'sourceMapFilename' => "{$file['baseDir']}/{$file['name']}.map",
136         
137                 // url of the map
138                 'sourceMapURL' => "{$file['name']}.map",
139         
140                 // absolute path to a file to write the map to
141                 'sourceMapWriteTo' => "{$file['baseDir']}/{$file['name']}.map",
142         
143                 // output source contents?
144                 'outputSourceFiles' => false,
145         
146                 // this is added to the file path.
147                 'sourceMapRootpath' =>  '../',
148         
149                 // this is removed from the filepath.
150                 'sourceMapBasepath' => $rootDir .'/roojs1/scss'
151             
152         ));
153        
154    
155         $css = "{$file['baseDir']}/{$file['name']}";
156         
157         echo "Compiling - {$src} To {$css}\n";
158         
159         $scss->setImportPaths($file['scssDir']);
160         $scss->setFormatter('Expanded');
161         
162         file_put_contents($css, $scss->compile("{$file['variables']}\n@import \"{$src}\";"));
163         
164         
165         $min = "{$file['baseDir']}/{$file['minify']}";
166         
167          
168         
169         echo "Minifing - {$src} To {$min}\n";
170         
171         $scss->setFormatter('Crunched');
172         
173         file_put_contents($min,  $scss->compile("{$file['variables']}\n@import \"{$src}\";")  );
174     } catch (Exception $ex) {
175         echo "scss fatal error: {$ex->getMessage()}\n";
176     }
177 }