Commit | Line | Data |
---|---|---|
545fc9df TO |
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 "Updating from $oldVersion to $newVersion...\n"; | |
45 | ||
46 | updateFile("xml/version.xml", function ($content) use ($newVersion, $oldVersion) { | |
47 | return str_replace($oldVersion, $newVersion, $content); | |
48 | }); | |
49 | ||
50 | updateFile("sql/civicrm_generated.mysql", function ($content) use ($newVersion, $oldVersion) { | |
51 | return str_replace($oldVersion, $newVersion, $content); | |
52 | }); | |
53 | ||
54 | $sqlFile = "CRM/Upgrade/Incremental/sql/{$newVersion}.mysql.tpl"; | |
55 | if (!file_exists($sqlFile)) { | |
56 | echo "Create \"$sqlFile\"\n"; | |
57 | file_put_contents($sqlFile, "{* file to handle db changes in $newVersion during upgrade *}\n"); | |
58 | } | |
59 | ||
60 | if ($doCommit) { | |
61 | $files = "xml/version.xml sql/civicrm_generated.mysql " . escapeshellarg($sqlFile); | |
62 | passthru("git add $files"); | |
63 | passthru("git commit $files -m " . escapeshellarg("Set version to $newVersion")); | |
64 | } | |
65 | ||
66 | /* *********************************************************************** */ | |
67 | /* Helper functions */ | |
68 | ||
69 | function updateFile($file, $callback) { | |
70 | if (!file_exists($file)) { | |
71 | die("File does not exist: $file\n"); | |
72 | } | |
73 | echo "Update \"$file\"\n"; | |
74 | $content = file_get_contents($file); | |
75 | $content = $callback($content); | |
76 | file_put_contents($file, $content); | |
77 | } | |
78 | ||
79 | function isVersionValid($v) { | |
80 | return $v && preg_match('/^[0-9a-z\.\-]+$/', $v); | |
81 | } | |
82 | ||
83 | /** | |
84 | * @param $error | |
85 | */ | |
86 | function fatal($error) { | |
87 | echo $error; | |
88 | echo "usage: set-version.php <new-version> [--commit|--no-commit]\n"; | |
89 | echo " With --commit, any changes will be committed automatically the current git branch.\n"; | |
90 | echo " With --no-commit, any changes will be left uncommitted.\n"; | |
91 | exit(1); | |
92 | } |