Check-in [b62946f7b1]
Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add `static` to class functions (php8), revert to \r\n in place of \R for older libpcre versions. |
---|---|
Timelines: | family | ancestors | trunk |
Files: | files | file ages | folders |
SHA1: |
b62946f7b1ea3d05644920ea43591192 |
User & Date: | mario 2020-10-31 21:18:33 |
Context
2020-10-31
| ||
21:18 | Add `static` to class functions (php8), revert to \r\n in place of \R for older libpcre versions. Leaf check-in: b62946f7b1 user: mario tags: trunk | |
2014-02-10
| ||
00:51 | Reintroduce --help as command, Allow symlinks on version script (set_version, write_version, show_version) to define a default action. check-in: ea215399f1 user: mario tags: trunk, stable | |
Changes
Changes to version.php.
1 2 3 4 5 6 | #!/usr/bin/php <?php /** * type: cli * title: Version Get/Write * description: Commandline tool to read and update source code version numbers | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #!/usr/bin/php <?php /** * type: cli * title: Version Get/Write * description: Commandline tool to read and update source code version numbers * version: 3.2.7-0 * category: tools * * * This tool reads various formats of "Version: x.y*" from and to source code * or distribution files. It's useful for keeping them synced or prepared without * further packaging tool scripting. * |
︙ | ︙ | |||
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | * * --incr Increment patch ver number (last colon-separated 0.0.x decimal) * --bump Add/count up packaging/suffix (first num after dash 0.0.0-x) * You can also bump in bigger spans using --bump::2 or --incr::3 * * --format:ini Explicit file format * --format:php:2 Also specify how many occurences to replace. * * You can add the :format:count extras to any command actually, so `-fmt:py:2` * is used in `-write:py:2` implicitly. * More aliases exist for actions, for example ++ for -increment. * For format types you can specify both file extension and internal rx format * monikers (ini, var, const, ... or `_raw_` for any versiony-looking string). * * Often you may wish to split up invocations from the shell, as in: * * VERSION=$(version get orig.c ++) * version write $VERSION dest.cpp * * There's no commandline option for custom regexen, to eschew code duplication * in said shell scripts. * | > > | | | | > > > > | | > | 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 | * * --incr Increment patch ver number (last colon-separated 0.0.x decimal) * --bump Add/count up packaging/suffix (first num after dash 0.0.0-x) * You can also bump in bigger spans using --bump::2 or --incr::3 * * --format:ini Explicit file format * --format:php:2 Also specify how many occurences to replace. * * --rx:name:.+ Add a custom content regex. * * You can add the :format:count extras to any command actually, so `-fmt:py:2` * is used in `-write:py:2` implicitly. * More aliases exist for actions, for example ++ for -increment. * For format types you can specify both file extension and internal rx format * monikers (ini, var, const, ... or `_raw_` for any versiony-looking string). * * Often you may wish to split up invocations from the shell, as in: * * VERSION=$(version get orig.c ++) * version write $VERSION dest.cpp * * There's no commandline option for custom regexen, to eschew code duplication * in said shell scripts. * *@version 3.2.7-0 */ // Tool-specific data class cfg { #-- Regular expressions // Generic version matching with (?&ver) static $rx_ver = "( (?<epoch>\d+:)? (?<major>\d+) \.(?<minor>\d+) (?:\.(?<patch>\d+))? (?<suffix>[\d\w\-+.~_]*) )"; // Whitespace without linebreaks, // will be applied in place of `\s` in the following rx_format list static $rx_ws = "[^\S\\r\\n]"; # mostly like `\h` // Default regex end match (?&end) static $rx_end = " \s* \r?$"; // Filetype/language-specific context matching static $rx_format = array( // Debian package description "debian" => "^ Version: \s+ ", // RPM package description "rpmspec" => "^ % version \s+", // Generic meta data comment block "plugin" => "^ \s* (?:\*|\#|\/\/) \s* (?i) version: \s* ", // Documentation "docblock" => "^ \s* [*] \s? @version \s+", // Constant declarations "const" => array("^ \s* const \s+ \w*VERSION \s+ = \s+ [\'\"]", '(?&ver)', "[\'\"]; (?&end)"), // PHP constant declarations "define" => array("^ \s* define \( [\'\"] \w*VERSION [\'\"], \s* [\'\"]", '(?&ver)', "[\'\"]\); (?&end)"), // Scripting language variable or property assignments "var" => array("^ \s* (?:var|public|private|protected|static|global)? \s* \$\w*version \s* = \s* [\'\"]", '(?&ver)', "[\'\"]; (?&end)"), // Powershell param "ps1" => array(" (?i) \$ VERSION \s* = \s* [\'\"]", '(?&ver)', "[\'\"]"), // Wordpress plugin meta data "wordpress"=> "^ \s* Version: \s* ", // Config.ini files "ini" => array("^ \s* version \s* = \s* [\'\"]?", '(?&ver)', "[\'\"]? (?&end)"), // Python module versions "python" => array("^ \s* __version__ \s* = \s* [\'\"]", '(?&ver)', "[\'\"] (?&end)"), // JSON dict as used by PHP composer "composer" => array("[{,] \s* \"version\" \s* : \s* \" ", '(?&ver)', " \" \s* [,}] "), // Single XML tag "xmltag" => array("<version>\s*", '(?&ver)', "\s*<\/version>"), "xmlval" => array("<version\s+value=\"", '(?&ver)', "\"\s*\/?>"), // Would match ANYTHING in a text file that looks like a version number. "_raw_" => array("", '(?&ver)', "") ); // Map file extensions to regex format names static $ext_to_rxformat = array( "php" => array("plugin", "docblock", "define", "const", "var", "wordpress"), "control" => array("debian"), "py" => array("python", "plugin", "ini"), "cpp" => array("plugin", "const"), "c" => array("plugin", "ini"), "ps1" => array("ps1", "plugin", "var"), "json" => array("composer"), "sh" => array("plugin", "ini"), "xml" => array("xmltag", "xmlval"), "spec" => array("rpmspec"), "list" => array("rpmspec"), #"txt" => array("_raw_"), // e.g. distutils2 version.txt "any" => array("debian","rpmspec","plugin","docblock","const","define","var","wordpress","ini","python","composer","xmltag","ps1"), ); // Binary file format handlers (see action:: class) static $bin_handlers = array( "phar" => "bin_phar", "deb" => "bin_deb", ); // Command line aliases static $action_aliases = array ( "write" => "write", "save" => "write", "update" => "write", "get" => "get", "from" => "get", "read" => "read", "fetch" => "read", "show" => "show", "echo" => "show", "print" => "show", "increment" => "increment", "++" => "increment", "add" => "increment", "inc" => "increment", "incr" => "increment", "bump" => "bump", "suffix" => "bump", "release" => "bump", "format" => "format", "fmt" => "format", "and" => "format", "then" => "format", "set" => "format", "rx" => "rx", "regex" => "rx", "last" => "last", "lastfn" => "last", "help" => "help", "dry" => "dry", "nowrite" => "dry", "dryrun" => "dry", ); }//cfg |
︙ | ︙ | |||
302 303 304 305 306 307 308 309 310 311 312 313 314 315 | break; // Make 'write' command non-writey case "dry": $cmd->dry = 1; break; // NOP case "": case "format": // do nothing (--format:TYPE:N params can be attached to any other command, --format itself incurs no action) break; // Show top level documentation as help | > > > > > > | 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | break; // Make 'write' command non-writey case "dry": $cmd->dry = 1; break; // Allow custom regexen after all case "rx": cfg::$rx_format[$cmd->format] = array($cmd->count, '(?&ver)', ""); $cmd->count = 1; break; // NOP case "": case "format": // do nothing (--format:TYPE:N params can be attached to any other command, --format itself incurs no action) break; // Show top level documentation as help |
︙ | ︙ | |||
334 335 336 337 338 339 340 | /** * Read file, and preg_match() for version number. * */ | | > > | | 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 | /** * Read file, and preg_match() for version number. * */ static function read($cmd) { #-- binary if (self::bin_read($cmd)) { return; } #-- text, read src $src = file_get_contents($cmd->file); // match foreach (rx::get($cmd) as $rx) { #print "trying $rx …\n"; #print substr($src, 0, 1024); if (preg_match($rx, $src, $match)) { $cmd->version = $match["version"]; return; } } //ooops } /** * Read file, and preg_match() for version number. * */ static function write($cmd) { #-- binary if (self::bin_write($cmd)) { return; } #-- text, read src |
︙ | ︙ | |||
409 410 411 412 413 414 415 | /** * Increment patch 0.0.x or minor version number 0.y * */ | | | | | | | | 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | /** * Increment patch 0.0.x or minor version number 0.y * */ static function increment($cmd) { $cmd->version = preg_replace_callback("/(?<=\.) (\d+) (?=[^.]*$)/x", function($m) use ($cmd) { return $m[1] + $cmd->count; }, $cmd->version); } /** * Increment -1 release suffix, or add one. * * @todo: support $cmd->dist for e.g. -0vendor2 */ static function bump($cmd) { if (preg_match("/-\d+/", $cmd->version)) { $cmd->version = preg_replace_callback("/(?<=-) (\d+)/x", function($m) use ($cmd) { return $m[1] + $cmd->count; }, $cmd->version); } else { $cmd->version .= "-0"; } } /** * Print out file comment as --help * */ static function help() { $src = file_get_contents(__FILE__) and preg_match("#/\*\*.+?\*/#s", $src, $m) and $src = $m[0]; $src = preg_replace("#^/\*+\s*$|^\s*\*/$|^ \*(@version.+)? ?#m", "", $src); $src = preg_replace( array("/^\w+:|--/m", "/version/", "/get|show|read|write|incr|bump|format/", "/[a-z]\w+\.\w+/", "/:/"), array("\33[30;1m$0\33[0m", "\33[32m$0\33[0m", "\33[31m$0\33[0m", "\33[36m$0\33[0m", "\33[33;1m$0\33[0m"), $src ); die($src); } #-- binary handlers // read "version" meta field via binary helpers static function bin_read($cmd) { if (isset(cfg::$bin_handlers[$cmd->ext])) { $cmd->version = ""; // unset ->version so it becomes a read action return self::bin_write($cmd); } } // write "version" meta field to binary static function bin_write($cmd) { if (isset(cfg::$bin_handlers[$cmd->ext])) { $h = cfg::$bin_handlers[$cmd->ext]; if (method_exists("action", $h)) { call_user_func(array("action",$h), $cmd); return TRUE; } } } // .phar files static function bin_phar($cmd) { // open and get old meta block $p = new Phar($cmd->file); $m = $p->getMetadata(); // read if (empty($cmd->version)) { |
︙ | ︙ | |||
500 501 502 503 504 505 506 | // same as: // phar meta-get -f filename.phar -k version } // .deb archives (read-only) | | | 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | // same as: // phar meta-get -f filename.phar -k version } // .deb archives (read-only) static function bin_deb($cmd) { // read if (empty($cmd->version)) { $fn = escapeshellarg($cmd->file); $src = `ar p {$fn} control.tar.gz | tar xOz ./control`; $rx = rx::combine(array(cfg::$rx_format["debian"])); if (preg_match($rx[0], $src, $m)) { |
︙ | ︙ | |||
529 530 531 532 533 534 535 | * Regex lookup and preparation * * */ class rx { // Turn list of formats or ext specifiers into regex list | | | | 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | * Regex lookup and preparation * * */ class rx { // Turn list of formats or ext specifiers into regex list static function get($cmd) { return rx::combine( rx::formats($cmd) ); } /** * Return ordered list of regular expression to try. * A specific ->type overrides the ->ext defaults. * */ static function formats($cmd) { // request a specific rx_format or file extension if ($cmd->format) { $search = explode(",", $cmd->format); } // base on file extensions elseif ($cmd->ext) { |
︙ | ︙ | |||
581 582 583 584 585 586 587 | * Combine a list of regex parts with $rx_ver, $rx_end * * / ($BEGIN) ($rx_ver) ($END | $rx_end) /mx * * Return list, to be readily used in preg_match or _replace. * */ | | | 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | * Combine a list of regex parts with $rx_ver, $rx_end * * / ($BEGIN) ($rx_ver) ($END | $rx_end) /mx * * Return list, to be readily used in preg_match or _replace. * */ static function combine($prefixes) { // patch together foreach ($prefixes as $i=>$parts) { // parts list($begin, $ver, $end) = is_array($parts) ? $parts |
︙ | ︙ | |||
611 612 613 614 615 616 617 | /** * Version string conformance testing. * * Applies $rx_ver obviously, * but also checks for `0.0-0error` specials (which --get may have returned on previous runs). * */ | | | 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | /** * Version string conformance testing. * * Applies $rx_ver obviously, * but also checks for `0.0-0error` specials (which --get may have returned on previous runs). * */ static function is_valid_ver($version) { return preg_match("/^(".cfg::$rx_ver.")$/x", $version) and !preg_match("/^0\.0([0.-]*|[12]?error)+$/", $version); } } |
︙ | ︙ |