1 | 'use strict';
|
2 |
|
3 | var path = require('path');
|
4 | var yeoman = require('yeoman-generator');
|
5 | var chalk = require('chalk');
|
6 | var yosay = require('yosay');
|
7 | var glob = require('glob');
|
8 |
|
9 | module.exports = yeoman.Base.extend({
|
10 |
|
11 | initializing: function () {
|
12 | this.sourceRoot(path.join(this.sourceRoot(), '../../ruby-starter-kit'));
|
13 | },
|
14 |
|
15 | prompting: function () {
|
16 | this.log(yosay(
|
17 | 'Welcome to the ' + chalk.green('Ruby Starter Kit') + ' generator!'
|
18 | ));
|
19 | },
|
20 |
|
21 | writing: function () {
|
22 | var done = this.async();
|
23 | glob('**/*', { cwd: this.sourceRoot(), dot: true }, function (err, files) {
|
24 | if (err) {
|
25 | this.log('Error:', err.message);
|
26 | return done();
|
27 | }
|
28 | files.forEach(function (file) {
|
29 | var dest = file;
|
30 | if (file === '.npmignore') {
|
31 | dest = '.gitignore';
|
32 | }
|
33 | this.fs.copy(
|
34 | this.templatePath(file),
|
35 | this.destinationPath(dest)
|
36 | );
|
37 | }, this);
|
38 | done();
|
39 | }.bind(this));
|
40 | },
|
41 |
|
42 | detectFeature: function (testCommand) {
|
43 | if (!testCommand) return;
|
44 | var result = this.spawnCommandSync(testCommand, ['--version'], {stdio: 'ignore'});
|
45 | return !result.error;
|
46 | },
|
47 |
|
48 | _promptUserWithGitInit: function() {
|
49 | var done = this.async();
|
50 | this.prompt({
|
51 | type: 'confirm',
|
52 | name: 'initGit',
|
53 | message: 'Initialize Git repo?',
|
54 | default: true
|
55 | }, function (answers) {
|
56 | if (answers.initGit) {
|
57 | this.log("Initializing Git repo..");
|
58 | this.spawnCommandSync('git', ['init']);
|
59 | }
|
60 |
|
61 | done();
|
62 | }.bind(this));
|
63 | },
|
64 |
|
65 | _showRvmInstructions: function () {
|
66 | this.log("You are using RVM. Please run these commands manually after this installation is complete:");
|
67 | this.log();
|
68 | this.log("rvm use `cat .ruby-version`");
|
69 | this.log("gem install bundler");
|
70 | this.log("bundle install");
|
71 | this.log();
|
72 | },
|
73 |
|
74 | _installGems: function () {
|
75 | this.log('Installing bundler gem..');
|
76 | this.spawnCommandSync('gem', ['install', 'bundler']);
|
77 | this.log("Installing dependent gems with 'bundle install'..");
|
78 | this.spawnCommandSync('bundle', ['install']);
|
79 | },
|
80 |
|
81 | install: {
|
82 |
|
83 | bundle: function() {
|
84 | if (this.detectFeature('rvm')) {
|
85 | this._showRvmInstructions();
|
86 | } else {
|
87 | this._installGems();
|
88 | }
|
89 | },
|
90 |
|
91 | gitInit: function() {
|
92 | if (this.detectFeature('git')) {
|
93 | var fs = require('fs');
|
94 | fs.exists('.git', function (exists) {
|
95 | if (!exists) {
|
96 | this._promptUserWithGitInit()
|
97 | } else {
|
98 | this._sayGoodbye();
|
99 | }
|
100 | }.bind(this));
|
101 | } else {
|
102 | this._sayGoodbye();
|
103 | }
|
104 | }
|
105 | },
|
106 |
|
107 | _sayGoodbye: function() {
|
108 | this.log(yosay("That's it. " + chalk.green("Happy hacking!")));
|
109 | }
|
110 | }); |
\ | No newline at end of file |