-
Notifications
You must be signed in to change notification settings - Fork 98
/
updateGruntfile.php
executable file
·120 lines (106 loc) · 3.94 KB
/
updateGruntfile.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
#!/usr/bin/env php
<?php
/**
* helper to update EGroupware Gruntfile.js
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @author Ralf Becker <[email protected]>
* @copyright (c) 2016-18 by Ralf Becker <[email protected]>
*/
use EGroupware\Api\Framework;
use EGroupware\Api\Framework\Bundle;
if (php_sapi_name() !== 'cli') die("This is a commandline ONLY tool!\n");
// force a domain for MServer install
$_REQUEST['domain'] = 'boulder.egroupware.org';
$GLOBALS['egw_info'] = array(
'flags' => array(
'currentapp' => 'login',
)
);
include(__DIR__.'/header.inc.php');
$gruntfile = EGW_SERVER_ROOT.'/Gruntfile.js';
if (!($content = @file_get_contents($gruntfile)))
{
die("\nFile '$gruntfile' not found!\n\n");
}
if (!preg_match('/grunt\.initConfig\(({.+})\);/s', $content, $matches) ||
!($json = preg_replace('/^(\s*)([a-z0-9_-]+):/mi', '$1"$2":', $matches[1])) ||
!($config = json_decode($json, true)))
{
die("\nCan't parse $gruntfile!\n\n");
}
//print_r($config); exit;
$uglify =& $config['terser'];
foreach(Bundle::all() as $name => $files)
{
if ($name == '.ts') continue; // ignore timestamp
// remove leading / from file-names
array_walk($files, function(&$path)
{
if ($path[0] == '/') $path = substr($path, 1);
});
// some files are not in a bundle, because they are big enough themselves
foreach(Bundle::$exclude as $file)
{
if (($key = array_search($file, $files))) unset($files[$key]);
}
//var_dump($name, $files);
if (isset($uglify[$name]))
{
$target = key($uglify[$name]['files']);
$uglify[$name]['files'][$target] = array_values($files);
}
elseif (isset($uglify[$append = substr($name, 0, -1)]))
{
reset($uglify[$append]['files']);
$target = key($uglify[$append]['files']);
$uglify[$append]['files'][$target] = array_merge($uglify[$append]['files'][$target], array_values($files));
}
else // create new bundle using last file as target
{
$target = str_replace('.js', '.min.js', end($files));
$uglify[$name]['files'][$target] = array_values($files);
}
}
// add css for all templates and themes
$cssmin =& $config['cssmin'];
$GLOBALS['egw_info']['flags']['currentapp'] = '*grunt*'; // to not find any app.css files
$GLOBALS['egw_info']['server']['debug_minify'] = 'True'; // otherwise we would only get minified file
foreach(array('pixelegg', 'jdots', 'kdots')/*array_keys(Framework::list_templates())*/ as $template)
{
$GLOBALS['egw_info']['server']['template_set'] = $template;
$tpl = Framework::factory();
$themes = $tpl->list_themes();
if ($template == 'pixelegg') $themes[] = 'fw_mobile'; // this is for mobile devices
foreach(array_keys($themes) as $theme)
{
// skip not working cssmin of pixelegg/traditional: Broken @import declaration of "../../etemplate/templates/default/etemplate2.css"
if ($template == 'pixelegg' && $theme == 'traditional') continue;
$GLOBALS['egw_info']['user']['preferences']['common']['theme'] = $theme;
// empty include list by not-existing file plus last true
Framework\CssIncludes::add('*grunt*', null, true, true);
$tpl->_get_css();
$dest = substr($tpl->template_dir, 1).($theme == 'fw_mobile' ? '/mobile/' : '/css/').$theme.'.min.css';
$cssmin[$template]['files'][$dest] =
// remove leading slash from src path
array_map(function($path)
{
return substr($path, 1);
},
// filter out all dynamic css, like categories.php
array_values(array_filter(Framework\CssIncludes::get(true), function($path)
{
return strpos($path, '.php?') === false;
})));
}
}
$new_json = str_replace("\n", "\n\t",
preg_replace_callback('/^( *)/m', function($matches)
{
return str_repeat("\t", strlen($matches[1])/4);
}, json_encode($config, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)));
$new_content = preg_replace('/^(\s*)"([a-z0-9]+)":/mi', '$1$2:', $new_json);
//die($new_content."\n");
rename($gruntfile, $gruntfile.'.old');
file_put_contents($gruntfile, str_replace($matches[1], $new_content, $content));