Merge pull request #11630 from maitrepylos/master
[civicrm-core.git] / tools / bin / scripts / set-version.php
1 #!/usr/bin/env php
2 <?php
3
4 // Update the data-files within this repo to reflect a new version number.
5 // Example usage:
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
10
11 /* *********************************************************************** */
12 /* Boot */
13
14 $civicrm_root = dirname(dirname(dirname(__DIR__)));
15 chdir($civicrm_root);
16
17 /* *********************************************************************** */
18 /* Parse inputs -- $oldVersion, $newVersion, $doCommit */
19
20 $oldVersion = (string) simplexml_load_file("xml/version.xml")->version_no;
21 if (!isVersionValid($oldVersion)) {
22 fatal("failed to read old version from \"xml/version.xml\"\n");
23 }
24
25 $newVersion = @$argv[1];
26 if (!isVersionValid($newVersion)) {
27 fatal("failed to read new version\n");
28 }
29
30 switch (@$argv[2]) {
31 case '--commit':
32 $doCommit = 1;
33 break;
34 case '--no-commit':
35 $doCommit = 0;
36 break;
37 default:
38 fatal("Must specify --commit or --no-commit\n");
39 }
40
41 /* *********************************************************************** */
42 /* Main */
43
44 echo "Changing version from $oldVersion to $newVersion...\n";
45
46 $verName = makeVerName($newVersion);
47 $phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function() use ($verName) {
48 ob_start();
49 global $camelNumber;
50 $camelNumber = $verName;
51 require 'CRM/Upgrade/Incremental/php/Template.php';
52 unset($camelNumber);
53 return ob_get_clean();
54 });
55
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";
58 });
59
60 updateFile("xml/version.xml", function ($content) use ($newVersion, $oldVersion) {
61 return str_replace($oldVersion, $newVersion, $content);
62 });
63
64 updateFile("civicrm-version.php", function ($content) use ($newVersion, $oldVersion) {
65 return str_replace($oldVersion, $newVersion, $content);
66 });
67
68 updateFile("sql/civicrm_generated.mysql", function ($content) use ($newVersion, $oldVersion) {
69 return str_replace($oldVersion, $newVersion, $content);
70 });
71
72 if ($doCommit) {
73 $files = "xml/version.xml sql/civicrm_generated.mysql " . escapeshellarg($phpFile) . ' ' . escapeshellarg($sqlFile);
74 passthru("git add $files");
75 passthru("git commit $files -m " . escapeshellarg("Set version to $newVersion"));
76 }
77
78 /* *********************************************************************** */
79 /* Helper functions */
80
81 /**
82 * Update the content of a file.
83 *
84 * @param string $file
85 * @param callable $callback
86 * Function(string $originalContent) => string $newContent.
87 */
88 function updateFile($file, $callback) {
89 if (!file_exists($file)) {
90 die("File does not exist: $file\n");
91 }
92 echo "Update \"$file\"\n";
93 $content = file_get_contents($file);
94 $content = $callback($content);
95 file_put_contents($file, $content);
96 }
97
98 /**
99 * Initialize a file (if it doesn't already exist).
100 * @param string $file
101 * @param callable $callback
102 * Function() => string $newContent.
103 */
104 function initFile($file, $callback) {
105 if (file_exists($file)) {
106 echo "File \"$file\" already exists.\n";
107 }
108 else {
109 echo "Initialize \"$file\"\n";
110 $content = $callback();
111 file_put_contents($file, $content);
112 }
113 return $file;
114 }
115
116 /**
117 * Render a pretty string for a major/minor version number.
118 *
119 * @param string $version
120 * Ex: '5.10.alpha1'
121 * @return string
122 * Ex: 'FiveTen'.
123 */
124 function makeVerName($version) {
125 list ($a, $b) = explode('.', $version);
126 require_once 'CRM/Utils/EnglishNumber.php';
127 return CRM_Utils_EnglishNumber::toCamelCase($a) . CRM_Utils_EnglishNumber::toCamelCase($b);
128 }
129
130 function isVersionValid($v) {
131 return $v && preg_match('/^[0-9a-z\.\-]+$/', $v);
132 }
133
134 /**
135 * @param $error
136 */
137 function fatal($error) {
138 echo $error;
139 echo "usage: set-version.php <new-version> [--commit|--no-commit]\n";
140 echo " With --commit, any changes will be committed automatically the current git branch.\n";
141 echo " With --no-commit, any changes will be left uncommitted.\n";
142 exit(1);
143 }