forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-67449 grunt: Refactor path calculation for AMD
- Loading branch information
1 parent
c554024
commit a8109e7
Showing
3 changed files
with
220 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle 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 General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Helper functions for working with Moodle component names, directories, and sources. | ||
* | ||
* @copyright 2019 Andrew Nicols <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
"use strict"; | ||
/* eslint-env node */ | ||
|
||
/** @var {Object} A list of subsystems in Moodle */ | ||
const componentData = {}; | ||
|
||
/** | ||
* Load details of all moodle modules. | ||
* | ||
* @returns {object} | ||
*/ | ||
const fetchComponentData = () => { | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const glob = require('glob'); | ||
const gruntFilePath = process.cwd(); | ||
|
||
if (!Object.entries(componentData).length) { | ||
componentData.subsystems = {}; | ||
componentData.pathList = []; | ||
|
||
// Fetch the component definiitions from the distributed JSON file. | ||
const components = JSON.parse(fs.readFileSync(`${gruntFilePath}/lib/components.json`)); | ||
|
||
// Build the list of moodle subsystems. | ||
componentData.subsystems.lib = 'core'; | ||
componentData.pathList.push(process.cwd() + path.sep + 'lib'); | ||
for (const [component, thisPath] of Object.entries(components.subsystems)) { | ||
if (thisPath) { | ||
// Prefix "core_" to the front of the subsystems. | ||
componentData.subsystems[thisPath] = `core_${component}`; | ||
componentData.pathList.push(process.cwd() + path.sep + thisPath); | ||
} | ||
} | ||
|
||
// The list of components incldues the list of subsystems. | ||
componentData.components = componentData.subsystems; | ||
|
||
// Go through each of the plugintypes. | ||
Object.entries(components.plugintypes).forEach(([pluginType, pluginTypePath]) => { | ||
// We don't allow any code in this place..? | ||
glob.sync(`${pluginTypePath}/*/version.php`).forEach(versionPath => { | ||
const componentPath = fs.realpathSync(path.dirname(versionPath)); | ||
const componentName = path.basename(componentPath); | ||
const frankenstyleName = `${pluginType}_${componentName}`; | ||
componentData.components[`${pluginTypePath}/${componentName}`] = frankenstyleName; | ||
componentData.pathList.push(componentPath); | ||
|
||
// Look for any subplugins. | ||
const subPluginConfigurationFile = `${componentPath}/db/subplugins.json`; | ||
if (fs.existsSync(subPluginConfigurationFile)) { | ||
const subpluginList = JSON.parse(fs.readFileSync(fs.realpathSync(subPluginConfigurationFile))); | ||
|
||
Object.entries(subpluginList.plugintypes).forEach(([subpluginType, subpluginTypePath]) => { | ||
glob.sync(`${subpluginTypePath}/*/version.php`).forEach(versionPath => { | ||
const componentPath = fs.realpathSync(path.dirname(versionPath)); | ||
const componentName = path.basename(componentPath); | ||
const frankenstyleName = `${subpluginType}_${componentName}`; | ||
|
||
componentData.components[`${subpluginTypePath}/${componentName}`] = frankenstyleName; | ||
componentData.pathList.push(componentPath); | ||
}); | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
} | ||
|
||
return componentData; | ||
}; | ||
|
||
/** | ||
* Get the list of paths to build AMD sources. | ||
* | ||
* @returns {Array} | ||
*/ | ||
const getAmdSrcGlobList = () => { | ||
const globList = []; | ||
fetchComponentData().pathList.forEach(componentPath => { | ||
globList.push(`${componentPath}/amd/src/*.js`); | ||
globList.push(`${componentPath}/amd/src/**/*.js`); | ||
}); | ||
|
||
return globList; | ||
}; | ||
|
||
/** | ||
* Find the name of the component matching the specified path. | ||
* | ||
* @param {String} path | ||
* @returns {String|null} Name of matching component. | ||
*/ | ||
const getComponentFromPath = path => { | ||
const componentList = fetchComponentData().components; | ||
|
||
if (componentList.hasOwnProperty(path)) { | ||
return componentList[path]; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
/** | ||
* Check whether the supplied path, relative to the Gruntfile.js, is in a known component. | ||
* | ||
* @param {String} checkPath The path to check | ||
* @returns {String|null} | ||
*/ | ||
const getOwningComponentDirectory = checkPath => { | ||
const path = require('path'); | ||
|
||
const pathList = fetchComponentData().components; | ||
for (const componentPath of Object.keys(pathList)) { | ||
if (checkPath === componentPath) { | ||
return componentPath; | ||
} | ||
if (checkPath.startsWith(componentPath + path.sep)) { | ||
return componentPath; | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
module.exports = { | ||
getAmdSrcGlobList, | ||
getComponentFromPath, | ||
getOwningComponentDirectory, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters