diff --git a/theme/boost/cli/import-bootswatch.php b/theme/boost/cli/import-bootswatch.php index ff84eb03a12..9e577de932c 100644 --- a/theme/boost/cli/import-bootswatch.php +++ b/theme/boost/cli/import-bootswatch.php @@ -28,106 +28,77 @@ define('CLI_SCRIPT', true); require(__DIR__.'/../../../config.php'); require_once($CFG->libdir.'/clilib.php'); +$usage = " +Utility to convert a Bootswatch theme to a Moodle preset compatible with Bootstrap 4. -// Now get cli options. -list($options, $unrecognized) = cli_get_params(array('help' => false), - array('h' => 'help', 'v' => 'variables', 'b' => 'bootswatch', 'p' => 'preset')); +Download _variables.scss and _bootswatch.scss files from https://bootswatch.com/ +Run this script. It will generate a new file 'preset.scss' which can be used as +a Moodle preset. -if ($unrecognized) { - $unrecognized = implode("\n ", $unrecognized); - cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); -} +Usage: + # php import-bootswatch.php [--help|-h] + # php import-bootswatch.php --variables= --bootswatch= --preset= -if (!isset($options['variables'])) { - $options['variables'] = '_variables.scss'; -} -if (!isset($options['bootswatch'])) { - $options['bootswatch'] = '_bootswatch.scss'; -} -if (!isset($options['preset'])) { - $options['preset'] = 'preset.scss'; +Options: + -h --help Print this help. + --variables= Path to the input variables file, defaults to _variables.scss + --bootswatch= Path to the input bootswatch file, defauls to _bootswatch.scss + --preset= Path to the output preset file, defaults to preset.scss +"; + +list($options, $unrecognised) = cli_get_params([ + 'help' => false, + 'variables' => '_variables.scss', + 'bootswatch' => '_bootswatch.scss', + 'preset' => 'preset.scss', +], [ + 'h' => 'help', +]); + +if ($unrecognised) { + $unrecognised = implode(PHP_EOL.' ', $unrecognised); + cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised)); } if ($options['help']) { - $help = "Convert a Bootswatch file from Bootstrap 3 to a Moodle preset file compatible with bootstrap 4. - - This scripts takes the scss files from a Bootstrap 3 Bootswatch and produces a Moodle compatible preset file. - - Options: - -h, --help Print out this help - -v, --variables= - -b, --bootswatch= - -p, --preset= - - Example: - \$import-bootswatch.php -v=_variables.scss -b=_bootswatch.scss -p=preset-paper.scss - "; - - echo $help; - die; + cli_writeln($usage); + exit(2); } -cli_heading('Convert a Bootswatch file from Bootstrap 3 to a Moodle preset file compatible with bootstrap 4.'); -$variablesfile = $options['variables']; -$bootswatchfile = $options['bootswatch']; -$presetfile = $options['preset']; - -$sourcevariables = @file_get_contents($variablesfile); -if (!$sourcevariables) { - die('Could not read variables file: ' . $variablesfile . "\n"); -} -$sourcebootswatch = @file_get_contents($bootswatchfile); -if (!$sourcebootswatch) { - die('Could not read bootswatch file: ' . $bootswatchfile . "\n"); +if (is_readable($options['variables'])) { + $sourcevariables = file_get_contents($options['variables']); +} else { + cli_writeln($usage); + cli_error('Error reading the variables file: '.$options['variables']); } + +if (is_readable($options['bootswatch'])) { + $sourcebootswatch = file_get_contents($options['bootswatch']); +} else { + cli_writeln($usage); + cli_error('Error reading the bootswatch file: '.$options['bootswatch']); +} + +/** + * Local helper function replacing only the first occurrence of a substring. + * + * @param string $needle Substring to be searched for + * @param string $replace New text replacing the old substring + * @param string $haystack The text where the replacement happens + * @return string + */ function str_replace_one($needle, $replace, $haystack) { $pos = strpos($haystack, $needle); if ($pos !== false) { - $newstring = substr_replace($haystack, $replace, $pos, strlen($needle)); + return substr_replace($haystack, $replace, $pos, strlen($needle)); + } else { + return $haystack; } - return $newstring; } -$out = @fopen($presetfile, "w"); - -if (!$out) { - die('Could not open preset file for writing: ' . $presetfile . "\n"); -} - -// Write the license (MIT). - -$license = <<