From 337dc87a118c9d2b2e35d78791ec5ced5399e005 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 27 Feb 2018 14:33:16 -0800 Subject: [PATCH] set-version.php - Add support for generating major/minor PHP files --- CRM/Upgrade/Incremental/php/Template.php | 93 ++++++++++++++++++++++++ tools/bin/scripts/set-version.php | 63 ++++++++++++++-- 2 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 CRM/Upgrade/Incremental/php/Template.php diff --git a/CRM/Upgrade/Incremental/php/Template.php b/CRM/Upgrade/Incremental/php/Template.php new file mode 100644 index 0000000000..fcd9708f3f --- /dev/null +++ b/CRM/Upgrade/Incremental/php/Template.php @@ -0,0 +1,93 @@ + +/* + +--------------------------------------------------------------------+ + | CiviCRM version 4.7 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2017 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License along with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ + */ + +/** + * Upgrade logic for + */ +class CRM_Upgrade_Incremental_php_ extends CRM_Upgrade_Incremental_Base { + + /** + * Compute any messages which should be displayed beforeupgrade. + * + * Note: This function is called iteratively for each upcoming + * revision to the database. + * + * @param string $preUpgradeMessage + * @param string $rev + * a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'. + * @param null $currentVer + */ + public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) { + // Example: Generate a pre-upgrade message. + // if ($rev == '5.12.34') { + // $preUpgradeMessage .= '

' . ts('A new permission has been added called %1 This Permission is now used to control access to the Manage Tags screen', array(1 => 'manage tags')) . '

'; + // } + } + + /** + * Compute any messages which should be displayed after upgrade. + * + * @param string $postUpgradeMessage + * alterable. + * @param string $rev + * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs. + */ + public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) { + // Example: Generate a post-upgrade message. + // if ($rev == '5.12.34') { + // $postUpgradeMessage .= '

' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'."); + // } + } + + /* + * Important! All upgrade functions MUST add a 'runSql' task. + * Uncomment and use the following template for a new upgrade version + * (change the x in the function name): + */ + + // /** + // * Upgrade function. + // * + // * @param string $rev + // */ + // public function upgrade_5_0_x($rev) { + // $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev); + // $this->addTask('Do the foo change', 'taskFoo', ...); + // // Additional tasks here... + // // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex. + // // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable. + // } + + // public static function taskFoo(CRM_Queue_TaskContext $ctx, ...) { + // return TRUE; + // } + +} diff --git a/tools/bin/scripts/set-version.php b/tools/bin/scripts/set-version.php index ebc4a5d057..8cde6c636d 100755 --- a/tools/bin/scripts/set-version.php +++ b/tools/bin/scripts/set-version.php @@ -41,7 +41,21 @@ switch (@$argv[2]) { /* *********************************************************************** */ /* Main */ -echo "Updating from $oldVersion to $newVersion...\n"; +echo "Changing version from $oldVersion to $newVersion...\n"; + +$verName = makeVerName($newVersion); +$phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function() use ($verName) { + ob_start(); + global $camelNumber; + $camelNumber = $verName; + require 'CRM/Upgrade/Incremental/php/Template.php'; + unset($camelNumber); + return ob_get_clean(); +}); + +$sqlFile = initFile("CRM/Upgrade/Incremental/sql/{$newVersion}.mysql.tpl", function() use ($newVersion) { + return "{* file to handle db changes in $newVersion during upgrade *}\n"; +}); updateFile("xml/version.xml", function ($content) use ($newVersion, $oldVersion) { return str_replace($oldVersion, $newVersion, $content); @@ -51,14 +65,8 @@ updateFile("sql/civicrm_generated.mysql", function ($content) use ($newVersion, 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); + $files = "xml/version.xml sql/civicrm_generated.mysql " . escapeshellarg($phpFile) . ' ' . escapeshellarg($sqlFile); passthru("git add $files"); passthru("git commit $files -m " . escapeshellarg("Set version to $newVersion")); } @@ -66,6 +74,13 @@ if ($doCommit) { /* *********************************************************************** */ /* Helper functions */ +/** + * Update the content of a file. + * + * @param string $file + * @param callable $callback + * Function(string $originalContent) => string $newContent. + */ function updateFile($file, $callback) { if (!file_exists($file)) { die("File does not exist: $file\n"); @@ -76,6 +91,38 @@ function updateFile($file, $callback) { file_put_contents($file, $content); } +/** + * Initialize a file (if it doesn't already exist). + * @param string $file + * @param callable $callback + * Function() => string $newContent. + */ +function initFile($file, $callback) { + if (file_exists($file)) { + echo "File \"$file\" already exists.\n"; + } + else { + echo "Initialize \"$file\"\n"; + $content = $callback(); + file_put_contents($file, $content); + } + return $file; +} + +/** + * Render a pretty string for a major/minor version number. + * + * @param string $version + * Ex: '5.10.alpha1' + * @return string + * Ex: 'FiveTen'. + */ +function makeVerName($version) { + list ($a, $b) = explode('.', $version); + require_once 'CRM/Utils/EnglishNumber.php'; + return CRM_Utils_EnglishNumber::toCamelCase($a) . CRM_Utils_EnglishNumber::toCamelCase($b); +} + function isVersionValid($v) { return $v && preg_match('/^[0-9a-z\.\-]+$/', $v); } -- 2.25.1