Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Advanced Extending

Matt Karl edited this page Aug 21, 2014 · 2 revisions

Grunt Game Builder was designed to be very extensible. Here's an example of how to add additional tasks or override tasks in the default builder tasks. In this example we create some custom exec call which export a MySQL database for our project. We'll be registering two additional Grunt tasks db-export and db-import.

./package.json

In addition to Grunt and the Builder, include some additional plugins.

{
  "private": true,
  "dependencies": {
    "grunt": "~0.4.5",
    "grunt-game-builder": "*",
    "grunt-exec": "~0.4.6",
    "load-grunt-config": "~0.13.1"
  }
}

./Gruntfile.js

This Grunt file combine the Grunt Game Builder plugin tasks with your custom project tasks. This relies heavily on the load-grunt-config plugin for making more manageable projects and tasks.

module.exports = function(grunt)
{
	var path = require('path'),
		_ = grunt.util._;

	// Combine the game builder and current project
	// configs into one object
	grunt.initConfig(_.extend(

		// Setup the default game tasks
		require('grunt-game-builder')(grunt, { autoInit: false }), 

		// Setup the current project tasks
		require('load-grunt-config')(grunt, {
			// The path for the tasks
			configPath: path.join(process.cwd(), 'tasks'),
			autoInit: false, 

			// We don't want to reload builder
			loadGruntTasks: { pattern: [
				'grunt-*', 
				'!grunt-game-builder'
			] },

			// Share the deploy folder with the tasks
			data: {	
				mysqlPath : '/Applications/XAMPP/xamppfiles/bin',
				dbFile : 'db/database.sql',
				dbName : 'site_db'
			}
		})
	));
};

./tasks/exec.js

Basic mysqldump and mysql import commands.

module.exports = {
	export:
	{
		cmd: '<%= mysqlPath %>/mysqldump <%= dbName %> <%= dbFile %>'
	},
	import:
	{
		cmd: '<%= mysqlPath %>/mysql <%= dbName %> < <%= dbFile %>'
	}
};

./tasks/aliases.js

Register custom tasks for importing and export. The aliases.js file is automatically loaded by the load-grunt-config plugin.

module.exports = function(grunt)
{
	grunt.registerTask(
		'db-import',
		'Import the database from the filesystem', 
		['exec:import']
	);
	grunt.registerTask(
		'db-export',
		'Export the database to the filesystem', [
		'exec:export']
	);
};

Clone this wiki locally