Skip to content
This repository
tag: v0.2.5
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 331 lines (295 sloc) 10.106 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
var fs = require('fs')
  , child_process = require('child_process')
  , path = require('path')
  , exec = child_process.exec
  , inflection = require('./deps/inflection')
  , utils = require('./lib/utils')
  , ejs = require('./lib/template/adapters/ejs/ejs')
  , createPackageTask
  , getCurrentVersionNumber;

var JSPAT = /\.js$/;

getCurrentVersionNumber = function () {
  pkg = JSON.parse(fs.readFileSync(__dirname + '/package.json').toString())
  version = pkg.version
  return version;
};

createPackageTask = function () {
  var version = getCurrentVersionNumber()
    , t;

  t = new jake.PackageTask('geddy', 'v' + version, function () {
    var fileList = [
        'Makefile'
      , 'Jakefile'
      , 'README.md'
      , 'package.json'
      , 'bin/*'
      , 'deps/*'
      , 'lib/*'
      , 'templates/*'
      , 'test/*'
      ];
    this.packageFiles.include(fileList);
    this.needTarGz = true;
  });
};

namespace('gen', function () {

  var _writeTemplate = function (name, filename, dirname, opts) {
        var names = _getInflections(name)
          , text = fs.readFileSync(__dirname +
                '/templates/' + filename +'.ejs', 'utf8').toString()
          , templ
          , filePath;
        // Render with the right model name
        templ = new ejs.Template({text: text});
        templ.process({data: {names: names}});
        filePath = './app/' + dirname + '/' + names.filename[opts.inflection] + '.js';
        fs.writeFileSync(filePath, templ.markup, 'utf8');
        console.log('[ADDED] ' + filePath);
      }

    , _getInflections = function (nameParam) {
        var name = utils.string.snakeize(nameParam)
          , namePlural = inflection.pluralize(name)
          , names = {
            filename: {
              singular: name
            , plural: namePlural
            }
          , constructor: {
              singular: utils.string.camelize(name, true)
            , plural: utils.string.camelize(namePlural, true)
            }
          , property: {
              singular: utils.string.camelize(name)
            , plural: utils.string.camelize(namePlural)
            }
          };
        return names;
      };

  desc('Creates a new Geddy app scaffold.');
  task('app', [], function (appName) {
    if (!appName) {
      throw new Error('No app-name specified.');
    }
    var dir = appName
      , templateDir = __dirname + '/templates/base'
      , cmds = [
          'mkdir -p ./' + dir
        , 'mkdir -p ./' + dir + '/config'
        , 'mkdir -p ./' + dir + '/app/models'
        , 'mkdir -p ./' + dir + '/app/controllers'
        , 'mkdir -p ./' + dir + '/app/views'
        , 'mkdir -p ./' + dir + '/lib'
        , 'mkdir -p ./' + dir + '/public'
        , 'mkdir -p ./' + dir + '/public/js'
        , 'mkdir -p ./' + dir + '/public/css'
        , 'mkdir -p ./' + dir + '/log'
        , 'cp ' + templateDir + '/router.js ' + dir + '/config/'
        , 'cp ' + templateDir + '/init.js ' + dir + '/config/'
        , 'cp ' + templateDir + '/environment.js ' + dir + '/config/'
        , 'cp ' + templateDir + '/development.js ' + dir + '/config/'
        , 'cp ' + templateDir + '/production.js ' + dir + '/config/'
        , 'cp ' + templateDir + '/main.js ' + dir + '/app/controllers/'
        , 'cp ' + templateDir + '/application.js ' + dir + '/app/controllers/'
        , 'cp ' + templateDir + '/master.css ' + dir + '/public/css/'
        , 'cp ' + templateDir + '/favicon.ico ' + dir + '/public/'
        ];
    jake.exec(cmds, function () {
      console.log('Created app ' + dir + '.');
      complete();
    });
  }, {async: true});

  desc('Creates a resource-based route with model and controller.');
  task('resource', function (name) {
    jake.Task['gen:model'].invoke(name);
    jake.Task['gen:controller'].invoke(name);
    jake.Task['gen:route'].invoke(name);
    jake.Task['gen:views'].invoke(name);
  });

  task('model', [], function (name) {
    _writeTemplate(name, 'resource_model', 'models',
        {inflection: 'singular'});
  });

  task('controller', [], function (name) {
    _writeTemplate(name, 'resource_controller', 'controllers',
        {inflection: 'plural'});
  });

  task('route', [], function (name, opts) {
    var names = _getInflections(name)
      , options = opts || {}
      , routeType = options.bare ? 'Bare' : 'Resource'
      , filePath = './config/router.js'
      , newRoute
      , text = fs.readFileSync(filePath, 'utf8').toString()
      , routeArr;

    if (options.bare) {
      newRoute = 'router.match(\'/' + names.filename.plural +
          '\').to({controller: \'' + names.constructor.plural +
          '\', action: \'index\'});';
    }
    else {
      newRoute = 'router.resource(\'' + names.filename.plural + '\');'
    }

    if (text.indexOf(newRoute) == -1) {
      // Add the new resource route just above the export
      routerArr = text.split('exports.router');
      routerArr[0] += newRoute + '\n';
      text = routerArr.join('exports.router');
      fs.writeFileSync(filePath, text, 'utf8');
      console.log(routeType + ' ' + names.filename.plural +
          ' route added to ' + filePath);
    }
    else {
      console.log('(' + routeType + ' ' + names.filename.plural +
          ' route already defined in ' + filePath + ')');
    }
  });

  task('views', [], function (name, opts) {
    var names = _getInflections(name)
      , options = opts || {}
      , viewDir = './app/views/' + names.filename.plural
      , actions
      , cmds = []
      , addActionView = function (action) {
          cmds.push('cp ' + __dirname + '/templates/views/' +
              action + '.html.ejs ' + viewDir + '/');
        };

    cmds.push('mkdir -p ' + viewDir);
    cmds.push('mkdir -p ./app/views/layouts');

    addActionView('index');
    // Add views for the other CRUD actions when doing a full-on resource
    if (!options.bare) {
      ['add', 'edit', 'show'].forEach(function (action) {
        addActionView(action);
      });
    }

    // Create an app-layout if one doesn't exist
    if (!path.existsSync(process.cwd() +
        '/app/views/layouts/application.html.ejs')) {
      cmds.push('cp ' + __dirname + '/templates/views/layout.html.ejs ' +
          './app/views/layouts/application.html.ejs');
    }

    jake.exec(cmds, function () {
      console.log('Created view templates.');
    });
  });

  task('bareController', [], function (name) {
    _writeTemplate(name, 'bare_controller', 'controllers',
        {inflection: 'plural'});
    jake.Task['gen:route'].invoke(name, {bare: true});
    jake.Task['gen:views'].invoke(name, {bare: true});
  });

  task('secret', [], function (name) {
    var filename = process.cwd() + '/config/environment.js'
      , conf = fs.readFileSync(filename).toString()
      , confArr
      , secret = utils.string.uuid(128);

    // Remove any old secret
    conf = conf.replace(/\nconfig.secret.+;\n/, '');

    confArr = conf.split('module.exports = config;');
    conf = confArr[0] + "config.secret = '" + secret + "';\n\n" +
      'module.exports = config;' + confArr[1];
    fs.writeFileSync(filename, conf);
    console.log('secret added to environment.js config.');
  });

});

