4 // Update the data-files within this repo to reflect a new version number.
6 // git checkout origin/master -b master-4.7.29
7 // ./tools/bin/scripts/set-version.php 4.7.29 --commit
8 // git commit -m "Update to version 4.7.29"
9 // git push origin master
11 /* *********************************************************************** */
13 if (!(php_sapi_name() == 'cli' ||
(is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0))) {
14 header("HTTP/1.0 404 Not Found");
17 $civicrm_root = dirname(dirname(dirname(__DIR__
)));
20 /* *********************************************************************** */
21 /* Parse inputs -- $oldVersion, $newVersion, $doCommit */
23 $oldVersion = (string) simplexml_load_file("xml/version.xml")->version_no
;
24 if (!isVersionValid($oldVersion)) {
25 fatal("failed to read old version from \"xml/version.xml\"\n");
28 /** @var string $newVersion */
29 /** @var bool $doCommit */
30 /** @var bool $doSql */
31 extract(parseArgs($argv));
33 if (!isVersionValid($newVersion)) {
34 fatal("failed to read new version\n");
37 /* *********************************************************************** */
40 echo "Changing version from $oldVersion to $newVersion...\n";
42 $verName = makeVerName($newVersion);
43 $phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function () use ($verName, $newVersion) {
46 global $versionNumber;
47 $camelNumber = $verName;
48 $versionNumber = $newVersion;
49 require 'CRM/Upgrade/Incremental/php/Template.php';
50 unset($camelNumber, $versionNumber);
51 return ob_get_clean();
54 // It is typical for `*.alpha` to need SQL file -- and for `*.beta1` and `*.0` to NOT need a SQL file.
55 if ($doSql === TRUE ||
($doSql === 'auto' && preg_match(';alpha;', $newVersion))) {
56 $sqlFile = initFile("CRM/Upgrade/Incremental/sql/{$newVersion}.mysql.tpl", function () use ($newVersion) {
57 return "{* file to handle db changes in $newVersion during upgrade *}\n";
61 updateFile("xml/version.xml", function ($content) use ($newVersion, $oldVersion) {
62 return str_replace($oldVersion, $newVersion, $content);
65 if (file_exists("civicrm-version.php")) {
66 updateFile("civicrm-version.php", function ($content) use ($newVersion, $oldVersion) {
67 return str_replace($oldVersion, $newVersion, $content);
71 updateFile("sql/civicrm_generated.mysql", function ($content) use ($newVersion, $oldVersion) {
72 return str_replace($oldVersion, $newVersion, $content);
75 updateFile("sql/test_data_second_domain.mysql", function ($content) use ($newVersion, $oldVersion) {
76 return str_replace($oldVersion, $newVersion, $content);
79 $infoXmls = findCoreInfoXml();
80 foreach ($infoXmls as $infoXml) {
81 updateXmlFile($infoXml, function (DOMDocument
$dom) use ($newVersion) {
82 foreach ($dom->getElementsByTagName('version') as $tag) {
83 /** @var \DOMNode $tag */
84 $tag->textContent
= $newVersion;
90 $files = array_filter(
91 array_merge(['xml/version.xml', 'sql/civicrm_generated.mysql', 'sql/test_data_second_domain.mysql', $phpFile, @$sqlFile], $infoXmls),
94 $filesEsc = implode(' ', array_map('escapeshellarg', $files));
95 passthru("git add $filesEsc");
96 passthru("git commit $filesEsc -m " . escapeshellarg("Set version to $newVersion"));
99 /* *********************************************************************** */
100 /* Helper functions */
103 * Update the content of a file.
105 * @param string $file
106 * @param callable $callback
107 * Function(string $originalContent) => string $newContent.
109 function updateFile($file, $callback) {
110 if (!file_exists($file)) {
111 die("File does not exist: $file\n");
113 echo "Update \"$file\"\n";
114 $content = file_get_contents($file);
115 $content = $callback($content);
116 file_put_contents($file, $content);
120 * Update the content of an XML file
122 * @param string $file
123 * @param callable $callback
124 * Function(DOMDocument $dom)
126 function updateXmlFile($file, $callback) {
127 updateFile($file, function ($content) use ($callback) {
128 $dom = new DomDocument();
129 $dom->loadXML($content);
130 $dom->preserveWhiteSpace
= FALSE;
131 $dom->formatOutput
= TRUE;
133 return $dom->saveXML();
138 * Initialize a file (if it doesn't already exist).
139 * @param string $file
140 * @param callable $callback
141 * Function() => string $newContent.
143 function initFile($file, $callback) {
144 if (file_exists($file)) {
145 echo "File \"$file\" already exists.\n";
148 echo "Initialize \"$file\"\n";
149 $content = $callback();
150 file_put_contents($file, $content);
156 * Render a pretty string for a major/minor version number.
158 * @param string $version
163 function makeVerName($version) {
164 [$a, $b] = explode('.', $version);
165 require_once 'CRM/Utils/EnglishNumber.php';
166 return CRM_Utils_EnglishNumber
::toCamelCase($a) . CRM_Utils_EnglishNumber
::toCamelCase($b);
169 function isVersionValid($v) {
170 return $v && preg_match('/^[0-9a-z\.\-]+$/', $v);
176 function fatal($error) {
178 echo "usage: set-version.php <new-version> [--sql|--no-sql] [--commit|--no-commit]\n";
179 echo " --sql A placeholder *.sql file will be created.\n";
180 echo " --no-sql A placeholder *.sql file will not be created.\n";
181 echo " --commit Any changes will be committed automatically the current git branch.\n";
182 echo " --no-commit Any changes will be left uncommitted.\n";
184 echo "If the SQL style is not specified, it will decide automatically. (Alpha versions get SQL files.)\n";
186 echo "You MUST indicate whether to commit.\n";
192 * Ex: ['myscript.php', '--no-commit', '5.6.7']
194 * Ex: ['scriptFile' => 'myscript.php', 'doCommit' => FALSE, 'newVersion' => '5.6.7']
196 function parseArgs($argv) {
198 $parsed['doSql'] = 'auto';
199 $positions = ['scriptFile', 'newVersion'];
202 foreach ($argv as $arg) {
205 $parsed['doCommit'] = TRUE;
209 $parsed['doCommit'] = FALSE;
213 $parsed['doSql'] = TRUE;
217 $parsed['doSql'] = FALSE;
221 if ($arg[0] !== '-') {
222 $positional[] = $arg;
225 fatal("Unrecognized argument: $arg\n");
231 foreach ($positional as $offset => $value) {
232 $name = $positions[$offset] ??
"unknown_$offset";
233 $parsed[$name] = $value;
236 if (!isset($parsed['doCommit'])) {
237 fatal("Must specify --commit or --no-commit\n");
245 * Ex: ['ext/afform/html/info.xml', 'ext/search_kit/info.xml']
247 function findCoreInfoXml() {
248 $lines = explode("\n", file_get_contents('distmaker/core-ext.txt'));
249 $exts = preg_grep(";^#;", $lines, PREG_GREP_INVERT
);
250 $exts = preg_grep(';[a-z-A-Z];', $exts);
253 foreach ($exts as $coreExtDir) {
254 $infoXmls = array_merge($infoXmls, (array) glob("ext/$coreExtDir/*/info.xml"));
255 if (file_exists("ext/$coreExtDir/info.xml")) {
256 $infoXmls[] = "ext/$coreExtDir/info.xml";