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) {
46 $camelNumber = $verName;
47 require 'CRM/Upgrade/Incremental/php/Template.php';
49 return ob_get_clean();
52 // It is typical for `*.alpha` to need SQL file -- and for `*.beta1` and `*.0` to NOT need a SQL file.
53 if ($doSql === TRUE ||
($doSql === 'auto' && preg_match(';alpha;', $newVersion))) {
54 $sqlFile = initFile("CRM/Upgrade/Incremental/sql/{$newVersion}.mysql.tpl", function () use ($newVersion) {
55 return "{* file to handle db changes in $newVersion during upgrade *}\n";
59 updateFile("xml/version.xml", function ($content) use ($newVersion, $oldVersion) {
60 return str_replace($oldVersion, $newVersion, $content);
63 if (file_exists("civicrm-version.php")) {
64 updateFile("civicrm-version.php", function ($content) use ($newVersion, $oldVersion) {
65 return str_replace($oldVersion, $newVersion, $content);
69 updateFile("sql/civicrm_generated.mysql", function ($content) use ($newVersion, $oldVersion) {
70 return str_replace($oldVersion, $newVersion, $content);
73 updateFile("sql/test_data_second_domain.mysql", function ($content) use ($newVersion, $oldVersion) {
74 return str_replace($oldVersion, $newVersion, $content);
77 $infoXmls = findCoreInfoXml();
78 foreach ($infoXmls as $infoXml) {
79 updateXmlFile($infoXml, function (DOMDocument
$dom) use ($newVersion) {
80 foreach ($dom->getElementsByTagName('version') as $tag) {
81 /** @var \DOMNode $tag */
82 $tag->textContent
= $newVersion;
88 $files = array_filter(
89 array_merge(['xml/version.xml', 'sql/civicrm_generated.mysql', 'sql/test_data_second_domain.mysql', $phpFile, @$sqlFile], $infoXmls),
92 $filesEsc = implode(' ', array_map('escapeshellarg', $files));
93 passthru("git add $filesEsc");
94 passthru("git commit $filesEsc -m " . escapeshellarg("Set version to $newVersion"));
97 /* *********************************************************************** */
98 /* Helper functions */
101 * Update the content of a file.
103 * @param string $file
104 * @param callable $callback
105 * Function(string $originalContent) => string $newContent.
107 function updateFile($file, $callback) {
108 if (!file_exists($file)) {
109 die("File does not exist: $file\n");
111 echo "Update \"$file\"\n";
112 $content = file_get_contents($file);
113 $content = $callback($content);
114 file_put_contents($file, $content);
118 * Update the content of an XML file
120 * @param string $file
121 * @param callable $callback
122 * Function(DOMDocument $dom)
124 function updateXmlFile($file, $callback) {
125 updateFile($file, function ($content) use ($callback) {
126 $dom = new DomDocument();
127 $dom->loadXML($content);
128 $dom->preserveWhiteSpace
= FALSE;
129 $dom->formatOutput
= TRUE;
131 return $dom->saveXML();
136 * Initialize a file (if it doesn't already exist).
137 * @param string $file
138 * @param callable $callback
139 * Function() => string $newContent.
141 function initFile($file, $callback) {
142 if (file_exists($file)) {
143 echo "File \"$file\" already exists.\n";
146 echo "Initialize \"$file\"\n";
147 $content = $callback();
148 file_put_contents($file, $content);
154 * Render a pretty string for a major/minor version number.
156 * @param string $version
161 function makeVerName($version) {
162 [$a, $b] = explode('.', $version);
163 require_once 'CRM/Utils/EnglishNumber.php';
164 return CRM_Utils_EnglishNumber
::toCamelCase($a) . CRM_Utils_EnglishNumber
::toCamelCase($b);
167 function isVersionValid($v) {
168 return $v && preg_match('/^[0-9a-z\.\-]+$/', $v);
174 function fatal($error) {
176 echo "usage: set-version.php <new-version> [--sql|--no-sql] [--commit|--no-commit]\n";
177 echo " --sql A placeholder *.sql file will be created.\n";
178 echo " --no-sql A placeholder *.sql file will not be created.\n";
179 echo " --commit Any changes will be committed automatically the current git branch.\n";
180 echo " --no-commit Any changes will be left uncommitted.\n";
182 echo "If the SQL style is not specified, it will decide automatically. (Alpha versions get SQL files.)\n";
184 echo "You MUST indicate whether to commit.\n";
190 * Ex: ['myscript.php', '--no-commit', '5.6.7']
192 * Ex: ['scriptFile' => 'myscript.php', 'doCommit' => FALSE, 'newVersion' => '5.6.7']
194 function parseArgs($argv) {
196 $parsed['doSql'] = 'auto';
197 $positions = ['scriptFile', 'newVersion'];
200 foreach ($argv as $arg) {
203 $parsed['doCommit'] = TRUE;
207 $parsed['doCommit'] = FALSE;
211 $parsed['doSql'] = TRUE;
215 $parsed['doSql'] = FALSE;
219 if ($arg[0] !== '-') {
220 $positional[] = $arg;
223 fatal("Unrecognized argument: $arg\n");
229 foreach ($positional as $offset => $value) {
230 $name = $positions[$offset] ??
"unknown_$offset";
231 $parsed[$name] = $value;
234 if (!isset($parsed['doCommit'])) {
235 fatal("Must specify --commit or --no-commit\n");
243 * Ex: ['ext/afform/html/info.xml', 'ext/search_kit/info.xml']
245 function findCoreInfoXml() {
246 $lines = explode("\n", file_get_contents('distmaker/core-ext.txt'));
247 $exts = preg_grep(";^#;", $lines, PREG_GREP_INVERT
);
248 $exts = preg_grep(';[a-z-A-Z];', $exts);
251 foreach ($exts as $coreExtDir) {
252 $infoXmls = array_merge($infoXmls, (array) glob("ext/$coreExtDir/*/info.xml"));
253 if (file_exists("ext/$coreExtDir/info.xml")) {
254 $infoXmls[] = "ext/$coreExtDir/info.xml";