| | 64 | } |
|---|
| | 65 | //set all modules if --modules was set to * |
|---|
| | 66 | if (isset($vars['modules']) && is_array($vars['modules']) && in_array('*', $vars['modules'])) { |
|---|
| | 67 | $vars['modules'] = array(); |
|---|
| | 68 | $ls = scandir(dirname(__FILE__)); |
|---|
| | 69 | foreach($ls as $item) { |
|---|
| | 70 | if (strpos($item, '.') !== 0 && is_dir($item)) { |
|---|
| | 71 | $vars['modules'][] = $item; |
|---|
| | 72 | } |
|---|
| | 73 | } |
|---|
| | 74 | sort($vars['modules']); |
|---|
| | 75 | } |
|---|
| | 76 | |
|---|
| | 77 | if (isset($vars['bump']) && $vars['debug'] != 'false') { |
|---|
| | 78 | $vars['bump'] = ctype_digit($vars['bump']) ? $vars['bump'] : true; |
|---|
| | 79 | } else { |
|---|
| | 80 | $vars['bump'] = false; |
|---|
| | 120 | //set re |
|---|
| | 121 | //move re to an array if there are commas as part of the value |
|---|
| | 122 | /*if (isset($vars['re']) && strpos($vars['re'], ',') !== false) { |
|---|
| | 123 | $vars['re'] = explode(',', $vars['re']); |
|---|
| | 124 | }*///while a nice idea, this is inconsistant with the rest of the script. use multiple --re options insread |
|---|
| | 125 | if (isset($vars['re'])) { |
|---|
| | 126 | switch (true) { |
|---|
| | 127 | case is_array($vars['re']): |
|---|
| | 128 | foreach ($vars['re'] as $k => $v) { |
|---|
| | 129 | if ($v) { |
|---|
| | 130 | $vars['re'][$k] = '#' . preg_replace("/[^0-9]/", '', $v); |
|---|
| | 131 | } else { |
|---|
| | 132 | unset($vars['re'][$k]); |
|---|
| | 133 | } |
|---|
| | 134 | } |
|---|
| | 135 | $vars['re'] = 're ' . implode(', ' . $vars['re']) . ' '; |
|---|
| | 136 | break; |
|---|
| | 137 | case is_string($vars['re']): |
|---|
| | 138 | $vars['re'] = 're #' . preg_replace("/[^0-9]/", '', $vars['re']) . ' '; |
|---|
| | 139 | break; |
|---|
| | 140 | default: |
|---|
| | 141 | break; |
|---|
| | 142 | } |
|---|
| | 143 | } else { |
|---|
| | 144 | $vars['re'] = ''; |
|---|
| | 145 | } |
|---|
| | 146 | |
|---|
| | 147 | $vars['msg'] = isset($vars['msg']) ? trim($vars['msg']) . ' ' : ''; |
|---|
| | 148 | $vars['msg'] = $vars['re'] ? $vars['re'] . '- ' . $vars['msg'] : ''; |
|---|
| | 149 | |
|---|
| | 150 | //if help was requested, show help and exit |
|---|
| | 151 | if ($vars['help']) { |
|---|
| | 152 | echo package_show_help(); |
|---|
| | 153 | exit(); |
|---|
| | 154 | } |
|---|
| | 210 | //bump version if requested, and reset $ver |
|---|
| | 211 | if ($vars['bump']) { |
|---|
| | 212 | package_bump_version($mod, $vars['bump']); |
|---|
| | 213 | //test xml file and get some of its values |
|---|
| | 214 | list($rawname, $ver) = check_xml($mod, $xml); |
|---|
| | 215 | $vars['log'] = true; |
|---|
| | 216 | } |
|---|
| | 217 | |
|---|
| | 218 | //add changelog if requested |
|---|
| | 219 | if ($vars['log']) { |
|---|
| | 220 | $msg = $vars['msg'] ? $vars['msg'] : 'Packaging of ver ' . $ver; |
|---|
| | 221 | package_update_changelog($mod, $msg); |
|---|
| | 222 | } |
|---|
| | 223 | |
|---|
| | 429 | //auto-bump module version, bumps last part by defualt |
|---|
| | 430 | function package_bump_version($mod, $pos = '') { |
|---|
| | 431 | global $mod_dir, $vars; |
|---|
| | 432 | $xml = simplexml_load_file($mod_dir . '/module.xml'); |
|---|
| | 433 | $ver = explode('.', (string) $xml->version); |
|---|
| | 434 | |
|---|
| | 435 | //if $pos === true, reset it |
|---|
| | 436 | if ($pos === true) { |
|---|
| | 437 | $pos = ''; |
|---|
| | 438 | } |
|---|
| | 439 | //pick last part if requested part isn't found |
|---|
| | 440 | if (!isset($ver[$pos - 1])) { |
|---|
| | 441 | $pos = count($ver); |
|---|
| | 442 | } |
|---|
| | 443 | $pos = $pos - 1; //array start at 0, but people will count from 1. |
|---|
| | 444 | |
|---|
| | 445 | //if we have only digits in this part, add 1 |
|---|
| | 446 | if (ctype_digit($ver[$pos])) { |
|---|
| | 447 | $ver[$pos] = $ver[$pos] + 1; |
|---|
| | 448 | } else {//find last groupe of digits and +1 them |
|---|
| | 449 | $num = preg_split('/[0-9]+$/', $ver[$pos], 1); |
|---|
| | 450 | $replace = strrpos($ver[$pos], $num); |
|---|
| | 451 | $num = $num[0] + 1; |
|---|
| | 452 | $ver[$pos] = substr($ver[$pos], 0, $replace -1) . $num; |
|---|
| | 453 | } |
|---|
| | 454 | |
|---|
| | 455 | echo 'Bumping ' . $mod . '\s verison to ' . (string) $xml->version . PHP_EOL; |
|---|
| | 456 | |
|---|
| | 457 | $xml->version = implode('.', $ver); |
|---|
| | 458 | |
|---|
| | 459 | if ($vars['debug'] || $vars['verbose']) { |
|---|
| | 460 | echo 'Writing to ' . $mod_dir . '/module.xml :' . PHP_EOL; |
|---|
| | 461 | echo $xml->asXML(); |
|---|
| | 462 | } |
|---|
| | 463 | if (!$vars['debug']) { |
|---|
| | 464 | file_put_contents($mod_dir . '/module.xml', $xml->asXML()); |
|---|
| | 465 | } |
|---|
| | 466 | |
|---|
| | 467 | return true; |
|---|
| | 468 | } |
|---|
| | 469 | |
|---|
| | 470 | //update module's changelog |
|---|
| | 471 | function package_update_changelog($mod, $msg) { |
|---|
| | 472 | global $mod_dir, $vars, $ver; |
|---|
| | 473 | $xml = simplexml_load_file($mod_dir . '/module.xml'); |
|---|
| | 474 | $log = explode("\n", (string) $xml->changelog); |
|---|
| | 475 | |
|---|
| | 476 | //firt element is ususally blank, remove it |
|---|
| | 477 | array_shift($log); |
|---|
| | 478 | |
|---|
| | 479 | //prune to last 5 entreis |
|---|
| | 480 | $log = array_slice($log, 0, 4); |
|---|
| | 481 | array_unshift($log, $ver . ' ' . $msg); |
|---|
| | 482 | |
|---|
| | 483 | echo 'Adding to ' . $mod . '\s changelog: ' . $ver . ' ' . $msg; |
|---|
| | 484 | |
|---|
| | 485 | $xml->changelog = "\n\t\t" . trim(implode("\n", $log)) . "\n\t"; |
|---|
| | 486 | |
|---|
| | 487 | if ($vars['verbose']) { |
|---|
| | 488 | echo 'Writing to ' . $mod_dir . '/module.xml :' . PHP_EOL; |
|---|
| | 489 | } |
|---|
| | 490 | if ($vars['debug']) { |
|---|
| | 491 | echo 'Writing to ' . $mod_dir . '/module.xml :' . PHP_EOL; |
|---|
| | 492 | echo $xml->asXML(); |
|---|
| | 493 | } |
|---|
| | 494 | if (!$vars['debug']) { |
|---|
| | 495 | file_put_contents($mod_dir . '/module.xml', $xml->asXML()); |
|---|
| | 496 | } |
|---|
| | 497 | |
|---|
| | 498 | return true; |
|---|
| | 499 | } |
|---|
| | 500 | |
|---|
| | 553 | //show help menu |
|---|
| | 554 | function package_show_help($short = false) { |
|---|
| | 555 | $final = ''; |
|---|
| | 556 | $ret[] = 'Package.php'; |
|---|
| | 557 | $ret[] = '-----------'; |
|---|
| | 558 | $ret[] = ''; |
|---|
| | 559 | if ($short) { |
|---|
| | 560 | $ret[] = 'SHORT OPS HAVE BEEN DEPRICATED - PLEASE USE ONLY LONG OPTS!'; |
|---|
| | 561 | } |
|---|
| | 562 | $ret[] = 'Short options MUST come after all the long options, or the long options will be ignored'; |
|---|
| | 563 | $ret[] = ''; |
|---|
| | 564 | |
|---|
| | 565 | //args |
|---|
| | 566 | $ret[] = array('--bump', 'Bump a modules version. You can specify the "octet" by adding a position ' |
|---|
| | 567 | . 'I.e. --bump=2 will turn 3.4.5.6 in to 3.5.5.6. Leaving the position blank will bump the last "octet"'); |
|---|
| | 568 | $ret[] = array('--debug = false', 'Debug only - just run through the command but don\'t make any changes'); |
|---|
| | 569 | $ret[] = array('--checkphp = true', 'Run PHP syntaxt check on php files (php -l <file name>)'); |
|---|
| | 570 | $ret[] = array('--help', 'Show this menu and exit'); |
|---|
| | 571 | $ret[] = array('--log', 'Update module.xml\'s changelog.'); |
|---|
| | 572 | $ret[] = array('-m, --modules', 'Modules to be packaged. One module per --module argument, or * for all'); |
|---|
| | 573 | $ret[] = array('--msg', 'Optional commit message.'); |
|---|
| | 574 | $ret[] = array('--re', 'A ticket number to be referenced in all checkins (i.e. "re #627...")'); |
|---|
| | 575 | $ret[] = array('--verbose', 'Run with extra verbosity and print each command before it\'s executed'); |
|---|
| | 576 | |
|---|
| | 577 | $ret[] = ''; |
|---|
| | 578 | |
|---|
| | 579 | //generate formated help message |
|---|
| | 580 | foreach ($ret as $r) { |
|---|
| | 581 | if (is_array($r)) { |
|---|
| | 582 | //pad the option |
|---|
| | 583 | $option = ' ' . str_pad($r[0], 20); |
|---|
| | 584 | |
|---|
| | 585 | //explode the definition to manageable chunks |
|---|
| | 586 | $def = explode('§', wordwrap($r[1], 55, "§", true)); |
|---|
| | 587 | |
|---|
| | 588 | //split definition in to chucks |
|---|
| | 589 | //and pad the with whitespace 20 chars to the left stating from the second line |
|---|
| | 590 | if (count($def) > 1) { |
|---|
| | 591 | $first = array_shift($def); |
|---|
| | 592 | foreach ($def as $my => $item) { |
|---|
| | 593 | $def[$my] = str_pad('', 22) . $item . PHP_EOL; |
|---|
| | 594 | } |
|---|
| | 595 | } elseif (count($def) == 1) { |
|---|
| | 596 | $first = implode($def); |
|---|
| | 597 | $def = array(); |
|---|
| | 598 | } else { |
|---|
| | 599 | $first = ''; |
|---|
| | 600 | $def = array(); |
|---|
| | 601 | } |
|---|
| | 602 | |
|---|
| | 603 | $definition = $first . PHP_EOL . implode($def); |
|---|
| | 604 | $final .= $option . $definition; |
|---|
| | 605 | } else { |
|---|
| | 606 | $final .= $r . PHP_EOL; |
|---|
| | 607 | } |
|---|
| | 608 | } |
|---|
| | 609 | return $final; |
|---|
| | 610 | } |
|---|