IDE re-format of tools directory
[civicrm-core.git] / tools / bin / scripts / set-version.php
CommitLineData
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__)));
15chdir($civicrm_root);
16
17/* *********************************************************************** */
18/* Parse inputs -- $oldVersion, $newVersion, $doCommit */
19
20$oldVersion = (string) simplexml_load_file("xml/version.xml")->version_no;
21if (!isVersionValid($oldVersion)) {
22 fatal("failed to read old version from \"xml/version.xml\"\n");
23}
24
25$newVersion = @$argv[1];
26if (!isVersionValid($newVersion)) {
27 fatal("failed to read new version\n");
28}
29
30switch (@$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
337dc87a
TO
44echo "Changing version from $oldVersion to $newVersion...\n";
45
46$verName = makeVerName($newVersion);
b7c0a88f 47$phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function () use ($verName) {
337dc87a
TO
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
b7c0a88f 56$sqlFile = initFile("CRM/Upgrade/Incremental/sql/{$newVersion}.mysql.tpl", function () use ($newVersion) {
337dc87a
TO
57 return "{* file to handle db changes in $newVersion during upgrade *}\n";
58});
545fc9df
TO
59
60updateFile("xml/version.xml", function ($content) use ($newVersion, $oldVersion) {
61 return str_replace($oldVersion, $newVersion, $content);
62});
63
653b713e
C
64if (file_exists("civicrm-version.php")) {
65 updateFile("civicrm-version.php", function ($content) use ($newVersion, $oldVersion) {
66 return str_replace($oldVersion, $newVersion, $content);
67 });
68}
4d4f529c 69
545fc9df
TO
70updateFile("sql/civicrm_generated.mysql", function ($content) use ($newVersion, $oldVersion) {
71 return str_replace($oldVersion, $newVersion, $content);
72});
73
545fc9df 74if ($doCommit) {
337dc87a 75 $files = "xml/version.xml sql/civicrm_generated.mysql " . escapeshellarg($phpFile) . ' ' . escapeshellarg($sqlFile);
545fc9df
TO
76 passthru("git add $files");
77 passthru("git commit $files -m " . escapeshellarg("Set version to $newVersion"));
78}
79
80/* *********************************************************************** */
81/* Helper functions */
82
337dc87a
TO
83/**
84 * Update the content of a file.
85 *
86 * @param string $file
87 * @param callable $callback
88 * Function(string $originalContent) => string $newContent.
89 */
545fc9df
TO
90function updateFile($file, $callback) {
91 if (!file_exists($file)) {
92 die("File does not exist: $file\n");
93 }
94 echo "Update \"$file\"\n";
95 $content = file_get_contents($file);
96 $content = $callback($content);
97 file_put_contents($file, $content);
98}
99
337dc87a
TO
100/**
101 * Initialize a file (if it doesn't already exist).
102 * @param string $file
103 * @param callable $callback
104 * Function() => string $newContent.
105 */
106function initFile($file, $callback) {
107 if (file_exists($file)) {
108 echo "File \"$file\" already exists.\n";
109 }
110 else {
111 echo "Initialize \"$file\"\n";
112 $content = $callback();
113 file_put_contents($file, $content);
114 }
115 return $file;
116}
117
118/**
119 * Render a pretty string for a major/minor version number.
120 *
121 * @param string $version
122 * Ex: '5.10.alpha1'
123 * @return string
124 * Ex: 'FiveTen'.
125 */
126function makeVerName($version) {
127 list ($a, $b) = explode('.', $version);
128 require_once 'CRM/Utils/EnglishNumber.php';
129 return CRM_Utils_EnglishNumber::toCamelCase($a) . CRM_Utils_EnglishNumber::toCamelCase($b);
130}
131
545fc9df
TO
132function isVersionValid($v) {
133 return $v && preg_match('/^[0-9a-z\.\-]+$/', $v);
134}
135
136/**
137 * @param $error
138 */
139function fatal($error) {
140 echo $error;
141 echo "usage: set-version.php <new-version> [--commit|--no-commit]\n";
142 echo " With --commit, any changes will be committed automatically the current git branch.\n";
143 echo " With --no-commit, any changes will be left uncommitted.\n";
144 exit(1);
145}