1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * Helper functions for working with Moodle component names, directories, and sources.
19 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 /** @var {Object} A list of subsystems in Moodle */
27 const componentData = {};
30 * Load details of all moodle modules.
34 const fetchComponentData = () => {
35 const fs = require('fs');
36 const path = require('path');
37 const glob = require('glob');
38 const gruntFilePath = process.cwd();
40 if (!Object.entries(componentData).length) {
41 componentData.subsystems = {};
42 componentData.pathList = [];
44 // Fetch the component definiitions from the distributed JSON file.
45 const components = JSON.parse(fs.readFileSync(`${gruntFilePath}/lib/components.json`));
47 // Build the list of moodle subsystems.
48 componentData.subsystems.lib = 'core';
49 componentData.pathList.push(process.cwd() + path.sep + 'lib');
50 for (const [component, thisPath] of Object.entries(components.subsystems)) {
52 // Prefix "core_" to the front of the subsystems.
53 componentData.subsystems[thisPath] = `core_${component}`;
54 componentData.pathList.push(process.cwd() + path.sep + thisPath);
58 // The list of components incldues the list of subsystems.
59 componentData.components = componentData.subsystems;
61 // Go through each of the plugintypes.
62 Object.entries(components.plugintypes).forEach(([pluginType, pluginTypePath]) => {
63 // We don't allow any code in this place..?
64 glob.sync(`${pluginTypePath}/*/version.php`).forEach(versionPath => {
65 const componentPath = fs.realpathSync(path.dirname(versionPath));
66 const componentName = path.basename(componentPath);
67 const frankenstyleName = `${pluginType}_${componentName}`;
68 componentData.components[`${pluginTypePath}/${componentName}`] = frankenstyleName;
69 componentData.pathList.push(componentPath);
71 // Look for any subplugins.
72 const subPluginConfigurationFile = `${componentPath}/db/subplugins.json`;
73 if (fs.existsSync(subPluginConfigurationFile)) {
74 const subpluginList = JSON.parse(fs.readFileSync(fs.realpathSync(subPluginConfigurationFile)));
76 Object.entries(subpluginList.plugintypes).forEach(([subpluginType, subpluginTypePath]) => {
77 glob.sync(`${subpluginTypePath}/*/version.php`).forEach(versionPath => {
78 const componentPath = fs.realpathSync(path.dirname(versionPath));
79 const componentName = path.basename(componentPath);
80 const frankenstyleName = `${subpluginType}_${componentName}`;
82 componentData.components[`${subpluginTypePath}/${componentName}`] = frankenstyleName;
83 componentData.pathList.push(componentPath);
96 * Get the list of paths to build AMD sources.
100 const getAmdSrcGlobList = () => {
102 fetchComponentData().pathList.forEach(componentPath => {
103 globList.push(`${componentPath}/amd/src/*.js`);
104 globList.push(`${componentPath}/amd/src/**/*.js`);
111 * Get the list of paths to build YUI sources.
113 * @param {String} relativeTo
116 const getYuiSrcGlobList = relativeTo => {
118 fetchComponentData().pathList.forEach(componentPath => {
119 const relativeComponentPath = componentPath.replace(relativeTo, '');
120 globList.push(`${relativeComponentPath}/yui/src/**/*.js`);
127 * Get the list of paths to thirdpartylibs.xml.
129 * @param {String} relativeTo
132 const getThirdPartyLibsList = relativeTo => {
133 const fs = require('fs');
135 return fetchComponentData().pathList
136 .map(componentPath => componentPath.replace(relativeTo, '') + '/thirdpartylibs.xml')
137 .filter(path => fs.existsSync(path))
142 * Find the name of the component matching the specified path.
144 * @param {String} path
145 * @returns {String|null} Name of matching component.
147 const getComponentFromPath = path => {
148 const componentList = fetchComponentData().components;
150 if (componentList.hasOwnProperty(path)) {
151 return componentList[path];
158 * Check whether the supplied path, relative to the Gruntfile.js, is in a known component.
160 * @param {String} checkPath The path to check
161 * @returns {String|null}
163 const getOwningComponentDirectory = checkPath => {
164 const path = require('path');
166 const pathList = fetchComponentData().components;
167 for (const componentPath of Object.keys(pathList)) {
168 if (checkPath === componentPath) {
169 return componentPath;
171 if (checkPath.startsWith(componentPath + path.sep)) {
172 return componentPath;
181 getComponentFromPath,
182 getOwningComponentDirectory,
184 getThirdPartyLibsList,