-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathcompiler.php
executable file
·139 lines (121 loc) · 3.96 KB
/
compiler.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
#!/usr/bin/env php
<?php
/**
* IFM compiler
*
* This script compiles all sources into one single file.
*/
chdir(realpath(dirname(__FILE__)));
// output files and common attrs
define( "IFM_VERSION", "v4.0.2" );
define( "IFM_RELEASE_DIR", "dist/");
define( "IFM_STANDALONE", "ifm.php" );
define( "IFM_STANDALONE_GZ", "ifm.min.php" );
define( "IFM_LIB", "libifm.php" );
// php source files
$IFM_SRC_PHP = [
0 => "src/main.php",
1 => "src/ifmarchive.php",
2 => "src/htpasswd.php"
];
// get options
$options = getopt(null, ["language::", "languages::", "lang::", "cdn"]);
// build CDN version?
if (isset($options['cdn'])) {
define("IFM_CDN", true);
} else {
define("IFM_CDN", false);
}
// process languages
$langs = [];
foreach ($options as $key => $value) {
if (substr($key, 0, 4) == "lang") {
$langs = array_merge($langs, explode(",", $value));
}
}
$langs = array_unique($langs);
if (!empty($langs)) {
$vars['default_lang'] = ($langs[0] == "all") ? "en" : $langs[0];
} else {
$vars['default_lang'] = "en";
}
// ensure english is available, as it gets merged with the other languages
// in case of missing keys in other languages.
if (!in_array("all", $langs) || !in_array("en", $langs)) {
array_push($langs, "en");
}
if (in_array("all", $langs))
$langs = array_map(
function ($lang_file) { return pathinfo($lang_file)['filename']; },
glob("src/i18n/*.json")
);
$vars['languageincludes'] = "";
foreach ($langs as $l) {
if (file_exists("src/i18n/".$l.".json")) {
$vars['languageincludes'] .=
'$i18n["'.$l.'"] = <<<\'f00bar\'' . "\n"
. file_get_contents( "src/i18n/".$l.".json" ) . "\n"
. 'f00bar;' . "\n"
. '$i18n["'.$l.'"] = json_decode( $i18n["'.$l.'"], true );' . "\n" ;
} else {
print "WARNING: Language file src/i18n/".$l.".json not found.\n";
}
}
// Concat PHP Files
$compiled = ["<?php"];
foreach ($IFM_SRC_PHP as $phpfile) {
$lines = file($phpfile);
unset($lines[0]); // remove <?php line
$compiled = array_merge($compiled, $lines);
}
$compiled = join($compiled);
// Include assets
$compiled = str_replace("###ASSETS_CSS###", file_get_contents("src/assets".(IFM_CDN?".cdn":"").".css"), $compiled);
$compiled = str_replace("###ASSETS_JS###", file_get_contents("src/assets".(IFM_CDN?".cdn":"").".js"), $compiled);
// Process file includes
$includes = NULL;
preg_match_all("/\#\#\#file:([^\#]+)\#\#\#/", $compiled, $includes, PREG_SET_ORDER);
foreach ($includes as $file) {
$compiled = str_replace($file[0], file_get_contents($file[1]), $compiled);
}
// Process ace includes
$includes = null;
$vars['ace_includes'] = "";
preg_match_all("/\#\#\#acedir:([^\#]+)\#\#\#/", $compiled, $includes, PREG_SET_ORDER);
foreach ($includes as $dir) {
$dircontent = "";
foreach (glob($dir[1] . "/*") as $file) {
if (is_file($file) && is_readable($file)) {
$vars['ace_includes'] .= "|" . substr(basename($file), 0, strrpos(basename($file), "."));
$dircontent .= file_get_contents($file)."\n\n";
}
}
$compiled = str_replace($dir[0], $dircontent, $compiled);
}
// Process variable includes
$includes = NULL;
preg_match_all("/\#\#\#vars:([^\#]+)\#\#\#/", $compiled, $includes, PREG_SET_ORDER);
foreach( $includes as $var ) {
$compiled = str_replace($var[0], $vars[$var[1]], $compiled);
}
$compiled = str_replace('IFM_VERSION', IFM_VERSION, $compiled);
if (!is_dir(IFM_RELEASE_DIR)) {
mkdir(IFM_RELEASE_DIR);
}
// build standalone ifm
file_put_contents(IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_STANDALONE, $compiled);
file_put_contents(IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_STANDALONE, '
/**
* start IFM
*/
$ifm = new IFM();
$ifm->run();
', FILE_APPEND);
// build compressed ifm
file_put_contents(
IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_STANDALONE_GZ,
'<?php eval(gzdecode(file_get_contents(__FILE__, false, null, 85))); exit(0); ?>'
. gzencode(file_get_contents(IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_STANDALONE, false, null, 5))
);
// build lib
file_put_contents(IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_LIB, $compiled);