forked from pluginsGLPI/formcreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoboFile.php
222 lines (191 loc) · 6.78 KB
/
RoboFile.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
<?php
/**
* This is project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
require_once 'RoboFilePlugin.php';
class RoboFile extends RoboFilePlugin
{
protected static $banned = [
'dist',
'vendor',
'.git',
'.gitignore',
'.tx',
'.settings',
'.project',
'.buildpath',
'tools',
'tests',
'screenshot*.png',
'RoboFile*.php',
'plugin.xml',
'phpunit.xml.*',
'.travis.yml',
'save.sql',
];
protected function getPluginPath() {
return __DIR__;
}
protected function getPluginName() {
return basename($this->getPluginPath());
}
protected function getVersion() {
$setupFile = $this->getPluginPath(). "/setup.php";
$setupContent = file_get_contents($setupFile);
$pluginName = $this->getPluginName();
$constantName = "PLUGIN_" . strtoupper($this->getPluginName()) . "_VERSION";
$pattern = "#^define\('$constantName', '([^']*)'\);$#m";
preg_match($pattern, $setupContent, $matches);
if (isset($matches[1])) {
return $matches[1];
}
throw new Exception("Could not determine version of the plugin");
}
protected function getGLPIMinVersion() {
$setupFile = $this->getPluginPath(). "/setup.php";
$setupContent = file_get_contents($setupFile);
$pluginName = $this->getPluginName();
$constantName = "PLUGIN_" . strtoupper($this->getPluginName()) . "_GLPI_MIN_VERSION";
$pattern = "#^define\('$constantName', '([^']*)'\);$#m";
preg_match($pattern, $setupContent, $matches);
if (isset($matches[1])) {
return $matches[1];
}
throw new Exception("Could not determine version of the plugin");
}
/**
* Override to change the banned list
* @return array
*/
protected function getBannedFiles() {
return static::$banned;
}
//Own plugin's robo stuff
public function archiveBuild() {
$version = $this->getVersion();
if (!$this->isSemVer($version)) {
throw new Exception("$version is not semver compliant. See http://semver.org/");
}
if (!$this->tagExists($version)) {
throw new Exception("The tag $version does not exists yet");
}
if (!$this->isTagMatchesCurrentCommit($version)) {
throw new Exception("HEAD is not pointing to the tag of the version to build");
}
$versionTag = $this->getVersionTagFromXML($version);
if (!is_array($versionTag)) {
throw new Exception("The version does not exists in the XML file");
}
$pluginName = $this->getPluginName();
$pluginPath = $this->getPluginPath();
$targetFile = $pluginPath. "/dist/glpi-" . $this->getPluginName() . "-$version.tar.bz2";
$toArchive = implode(' ', $this->getFileToArchive($version));
@mkdir($pluginPath. "/dist");
$this->_exec("git archive --prefix=$pluginName/ $version $toArchive | bzip2 > $targetFile");
}
protected function getTrackedFiles($version) {
$output = [];
exec("git ls-tree -r '$version' --name-only", $output, $retCode);
if ($retCode != '0') {
throw new Exception("Unable to get tracked files");
}
return $output;
}
protected function getFileToArchive($version) {
$filesToArchive = $this->getTrackedFiles($version);
// prepare banned items for regex
$patterns = [];
foreach ($this->getBannedFiles() as $bannedItem) {
$pattern = "#" . preg_quote("$bannedItem", "#") . "#";
$pattern = str_replace("\\?", ".", $pattern);
$pattern = str_replace("\\*", ".*", $pattern);
$patterns[] = $pattern;
}
// remove banned files from the list
foreach ($patterns as $pattern) {
$filteredFiles = [];
foreach ($filesToArchive as $file) {
if (preg_match($pattern, $file) == 0) {
//Include the tracked file
$filteredFiles[] = $file;
}
}
// Repeat filtering from result with next banned files pattern
$filesToArchive = $filteredFiles;
}
return $filesToArchive;
}
protected function getAllTags() {
exec("git tag -l", $output, $retCode);
if ($retCode != '0') {
// An error occured
throw new Exception("Unable to get tags from the repository");
}
return $output;
}
protected function tagExists($version) {
$tags = $this->getAllTags();
return in_array($version, $tags);
}
/**
* Check the version is made of numbers separated by dots
*
* Returns true if the version is well formed, false otherwise
*
* @param string $version
* @return boolean
*/
protected function isSemVer($version) {
$semverPattern = '#\bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?\b#i';
if (preg_match($semverPattern, $version) !== 1) {
return false;
}
return true;
}
protected function getPluginXMLDescription() {
$pluginXML = 'plugin.xml';
if (!is_file($pluginXML) || !is_readable($pluginXML)) {
throw Exception("plugin.xml file not found");
}
$xml = simplexml_load_string(file_get_contents($pluginXML));
$json = json_encode($xml);
return json_decode($json, true);
}
protected function getVersionTagFromXML($versionToSearch) {
$xml = $this->getPluginXMLDescription();
foreach ($xml['versions']['version'] as $version) {
if ($version['num'] == $versionToSearch) {
// version found
return $version;
}
}
return null;
}
protected function getCurrentCommitHash() {
exec('git rev-parse HEAD', $output, $retCode);
if ($retCode != '0') {
throw new Exception("failed to get curent commit hash");
}
return $output[0];
}
protected function isTagMatchesCurrentCommit($tag) {
$commitHash = $this->getCurrentCommitHash();
exec("git tag -l --contains $commitHash", $output, $retCode);
if (isset($output[0]) && $output[0] == $tag) {
return true;
}
return false;
}
public function localesExtract() {
$potfile = strtolower($this->getPluginName()) . ".pot";
$phpSources = "*.php ajax/*.php front/*.php inc/*.php install/*.php";
// extract locales from source code
$command = "xgettext $phpSources -o locales/$potfile -L PHP --add-comments=TRANS --from-code=UTF-8 --force-po";
$command.= " --keyword=_n:1,2,4t --keyword=__s:1,2t --keyword=__:1,2t --keyword=_e:1,2t --keyword=_x:1c,2,3t --keyword=_ex:1c,2,3t";
$command.= " --keyword=_sx:1c,2,3t --keyword=_nx:1c,2,3,5t";
$this->_exec($command);
return $this;
}
}