Issues (4868)

updateGruntfile.php (2 issues)

1
#!/usr/bin/env php
2
<?php
3
/**
4
 * helper to update EGroupware Gruntfile.js
5
 *
6
 * @link http://www.egroupware.org
7
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
8
 * @author Ralf Becker <[email protected]>
9
 * @copyright (c) 2016-18 by Ralf Becker <[email protected]>
10
 */
11
12
use EGroupware\Api\Framework;
13
use EGroupware\Api\Framework\Bundle;
14
15
if (php_sapi_name() !== 'cli') die("This is a commandline ONLY tool!\n");
16
17
// force a domain for MServer install
18
$_REQUEST['domain'] = 'boulder.egroupware.org';
19
$GLOBALS['egw_info'] = array(
20
	'flags' => array(
21
		'currentapp' => 'login',
22
	)
23
);
24
include(__DIR__.'/header.inc.php');
25
26
$gruntfile = __DIR__.'/Gruntfile.js';
27
if (!($content = @file_get_contents($gruntfile)))
28
{
29
	die("\nFile '$gruntfile' not found!\n\n");
30
}
31
32
if (!preg_match('/grunt\.initConfig\(({.+})\);/s', $content, $matches) ||
33
	!($json = preg_replace('/^(\s*)([a-z0-9_-]+):/mi', '$1"$2":', $matches[1])) ||
34
	!($config = json_decode($json, true)))
35
{
36
	die("\nCan't parse $path!\n\n");
37
}
38
//print_r($config); exit;
39
40
$uglify =& $config['uglify'];
41
42
// some files are not in a bundle, because loaded otherwise or are big enough themselfs
43
$exclude = array(
44
	// api/js/jsapi/egw.js loaded via own tag, and we must not load it twice!
45
	'api/js/jsapi/egw.js',
46
	// TinyMCE is loaded separate before the bundle
47
	'vendor/tinymce/tinymce/tinymce.min.js',
48
);
49
50
foreach(Bundle::all() as $name => $files)
51
{
52
	if ($name == '.ts') continue;	// ignore timestamp
53
54
	// remove leading / from file-names
55
	array_walk($files, function(&$path)
56
	{
57
		if ($path[0] == '/') $path = substr($path, 1);
58
	});
59
60
	// some files are not in a bundle, because they are big enough themselfs
61
	foreach($exclude as $file)
62
	{
63
		if (($key = array_search($file, $files))) unset($files[$key]);
64
	}
65
66
	//var_dump($name, $files);
67
	if (isset($uglify[$name]))
68
	{
69
		list($target) = each($uglify[$name]['files']);
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
		list($target) = /** @scrutinizer ignore-deprecated */ each($uglify[$name]['files']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
70
		$uglify[$name]['files'][$target] = array_values($files);
71
	}
72
	elseif (isset($uglify[$append = substr($name, 0, -1)]))
73
	{
74
		reset($uglify[$append]['files']);
75
		list($target) = each($uglify[$append]['files']);
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

75
		list($target) = /** @scrutinizer ignore-deprecated */ each($uglify[$append]['files']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
76
		$uglify[$append]['files'][$target] = array_merge($uglify[$append]['files'][$target], array_values($files));
77
	}
78
	else	// create new bundle using last file as target
79
	{
80
		$target = str_replace('.js', '.min.js', end($files));
81
		$uglify[$name]['files'][$target] = array_values($files);
82
	}
83
}
84
85
// add css for all templates and themes
86
$cssmin =& $config['cssmin'];
87
$GLOBALS['egw_info']['flags']['currentapp'] = '*grunt*';	// to not find any app.css files
88
$GLOBALS['egw_info']['server']['debug_minify'] = 'True';	// otherwise we would only get minified file
89
foreach(array('pixelegg','jdots')/*array_keys(Framework::list_templates())*/ as $template)
90
{
91
	$GLOBALS['egw_info']['server']['template_set'] = $template;
92
	$tpl = Framework::factory();
93
	$themes = $tpl->list_themes();
94
	if ($template == 'pixelegg') $themes[] = 'fw_mobile';	// this is for mobile devices
95
	foreach(array_keys($themes) as $theme)
96
	{
97
		// skip not working cssmin of pixelegg/traditional: Broken @import declaration of "../../etemplate/templates/default/etemplate2.css"
98
		if ($template == 'pixelegg' && $theme == 'traditional') continue;
99
		$GLOBALS['egw_info']['user']['preferences']['common']['theme'] = $theme;
100
		// empty include list by not-existing file plus last true
101
		Framework\CssIncludes::add('*grunt*', null, true, true);
102
		$tpl->_get_css();
103
		$dest = substr($tpl->template_dir, 1).($theme == 'fw_mobile' ? '/mobile/' : '/css/').$theme.'.min.css';
104
		$cssmin[$template]['files'][$dest] =
105
			// remove leading slash from src path
106
			array_map(function($path)
107
			{
108
				return substr($path, 1);
109
			},
110
			// filter out all dynamic css, like categories.php
111
			array_values(array_filter(Framework\CssIncludes::get(true), function($path)
112
			{
113
				return strpos($path, '.php?') === false;
114
			})));
115
	}
116
}
117
118
$new_json = str_replace("\n", "\n\t",
119
	preg_replace_callback('/^( *)/m', function($matches)
120
	{
121
		return str_repeat("\t", strlen($matches[1])/4);
122
	}, json_encode($config, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)));
123
124
$new_content = preg_replace('/^(\s*)"([a-z0-9]+)":/mi', '$1$2:', $new_json);
125
//die($new_content."\n");
126
127
rename($gruntfile, $gruntfile.'.old');
128
file_put_contents($gruntfile, str_replace($matches[1], $new_content, $content));
129