namespace('doc', function () {
  desc('Generate docs for Geddy');
  task('generate', ['doc:clobber'], function () {
    var cmd = '../node-jsdoc-toolkit/app/run.js -n -r=100 ' +
        '-t=../node-jsdoc-toolkit/templates/codeview -d=./doc/ ./lib';
    console.log('Generating docs ...');
    exec(cmd, function (err, stdout, stderr) {
      if (err) {
        throw err;
      }
      if (stderr) {
        console.log(stderr);
      }
      if (stdout) {
        console.log(stdout);
      }
      console.log('Done.');
      complete();
    });
  }, {async: true});

  desc('Clobber the generated docs.');
  task('clobber', function () {
    var cmd = 'rm -fr ./doc/*';
    exec(cmd, function (err, stdout, stderr) {
      if (err) {
        throw err;
      }
      if (stderr) {
        console.log(stderr);
      }
      if (stdout) {
        console.log(stdout);
      }
      console.log('Clobbered old docs.');
      complete();
    });
  }, {async: true});

});

desc('Runs the tests.');
task('test', function () {
  var dir = process.cwd()
    , dirList = fs.readdirSync(dir + '/test')
    , fileName
    , cmds = [];
  for (var i = 0; i < dirList.length; i++) {
    fileName = dirList[i];
    // Any files ending in '.js'
    if (JSPAT.test(fileName)) {
      cmds.push('node ./test/' + fileName);
    }
  }
  jake.exec(cmds, function () {
    console.log('Tests passed.');
    complete();
  }, {stdout: true});
}, {async: true});

namespace('npm', function () {
 task('version', function () {
    cmds = [
      'npm version patch --message "Bumped version number."'
    , 'git push origin master'
    , 'git push --tags'
    ];
    jake.exec(cmds, function () {
      console.log('Bumped version number.');
      complete();
    });
  }, {async: true});

  task('package', function () {
    // Recreate the PackageTask with the updated version-number,
    // run 'package'
    createPackageTask();
    // FIXME: Shouldn't have to shell out to get a real prereqs-tree
    jake.exec(['jake package'], function () {
      console.log('Created package.');
      complete();
    });

  }, {async: true});

  task('publish', function () {
    var version = getCurrentVersionNumber();
    cmds = [
      'sudo npm publish pkg/geddy-v' + version + '.tar.gz'
    ];
    // Hackity hack -- NPM publish sometimes returns errror like:
    // Error sending version data\nnpm ERR!
    // Error: forbidden 0.2.4 is modified, should match modified time
    setTimeout(function () {
      jake.exec(cmds, function () {
        console.log('Published to NPM.');
        complete();
      }, {stdout: true});
    }, 5000);
  }, {async: true});
});

desc('Bump version-number, package, and publish to NPM.');
task('publish', ['npm:version', 'npm:package', 'npm:publish'], function () {
});

// Don't create the package-tasks when being called as a generator
if (!process.env.generator) {
  createPackageTask();
}

Something went wrong with that request. Please try again.