xref: /dokuwiki/.github/release.php (revision 9425586fc5258f41619fb2d72ba7423c75cb8074)
1<?php
2
3if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../');
4require_once(DOKU_INC . 'vendor/autoload.php');
5require_once DOKU_INC . 'inc/load.php';
6
7/**
8 * Command Line utility to gather and check data for building a release
9 */
10class Release extends splitbrain\phpcli\CLI
11{
12    const TYPES = ['stable', 'hotfix', 'rc'];
13
14    // base URL to fetch raw files from the stable branch
15    protected $BASERAW = 'https://raw.githubusercontent.com/dokuwiki/dokuwiki/stable/';
16
17    /** @inheritdoc */
18    public function __construct($autocatch = true)
19    {
20        parent::__construct($autocatch);
21
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/';
26        }
27    }
28
29
30    protected function setup(\splitbrain\phpcli\Options $options)
31    {
32        $options->setHelp('This tool is used to gather and check data for building a release');
33
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');
38
39        $options->registerCommand('current', 'Get environment of the current release');
40    }
41
42    protected function main(\splitbrain\phpcli\Options $options)
43    {
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();
53        }
54    }
55
56    /**
57     * Prepare environment for the current branch
58     */
59    protected function prepareCurrentEnvironment(\splitbrain\phpcli\Options $options)
60    {
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'];
64
65        // output to be piped into GITHUB_ENV
66        foreach ($current as $k => $v) {
67            echo "current_$k=$v\n";
68        }
69    }
70
71    /**
72     * Prepare environment for creating a new release
73     */
74    protected function prepareNewEnvironment(\splitbrain\phpcli\Options $options)
75    {
76        $current = $this->getUpstreamVersion();
77
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' => '',
84        ];
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']));
89
90        if (!in_array($next['type'], self::TYPES)) {
91            throw new \splitbrain\phpcli\Exception('Invalid release type. Use one of ' . join(', ', self::TYPES));
92        }
93
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;
101        }
102
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');
105        }
106
107        if ($current['date'] > $next['date']) {
108            throw new \splitbrain\phpcli\Exception('Date must be equal or later than the last release');
109        }
110
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'
114            );
115        }
116
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');
119        }
120
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'] . '"';
126
127        // output to be piped into GITHUB_ENV
128        foreach ($current as $k => $v) {
129            echo "current_$k=$v\n";
130        }
131        foreach ($next as $k => $v) {
132            echo "next_$k=$v\n";
133        }
134    }
135
136    /**
137     * Get current version info from local VERSION file
138     *
139     * @return string[]
140     */
141    protected function getLocalVersion()
142    {
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');
147        }
148        $versioninfo['update'] = floatval($m[1]);
149        return $versioninfo;
150    }
151
152    /**
153     * Get current version info from stable branch
154     *
155     * @return string[]
156     * @throws Exception
157     */
158    protected function getUpstreamVersion()
159    {
160        // basic version info
161        $versioninfo = \dokuwiki\Info::parseVersionString(trim(file_get_contents($this->BASERAW . 'VERSION')));
162
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');
167        }
168        $versioninfo['update'] = floatval($m[1]);
169
170        return $versioninfo;
171    }
172
173    /**
174     * Increase the hotfix letter
175     *
176     * (max 26 hotfixes)
177     *
178     * @param string $hotfix
179     * @return string
180     */
181    protected function increaseHotfix($hotfix)
182    {
183        if (empty($hotfix)) return 'a';
184        return substr($hotfix, 0, -1) . chr(ord($hotfix) + 1);
185    }
186}
187
188(new Release())->run();
189