Merge branch 'MDL-79673' of https://github.com/paulholden/moodle
[moodle.git] / .grunt / components.js
blobe494521624b8b2f6bd0bbbc0212e28487935699f
1 // This file is part of Moodle - http://moodle.org/
2 //
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.
7 //
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/>.
16 /**
17  * Helper functions for working with Moodle component names, directories, and sources.
18  *
19  * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
20  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21  */
23 "use strict";
24 /* eslint-env node */
26 /** @var {Object} A list of subsystems in Moodle */
27 const componentData = {};
29 /**
30  * Load details of all moodle modules.
31  *
32  * @returns {object}
33  */
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)) {
51             if (thisPath) {
52                 // Prefix "core_" to the front of the subsystems.
53                 componentData.subsystems[thisPath] = `core_${component}`;
54                 componentData.pathList.push(process.cwd() + path.sep + thisPath);
55             }
56         }
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);
84                         });
85                     });
86                 }
87             });
88         });
90     }
92     return componentData;
95 /**
96  * Get the list of component paths.
97  *
98  * @param   {string} relativeTo
99  * @returns {array}
100  */
101 const getComponentPaths = (relativeTo = '') => fetchComponentData().pathList.map(componentPath => {
102     return componentPath.replace(relativeTo, '');
106  * Get the list of paths to build AMD sources.
108  * @returns {Array}
109  */
110 const getAmdSrcGlobList = () => {
111     const globList = [];
112     fetchComponentData().pathList.forEach(componentPath => {
113         globList.push(`${componentPath}/amd/src/*.js`);
114         globList.push(`${componentPath}/amd/src/**/*.js`);
115     });
117     return globList;
121  * Get the list of paths to build YUI sources.
123  * @param {String} relativeTo
124  * @returns {Array}
125  */
126 const getYuiSrcGlobList = relativeTo => {
127     const globList = [];
128     fetchComponentData().pathList.forEach(componentPath => {
129         const relativeComponentPath = componentPath.replace(relativeTo, '');
130         globList.push(`${relativeComponentPath}/yui/src/**/*.js`);
131     });
133     return globList;
137  * Get the list of paths to thirdpartylibs.xml.
139  * @param {String} relativeTo
140  * @returns {Array}
141  */
142 const getThirdPartyLibsList = relativeTo => {
143     const fs = require('fs');
144     const path = require('path');
146     return fetchComponentData().pathList
147         .map(componentPath => path.relative(relativeTo, componentPath) + '/thirdpartylibs.xml')
148         .map(componentPath => componentPath.replace(/\\/g, '/'))
149         .filter(path => fs.existsSync(path))
150         .sort();
154  * Get the list of thirdparty library paths.
156  * @returns {array}
157  */
158 const getThirdPartyPaths = () => {
159     const DOMParser = require('@xmldom/xmldom').DOMParser;
160     const fs = require('fs');
161     const path = require('path');
162     const xpath = require('xpath');
164     const thirdpartyfiles = getThirdPartyLibsList(fs.realpathSync('./'));
165     const libs = ['node_modules/', 'vendor/'];
167     const addLibToList = lib => {
168         if (!lib.match('\\*') && fs.statSync(lib).isDirectory()) {
169             // Ensure trailing slash on dirs.
170             lib = lib.replace(/\/?$/, '/');
171         }
173         // Look for duplicate paths before adding to array.
174         if (libs.indexOf(lib) === -1) {
175             libs.push(lib);
176         }
177     };
179     thirdpartyfiles.forEach(function(file) {
180         const dirname = path.dirname(file);
182         const xmlContent = fs.readFileSync(file, 'utf8');
183         const doc = new DOMParser().parseFromString(xmlContent);
184         const nodes = xpath.select("/libraries/library/location/text()", doc);
186         nodes.forEach(function(node) {
187             let lib = path.posix.join(dirname, node.toString());
188             addLibToList(lib);
189         });
190     });
192     return libs;
197  * Find the name of the component matching the specified path.
199  * @param {String} path
200  * @returns {String|null} Name of matching component.
201  */
202 const getComponentFromPath = path => {
203     const componentList = fetchComponentData().components;
205     if (componentList.hasOwnProperty(path)) {
206         return componentList[path];
207     }
209     return null;
213  * Check whether the supplied path, relative to the Gruntfile.js, is in a known component.
215  * @param {String} checkPath The path to check. This can be with either Windows, or Linux directory separators.
216  * @returns {String|null}
217  */
218 const getOwningComponentDirectory = checkPath => {
219     const path = require('path');
221     // Fetch all components into a reverse sorted array.
222     // This ensures that components which are within the directory of another component match first.
223     const pathList = Object.keys(fetchComponentData().components).sort().reverse();
224     for (const componentPath of pathList) {
225         // If the componentPath is the directory being checked, it will be empty.
226         // If the componentPath is a parent of the directory being checked, the relative directory will not start with ..
227         if (!path.relative(componentPath, checkPath).startsWith('..')) {
228             return componentPath;
229         }
230     }
232     return null;
236  * Get the latest tag in a remote GitHub repository.
238  * @param {string} url The remote repository.
239  * @returns {Array}
240  */
241 const getRepositoryTags = async(url) => {
242     const gtr = require('git-tags-remote');
243     try {
244         const tags = await gtr.get(url);
245         if (tags !== undefined) {
246             return tags;
247         }
248     } catch (error) {
249         return [];
250     }
251     return [];
255  * Get the list of thirdparty libraries that could be upgraded.
257  * @returns {Array}
258  */
259 const getThirdPartyLibsUpgradable = async() => {
260     const libraries = getThirdPartyLibsData().filter((library) => !!library.repository);
261     const upgradableLibraries = [];
262     const versionCompare = (a, b) => {
263         if (a === b) {
264             return 0;
265         }
267         const aParts = a.split('.');
268         const bParts = b.split('.');
270         for (let i = 0; i < Math.min(aParts.length, bParts.length); i++) {
271             const aPart = parseInt(aParts[i], 10);
272             const bPart = parseInt(bParts[i], 10);
273             if (aPart > bPart) {
274                 // 1.1.0 > 1.0.9
275                 return 1;
276             } else if (aPart < bPart) {
277                 // 1.0.9 < 1.1.0
278                 return -1;
279             } else {
280                 // Same version.
281                 continue;
282             }
283         }
285         if (aParts.length > bParts.length) {
286             // 1.0.1 > 1.0
287             return 1;
288         }
290         // 1.0 < 1.0.1
291         return -1;
292     };
294     for (let library of libraries) {
295         upgradableLibraries.push(
296             getRepositoryTags(library.repository).then((tagMap) => {
297                 library.version = library.version.replace(/^v/, '');
298                 const currentVersion = library.version.replace(/moodle-/, '');
299                 const currentMajorVersion = library.version.split('.')[0];
300                 const tags = [...tagMap]
301                     .map((tagData) => tagData[0])
302                     .filter((tag) => !tag.match(/(alpha|beta|rc)/))
303                     .map((tag) => tag.replace(/^v/, ''))
304                     .sort((a, b) => versionCompare(b, a));
305                 if (!tags.length) {
306                     library.warning = "Unable to find any comparable tags.";
307                     return library;
308                 }
310                 library.latestVersion = tags[0];
311                 tags.some((tag) => {
312                     if (!tag) {
313                         return false;
314                     }
316                     // See if the version part matches.
317                     const majorVersion = tag.split('.')[0];
318                     if (majorVersion === currentMajorVersion) {
319                         library.latestSameMajorVersion = tag;
320                         return true;
321                     }
322                     return false;
323                 });
326                 if (versionCompare(currentVersion, library.latestVersion) > 0) {
327                     // Moodle somehow has a newer version than the latest version.
328                     library.warning = `Newer version found: ${currentVersion} > ${library.latestVersion} for ${library.name}`;
329                     return library;
330                 }
333                 if (library.version !== library.latestVersion) {
334                     // Delete version and add it again at the end of the array. That way, current and new will stay closer.
335                     delete library.version;
336                     library.version = currentVersion;
337                     return library;
338                 }
339                 return null;
340             })
341         );
342     }
344     return (await Promise.all(upgradableLibraries)).filter((library) => !!library);
348  * Get the list of thirdparty libraries.
350  * @returns {Array}
351  */
352 const getThirdPartyLibsData = () => {
353     const DOMParser = require('@xmldom/xmldom').DOMParser;
354     const fs = require('fs');
355     const xpath = require('xpath');
356     const path = require('path');
358     const libraryList = [];
359     const libraryFields = [
360         'location',
361         'name',
362         'version',
363         'repository',
364     ];
366     const thirdpartyfiles = getThirdPartyLibsList(fs.realpathSync('./'));
367     thirdpartyfiles.forEach(function(libraryPath) {
368         const xmlContent = fs.readFileSync(libraryPath, 'utf8');
369         const doc = new DOMParser().parseFromString(xmlContent);
370         const libraries = xpath.select("/libraries/library", doc);
371         for (const library of libraries) {
372             const libraryData = [];
373             for (const field of libraryFields) {
374                 libraryData[field] = xpath.select(`${field}/text()`, library)?.toString();
375             }
376             libraryData.location = path.join(path.dirname(libraryPath), libraryData.location);
377             libraryList.push(libraryData);
378         }
379     });
381     return libraryList.sort((a, b) => a.location.localeCompare(b.location));
384 module.exports = {
385     fetchComponentData,
386     getAmdSrcGlobList,
387     getComponentFromPath,
388     getComponentPaths,
389     getOwningComponentDirectory,
390     getYuiSrcGlobList,
391     getThirdPartyLibsList,
392     getThirdPartyPaths,
393     getThirdPartyLibsUpgradable,