dolibarr 24.0.0-beta
buildzip.php
1#!/usr/bin/env php -d memory_limit=256M
2<?php
23/*
24 The goal of that php CLI script is to make zip package of your module
25 as an alternative to web "build zip" or "perl script makepack"
26*/
27
28// ============================================= configuration
29
35$listOfModuleContent = [
36 'admin',
37 'ajax',
38 'backport',
39 'class',
40 'css',
41 'COPYING',
42 'core',
43 'img',
44 'js',
45 'langs',
46 'lib',
47 'sql',
48 'tpl',
49 '*.md',
50 '*.json',
51 '*.php',
52 'modulebuilder.txt',
53];
54
60$exclude_list = [
61 '/^.git$/',
62 '/.*js.map/',
63 '/DEV.md/'
64];
65
66// ============================================= end of configuration
67
73function detectModule()
74{
75 $name = $version = "";
76 $tab = glob("core/modules/mod*.class.php");
77 if (count($tab) == 0) {
78 echo "[fail] Error on auto detect data : there is no mod*.class.php file into core/modules dir\n";
79 exit(1);
80 }
81 if (count($tab) == 1) {
82 $file = $tab[0];
83 $pattern = "/.*mod(?<mod>.*)\.class\.php/";
84 if (preg_match_all($pattern, $file, $matches)) {
85 $name = strtolower(reset($matches['mod']));
86 }
87
88 echo "extract data from $file\n";
89 if (!file_exists($file) || $name == "") {
90 echo "[fail] Error on auto detect data\n";
91 exit(2);
92 }
93 } else {
94 echo "[fail] Error there is more than one mod*.class.php file into core/modules dir\n";
95 exit(3);
96 }
97
98 //extract version from file
99 $contents = file_get_contents($file);
100 $pattern = "/^.*this->version\s*=\s*'(?<version>.*)'\s*;.*\$/m";
101
102 // search, and store all matching occurrences in $matches
103 if (preg_match_all($pattern, $contents, $matches)) {
104 $version = reset($matches['version']);
105 }
106
107 if (version_compare($version, '0.0.1', '>=') != 1) {
108 echo "[fail] Error auto extract version fail\n";
109 exit(4);
110 }
111
112 echo "module name = $name, version = $version\n";
113 return [(string) $name, (string) $version];
114}
115
123function delTree($dir)
124{
125 $files = array_diff(scandir($dir), array('.', '..'));
126 foreach ($files as $file) {
127 (is_dir("$dir/$file")) ? delTree("$dir/$file") : secureUnlink("$dir/$file");
128 }
129 return rmdir($dir);
130}
131
132
141function secureUnlink($path)
142{
143 if (file_exists($path)) {
144 if (unlink($path)) {
145 //then check if really deleted
146 clearstatcache();
147 if (file_exists($path)) { // @phpstan-ignore-line
148 echo "[fail] unlink of $path fail !\n";
149 exit(5);
150 }
151 } else {
152 echo "[fail] unlink of $path fail !\n";
153 exit(6);
154 }
155 }
156 return true;
157}
158
166function mkdirAndCheck($path)
167{
168 if (mkdir($path)) {
169 clearstatcache();
170 if (is_dir($path)) {
171 return true;
172 }
173 }
174 echo "[fail] Error on $path (mkdir)\n";
175 exit(7);
176}
177
185function is_excluded($filename)
186{
187 global $exclude_list;
188 $count = 0;
189 $notused = preg_filter($exclude_list, '1', $filename, -1, $count);
190 if ($count > 0) {
191 echo " - exclude $filename\n";
192 return true;
193 }
194 return false;
195}
196
205function rcopy($src, $dst)
206{
207 if (is_dir($src)) {
208 // Make the destination directory if not exist
209 mkdirAndCheck($dst);
210 // open the source directory
211 $dir = opendir($src);
212
213 // Loop through the files in source directory
214 while ($file = readdir($dir)) {
215 if (($file != '.') && ($file != '..')) {
216 if (is_dir($src . '/' . $file)) {
217 // Recursively calling custom copy function
218 // for sub directory
219 if (!rcopy($src . '/' . $file, $dst . '/' . $file)) {
220 return false;
221 }
222 } else {
223 if (!is_excluded($file)) {
224 if (!copy($src . '/' . $file, $dst . '/' . $file)) {
225 return false;
226 }
227 }
228 }
229 }
230 }
231 closedir($dir);
232 } elseif (is_file($src)) {
233 if (!is_excluded($src)) {
234 if (!copy($src, $dst)) {
235 return false;
236 }
237 }
238 }
239 return true;
240}
241
252function zipDir($folder, &$zip, $root = "")
253{
254 foreach (new \DirectoryIterator($folder) as $f) {
255 if ($f->isDot()) {
256 continue;
257 } //skip . ..
258 $src = $folder . '/' . $f;
259 $dst = substr($f->getPathname(), strlen($root));
260 if ($f->isDir()) {
261 if ($zip->addEmptyDir($dst)) {
262 if (zipDir($src, $zip, $root)) {
263 continue;
264 } else {
265 return false;
266 }
267 } else {
268 return false;
269 }
270 }
271 if ($f->isFile()) {
272 if (! $zip->addFile($src, $dst)) {
273 return false;
274 }
275 }
276 }
277 return true;
278}
279
284list($mod, $version) = detectModule();
285$outzip = sys_get_temp_dir() . "/module_" . $mod . "-" . $version . ".zip";
286if (file_exists($outzip)) {
287 secureUnlink($outzip);
288}
289
290//copy all sources into system temp directory
291$tmpdir = tempnam(sys_get_temp_dir(), $mod . "-module");
292secureUnlink($tmpdir);
293mkdirAndCheck($tmpdir);
294$dst = $tmpdir . "/" . $mod;
295mkdirAndCheck($dst);
296
297foreach ($listOfModuleContent as $moduleContent) {
298 foreach (glob($moduleContent) as $entry) {
299 if (!rcopy($entry, $dst . '/' . $entry)) {
300 echo "[fail] Error on copy " . $entry . " to " . $dst . "/" . $entry . "\n";
301 echo "Please take time to analyze the problem and fix the bug\n";
302 exit(8);
303 }
304 }
305}
306
307$z = new ZipArchive();
308$z->open($outzip, ZipArchive::CREATE);
309zipDir($tmpdir, $z, $tmpdir . '/');
310$z->close();
311delTree($tmpdir);
312if (file_exists($outzip)) {
313 echo "[success] module archive is ready : $outzip ...\n";
314} else {
315 echo "[fail] build zip error\n";
316 exit(9);
317}
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php