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");
29 * @var string $newVersion */
31 * @var bool $doCommit */
34 extract(parseArgs($argv));
36 if (!isVersionValid($newVersion)) {
37 fatal("failed to read new version\n");
40 /* *********************************************************************** */
43 echo "Changing version from $oldVersion to $newVersion...\n";
45 $verName = makeVerName($newVersion);
46 $phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function () use ($verName, $newVersion) {
49 global $versionNumber;
50 $camelNumber = $verName;
51 $versionNumber = $newVersion;
52 require 'CRM/Upgrade/Incremental/php/Template.php';
53 unset($camelNumber, $versionNumber);
54 return ob_get_clean();
57 // It is typical for `*.alpha` to need SQL file -- and for `*.beta1` and `*.0` to NOT need a SQL file.
58 if ($doSql === TRUE ||
($doSql === 'auto' && preg_match(';alpha;', $newVersion))) {
59 $sqlFile = initFile("CRM/Upgrade/Incremental/sql/{$newVersion}.mysql.tpl", function () use ($newVersion) {
60 return "{* file to handle db changes in $newVersion during upgrade *}\n";
64 updateFile("xml/version.xml", function ($content) use ($newVersion, $oldVersion) {
65 return str_replace($oldVersion, $newVersion, $content);
68 if (file_exists("civicrm-version.php")) {
69 updateFile("civicrm-version.php", function ($content) use ($newVersion, $oldVersion) {
70 return str_replace($oldVersion, $newVersion, $content);
74 updateFile("sql/civicrm_generated.mysql", function ($content) use ($newVersion, $oldVersion) {
75 return str_replace($oldVersion, $newVersion, $content);
78 updateFile("sql/test_data_second_domain.mysql", function ($content) use ($newVersion, $oldVersion) {
79 return str_replace($oldVersion, $newVersion, $content);
82 $infoXmls = findCoreInfoXml();
83 foreach ($infoXmls as $infoXml) {
84 updateXmlFile($infoXml, function (DOMDocument
$dom) use ($newVersion) {
85 foreach ($dom->getElementsByTagName('version') as $tag) {
86 /* @var \DOMNode $tag */
87 $tag->textContent
= $newVersion;
93 $files = array_filter(
94 array_merge(['xml/version.xml', 'sql/civicrm_generated.mysql', 'sql/test_data_second_domain.mysql', $phpFile, @$sqlFile], $infoXmls),
97 $filesEsc = implode(' ', array_map('escapeshellarg', $files));
98 passthru("git add $filesEsc");
99 passthru("git commit $filesEsc -m " . escapeshellarg("Set version to $newVersion"));
102 /* *********************************************************************** */
103 /* Helper functions */
106 * Update the content of a file.
108 * @param string $file
109 * @param callable $callback
110 * Function(string $originalContent) => string $newContent.
112 function updateFile($file, $callback) {
113 if (!file_exists($file)) {
114 die("File does not exist: $file\n");
116 echo "Update \"$file\"\n";
117 $content = file_get_contents($file);
118 $content = $callback($content);
119 file_put_contents($file, $content);
123 * Update the content of an XML file
125 * @param string $file
126 * @param callable $callback
127 * Function(DOMDocument $dom)
129 function updateXmlFile($file, $callback) {
130 updateFile($file, function ($content) use ($callback) {
131 $dom = new DomDocument();
132 $dom->loadXML($content);
133 $dom->preserveWhiteSpace
= FALSE;
134 $dom->formatOutput
= TRUE;
136 return $dom->saveXML();
141 * Initialize a file (if it doesn't already exist).
142 * @param string $file
143 * @param callable $callback
144 * Function() => string $newContent.
146 function initFile($file, $callback) {
147 if (file_exists($file)) {
148 echo "File \"$file\" already exists.\n";
151 echo "Initialize \"$file\"\n";
152 $content = $callback();
153 file_put_contents($file, $content);
159 * Render a pretty string for a major/minor version number.
161 * @param string $version
166 function makeVerName($version) {
167 [$a, $b] = explode('.', $version);
168 require_once 'CRM/Utils/EnglishNumber.php';
169 return CRM_Utils_EnglishNumber
::toCamelCase($a) . CRM_Utils_EnglishNumber
::toCamelCase($b);
172 function isVersionValid($v) {
173 return $v && preg_match('/^[0-9a-z\.\-]+$/', $v);
179 function fatal($error) {
181 echo "usage: set-version.php <new-version> [--sql|--no-sql] [--commit|--no-commit]\n";
182 echo " --sql A placeholder *.sql file will be created.\n";
183 echo " --no-sql A placeholder *.sql file will not be created.\n";
184 echo " --commit Any changes will be committed automatically the current git branch.\n";
185 echo " --no-commit Any changes will be left uncommitted.\n";
187 echo "If the SQL style is not specified, it will decide automatically. (Alpha versions get SQL files.)\n";
189 echo "You MUST indicate whether to commit.\n";
195 * Ex: ['myscript.php', '--no-commit', '5.6.7']
197 * Ex: ['scriptFile' => 'myscript.php', 'doCommit' => FALSE, 'newVersion' => '5.6.7']
199 function parseArgs($argv) {
201 $parsed['doSql'] = 'auto';
202 $positions = ['scriptFile', 'newVersion'];
205 foreach ($argv as $arg) {
208 $parsed['doCommit'] = TRUE;
212 $parsed['doCommit'] = FALSE;
216 $parsed['doSql'] = TRUE;
220 $parsed['doSql'] = FALSE;
224 if ($arg[0] !== '-') {
225 $positional[] = $arg;
228 fatal("Unrecognized argument: $arg\n");
234 foreach ($positional as $offset => $value) {
235 $name = $positions[$offset] ??
"unknown_$offset";
236 $parsed[$name] = $value;
239 if (!isset($parsed['doCommit'])) {
240 fatal("Must specify --commit or --no-commit\n");
248 * Ex: ['ext/afform/html/info.xml', 'ext/search_kit/info.xml']
250 function findCoreInfoXml() {
251 $lines = explode("\n", file_get_contents('distmaker/core-ext.txt'));
252 $exts = preg_grep(";^#;", $lines, PREG_GREP_INVERT
);
253 $exts = preg_grep(';[a-z-A-Z];', $exts);
256 foreach ($exts as $coreExtDir) {
257 $infoXmls = array_merge($infoXmls, (array) glob("ext/$coreExtDir/*/info.xml"));
258 if (file_exists("ext/$coreExtDir/info.xml")) {
259 $infoXmls[] = "ext/$coreExtDir/info.xml";