forked from pluginsGLPI/formcreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoboFilePlugin.php
224 lines (204 loc) · 5.76 KB
/
RoboFilePlugin.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Formcreator 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 Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2020 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/
class RoboFilePlugin extends \Robo\Tasks
{
protected $csignore = [
'/vendor/',
'/node_modules/',
'/lib/',
'/development/',
'/output/',
];
protected $csfiles = ['./'];
/**
* Minify all
*
* @return void
*/
public function minify() {
$this->minifyCSS()
->minifyJS();
}
/**
* Minify CSS stylesheets
*
* @return void
*/
public function minifyCSS() {
$css_dir = __DIR__ . '/css';
if (is_dir($css_dir)) {
foreach (glob("$css_dir/*.css") as $css_file) {
if (!$this->endsWith($css_file, 'min.css')) {
$this->taskMinify($css_file)
->to(str_replace('.css', '.min.css', $css_file))
->type('css')
->run();
}
}
}
return $this;
}
/**
* Minify JavaScript files stylesheets
*
* @return void
*/
public function minifyJS() {
$js_dir = __DIR__ . '/js';
if (is_dir($js_dir)) {
foreach (glob("$js_dir/*.js") as $js_file) {
if (!$this->endsWith($js_file, 'min.js')) {
$this->taskMinify($js_file)
->to(str_replace('.js', '.min.js', $js_file))
->type('js')
->run();
}
}
}
return $this;
}
/**
* Extract translatable strings
*
* @return void
*/
public function localesExtract() {
$this->_exec('tools/extract_template.sh');
return $this;
}
/**
* Push locales to transifex
*
* @return void
*/
public function localesPush() {
$this->_exec('tx push -s');
return $this;
}
/**
* Pull locales from transifex.
*
* @param integer $percent Completeness percentage
*
* @return void
*/
public function localesPull($percent = 70) {
$this->_exec('tx pull -a --minimum-perc=' .$percent);
return $this;
}
/**
* Build MO files
*
* @return void
*/
public function localesMo() {
$this->_exec('./tools/release --compile-mo');
return $this;
}
/**
* Extract and send locales
*
* @return void
*/
public function localesSend() {
$this->localesExtract()
->localesPush();
return $this;
}
/**
* Retrieve locales and generate mo files
*
* @param integer $percent Completeness percentage
*
* @return void
*/
public function localesGenerate($percent = 70) {
$this->localesPull($percent)
->localesMo();
return $this;
}
/**
* Checks if a string ends with another string
*
* @param string $haystack Full string
* @param string $needle Ends string
*
* @return boolean
* @see http://stackoverflow.com/a/834355
*/
private function endsWith($haystack, $needle) {
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
/**
* Code sniffer.
*
* Run the PHP Codesniffer on a file or directory.
*
* @param string $file A file or directory to analyze.
* @param array $options Options:
* @option $autofix Whether to run the automatic fixer or not.
* @option $strict Show warnings as well as errors.
* Default is to show only errors.
*
* @return void
*/
public function codeCs(
$file = null,
$options = [
'autofix' => false,
'strict' => false,
]
) {
if ($file === null) {
$file = implode(' ', $this->csfiles);
}
$csignore = '';
if (count($this->csignore)) {
$csignore .= '--ignore=';
$csignore .= implode(',', $this->csignore);
}
$strict = $options['strict'] ? '' : '-n';
$result = $this->taskExec("./vendor/bin/phpcs $csignore --standard=vendor/glpi-project/coding-standard/GlpiStandard/ --standard=tests/rulest.xml {$strict} {$file}")->run();
if (!$result->wasSuccessful()) {
if (!$options['autofix'] && !$options['no-interaction']) {
$options['autofix'] = $this->confirm('Would you like to run phpcbf to fix the reported errors?');
}
if ($options['autofix']) {
$result = $this->taskExec("./vendor/bin/phpcbf $csignore --standard=vendor/glpi-project/coding-standard/GlpiStandard/ {$file}")->run();
}
}
return $result;
}
}