-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilder
More file actions
executable file
·382 lines (291 loc) · 10.4 KB
/
builder
File metadata and controls
executable file
·382 lines (291 loc) · 10.4 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env php
###########################
# ComunicWeb build script #
# #
# @author Pierre HUBERT #
###########################
<?php
//Output directory
define("OUTPUT_DIRECTORY", __DIR__."/output/");
define("OUTPUT_FILE", __DIR__."/output.tar");
//Temporary file
define("TEMP_FILE", __DIR__."/output/temp");
// Build time
define("BUILD_TIME", time());
//Defines some utilities
/**
* Display a message on the screen
*
* @param string $message The message to display on the screen
* @param bool $new_section Specify whether the message refers to a
* new build section or not
*/
function notice(string $message, bool $new_section = false) {
echo ($new_section ? "\n\n" : "").$message,"\n";
}
/**
* Append a string at the begining of each entry of an array
*
* @param string $input The string to append to each array entry
* @param array $array The array to process
* @return array Updated array
*/
function array_put_begining(string $input, array $array){
foreach($array as $num => $val)
$array[$num] = $input.$val;
return $array;
}
/**
* Copy an array of file into a specific target file
*
* @param array $files The list of file to copy
* @param string $target The target file to create
* @param bool TRUE for a success / FALSE else
*/
function files_to_file(array $files, string $target) : bool {
$source = "";
foreach($files as $file){
$source .= file_get_contents($file)."\n";
}
return file_put_contents($target, $source) != FALSE;
}
/**
* Copy an array of files into a specific target file using uglifyJS
*
* @param string $begin_path The begining of each path
* @param array $files The name of the source file
* @param string $target The target file
* @return bool TRUE in case of success / FALSE in case of failure
*/
function js_files_to_file(string $begin_path, array $files, string $target){
$source = "";
//Delete any previous temporary file
if(file_exists(TEMP_FILE))
unlink(TEMP_FILE);
foreach($files as $file){
$uglifyjs = true;
//Check if file entry is an array or a string
if(is_string($file))
$file = $begin_path.$file;
//It is an array
else if(is_array($file)) {
//Check if we have special information for uglifyjs
if(isset($file["uglifyjs"]))
$uglifyjs = $file["uglifyjs"];
$file = $begin_path.$file["path"];
}
//Else the kind of entry is not supported
else
throw new Exception("Excepted string or array, got something else for javascript entry!");
//Compress file
if(FALSE && $uglifyjs){
notice("Parsing with UGLIFYJS: ".$file);
exec("/usr/bin/uglifyjs '".$file."' -c -o ".TEMP_FILE, $output, $exit_code);
//Get the content of the file
$source .= "\n".file_get_contents(TEMP_FILE);
if($exit_code != 0){
notice("An error (".$exit_code.") occured while parsing file ".$file, TRUE);
exit(10);
}
}
//Else we take the file as is
else
$source .= "\n".file_get_contents($file);
}
//Delete the temp file
unlink(TEMP_FILE);
return file_put_contents($target, $source) != FALSE;
}
/**
* Delete the entire content of directory
*
* @param string $path The path of the directory to delete
*/
function delDir(string $path){
if(is_dir($path) == TRUE){
$rootFolder = scandir($path);
if(sizeof($rootFolder) > 2){
foreach($rootFolder as $folder){
if($folder != "." && $folder != ".."){
//Pass the subfolder to function
delDir($path."/".$folder);
}
}
//On the end of foreach the directory will be cleaned, and you will can use rmdir, to remove it
rmdir($path);
}
}
else {
if(file_exists($path) == TRUE){
//Suppression du fichier
unlink($path);
}
}
}
// copies files and non-empty directories
function rcopy(string $src, string $dst) {
if (is_dir($src)) {
mkdir($dst, 0777, true);
$files = scandir($src);
foreach ($files as $file)
if ($file != "." && $file != "..") rcopy("$src/$file", "$dst/$file");
}
else if (file_exists($src)) copy($src, $dst);
}
//Initialize page
require_once __DIR__."/system/system.php";
/**
* Build application
*/
function build() {
if(!isset($_SERVER['argv'][2]))
exit(notice("Usage: ./build build [configuration]", TRUE));
//Defines some variables
$debug_conf = "dev";
$release_conf = $_SERVER['argv'][2];
//Load configurations
notice("Load configurations.", TRUE);
notice("Debug config: ".$debug_conf);
notice("Release config: ".$release_conf);
load_config($debug_conf);
$debug = new $debug_conf;
$path_debug_assets = __DIR__."/".$debug::PATH_ASSETS;
load_config($release_conf);
$release = new $release_conf;
$path_release_assets = OUTPUT_DIRECTORY.$release::PATH_ASSETS;
//Clean directory
notice("Clean build directory", TRUE);
if(file_exists(OUTPUT_DIRECTORY))
delDir(OUTPUT_DIRECTORY);
if(file_exists(OUTPUT_FILE))
unlink(OUTPUT_FILE);
mkdir(OUTPUT_DIRECTORY, 0777, true);
mkdir($path_release_assets, 0777, true);
mkdir($path_release_assets."/css", 0777, true);
mkdir($path_release_assets."/zip", 0777, true);
//Create unminified version
notice("Create unminified files versions", TRUE);
//3rd party CSS
notice("Third Party CSS");
$thirdPartyDebugFiles = array_put_begining($path_debug_assets, $debug::THIRD_PARTY_CSS);
$targetThirdPartyCSS = $path_release_assets.$release::THIRD_PARTY_CSS;
files_to_file($thirdPartyDebugFiles, $targetThirdPartyCSS);
//3rd party JS
notice("Third Party JS");
$targetThirdPartyJS = $path_release_assets.$release::THIRD_PARTY_JS;
js_files_to_file($path_debug_assets, $debug::THIRD_PARTY_JS, $targetThirdPartyJS);
//App CSS
notice("App CSS");
$appDebugFiles = array_put_begining($path_debug_assets, $debug::APP_CSS);
$targetAppCSS = $path_release_assets.$release::APP_CSS;
files_to_file($appDebugFiles, $targetAppCSS);
//App JS
notice("App JS");
$targetAppJS = $path_release_assets.$release::APP_JS;
js_files_to_file($path_debug_assets, $debug::APP_JS, $targetAppJS);
//Make some adpations on third party files
$source = file_get_contents($targetThirdPartyCSS);
$source = str_replace("../fonts/fontawesome", "fontawesome_fonts/fontawesome", $source);
$source = str_replace("../fonts/ionicons", "ionicons_fonts/ionicons", $source);
$source = str_replace("../fonts/glyphicons", "fonts/glyphicons", $source);
file_put_contents($targetThirdPartyCSS, $source);
//Copy font awesome files + ionicons files + bootstrap fond + and twemojies files + Google Fonts
rcopy($path_debug_assets."3rdparty/adminLTE/plugins/font-awesome/fonts", $path_release_assets."fontawesome_fonts");
rcopy($path_debug_assets."3rdparty/adminLTE/plugins/ionicons/fonts", $path_release_assets."ionicons_fonts");
rcopy($path_debug_assets."3rdparty/adminLTE/bootstrap/fonts", $path_release_assets."fonts");
rcopy($path_debug_assets."3rdparty/twemoji/2/72x72/", $path_release_assets."3rdparty/twemoji/2/72x72/");
rcopy($path_debug_assets."3rdparty/adminLTE/plugins/googleFonts/googleFonts/", $path_release_assets."googleFonts/");
rcopy($path_debug_assets."3rdparty/wdt-emoji/sheets/", $path_release_assets."3rdparty/wdt-emoji/sheets/");
//Copy iCheck images
rcopy($path_debug_assets."3rdparty/adminLTE/plugins/iCheck/flat/icheck-flat-imgs/", $path_release_assets."icheck-flat-imgs/");
//Copy images and templates
rcopy($path_debug_assets."img/", $path_release_assets."img/");
rcopy($path_debug_assets."templates/", $path_release_assets."templates/");
//Copy songs
rcopy($path_debug_assets."audio/", $path_release_assets."audio/");
//Copy dark theme
rcopy($path_debug_assets."css/dark_theme.css", $path_release_assets."css/dark_theme.css");
//Copy pacman
rcopy($path_debug_assets."3rdparty/pacman", $path_release_assets."3rdparty/pacman");
//Copy clippy.js
rcopy($path_debug_assets."3rdparty/clippy.js", $path_release_assets."3rdparty/clippy.js");
//Copy TFS & TFS models
rcopy($path_debug_assets."3rdparty/tfjs", $path_release_assets."3rdparty/tfjs");
rcopy($path_debug_assets."3rdparty/tensorflow-models", $path_release_assets."3rdparty/tensorflow-models");
rcopy($path_debug_assets."3rdparty/tfjs-models", $path_release_assets."3rdparty/tfjs-models");
// Copy files for color picker
rcopy($path_debug_assets."3rdparty/adminLTE/plugins/colorpicker/img-colorpicker", $path_release_assets."img-colorpicker");
//Build and copy personnal data navigator
notice("Build personnal data export navigator and add it to built files");
exec($path_debug_assets."zip/personnal-data-export-navigator-builder.sh");
rcopy($path_debug_assets."zip/personnal-data-export-navigator.zip", $path_release_assets."zip/personnal-data-export-navigator.zip");
//Begin to write root PHP File
notice("Generate PHP root file");
$page_src = '<?php
//We check if it is a redirection to handle 404 errors
if(isset($_SERVER["REDIRECT_URL"])){
//We check if it is an asset request
if(preg_match("<assets>", $_SERVER["REDIRECT_URL"])){
//This is a 404 not found error...
echo "<p>Error! 404 not found</p>";
http_response_code(404);
exit();
}
}';
// Begin .htaccess file
$htaccess = '<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
';
//Check if we have to force https connection
if(defined(get_class($release)."::FORCE_HTTPS")){
if($release::FORCE_HTTPS){
//Inform user
notice("This build will work only with https.");
//Add rules in .htaccess
$htaccess .= "\n\nRewriteCond %{HTTPS} !on\n";
$htaccess .= "RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}";
//Add rules in root PHP file
$page_src .= "\n//Force HTTPS connection\n";
$page_src .= "if(!isset(\$_SERVER[\"HTTPS\"]) || \$_SERVER[\"HTTPS\"] != \"on\")\n";
$page_src .= "\theader('Location: https://'.\$_SERVER['HTTP_HOST'].\$_SERVER['REQUEST_URI']);\n";
}
}
//Write .htaccess file
file_put_contents(OUTPUT_DIRECTORY.".htaccess", $htaccess);
//Write root index file
$page_src .= ' ?>';
$page_src .= load_page($release_conf);
file_put_contents(OUTPUT_DIRECTORY."index.php", $page_src);
// Create output archive
system("bash -c \"cd ".OUTPUT_DIRECTORY." && tar -cf ".OUTPUT_FILE." * .htaccess\"");
//Done
notice("Done.", TRUE);
} //BUILD
/**
* Clean build directory
*/
function clean(){
notice("Cleaning build directory.", TRUE);
delDir(OUTPUT_DIRECTORY);
if(file_exists(OUTPUT_FILE))
unlink(OUTPUT_FILE);
}
//Get the action and do it
if(!isset($_SERVER['argv'][1]))
exit("Usage: ./build [action]");
$action = $_SERVER['argv'][1];
switch($action){
case "build":
build();
break;
case "clean":
clean();
break;
default:
notice("Accepted commands are build, clean.", TRUE);
}