forked from crosswalk-project/crosswalk-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.css.php
147 lines (129 loc) · 3.38 KB
/
generate.css.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
header('Content-type: text/css');
$sass = getenv('SASS');
if (!$sass) {
$sass = 'sass';
}
// get the name of the css.php file to be processed;
// on the server, it's the SCRIPT_FILENAME (i.e. the php script
// which require'd this script);
// on the command line, it's the name of the script passed to the php command
$f = '';
if (isset($_SERVER)) {
$f = $_SERVER['SCRIPT_FILENAME'];
}
else if (PHP_SAPI === 'cli') {
$f = $argv[0];
}
$basename = preg_replace ('/\.css\.php$/', '', basename($f));
$scss = @stat ($basename.'.scss');
$css = @stat ($basename.'.css');
$script = @stat (__FILE__);
$rebuild = false;
if (!$css ||
$scss['mtime'] > $css['mtime'] ||
$script['mtime'] > $css['mtime']) {
$rebuild = true;
}
if (!$rebuild) {
success ();
}
function success () {
global $basename;
require ($basename.'.css');
exit;
}
function failure ($msg) {
global $basename;
?>
/*
* <?= strftime ("%F %r") ?>
*
* scss => css conversion failed:
* <?= $msg ?>
*/
body {
background: red;
color: red;
}
<?php
exit;
}
if (@file_exists ('.'.$basename.'.css')) {
@unlink ('.'.$basename.'.css');
}
if (@file_exists ('.'.$basename.'.msg')) {
@unlink ('.'.$basename.'.msg');
}
$sourcemaps = '';
$p = @popen ($sass.' -v', 'r');
if (!$p) {
failure ('sass not found in path', $sass);
}
while (!feof ($p)) {
$l = trim (fgets ($p));
if (preg_match ('/^[^0-9]*([0-9]*\.[0-9]*)\..*$/', $l, $matches)) {
if ($matches[1] >= 3.3) {
$sourcemaps = ' --sourcemap ';
}
break;
}
}
pclose ($p);
# -C turns off the cache
$cmd = $sass.' -C '.$sourcemaps.' '.$basename.'.scss:.'.$basename.'.css';
$p = @popen ($cmd.' 2>&1', 'r');
if (!$p) {
failure ('Error executing: '.$cmd);
}
/* Write any output from the sass command into BASENAME.msg for later retrieval (if needed) */
$m = @fopen ($basename.'.msg', 'w');
if (!$m) {
failure ('Unable to open '.$basename.'.msg for writing');
}
fwrite ($m, "/* $basename.'.css' generated ".strftime ("%F %r")." via:\n *\n * ".$cmd."\n".
" * sass output:\n");
while (!feof ($p)) {
fwrite ($m, fgets ($p));
}
pclose ($p);
$f = @fopen ('.'.$basename.'.css', 'r');
if (!$f) {
fwrite ($m, " * Temporary file .'.$basename.'.css not found after conversion.\n");
fwrite ($m, " */\n");
fclose ($m);
failure ('Could not open .'.$basename.'.css after conversion.');
}
fwrite ($m, " */\n");
fclose ($m);
/* Translate .BASENAME.css => BASENAME.css replacing the string
* '.BASENAME.css' with 'BASENAME.css'
*/
$c = @fopen ($basename.'.css', 'w');
if (!$c) {
failure ('Can not create '.$basename.'.css');
}
while (!feof ($f)) {
fwrite ($c, preg_replace ('/\.'.$basename.'\.css/', $basename.'.css', fgets ($f)));
}
fclose ($f);
fclose ($c);
/* And then remove the old .'.$basename.'.css */
unlink ('.'.$basename.'.css');
if (file_exists ('.'.$basename.'.css.map')) {
/* Translate .BASENAME.css.map => BASENAME.css.map, replacing the string
* '.BASENAME.css' with 'BASENAME.css'
*/
$f = @fopen ('.'.$basename.'.css.map', 'r');
$c = @fopen ($basename.'.css.map', 'w');
while (!feof ($f)) {
fwrite ($c, preg_replace ('/\.'.$basename.'\.css/', $basename.'.css', fgets ($f)));
}
fclose ($f);
fclose ($c);
unlink ('.'.$basename.'.css.map');
}
touch ($basename.'.css', $scss['mtime']);
touch ($basename.'.css.map', $scss['mtime']);
success ();
?>