Skip to content

Instantly share code, notes, and snippets.

@peterchoo
Created October 3, 2014 10:13
Show Gist options
  • Save peterchoo/f0ff0ba5e1ecacd0e81a to your computer and use it in GitHub Desktop.
Save peterchoo/f0ff0ba5e1ecacd0e81a to your computer and use it in GitHub Desktop.
Ember-CLI Addon Chaining - Running sub-addon blueprints automatically

Calling sub-addon blueprints from our blueprint

With Ember-CLI you now need to run ember generate <addon-name> to instantiate the addon into your application. This is also true with sub-addons.

Using the above method, you can essentially run ember generate <sub-addon-name>. The addon needs to be included as a hard dependency in your package.json. We can then configure our blueprint in blueprints/<addon-name>/index.js.

Running a sub-addon blueprint

We want to run the sub addon blueprints after our addon blueprint has completed. We will use the afterInstall hook for this.

The first thing we need to do is gain access to the task for running a blueprint:

var blueprintTask = this.taskFor('generate-from-blueprint');

Once we have the task, we need to create the options object to pass to it. The options object reflects the arguments available at the command line, --dry-run and --verbose.

var options = {
  args: ['<sub-addon-name>'],
  dryRun: false,
  verbose: false,
  disableAnalytics: false
};

The generate-from-blueprint task requires the blueprint name to be in the options.args array as the first element.

To properly complete the afterInstall hook, we need to return a promise. To allow tasks to run in an asynchronous manner, methods such as addPackageToProject and addBowerPackageToProject return a promise.

The blueprint task's run method also returns a promise. This allows us to call multiple sub-addon blueprints form our blueprint.

Using this method to skeleton a project

This method can be used to call built in blueprints from Ember-CLI. This means you can generate routes, resources, controllers etc. by chaining the blueprintsTask.run() calls at the end of the afterInstall return.

// File: blueprints/application-base/index.js
'use strict';
module.exports = {
normalizeEntityName: function() {},
afterInstall: function(options) {
/**
* Output messages to ui, enable in promises
*/
var ui = this.ui;
/**
* Get the blueprint task
*/
var blueprintTask = this.taskFor('generate-from-blueprint');
/**
* Blueprint task base options
*/
var options = {
args: [],
dryRun: false,
verbose: false,
disableAnalytics: false
};
/**
* We must use a promise chain on all items returned from the afterInstall
* hook, otherwise we break the install process.
*/
ui.writeLine(' Including Bootstrap For Ember library');
return this.addBowerPackageToProject("ember-addons.bs_for_ember", "0.7.0")
.then(function() {
ui.writeLine(' Running the ember-cli-simple-auth blueprint');
options.args = ['ember-cli-simple-auth'];
return blueprintTask.run(options);
})
.then(function() {
ui.writeLine(' Running the ember-cli-simple-auth-oauth2 blueprint');
options.args = ['ember-cli-simple-auth-oauth2'];
return blueprintTask.run(options);
});
}
};
'use strict';
module.exports = {
name: 'application-base',
config: function(environment, appConfig) {
console.log('app config', appConfig);
},
included: function(app) {
/**
* Call the super!
*/
this._super.included(app);
/**
* Import the base Bootstrap for Ember addons we will be using
*/
app.import(app.bowerDirectory + '/ember-addons.bs_for_ember/dist/js/bs-core.min.js');
app.import(app.bowerDirectory + '/ember-addons.bs_for_ember/dist/js/bs-basic.min.js');
app.import(app.bowerDirectory + '/ember-addons.bs_for_ember/dist/js/bs-modal.min.js');
app.import(app.bowerDirectory + '/ember-addons.bs_for_ember/dist/js/bs-button.min.js');
}
};
{
"name": "application-base",
"version": "0.0.0",
"private": true,
"directories": {
"doc": "doc",
"test": "tests"
},
"scripts": {
"start": "ember server",
"build": "ember build",
"test": "ember test"
},
"repository": "https://github.com/stefanpenner/ember-cli",
"engines": {
"node": ">= 0.10.0"
},
"author": "",
"license": "MIT",
"devDependencies": {
"body-parser": "^1.2.0",
"broccoli-asset-rev": "0.1.1",
"broccoli-ember-hbs-template-compiler": "^1.6.1",
"ember-cli": "0.0.46",
"ember-cli-ic-ajax": "0.1.1",
"ember-cli-inject-live-reload": "^1.0.2",
"ember-cli-qunit": "0.1.0",
"ember-data": "1.0.0-beta.10",
"express": "^4.8.5",
"glob": "^4.0.5"
},
"keywords": [
"ember-addon"
],
"ember-addon": {
"configPath": "tests/dummy/config"
},
"dependencies": {
"ember-cli-simple-auth": "0.6.7",
"ember-cli-simple-auth-oauth2": "0.6.7"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment