Permalink
Browse files

Added new generator methods: tarball, fetch and remote

  • Loading branch information...
1 parent 1a05339 commit 81f7a25232ab3b723cab804376d574bd68dfd1b4 @mklabs mklabs committed Jul 9, 2012
View
@@ -5,5 +5,6 @@
#*.min.js
.sass-cache/*
node_modules
+cli/_cache/
cli/tasks/init/remotes/_cache/
-cli/tasks/init/yeoman/root/
+cli/tasks/init/yeoman/root/
@@ -1,6 +1,8 @@
-var path = require('path'),
+var fs = require('fs'),
+ path = require('path'),
grunt = require('grunt'),
+ fetch = require('../utils/fetch'),
_ = grunt.util._;
var actions = module.exports;
@@ -89,3 +91,70 @@ actions.directory = function directory(source, destination) {
self.log.ok();
});
};
+
+// Fetch a remote tarball, and untar at the given location
+actions.tarball = fetch;
+
+// Download a single file at the given destination.
+actions.fetch = function(url, destination, cb) {
+ this.mkdir(path.dirname(destination));
+ fetch.request(url)
+ .on('error', cb)
+ .pipe(fs.createWriteStream(destination))
+ .on('error', cb)
+ .on('close', cb);
+};
+
+// Remotely fetch a package on github, store this into a _cache folder,
+// and provide a "remote" object as an a facade API to ourself (part of
+// genrator API, copy, template, directory)
+actions.remote = function(username, repo, branch, cb) {
+ if(!cb) { cb = branch; branch = 'master'; }
+
+ var self = this,
+ cache = path.join(__dirname, '../../_cache/', username, repo, branch),
+ url = 'http://nodeload.github.com/' + [username, repo, 'tarball', branch].join('/');
+
+ fs.stat(cache, function(err) {
+ // already cached
+ if(!err) return done();
+ // first time fetch
+ self.tarball(url, cache, done);
+ });
+
+ // XXX remote should probably be in its own file,
+ function done(err) {
+ if(err) return cb(err);
+
+ var files = grunt.file.expandFiles(path.join(cache, '**')).map(function(filepath) {
+ return filepath.slice(cache.length + 1);
+ });
+
+ var remote = {};
+
+ remote.copy = function copy(source, destination, options) {
+ source = path.join(cache, source);
+ grunt.file.copy(source, destination, options);
+ return this;
+ };
+
+ remote.template = function template(source, destination, data) {
+ // data is meant to be the whole instance for now. Will change.
+ data = data || self;
+ destination = destination || source;
+ source = path.join(cache, source);
+
+ var body = grunt.template.process(grunt.file.read(source), data);
+ self.write(destination, body);
+ };
+
+ remote.directory = function directory(source, destination) {
+ var root = self.sourceRoot();
+ self.sourceRoot(cache);
+ self.directory(source, destination);
+ self.sourceRoot(root);
+ };
+
+ cb(err, remote, files);
+ }
+};
@@ -56,15 +56,43 @@ AppGenerator.prototype.gitignore = function gitignore() {
this.copy('gitignore', '.gitignore');
};
+
+AppGenerator.prototype.fetchH5bp = function fetchH5bp() {
+ var cb = this.async();
+ // Fecth allows the download of single files, into the destination directory
+ this.fetch('https://raw.github.com/h5bp/html5-boilerplate/master/index.html', 'index.html', function(err) {
+ if(err) return cb(err);
+ cb();
+ });
+
+};
+
+AppGenerator.prototype.fetchPackage = function fetchPackage() {
+ this.log.writeln('Fetching h5bp/html5-boilerplate pkg');
+
+ var cb = this.async();
+
+ this.remote('h5bp', 'html5-boilerplate', 'master', function(err, remote) {
+ if(err) return cb(err);
+
+ // Remote allows the download of full repository, copying of single of
+ // multiple files with glob patterns. `remote` is your API to access this
+ // fetched (or cached) package, to copy or process through _.template
+
+ // remote.copy('index.html', 'index.html');
+ // remote.template('index.html', 'will/be/templated/at/index.html');
+
+ remote.directory('.', 'app');
+ cb();
+ });
+
+};
+
AppGenerator.prototype.app = function app() {
this.mkdir('app');
this.mkdir('app/js');
this.mkdir('app/css');
this.mkdir('app/templates');
-
- // create the index.html file (until we remotely fetch again h5bp repository,
- // and copy index.html + stripped conversion)
- this.copy('index.html', 'app/index.html');
};
AppGenerator.prototype.lib = function lib() {
@@ -1,60 +0,0 @@
-<!doctype html>
-<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
-<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
-<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
-<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->
-<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
-<head>
- <meta charset="utf-8">
-
- <!-- Use the .htaccess and remove these lines to avoid edge case issues.
- More info: h5bp.com/i/378 -->
- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
-
- <title><%= title %></title>
- <meta name="description" content"<%= description %>">
-
- <!-- Mobile viewport optimized: h5bp.com/viewport -->
- <meta name="viewport" content="width=device-width">
-
- <!-- Place favicon.ico and apple-touch-icon.png in the root directory: mathiasbynens.be/notes/touch-icons -->
-
- <link rel="stylesheet" href="css/style.css">
-
- <!-- More ideas for your <head> here: h5bp.com/d/head-Tips -->
-
- <!-- All JavaScript at the bottom, except this Modernizr build.
- Modernizr enables HTML5 elements & feature detects for optimal performance.
- Create your own custom Modernizr build: www.modernizr.com/download/ -->
- <script src="js/vendor/modernizr-2.5.3.min.js"></script>
-</head>
-<body>
- <!-- Prompt IE 6 users to install Chrome Frame. Remove this if you support IE 6.
- chromium.org/developers/how-tos/chrome-frame-getting-started -->
- <!--[if lt IE 7]><p class=chromeframe>Your browser is <em>ancient!</em> <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience this site.</p><![endif]-->
-
- <!-- Add your site or application content here -->
-
-
- <!-- JavaScript at the bottom for fast page loading: http://developer.yahoo.com/performance/rules.html#js_bottom -->
-
- <!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline -->
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
- <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.7.2.min.js"><\/script>')</script>
-
- <!-- scripts concatenated and minified via build script -->
- <script src="js/plugins.js"></script>
- <script src="js/main.js"></script>
- <!-- end scripts -->
-
- <!-- Asynchronous Google Analytics snippet. Change UA-XXXXX-X to be your site's ID.
- mathiasbynens.be/notes/async-analytics-snippet -->
- <script>
- var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
- (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
- g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
- s.parentNode.insertBefore(g,s)}(document,'script'));
- </script>
-</body>
-</html>
-
View
@@ -9,10 +9,15 @@ var fs = require('fs'),
var proxy = process.env.http_proxy || process.env.HTTP_PROXY ||
process.env.https_proxy || process.env.HTTPS_PROXY || '';
+module.exports = fetch;
+
+// re-expose the request with proxy defaults, so that we can
+// reuse this instance of request.
+fetch.request = request.defaults({ proxy: proxy });
+
// heavily based on npm's util/untar.js file
-module.exports = function fetch(tarball, target, cb) {
- var r = request.defaults({ proxy: proxy }),
- now = +new Date;
+function fetch(tarball, target, cb) {
+ var now = +new Date;
var log = this.log
.subhead('Fetching ' + tarball)
@@ -22,7 +27,7 @@ module.exports = function fetch(tarball, target, cb) {
var extractOpts = { type: 'Directory', path: target, strip: 1 };
// remote request --> zlib.Unzip() --> untar into h5bp/root
- var req = r.get(tarball).on('error', cb);
+ var req = fetch.request.get(tarball).on('error', cb);
req.on('data', function() { log.write('.'); }).on('end', function() {
log.ok().writeln();

0 comments on commit 81f7a25

Please sign in to comment.