-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.php
182 lines (164 loc) · 5.61 KB
/
release.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
<?php
/*
This file is part of WebDRIP Designer
Copyright (C) 2016-2017, 2019, 2020, 2022 Jasper Vries
WebDRIP Designer is free software: you can redistribute it and/or
modify it under the terms of version 3 of the GNU General Public
License as published by the Free Software Foundation.
WebDRIP Designer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WebDRIP Designer. If not, see <http://www.gnu.org/licenses/>.
*/
$release_dir = 'release';
$jshrink_dir = 'bundled/JShrink';
//check if JShrink available, otherwise do not minify
if (is_file($jshrink_dir.'/src/JShrink/Minifier.php')) {
$jshrink = TRUE;
}
else {
$jshrink = FALSE;
}
//function to clean directory
function rm_r($dir) {
$files = scandir($dir);
foreach ($files as $file) {
$path = $dir . '/' . $file;
if (is_dir($path) && ($file != '.') && ($file != '..')) {
rm_r($path);
}
elseif (is_file($path)) {
unlink($path);
}
}
}
//function to copy directory recursively
function copy_r($source, $target) {
if (is_dir($source)) {
if (!is_dir($target)) {
mkdir($target);
}
$files = scandir($source);
foreach ($files as $file) {
if (($file != '.') && ($file != '..')) {
$source_path = $source . '/' . $file;
$target_path = $target . '/' . $file;
copy_r($source_path, $target_path);
}
}
}
elseif (is_file($source)) {
//source is a file, only copy a single file
copy($source, $target);
}
}
//check if release dir exists, if not create it
if (!is_dir($release_dir)) {
mkdir($release_dir);
echo 'created '.$release_dir.' directory'.PHP_EOL;
}
else {
echo 'NOTICE: directory '.$release_dir.' already exists'.PHP_EOL;
//clean release directory
rm_r($release_dir);
}
//run spritesheet generator
echo 'generating spritesheet'.PHP_EOL;
chdir('resources');
exec('php -f generatespritesheet.php', $output);
chdir('..');
//copy files
//main dir
$files = scandir('.');
$filetypes = array('.html', '.php', '.png', '.css', '.txt', '.htaccess');
if ($jshrink === FALSE) $filetypes[] = '.js';
$ignore = array('config.cfg.php', 'release.php', 'migrate_symbols.php');
$md5ify = array('sprites.png', 'style.css');
$md5ify = array_flip($md5ify);
$js = array();
foreach ($files as $file) {
if (is_file($file) && !in_array($file, $ignore) && in_array(substr($file, strrpos($file, '.')), $filetypes)) {
$target_file = $file;
//check if md5ify
if (array_key_exists($file, $md5ify)) {
$md5 = md5_file($file);
$target_file = explode('.', $file);
$target_file = $target_file[0] . '-' . $md5 . '.' . $target_file[1];
$md5ify[$file] = $target_file;
}
copy($file, $release_dir.'/'.$target_file);
}
elseif (is_file($file) && (substr($file, strrpos($file, '.')) == '.js')) {
//get list of js files to minify
$js[] = $file;
}
}
//copy directories
copy_r('icon', $release_dir.'/icon');
echo 'created icon directory'.PHP_EOL;
copy_r('bundled', $release_dir.'/bundled');
echo 'created bundled directory'.PHP_EOL;
//minify js
if ($jshrink === TRUE) {
echo 'generating minified javascript'.PHP_EOL;
$combined_js = array();
foreach ($js as $file) {
$combined_js[] = file_get_contents($file);
}
$combined_js = join(PHP_EOL, $combined_js);
require_once($jshrink_dir.'/src/JShrink/Minifier.php');
$minifiedCode = \JShrink\Minifier::minify($combined_js);
//add copyright notice
$minifiedCode = '/*
WebDRIP Designer - webgebaseerde ontwerptool voor DRIP-teksten
Copyright (C) 2013-2020 Jasper Vries
WebDRIP Designer is free software: you can redistribute it and/or
modify it under the terms of version 3 of the GNU General Public
License as published by the Free Software Foundation.
WebDRIP Designer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WebDRIP Designer. If not, see <http://www.gnu.org/licenses/>.
*/
'. $minifiedCode;
//get md5
$js_md5 = md5($minifiedCode);
$js_filename = 'webdripdesigner-' . $js_md5 . '.min.js';
//replace md5ified filenames
$minifiedCode = str_replace(array_keys($md5ify), $md5ify, $minifiedCode);
//write to file
file_put_contents($release_dir . '/' . $js_filename, $minifiedCode);
//replace script tags
$html = file_get_contents('index.php');
$html = preg_replace_callback('#<script.*src="(.*)".*</script>\v+?#U',
function($matches) use ($js) {
global $js_filename;
if (in_array($matches[1], $js)) {
if ($matches[1] == 'webdripdesigner.js') {
return '<script src="' . $js_filename . '" type="text/javascript"></script>';
}
else {
return '';
}
}
else {
return $matches[0];
}
},
$html);
//replace md5ified filenames
$html = str_replace(array_keys($md5ify), $md5ify, $html);
file_put_contents($release_dir.'/index.php', $html);
}
//get git version
$git_ver = exec('git log -1 --pretty=format:%H');
$build = 'build <a href="https://github.com/jaspervries/webdripdesigner/commits">' . substr($git_ver, 0, 7) . '</a> ' . date('Y-m-d');
//insert version into file
$html = file_get_contents($release_dir.'/index.php');
$html = str_replace('{{BUILD}}', $build, $html);
file_put_contents($release_dir.'/index.php', $html);
?>