API: remove file and date transformations
[dokuwiki.git] / .github / release.php
blob7a9a9462fb1d3f27f4e7e56f2e604e6044718d19
1 <?php
3 if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../');
4 require_once(DOKU_INC . 'vendor/autoload.php');
5 require_once DOKU_INC . 'inc/load.php';
7 /**
8 * Command Line utility to gather and check data for building a release
9 */
10 class Release extends splitbrain\phpcli\CLI
12 const TYPES = ['stable', 'hotfix', 'rc'];
14 // base URL to fetch raw files from the stable branch
15 protected $BASERAW = 'https://raw.githubusercontent.com/dokuwiki/dokuwiki/stable/';
17 /** @inheritdoc */
18 public function __construct($autocatch = true)
20 parent::__construct($autocatch);
22 // when running on a clone, use the correct base URL
23 $repo = getenv('GITHUB_REPOSITORY');
24 if ($repo) {
25 $this->BASERAW = 'https://raw.githubusercontent.com/' . $repo . '/stable/';
30 protected function setup(\splitbrain\phpcli\Options $options)
32 $options->setHelp('This tool is used to gather and check data for building a release');
34 $options->registerCommand('new', 'Get environment for creating a new release');
35 $options->registerOption('type', 'The type of release to build', null, join('|', self::TYPES), 'new');
36 $options->registerOption('date', 'The date to use for the version. Defaults to today', null, 'YYYY-MM-DD', 'new');
37 $options->registerOption('name', 'The codename to use for the version. Defaults to the last used one', null, 'codename', 'new');
39 $options->registerCommand('current', 'Get environment of the current release');
42 protected function main(\splitbrain\phpcli\Options $options)
44 switch ($options->getCmd()) {
45 case 'new':
46 $this->prepareNewEnvironment($options);
47 break;
48 case 'current':
49 $this->prepareCurrentEnvironment($options);
50 break;
51 default:
52 echo $options->help();
56 /**
57 * Prepare environment for the current branch
59 protected function prepareCurrentEnvironment(\splitbrain\phpcli\Options $options)
61 $current = $this->getLocalVersion();
62 // we name files like the string in the VERSION file, with rc at the front
63 $current['file'] = ($current['type'] === 'rc' ? 'rc' : '') . $current['date'] . $current['hotfix'];
65 // output to be piped into GITHUB_ENV
66 foreach ($current as $k => $v) {
67 echo "current_$k=$v\n";
71 /**
72 * Prepare environment for creating a new release
74 protected function prepareNewEnvironment(\splitbrain\phpcli\Options $options)
76 $current = $this->getUpstreamVersion();
78 // continue if we want to create a new release
79 $next = [
80 'type' => $options->getOpt('type'),
81 'date' => $options->getOpt('date'),
82 'codename' => $options->getOpt('name'),
83 'hotfix' => '',
85 if (!$next['type']) $next['type'] = 'stable';
86 if (!$next['date']) $next['date'] = date('Y-m-d');
87 if (!$next['codename']) $next['codename'] = $current['codename'];
88 $next['codename'] = ucwords(strtolower($next['codename']));
90 if (!in_array($next['type'], self::TYPES)) {
91 throw new \splitbrain\phpcli\Exception('Invalid release type. Use one of ' . join(', ', self::TYPES));
94 if ($next['type'] === 'hotfix') {
95 $next['update'] = floatval($current['update']) + 0.1;
96 $next['codename'] = $current['codename'];
97 $next['date'] = $current['date'];
98 $next['hotfix'] = $this->increaseHotfix($current['hotfix']);
99 } else {
100 $next['update'] = intval($current['update']) + 1;
103 if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $next['date'])) {
104 throw new \splitbrain\phpcli\Exception('Invalid date format, use YYYY-MM-DD');
107 if ($current['date'] > $next['date']) {
108 throw new \splitbrain\phpcli\Exception('Date must be equal or later than the last release');
111 if ($current['type'] === 'rc' && $next['type'] === 'hotfix') {
112 throw new \splitbrain\phpcli\Exception(
113 'Cannot create hotfixes for release candidates, create a new RC instead'
117 if ($current['type'] === 'stable' && $next['type'] !== 'hotfix' && $current['codename'] === $next['codename']) {
118 throw new \splitbrain\phpcli\Exception('Codename must be different from the last release');
121 $next['version'] = $next['date'] . ($next['type'] === 'rc' ? 'rc' : $next['hotfix']);
122 $next['raw'] = ($next['type'] === 'rc' ? 'rc' : '') .
123 $next['date'] .
124 $next['hotfix'] .
125 ' "' . $next['codename'] . '"';
127 // output to be piped into GITHUB_ENV
128 foreach ($current as $k => $v) {
129 echo "current_$k=$v\n";
131 foreach ($next as $k => $v) {
132 echo "next_$k=$v\n";
137 * Get current version info from local VERSION file
139 * @return string[]
141 protected function getLocalVersion()
143 $versioninfo = \dokuwiki\Info::parseVersionString(trim(file_get_contents('VERSION')));
144 $doku = file_get_contents('doku.php');
145 if (!preg_match('/\$updateVersion = "(\d+(\.\d+)?)";/', $doku, $m)) {
146 throw new \Exception('Could not find $updateVersion in doku.php');
148 $versioninfo['update'] = floatval($m[1]);
149 return $versioninfo;
153 * Get current version info from stable branch
155 * @return string[]
156 * @throws Exception
158 protected function getUpstreamVersion()
160 // basic version info
161 $versioninfo = \dokuwiki\Info::parseVersionString(trim(file_get_contents($this->BASERAW . 'VERSION')));
163 // update version grepped from the doku.php file
164 $doku = file_get_contents($this->BASERAW . 'doku.php');
165 if (!preg_match('/\$updateVersion = "(\d+(\.\d+)?)";/', $doku, $m)) {
166 throw new \Exception('Could not find $updateVersion in doku.php');
168 $versioninfo['update'] = floatval($m[1]);
170 return $versioninfo;
174 * Increase the hotfix letter
176 * (max 26 hotfixes)
178 * @param string $hotfix
179 * @return string
181 protected function increaseHotfix($hotfix)
183 if (empty($hotfix)) return 'a';
184 return substr($hotfix, 0, -1) . chr(ord($hotfix) + 1);
188 (new Release())->run();