-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.php
executable file
·295 lines (251 loc) · 7.56 KB
/
build.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
require_once 'vendor/autoload.php';
use Assetic\AssetWriter;
use Assetic\Asset\AssetCollection;
use Assetic\Asset\AssetInterface;
use Assetic\Asset\GlobAsset;
use Assetic\Filter\CssMinFilter;
use Assetic\Filter\LessphpFilter;
$target = 'default';
if (isset($_SERVER['argv'][1])) {
$target = $_SERVER['argv'][1];
}
switch ($target) {
case 'zip':
less();
locales();
zip();
break;
case 'watch':
$timeout = 5;
if (isset($_SERVER['argv'][2]) && $_SERVER['argv'][2] > 0) {
$timeout = (int) $_SERVER['argv']['timeout'];
}
watch($timeout);
break;
default:
less();
locales();
break;
}
/**
* Compiles LESS files to CSS files.
*/
function less()
{
dumpAssets(getAssets());
printSuccess('compiled LESS files');
}
/**
* Creates the Stud.IP plugin zip archive.
*/
function zip()
{
$archive = new ZipArchive();
$archive->open('courseware.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
addDirectories($archive, array(
'assets',
'blocks',
'controllers',
'cronjobs',
'docs',
'export',
'import',
'locale',
'migrations',
'models',
'vendor',
'views',
), '/^(assets|blocks).*\.less$/');
$archive->addFile('LICENSE');
$archive->addFile('Courseware.php');
$archive->addFile('CoursewareObserver.php');
$archive->addFile('plugin.manifest');
$archive->addFile('README.md');
$archive->close();
printSuccess('created the Stud.IP plugin zip archive');
}
/**
* Watch for changes in LESS files and compile them on demand.
*
* @param int $timeout The timeout between two watch cycles
*/
function watch($timeout)
{
printSuccess('watching for changes in LESS files to be compiled');
$assets = getAssets();
while (true) {
$updateNeeded = false;
foreach ($assets as $asset) {
/** @var AssetInterface $asset */
if (assetNeedsUpdate($asset)) {
printInfo($asset->getSourceRoot().'/'.$asset->getSourcePath().' has changed');
$updateNeeded = true;
}
}
if ($updateNeeded) {
dumpAssets($assets);
printSuccess('compiled changed LESS files');
}
sleep($timeout);
}
}
function locales()
{
$output = array();
$fd = fopen('locale/js.pot', 'w');
fputs($fd, 'msgid ""' ."\n"
. 'msgstr ""' . "\n"
. '"Project-Id-Version: PACKAGE VERSION\n"' . "\n"
. '"Report-Msgid-Bugs-To: \n"' . "\n"
. '"POT-Creation-Date: ' . date('Y-m-md H:i') .'+0200\n"' . "\n"
. '"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"' . "\n"
. '"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"' . "\n"
. '"Language-Team: LANGUAGE <[email protected]>\n"' . "\n"
. '"Language: \n"' . "\n"
. '"MIME-Version: 1.0\n"' . "\n"
. '"Content-Type: text/plain; charset=ISO-8859-1\n"' . "\n"
. '"Content-Transfer-Encoding: 8bit\n"' ."\n\n");
// collect translatable texts
exec("for i in blocks/*/templates/*mustache; do iconv -c -f cp1252 \$i | awk '{if (match($0, /i18n}}([^{]*){{/)) {print substr($0, RSTART+6, RLENGTH-8)}}'; done | sort -u", $output);
exec("for i in blocks/*/*/*js; do iconv -c -f utf-8 \$i | awk '{if (match($0, /i18n([^{]*)\")/)) {print substr($0, RSTART+6, RLENGTH-8)}}'; done | sort -u", $output);
$output[] = 'Bestätigung';
$output[] = 'Diskussion';
$output[] = 'Evaluationen';
$output[] = 'Freitext';
foreach ($output as $entry) {
if (strlen($entry)) {
fputs($fd, 'msgid "'. str_replace('"', '\\"', utf8_decode($entry)) .'"' . "\n");
fputs($fd, 'msgstr ""' ."\n\n");
}
}
fclose($fd);
exec('make -f locale/Makefile');
}
/**
* Returns the collection of assets that need to be processed.
*
* @return AssetCollection The assets
*/
function getAssets()
{
$assets = new AssetCollection(
array(
new GlobAsset('assets/*.less'),
new GlobAsset('blocks/*/css/[a-zA-Z0-9]*.less'),
),
array(new LessphpFilter())
);
foreach ($assets as $asset) {
/** @var AssetInterface $asset */
$sourcePath = $asset->getSourcePath();
$asset->setTargetPath(substr($sourcePath, 0, strrpos($sourcePath, '.')).'.css');
}
$assets->setTargetPath('courseware.min.css');
return $assets;
}
/**
* Checks whether or not an asset needs to be compiled.
*
* @param AssetInterface $asset The asset to check
*
* @return bool True, if the asset is not up-to-date, false otherwise
*/
function assetNeedsUpdate(AssetInterface $asset)
{
$targetFile = $asset->getSourceRoot().'/'.$asset->getTargetPath();
if (!is_file($targetFile)) {
return true;
}
return filemtime($targetFile) < $asset->getLastModified();
}
/**
* Dumps a collection of assets.
*
* @param AssetCollection $assets The assets to dump
*/
function dumpAssets(AssetCollection $assets)
{
foreach ($assets as $asset) {
/** @var AssetInterface $asset */
$assetWriter = new AssetWriter($asset->getSourceRoot());
try {
$assetWriter->writeAsset($asset);
} catch (Exception $e) {
printError($e->getMessage());
}
}
// apply the CSS min filter only to the moocip.min.css file
$assets = clone $assets;
$assets->ensureFilter(new CssMinFilter());
$assetWriter = new AssetWriter('assets');
try {
$assetWriter->writeAsset($assets);
} catch (Exception $e) {
printError($e->getMessage());
}
}
/**
* Recursively adds a directory tree to a zip archive.
*
* @param ZipArchive $archive The zip archive
* @param string $directory The directory to add
* @param string $ignoredFilesRegex Regular expression that matches
* files which should be ignored
*/
function addDirectory(ZipArchive $archive, $directory, $ignoredFilesRegex = '')
{
$archive->addEmptyDir($directory);
foreach (glob($directory.'/*') as $file) {
if (is_dir($file)) {
addDirectory($archive, $file, $ignoredFilesRegex);
} else {
if ($ignoredFilesRegex === '' || !preg_match($ignoredFilesRegex, $file)) {
$archive->addFile($file);
} else {
printError('ignore '.$file);
}
}
}
}
/**
* Recursively adds directory trees to a zip archive.
*
* @param ZipArchive $archive The zip archive
* @param array $directories The directories to add
* @param string $ignoredFilesRegex Regular expression that matches
* files which should be ignored
*/
function addDirectories(ZipArchive $archive, array $directories, $ignoredFilesRegex = '')
{
foreach ($directories as $directory) {
addDirectory($archive, $directory, $ignoredFilesRegex);
}
}
/**
* Prints a success message to the standard output stream of the console.
*
* @param string $message The message to print
*/
function printSuccess($message)
{
echo "\033[32m".$message."\033[39m".PHP_EOL;
}
/**
* Prints an info message to the standard output stream of the console.
*
* @param string $message The message to print
*/
function printInfo($message)
{
echo $message.PHP_EOL;
}
/**
* Prints an error message to the standard output stream of the console.
*
* @param string $message The message to print
*/
function printError($message)
{
file_put_contents('php://stderr', "\033[31m".$message."\033[39m".PHP_EOL);
}