tools/bin/scripts/set-version.php - Add helper script
authorTim Otten <totten@civicrm.org>
Mon, 25 Apr 2016 22:14:00 +0000 (15:14 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 4 May 2017 06:08:59 +0000 (01:08 -0500)
This is similar to the function `do_update` in `releaser`, except that :
 * It it doesn't care if you use the `$workdir/$version/export` directory structure or something else
 * It it doesn't care if you push directly to the canonical repo or use a pull-request

tools/bin/scripts/set-version.php [new file with mode: 0755]

diff --git a/tools/bin/scripts/set-version.php b/tools/bin/scripts/set-version.php
new file mode 100755 (executable)
index 0000000..ebc4a5d
--- /dev/null
@@ -0,0 +1,92 @@
+#!/usr/bin/env php
+<?php
+
+// Update the data-files within this repo to reflect a new version number.
+// Example usage:
+//   git checkout origin/master -b master-4.7.29
+//   ./tools/bin/scripts/set-version.php 4.7.29 --commit
+//   git commit -m "Update to version 4.7.29"
+//   git push origin master
+
+/* *********************************************************************** */
+/* Boot */
+
+$civicrm_root = dirname(dirname(dirname(__DIR__)));
+chdir($civicrm_root);
+
+/* *********************************************************************** */
+/* Parse inputs -- $oldVersion, $newVersion, $doCommit */
+
+$oldVersion = (string) simplexml_load_file("xml/version.xml")->version_no;
+if (!isVersionValid($oldVersion)) {
+  fatal("failed to read old version from \"xml/version.xml\"\n");
+}
+
+$newVersion = @$argv[1];
+if (!isVersionValid($newVersion)) {
+  fatal("failed to read new version\n");
+}
+
+switch (@$argv[2]) {
+  case '--commit':
+    $doCommit = 1;
+    break;
+  case '--no-commit':
+    $doCommit = 0;
+    break;
+  default:
+    fatal("Must specify --commit or --no-commit\n");
+}
+
+/* *********************************************************************** */
+/* Main */
+
+echo "Updating from $oldVersion to $newVersion...\n";
+
+updateFile("xml/version.xml", function ($content) use ($newVersion, $oldVersion) {
+  return str_replace($oldVersion, $newVersion, $content);
+});
+
+updateFile("sql/civicrm_generated.mysql", function ($content) use ($newVersion, $oldVersion) {
+  return str_replace($oldVersion, $newVersion, $content);
+});
+
+$sqlFile = "CRM/Upgrade/Incremental/sql/{$newVersion}.mysql.tpl";
+if (!file_exists($sqlFile)) {
+  echo "Create \"$sqlFile\"\n";
+  file_put_contents($sqlFile, "{* file to handle db changes in $newVersion during upgrade *}\n");
+}
+
+if ($doCommit) {
+  $files = "xml/version.xml sql/civicrm_generated.mysql " . escapeshellarg($sqlFile);
+  passthru("git add $files");
+  passthru("git commit $files -m " . escapeshellarg("Set version to $newVersion"));
+}
+
+/* *********************************************************************** */
+/* Helper functions */
+
+function updateFile($file, $callback) {
+  if (!file_exists($file)) {
+    die("File does not exist: $file\n");
+  }
+  echo "Update \"$file\"\n";
+  $content = file_get_contents($file);
+  $content = $callback($content);
+  file_put_contents($file, $content);
+}
+
+function isVersionValid($v) {
+  return $v && preg_match('/^[0-9a-z\.\-]+$/', $v);
+}
+
+/**
+ * @param $error
+ */
+function fatal($error) {
+  echo $error;
+  echo "usage: set-version.php <new-version> [--commit|--no-commit]\n";
+  echo "  With --commit, any changes will be committed automatically the current git branch.\n";
+  echo "  With --no-commit, any changes will be left uncommitted.\n";
+  exit(1);
+}