Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Merge branch 'MDL-49046-master' of git://github.com/damyon/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Feb 23, 2015
2 parents 558354d + adeb96d commit c40678f
Show file tree
Hide file tree
Showing 16 changed files with 2,797 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ composer.lock
# lib/yuilib/version/module/module-coverage.js
/lib/yuilib/*/*/*-coverage.js
atlassian-ide-plugin.xml
/node_modules/
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"passfail": false,
"plusplus": false,
"predef": [
"M"
"M",
"define"
],
"proto": false,
"regexdash": false,
Expand Down
149 changes: 149 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// 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/>.

/**
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Grunt configuration
*/

module.exports = function(grunt) {
var path = require('path'),
tasks = {};

// Project configuration.
grunt.initConfig({
jshint: {
options: {jshintrc: '.jshintrc'},
files: ['**/amd/src/*.js']
},
uglify: {
dynamic_mappings: {
files: grunt.file.expandMapping(
['**/src/*.js', '!**/node_modules/**'],
'',
{
cwd: process.env.PWD,
rename: function(destBase, destPath) {
destPath = destPath.replace('src', 'build');
destPath = destPath.replace('.js', '.min.js');
destPath = path.resolve(process.env.PWD, destPath);
return destPath;
}
}
)
}
}
});

tasks.shifter = function() {
var exec = require('child_process').spawn,
done = this.async(),
args = [],
options = {
recursive: true,
watch: false,
walk: false,
module: false
},
shifter;

// Determine the most appropriate options to run with based upon the current location.
if (path.basename(process.env.PWD) === 'src') {
// Detect whether we're in a src directory.
grunt.log.debug('In a src directory');
args.push('--walk');
options.walk = true;
} else if (path.basename(path.dirname(process.env.PWD)) === 'src') {
// Detect whether we're in a module directory.
grunt.log.debug('In a module directory');
options.module = true;
}

if (grunt.option('watch')) {
if (!options.walk && !options.module) {
grunt.fail.fatal('Unable to watch unless in a src or module directory');
}

// It is not advisable to run with recursivity and watch - this
// leads to building the build directory in a race-like fashion.
grunt.log.debug('Detected a watch - disabling recursivity');
options.recursive = false;
args.push('--watch');
}

if (options.recursive) {
args.push('--recursive');
}

// Always ignore the node_modules directory.
args.push('--excludes', 'node_modules');

// Add the stderr option if appropriate
if (grunt.option('verbose')) {
args.push('--lint-stderr');
}

// Actually run shifter.
shifter = exec(process.cwd() + '/node_modules/shifter/bin/shifter', args, {
cwd: process.env.PWD,
stdio: 'inherit',
env: process.env
});

// Tidy up after exec.
shifter.on('exit', function (code) {
if (code) {
grunt.fail.fatal('Shifter failed with code: ' + code);
} else {
grunt.log.ok('Shifter build complete.');
done();
}
});
};

tasks.startup = function() {
// Are we in a YUI directory?
if (path.basename(path.resolve(process.env.PWD, '../../')) == 'yui') {
grunt.task.run('shifter');
// Are we in an AMD directory?
} else if (path.basename(process.env.PWD) == 'amd') {
grunt.task.run('jshint');
grunt.task.run('uglify');
} else {
// Run them all!.
grunt.task.run('shifter');
grunt.task.run('jshint');
grunt.task.run('uglify');
}
};


// Register NPM tasks.
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');

// Register the shifter task.
grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);

// Register the startup task.
grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);

// Register the default task.
grunt.registerTask('default', ['startup']);
};
1 change: 1 addition & 0 deletions lib/amd/build/first.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
define(function(){});
26 changes: 26 additions & 0 deletions lib/amd/src/first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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/>.

/**
* This is an empty module, that is required before all other modules.
* Because every module is returned from a request for any other module, this
* forces the loading of all modules with a single request.
*
* @module core/first
* @package core
* @copyright 2015 Damyon Wiese <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(function() { });
130 changes: 130 additions & 0 deletions lib/classes/requirejs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
// 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/>.

/**
* RequireJS helper functions.
*
* @package core
* @copyright 2015 Damyon Wiese <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Collection of requirejs related methods.
*
* @copyright 2015 Damyon Wiese <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_requirejs {

/**
* Check a single module exists and return the full path to it.
*
* The expected location for amd modules is:
* <componentdir>/amd/src/modulename.js
*
* @param string $component The component determines the folder the js file should be in.
* @param string $jsfilename The filename for the module (with the js extension).
* @param boolean $debug If true, returns the paths to the original (unminified) source files.
* @return array $files An array of mappings from module names to file paths.
* Empty array if the file does not exist.
*/
public static function find_one_amd_module($component, $jsfilename, $debug = false) {
$jsfileroot = core_component::get_component_directory($component);
if (!$jsfileroot) {
return array();
}

$module = str_replace('.js', '', $jsfilename);

$srcdir = $jsfileroot . '/amd/build';
$minpart = '.min';
if ($debug) {
$srcdir = $jsfileroot . '/amd/src';
$minpart = '';
}

$filename = $srcdir . '/' . $module . $minpart . '.js';
if (!file_exists($filename)) {
return array();
}

$fullmodulename = $component . '/' . $module;
return array($fullmodulename => $filename);
}

/**
* Scan the source for AMD modules and return them all.
*
* The expected location for amd modules is:
* <componentdir>/amd/src/modulename.js
*
* @param boolean $debug If true, returns the paths to the original (unminified) source files.
* @return array $files An array of mappings from module names to file paths.
*/
public static function find_all_amd_modules($debug = false) {
global $CFG;

$jsdirs = array();
$jsfiles = array();

$dir = $CFG->libdir . '/amd';
if (!empty($dir) && is_dir($dir)) {
$jsdirs['core'] = $dir;
}
$subsystems = core_component::get_core_subsystems();
foreach ($subsystems as $subsystem => $dir) {
if (!empty($dir) && is_dir($dir . '/amd')) {
$jsdirs[$subsystem] = $dir . '/amd';
}
}
$plugintypes = core_component::get_plugin_types();
foreach ($plugintypes as $type => $dir) {
$plugins = core_component::get_plugin_list_with_file($type, 'amd', false);
foreach ($plugins as $plugin => $dir) {
if (!empty($dir) && is_dir($dir)) {
$jsdirs[$type . '_' . $plugin] = $dir;
}
}
}

foreach ($jsdirs as $component => $dir) {
$srcdir = $dir . '/build';
if ($debug) {
$srcdir = $dir . '/src';
}
$items = new RecursiveDirectoryIterator($srcdir);
foreach ($items as $item) {
$extension = $item->getExtension();
if ($extension === 'js') {
$filename = str_replace('.min', '', $item->getBaseName('.js'));
// We skip lazy loaded modules.
if (strpos($filename, '-lazy') === false) {
$modulename = $component . '/' . $filename;
$jsfiles[$modulename] = $item->getRealPath();
}
}
unset($item);
}
unset($items);
}

return $jsfiles;
}

}
Loading

0 comments on commit c40678f

Please sign in to comment.