Merge branch 'MDL-73827-311' of https://github.com/sarjona/moodle into MOODLE_311_STABLE
[moodle.git] / admin / cli / build_theme_css.php
blob30cd9968e619c84e4ddfc00401f16c525e1035ea
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Build and store theme CSS.
20 * @package core
21 * @subpackage cli
22 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('CLI_SCRIPT', true);
28 require(__DIR__.'/../../config.php');
29 require_once("$CFG->libdir/clilib.php");
30 require_once("$CFG->libdir/csslib.php");
31 require_once("$CFG->libdir/outputlib.php");
33 $longparams = [
34 'themes' => null,
35 'direction' => null,
36 'help' => false,
37 'verbose' => false
40 $shortmappings = [
41 't' => 'themes',
42 'd' => 'direction',
43 'h' => 'help',
44 'v' => 'verbose'
47 // Get CLI params.
48 list($options, $unrecognized) = cli_get_params($longparams, $shortmappings);
50 if ($unrecognized) {
51 $unrecognized = implode("\n ", $unrecognized);
52 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
55 if ($options['help']) {
56 echo
57 "Compile the CSS for one or more installed themes.
58 Existing CSS caches will replaced.
59 By default all themes will be recompiled unless otherwise specified.
61 Options:
62 -t, --themes A comma separated list of themes to be compiled
63 -d, --direction Only compile a single direction (either ltr or rtl)
64 -v, --verbose Print info comments to stdout
65 -h, --help Print out this help
67 Example:
68 \$ sudo -u www-data /usr/bin/php admin/cli/build_theme_css.php --themes=boost --direction=ltr
71 die;
74 if (empty($options['verbose'])) {
75 $trace = new null_progress_trace();
76 } else {
77 $trace = new text_progress_trace();
80 cli_heading('Build theme css');
82 // Determine which themes we need to build.
83 $themenames = [];
84 if (is_null($options['themes'])) {
85 $trace->output('No themes specified. Finding all installed themes.');
86 $themenames = array_keys(core_component::get_plugin_list('theme'));
87 } else {
88 if (is_string($options['themes'])) {
89 $themenames = explode(',', $options['themes']);
90 } else {
91 cli_error('--themes must be a comma separated list of theme names');
95 $trace->output('Checking that each theme is correctly installed...');
96 $themeconfigs = [];
97 foreach ($themenames as $themename) {
98 if (is_null(theme_get_config_file_path($themename))) {
99 cli_error("Unable to find theme config for {$themename}");
102 // Load the config for the theme.
103 $themeconfigs[] = theme_config::load($themename);
106 $directions = ['ltr', 'rtl'];
108 if (!is_null($options['direction'])) {
109 if (!in_array($options['direction'], $directions)) {
110 cli_error("--direction must be either ltr or rtl");
113 $directions = [$options['direction']];
116 $trace->output('Building CSS for themes: ' . implode(', ', $themenames));
117 theme_build_css_for_themes($themeconfigs, $directions);
119 exit(0);