comparison bin/install-jsdeps.sh @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 #!/usr/bin/env php
2 <?php
3 /*
4 +-----------------------------------------------------------------------+
5 | bin/install-jsdeps.sh |
6 | |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2016, The Roundcube Dev Team |
9 | |
10 | Licensed under the GNU General Public License version 3 or |
11 | any later version with exceptions for skins & plugins. |
12 | See the README file for a full license statement. |
13 | |
14 | PURPOSE: |
15 | Utility script to fetch and install all 3rd party javascript |
16 | libraries unsed in Roundcube from source. |
17 +-----------------------------------------------------------------------+
18 | Author: Thomas Bruederli <thomas@roundcube.net> |
19 +-----------------------------------------------------------------------+
20 */
21
22 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
23
24 require_once INSTALL_PATH . 'program/include/clisetup.php';
25
26 if (!function_exists('exec')) {
27 die("PHP exec() function is required. Check disable_functions in php.ini\n");
28 }
29
30 $SOURCES = json_decode(file_get_contents(INSTALL_PATH . 'jsdeps.json'), true);
31
32 if (empty($SOURCES['dependencies'])) {
33 die("ERROR: Failed to read sources from " . INSTALL_PATH . "jsdeps.json\n");
34 }
35
36 $CURL = trim(`which curl`);
37 $WGET = trim(`which wget`);
38 $UNZIP = trim(`which unzip`);
39 $FILEINFO = trim(`which file`);
40
41 if (empty($UNZIP)) {
42 die("ERROR: Required program 'unzip' not found\n");
43 }
44 if (empty($FILEINFO)) {
45 die("ERROR: Required program 'file' not found\n");
46 }
47 if (empty($CURL) && empty($WGET)) {
48 die("ERROR: Required program 'wget' or 'curl' not found\n");
49 }
50
51 $CACHEDIR = sys_get_temp_dir();
52
53 if (is_writeable(INSTALL_PATH . 'temp/js_cache') || @mkdir(INSTALL_PATH . 'temp/js_cache', 0774, true)) {
54 $CACHEDIR = INSTALL_PATH . 'temp/js_cache';
55 }
56
57
58 //////////////// License definitions
59
60 $LICENSES = array();
61 $LICENSES['MIT'] = <<<EOM
62 * Licensed under the MIT licenses
63 *
64 * Permission is hereby granted, free of charge, to any person obtaining
65 * a copy of this software and associated documentation files (the
66 * "Software"), to deal in the Software without restriction, including
67 * without limitation the rights to use, copy, modify, merge, publish,
68 * distribute, sublicense, and/or sell copies of the Software, and to
69 * permit persons to whom the Software is furnished to do so, subject to
70 * the following conditions:
71 *
72 * The above copyright notice and this permission notice shall be
73 * included in all copies or substantial portions of the Software.
74 *
75 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
76 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
77 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
78 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
79 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
80 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
81 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
82
83 EOM;
84
85 $LICENSES['GPLv3'] = <<<EOG
86 * The JavaScript code in this page is free software: you can
87 * redistribute it and/or modify it under the terms of the GNU
88 * General Public License (GNU GPL) as published by the Free Software
89 * Foundation, either version 3 of the License, or (at your option)
90 * any later version. The code is distributed WITHOUT ANY WARRANTY;
91 * without even the implied warranty of MERCHANTABILITY or FITNESS
92 * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
93 *
94 * As additional permission under GNU GPL version 3 section 7, you
95 * may distribute non-source (e.g., minimized or compacted) forms of
96 * that code without the copy of the GNU GPL normally required by
97 * section 4, provided you include this license notice and a URL
98 * through which recipients can access the Corresponding Source.
99
100 EOG;
101
102 $LICENSES['LGPL'] = <<<EOL
103 * The JavaScript code in this page is free software: you can
104 * redistribute it and/or modify it under the terms of the GNU
105 * Lesser General Public License as published by the Free Software
106 * Foundation, either version 3 of the License, or (at your option)
107 * any later version.
108
109 EOL;
110
111
112 //////////////// Functions
113
114 /**
115 * Fetch package file from source
116 */
117 function fetch_from_source($package, $useCache = true, &$filetype = null)
118 {
119 global $CURL, $WGET, $FILEINFO, $CACHEDIR;
120
121 $filetype = pathinfo($package['url'], PATHINFO_EXTENSION) ?: 'tmp';
122 $cache_file = $CACHEDIR . '/' . $package['lib'] . '-' . $package['version'] . '.' . $filetype;
123
124 if (!is_readable($cache_file) || !$useCache) {
125 echo "Fetching $package[url]\n";
126
127 if ($CURL)
128 exec(sprintf('%s -s %s -o %s', $CURL, escapeshellarg($package['url']), $cache_file), $out, $retval);
129 else
130 exec(sprintf('%s -q %s -O %s', $WGET, escapeshellarg($package['url']), $cache_file), $out, $retval);
131
132 if ($retval !== 0) {
133 die("ERROR: Failed to download source file from " . $package['url'] . "\n");
134 }
135 }
136
137 if (!empty($package['sha1']) && ($sum = sha1_file($cache_file)) !== $package['sha1']) {
138 die("ERROR: Incorrect sha1 sum of $cache_file. Expected: $package[sha1], got: $sum\n");
139 }
140
141 // detect downloaded/cached file type
142 exec(sprintf('%s -b %s', $FILEINFO, $cache_file), $out);
143 if (stripos($out[0], 'zip') === 0) {
144 $filetype = 'zip';
145 }
146
147 return $cache_file;
148 }
149
150 /**
151 * Create a destination javascript file with copyright and license header
152 */
153 function compose_destfile($package, $srcfile)
154 {
155 global $LICENSES;
156
157 $header = sprintf("/**\n * %s - v%s\n *\n", $package['name'], $package['version']);
158
159 if (!empty($package['source'])) {
160 $header .= " * @source " . $package['source'] . "\n";
161 $header .= " *\n";
162 }
163
164 if (!empty($package['license']) && isset($LICENSES[$package['license']])) {
165 $header .= " * @licstart The following is the entire license notice for the\n";
166 $header .= " * JavaScript code in this file.\n";
167 $header .= " *\n";
168 if (!empty($package['copyright'])) {
169 $header .= " * " . $package['copyright'] . "\n";
170 $header .= " *\n";
171 }
172
173 $header .= $LICENSES[$package['license']];
174 $header .= " *\n";
175 $header .= " * @licend The above is the entire license notice\n";
176 $header .= " * for the JavaScript code in this file.\n";
177 }
178
179 $header .= " */\n";
180
181 if (file_put_contents(INSTALL_PATH . $package['dest'], $header . file_get_contents($srcfile))) {
182 echo "Wrote file " . INSTALL_PATH . $package['dest'] . "\n";
183 }
184 else {
185 die("ERROR: Failed to write destination file " . INSTALL_PATH . $package['dest'] . "\n");
186 }
187 }
188
189 /**
190 * Extract a Zip archive into the destination specified by the package config
191 */
192 function extract_zipfile($package, $srcfile)
193 {
194 global $UNZIP, $CACHEDIR;
195
196 $destdir = INSTALL_PATH . $package['dest'];
197 if (!is_dir($destdir)) {
198 mkdir($destdir, 0774, true);
199 }
200
201 if (!is_writeable($destdir)) {
202 die("ERROR: Cannot write to destination directory $destdir\n");
203 }
204
205 // pick files from zip archive
206 if (!empty($package['pick'])) {
207 foreach ($package['pick'] as $pattern) {
208 echo "Extracting files $pattern into $destdir\n";
209 exec(sprintf('%s -o %s %s -d %s', $UNZIP, escapeshellarg($srcfile), escapeshellarg($pattern), $destdir), $out, $retval);
210 if ($retval !== 0) {
211 echo "ERROR: Failed to unpack $pattern; " . join('; ' . $out) . "\n";
212 }
213 }
214 }
215 // unzip the archive and map source to dest files/directories
216 else if (!empty($package['map'])) {
217 $extract = $CACHEDIR . '/' . $package['lib'] . '-extract';
218 if (!is_dir($extract)) {
219 mkdir($extract, 0774, true);
220 }
221 exec(sprintf('%s -o %s -d %s', $UNZIP, escapeshellarg($srcfile), $extract), $out, $retval);
222
223 // get the root folder of the extracted package
224 $extract_tree = glob("$extract/*", GLOB_ONLYDIR);
225 $sourcedir = $extract_tree[0];
226
227 foreach ($package['map'] as $src => $dest) {
228 echo "Installing files $sourcedir/$src into $destdir/$dest\n";
229
230 // make sure the destination's parent directory exists
231 if (strpos($dest, '/') !== false) {
232 $parentdir = dirname($destdir . '/' . $dest);
233 if (!is_dir($parentdir)) {
234 mkdir($parentdir, 0774, true);
235 }
236 }
237
238 // avoid copying source directory as a child into destination
239 if (is_dir($sourcedir . '/' . $src) && is_dir($destdir . '/' . $dest)) {
240 exec(sprintf('rm -rf %s/%s', $destdir, $dest));
241 }
242
243 exec(sprintf('mv -f %s/%s %s/%s', $sourcedir, $src, $destdir, $dest), $out, $retval);
244 if ($retval !== 0) {
245 echo "ERROR: Failed to move $src into $destdir/$dest; " . join('; ' . $out) . "\n";
246 }
247 }
248
249 // remove temp extraction dir
250 exec('rm -rf ' . $extract);
251 }
252 // extract the archive into the destination directory
253 else {
254 echo "Extracting zip archive into $destdir\n";
255 exec(sprintf('%s -o %s -d %s', $UNZIP, escapeshellarg($srcfile), $destdir), $out, $retval);
256 if ($retval !== 0) {
257 echo "ERROR: Failed to unzip $srcfile; " . join('; ' . $out) . "\n";
258 }
259 }
260
261 // remove some files from the destination
262 if (!empty($package['omit'])) {
263 foreach ((array)$package['omit'] as $glob) {
264 exec(sprintf('rm -rf %s/%s', $destdir, escapeshellarg($glob)));
265 }
266 }
267
268 // prepend license header to extracted files
269 if (!empty($package['addlicense'])) {
270 foreach ((array)$package['addlicense'] as $filename) {
271 $pkg = $package;
272 $pkg['dest'] = $package['dest'] . '/' . $filename;
273 compose_destfile($pkg, $destdir . '/' . $filename);
274 }
275 }
276 }
277
278 /**
279 * Delete the package destination file/dir
280 */
281 function delete_destfile($package)
282 {
283 $destdir = INSTALL_PATH . ($package['rm'] ?: $package['dest']);
284
285 if (file_exists($destdir)) {
286 if (PHP_OS === 'Windows') {
287 exec(sprintf("rd /s /q %s", escapeshellarg($destdir)));
288 }
289 else {
290 exec(sprintf("rm -rf %s", escapeshellarg($destdir)));
291 }
292 }
293 }
294
295
296 //////////////// Execution
297
298 $args = rcube_utils::get_opt(array('f' => 'force:bool', 'd' => 'delete:bool'))
299 + array('force' => false, 'delete' => false);
300 $WHAT = $args[0];
301
302 foreach ($SOURCES['dependencies'] as $package) {
303 if (!isset($package['name'])) {
304 $package['name'] = $package['lib'];
305 }
306
307 if ($WHAT && $package['lib'] !== $WHAT) {
308 continue;
309 }
310
311 if ($args['delete']) {
312 delete_destfile($package);
313 continue;
314 }
315
316 echo "Installing $package[name]...\n";
317
318 $srcfile = fetch_from_source($package, !$args['force'], $filetype);
319
320 if ($filetype === 'zip') {
321 extract_zipfile($package, $srcfile);
322 }
323 else {
324 compose_destfile($package, $srcfile);
325 }
326
327 echo "Done.\n\n";
328 }