/
make.php
executable file
·400 lines (334 loc) · 13.9 KB
/
make.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env php
<?php
/**
* Make for Multirename
*
* @license LGPL Version 3 http://www.gnu.org/licenses/lgpl-3.0.txt
* @copyright (c) 2015 by Florian Blasel
* @author Florian Blasel <flobee.code@gmail.com>
*
* @version 1.1.0 Created 2015-04-06
*/
// cd to project root and revert after
$dirCurrent = getcwd();
chdir( __DIR__ . '/../' );
$newRelease = false;
ini_set( 'include_path', 'src/library/mumsys/' );
require_once('Mumsys_Loader.php');
spl_autoload_extensions( '.php' );
spl_autoload_register( array('Mumsys_Loader', 'autoload') );
/**
* relevant docs for wiki and readme.md ONLY for the stable/master branch at github or
* for local, individual installations or bundles
* Array value contains the location of the wiki file name
*/
$docs = array(
'README.txt' => 'externals/multirename.wiki/Home.md',
// 'SUMMARY.txt', // content to be generated where the "# Summary" tag is
'FEATURES.txt' => 'externals/multirename.wiki/1_Features-of-multirename.md',
'EXAMPLES.txt' => 'externals/multirename.wiki/2_Examples-for-multirename.md',
'INSTALL.txt' => 'externals/multirename.wiki/3_Installing-multirename.md',
'USAGE.txt' => 'externals/multirename.wiki/4_Usage-of-multirename.md',
'CONTRIBUTE.txt'=> 'externals/multirename.wiki/5_Contributions.md',
'AUTHORS.txt' => 'externals/multirename.wiki/6_Contribution_Authors.md',
'HISTORY.txt' => 'externals/multirename.wiki/7_History-of-multirename.md',
'CHANGELOG.txt' => 'externals/multirename.wiki/8_0_Changelog-of-multirename.md',
'BUGS.txt' => 'externals/multirename.wiki/8_1_Bugs-of-multirename.md',
'LICENSE.txt' => 'externals/multirename.wiki/9_License-for-multirename.md',
);
/**
* Creates the multirename.phar file
*
* @param string $version Version string for the target file name
*
* @return bool True on success or false on error or phar file already exists
*/
function makePhar( $version = '0.0.0' )
{
$pharFile = 'deploy/multirename-' . $version . '.phar';
if ( file_exists( $pharFile ) ) {
return false;
}
$phar = new Phar(
$pharFile,
FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME,
"multirename.phar"
);
$phar->startBuffering();
//$stub = "#!/usr/bin/env php\n" . $phar->createDefaultStub('multirename.php','multirename.php');
//file_put_contents('build/stub.php', $stub);
exec('php -w build/stub.php > build/stub.php.min');
$phar->setStub( "#!/usr/bin/env php\n" . file_get_contents( 'build/stub.php.min' ) );
//$phar->setStub( $stub );
$pRoot = '';
$pLibFrom = 'src/library/mumsys/';
$pLibTo = 'library/mumsys/';
$filesForBuild = array(
'src/multirename.php' => $pRoot, // root level
$pLibFrom . 'Mumsys_Abstract.php' => $pLibTo,
$pLibFrom . 'Mumsys_Exception.php' => $pLibTo,
$pLibFrom . 'Mumsys_Loader_Exception.php' => $pLibTo,
$pLibFrom . 'Mumsys_Loader.php' => $pLibTo,
$pLibFrom . 'Mumsys_Php_Globals.php' => $pLibTo,
$pLibFrom . 'Mumsys_File_Interface.php' => $pLibTo,
$pLibFrom . 'Mumsys_File_Exception.php' => $pLibTo,
$pLibFrom . 'Mumsys_File.php' => $pLibTo,
$pLibFrom . 'Mumsys_Logger_Writer_Interface.php' => $pLibTo,
$pLibFrom . 'Mumsys_Logger_Decorator_Interface.php' => $pLibTo,
$pLibFrom . 'Mumsys_Logger_Decorator_Abstract.php' => $pLibTo,
$pLibFrom . 'Mumsys_Logger_Decorator_Messages.php' => $pLibTo,
$pLibFrom . 'Mumsys_Logger_Interface.php' => $pLibTo,
$pLibFrom . 'Mumsys_Logger_Exception.php' => $pLibTo,
$pLibFrom . 'Mumsys_Logger_Abstract.php' => $pLibTo,
$pLibFrom . 'Mumsys_Logger_File.php' => $pLibTo,
$pLibFrom . 'Mumsys_GetOpts_Exception.php' => $pLibTo,
$pLibFrom . 'Mumsys_GetOpts.php' => $pLibTo,
$pLibFrom . 'Mumsys_FileSystem_Interface.php' => $pLibTo,
$pLibFrom . 'Mumsys_FileSystem_Exception.php' => $pLibTo,
$pLibFrom . 'Mumsys_FileSystem_Common_Abstract.php' => $pLibTo,
$pLibFrom . 'Mumsys_FileSystem.php' => $pLibTo,
$pLibFrom . 'Mumsys_Multirename_Exception.php' => $pLibTo,
$pLibFrom . 'Mumsys_Multirename.php' => $pLibTo,
);
$filesForBuildCnt = count($filesForBuild);
foreach ( $filesForBuild as $fileSrc => $fileGoesTo ) {
$fileTmp = 'tmp/' . basename( $fileSrc ) . '.min';
exec( 'php -w ' . $fileSrc . ' > ' . $fileTmp );
$phar->addFile( $fileTmp, $fileGoesTo . basename( $fileSrc ) );
echo '.';
unlink( $fileTmp);
}
echo PHP_EOL;
$phar->stopBuffering();
return true;
}
/**
* Scan file and replace all after "## Usage options" with multirename --help output
*
* @param string $keyword
*/
function updUsageFile( $keyword = '## Usage options (--help)' )
{
$newUsage = '';
$file = './docs/' . 'USAGE.txt';
$lines = file($file);
foreach ($lines as $key => $line) {
if ($line === $keyword.PHP_EOL) {
$newUsage .= $keyword . PHP_EOL;
break;
}
$newUsage .= $line;
}
$newUsage .= PHP_EOL;
$list = Mumsys_Multirename::getSetup(true);
$list['--help'] = 'Show this help';
$wrap = 72;
$indentOption = ' ';
$indentComment = " ";
foreach($list as $option => $desc)
{
$needvalue = strpos($option, ':');
$option = str_replace(':', '', $option);
if ($needvalue) {
$option .= ' <yourValue/s>';
}
if ($desc) {
$desc = $indentComment . wordwrap($desc, $wrap, PHP_EOL . $indentComment);
}
$newUsage .= $indentOption . $option . PHP_EOL
. $desc . '' . PHP_EOL . PHP_EOL;
}
$newUsage .= PHP_EOL . PHP_EOL;
file_put_contents($file, $newUsage);
}
/**
* creates the README.md file for github
*/
function makeReadmeMd()
{
global $docs;
$summary = [];
$content = '';
$target = './README.md';
// TOC tree to show. Value 1 - ~5
$levelsToShow = 2;
$summary = [];
$content = '';
foreach ($docs as $doc => $wiki)
{
if (is_int($doc)) {
$doc = $wiki;
}
$docUsage = false;
$lines = file('./docs/' . $doc);
foreach ($lines as $line)
{
$content .= $line;
// build summary list (h1-h6)
if ( $line !== "# Summary\n"
&& preg_match( '/^(#|##|###|####|#####|######)?( \w)+/i', $line, $matches ) ) {
if ( ($cntIndent = strlen( $matches[1] ) - 1 ) < 0 ) {
$cntIndent = 0;
}
if ($levelsToShow -1 < $cntIndent ) {
continue;
}
$prefix = strstr($line, ' ', true);
$line = str_replace('#', '', $line);
if (!isset($prefix[0])) { // ???
echo 'ERROR with line: "' . $line . '"' . PHP_EOL;
}
$indent = str_repeat( "\t", $cntIndent );
$prefix = str_replace($matches[1], $indent . '-', $prefix);
$line = trim($line);
$linkLine = strtolower($line);
$linkLine = preg_replace('/(\s|\W)+/', '-', $linkLine);
$linkLine = trim($linkLine, '-');
// the docs have *nix ending: \n
$summary[] = $prefix . ' [' . $line . '](#' . $linkLine . ')' . "\n";
}
}
}
$summary = '# Summary' . "\n" . "\n" . implode('', $summary);
$content = str_replace('# Summary', $summary, $content);
// replace makers
$content = str_replace(array('##VERSIONSTRING##'), array(Mumsys_Multirename::VERSION), $content);
file_put_contents($target, $content);
}
/**
* Creates wiki documentation files
*/
function mkWikiMd()
{
global $docs;
foreach ( $docs as $doc => $wikifile ) {
if ( is_int( $doc ) ) {
continue;
}
$text = str_replace(
array('##VERSIONSTRING##'),
array(Mumsys_Multirename::VERSION),
file_get_contents( 'docs/' . $doc )
);
file_put_contents( $wikifile, $text );
}
}
try
{
$cliOptsCfg = array(
'install' => 'Compile the phar file',
'clean' => 'Removes the phar and created tmp files',
'deploy' => array(
'Generates the phar file and updates the wiki docs and /README.md file.' . PHP_EOL => '',
'--compress' => 'Flag; If given e.g: `php make.php deploy '
. '--compress` it creates e.g `deploy/multirename-CURRENT_VERSION.tgz` '
. 'including the phar and the README.md',
),
'--help|-h' => 'Show this help',
);
$cliOpts = new Mumsys_GetOpts( $cliOptsCfg );
$cliOptsResult = $cliOpts->getResult();
if ( isset( $cliOptsResult['help'] ) ) {
echo $cliOpts->getHelp();
exit(); // only here stop to prevent action calls
}
if ( $cliOptsResult === array() ) {
$cliOptsResult['help'] = true;
}
//require_once 'Mumsys_Multirename.php';
echo 'Make file for ' . Mumsys_Multirename::getVersion() . PHP_EOL . PHP_EOL;
$version = Mumsys_Multirename::VERSION;
foreach ( $cliOptsResult as $action => $actionOptions ) {
switch ( $action ) {
case 'install':
echo 'run: ' . $action . ' start:' . PHP_EOL;
$makePharStatus = makePhar( $version );
if ( !file_exists( 'deploy/multirename-' . $version . '.phar' ) ) {
echo 'deploy/multirename-' . $version . '.phar not found. '
.'Creation failed!
';
} else {
if ( $makePharStatus ) {
echo 'If you dont see any errors... multirename-' . $version
. '.phar was created successfully' . PHP_EOL
. PHP_EOL;
} else {
echo 'FAIL: deploy/multirename-' . $version . '.phar '
.' already exists. ' . PHP_EOL
. 'Remove/ rename the file to generate '
. 'a new phar file.'. PHP_EOL
. PHP_EOL;
}
echo '#### test it:' . PHP_EOL
. '# chmod +x deploy/multirename-' . $version . '.phar' . PHP_EOL
. '# ./deploy/multirename-' . $version . '.phar --help' . PHP_EOL
. '#' . PHP_EOL
. '### make globaly available' . PHP_EOL
. '# mv build/multirename-' . $version . '.phar /usr/local/bin/multirename' . PHP_EOL
. '# multirename --help' . PHP_EOL
. PHP_EOL
. PHP_EOL
;
}
echo 'run: ' . $action . ' end.' . PHP_EOL;
break;
case 'clean':
echo 'run: ' . $action . ' start:' . PHP_EOL;
$cleanList = array(
'build/stub.php.min' => 'Precompiled by phar extension',
'deploy/multirename.phar' => 'The created multirename.phar file',
'deploy/multirename-' . $version . '.phar' => 'The created multirename.VERSION.phar file',
'deploy/multirename-' . $version . '.tgz' => 'The created compressed package',
);
foreach ( $cleanList as $idx => $message ) {
if ( file_exists( $idx ) ) {
unlink( $idx );
printf( 'Removed file "%1$s": %2$s%3$s', $idx, $message, PHP_EOL );
}
}
echo 'run: ' . $action . ' end.' . PHP_EOL . PHP_EOL;
break;
case 'deploy':
echo 'run: ' . $action . ' start:' . PHP_EOL;
// for deployment of a new releases or updating the docs
makePhar( $version );
updUsageFile('## Usage options (--help)');
echo 'USAGE.txt updated' . PHP_EOL;
makeReadmeMd();
echo 'README.md created' . PHP_EOL;
mkWikiMd();
echo 'Wiki files created' . PHP_EOL;
$compress = $actionOptions['compress'] ?? false;
if ( $compress ) {
// rename('build/multirename.phar', 'build/multirename-'.$version.'.phar');
$tgzFile = 'deploy/multirename-'.$version.'.tgz';
if ( file_exists( $tgzFile ) ) {
echo 'EXISTS: tgz in deploy/ already exists. Asume only one package per version' . PHP_EOL;
} else {
$cmd = 'tar -czf "' . $tgzFile . '" '
. 'deploy/multirename-' . $version . '.phar '
. 'docs/LICENSE.txt '
. 'README.md'
;
exec( $cmd );
echo 'tgz in deploy/ created' . PHP_EOL;
}
}
echo 'run: ' . $action . ' end.' . PHP_EOL . PHP_EOL;
break;
default:
echo <<<EOTXT
Please read the README.txt and INSTALL.txt befor you go on.
Deployment tasks: Please read the CONTRIBUTE.txt informations
Several actions possible, e.g: make.php clean install deploy --compress
EOTXT;
echo $cliOpts->getHelp();
break;
}
}
} catch ( Exception $e ) {
echo $e->getMessage() . PHP_EOL;
echo $e->getTraceAsString() . PHP_EOL;
exit( 1 );
}
chdir( $dirCurrent );