More phpcbs code cleanups
[civicrm-core.git] / tools / bin / scripts / set-version.php
index 7a71324ad06c1fbe9fee91fd618f692a50e733f3..ceec68eaa53c347a77159ebd5bb2413f36e5495a 100755 (executable)
@@ -25,9 +25,12 @@ if (!isVersionValid($oldVersion)) {
   fatal("failed to read old version from \"xml/version.xml\"\n");
 }
 
-/** @var string $newVersion */
-/** @var bool $doCommit */
-/** @var bool $doSql */
+/**
+ * @var string $newVersion */
+/**
+ * @var bool $doCommit */
+/**
+ * @var bool $doSql */
 extract(parseArgs($argv));
 
 if (!isVersionValid($newVersion)) {
@@ -40,12 +43,14 @@ if (!isVersionValid($newVersion)) {
 echo "Changing version from $oldVersion to $newVersion...\n";
 
 $verName = makeVerName($newVersion);
-$phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function () use ($verName) {
+$phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function () use ($verName, $newVersion) {
   ob_start();
   global $camelNumber;
+  global $versionNumber;
   $camelNumber = $verName;
+  $versionNumber = $newVersion;
   require 'CRM/Upgrade/Incremental/php/Template.php';
-  unset($camelNumber);
+  unset($camelNumber, $versionNumber);
   return ob_get_clean();
 });
 
@@ -74,9 +79,19 @@ updateFile("sql/test_data_second_domain.mysql", function ($content) use ($newVer
   return str_replace($oldVersion, $newVersion, $content);
 });
 
+$infoXmls = findCoreInfoXml();
+foreach ($infoXmls as $infoXml) {
+  updateXmlFile($infoXml, function (DOMDocument $dom) use ($newVersion) {
+    foreach ($dom->getElementsByTagName('version') as $tag) {
+      /* @var \DOMNode $tag */
+      $tag->textContent = $newVersion;
+    }
+  });
+}
+
 if ($doCommit) {
   $files = array_filter(
-    ['xml/version.xml', 'sql/civicrm_generated.mysql', 'sql/test_data_second_domain.mysql', $phpFile, @$sqlFile],
+    array_merge(['xml/version.xml', 'sql/civicrm_generated.mysql', 'sql/test_data_second_domain.mysql', $phpFile, @$sqlFile], $infoXmls),
     'file_exists'
   );
   $filesEsc = implode(' ', array_map('escapeshellarg', $files));
@@ -104,6 +119,24 @@ function updateFile($file, $callback) {
   file_put_contents($file, $content);
 }
 
+/**
+ * Update the content of an XML file
+ *
+ * @param string $file
+ * @param callable $callback
+ *   Function(DOMDocument $dom)
+ */
+function updateXmlFile($file, $callback) {
+  updateFile($file, function ($content) use ($callback) {
+    $dom = new DomDocument();
+    $dom->loadXML($content);
+    $dom->preserveWhiteSpace = FALSE;
+    $dom->formatOutput = TRUE;
+    $callback($dom);
+    return $dom->saveXML();
+  });
+}
+
 /**
  * Initialize a file (if it doesn't already exist).
  * @param string $file
@@ -158,10 +191,10 @@ function fatal($error) {
 }
 
 /**
-* @param array $argv
+ * @param array $argv
  *  Ex: ['myscript.php', '--no-commit', '5.6.7']
  * @return array
- *  Ex: ['scriptFile' => 'myscript.php', 'doCommit' => FALSE, 'newVersion' => '5.6.7']
+ *   Ex: ['scriptFile' => 'myscript.php', 'doCommit' => FALSE, 'newVersion' => '5.6.7']
  */
 function parseArgs($argv) {
   $parsed = [];
@@ -209,3 +242,23 @@ function parseArgs($argv) {
 
   return $parsed;
 }
+
+/**
+ * @return array
+ *   Ex: ['ext/afform/html/info.xml', 'ext/search_kit/info.xml']
+ */
+function findCoreInfoXml() {
+  $lines = explode("\n", file_get_contents('distmaker/core-ext.txt'));
+  $exts = preg_grep(";^#;", $lines, PREG_GREP_INVERT);
+  $exts = preg_grep(';[a-z-A-Z];', $exts);
+
+  $infoXmls = [];
+  foreach ($exts as $coreExtDir) {
+    $infoXmls = array_merge($infoXmls, (array) glob("ext/$coreExtDir/*/info.xml"));
+    if (file_exists("ext/$coreExtDir/info.xml")) {
+      $infoXmls[] = "ext/$coreExtDir/info.xml";
+    }
+  }
+
+  return $infoXmls;
+}