Anonymous avatar Anonymous committed 28292e9

initial very alpha version

Comments (0)

Files changed (9)

+node_modules
+/node_modules/
+Copyright (c) 2013 
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+# grunt-download-manifest
+
+Downloads files from an HTML5 manifest
+
+## Getting Started
+Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-download-manifest`
+
+Then add this line to your project's `grunt.js` gruntfile:
+
+```javascript
+grunt.loadNpmTasks('grunt-download-manifest');
+```
+
+[grunt]: http://gruntjs.com/
+[getting_started]: https://github.com/gruntjs/grunt/blob/master/docs/getting_started.md
+
+## Documentation
+_(Coming soon)_
+
+## Contributing
+In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt][grunt].
+
+## Release History
+_(Nothing yet)_
+
+## License
+Copyright (c) 2013   
+Licensed under the MIT license.

bin/grunt-download

+#!/usr/bin/env node
+require('grunt').npmTasks('grunt-download').cli();
+module.exports = function(grunt) {
+
+  // Project configuration.
+  grunt.initConfig({
+    test: {
+      files: ['test/**/*.js']
+    },
+    lint: {
+      files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js']
+    },
+    watch: {
+      files: '<config:lint.files>',
+      tasks: 'default'
+    },
+    jshint: {
+      options: {
+        curly: true,
+        eqeqeq: true,
+        immed: true,
+        latedef: true,
+        newcap: true,
+        noarg: true,
+        sub: true,
+        undef: true,
+        boss: true,
+        eqnull: true,
+        node: true,
+        es5: true
+      },
+      globals: {}
+    },
+    download: {
+    	m1: {
+		url: 'http://www.healthyachievements.org/4670/dist/manifest.appcache',
+		manifest: true,
+		filename: '/tmp/t/'
+	},
+    	m2: {
+		url: 'http://www.healthyachievements.org/4670/dist/manifest.appcache1234',
+		manifest: true,
+		filename: '/tmp/t/'
+	}
+
+
+    }
+  });
+
+  // Load local tasks.
+  grunt.loadTasks('tasks');
+
+  // Default task.
+  grunt.registerTask('default', 'download test');
+
+};
+{
+  "name": "grunt-download",
+  "description": "Downloads files from an HTML5 manifest",
+  "version": "0.1.0",
+  "homepage": "https://bitbucket.org/eug48/grunt-download",
+  "author": {},
+  "repository": {
+    "type": "git",
+    "url": "git+https://bitbucket.org/eug48/grunt-download.git"
+  },
+  "bugs": {
+    "url": "https://bitbucket.org/eug48/grunt-download/issues"
+  },
+  "licenses": [
+    {
+      "type": "MIT",
+      "url": "https://bitbucket.org/eug48/grunt-download/blob/master/LICENSE-MIT"
+    }
+  ],
+  "main": "grunt.js",
+  "bin": "bin/grunt-download",
+  "engines": {
+    "node": "*"
+  },
+  "scripts": {
+    "test": "grunt test"
+  },
+  "devDependencies": {
+    "grunt": "~0.3"
+  },
+  "keywords": [
+    "gruntplugin"
+  ],
+  "dependencies": {
+    "async": "~0.2",
+    "mkdirp": "~0.3"
+  }
+}

tasks/download_manifest.js

+/*
+ * grunt-download
+ * https://github.com/eug48/grunt-download
+ *
+ * Copyright (c) 2013 
+ * Licensed under the MIT license.
+ */
+
+async = require('async');
+url = require('url');
+http = require('http');
+fs = require('fs');
+path = require('path');
+mkdirp = require('mkdirp');
+
+function clone(obj){ // from http://stackoverflow.com/a/122190/94078
+      if(obj == null || typeof(obj) != 'object')
+        return obj;
+
+      var temp = obj.constructor(); // changed
+
+      for(var key in obj)
+        temp[key] = clone(obj[key]);
+      return temp;
+}
+
+module.exports = function(grunt) {
+
+  // Please see the grunt documentation for more information regarding task and
+  // helper creation: https://github.com/gruntjs/grunt/blob/master/docs/toc.md
+
+  // ==========================================================================
+  // TASKS
+  // ==========================================================================
+
+  grunt.registerMultiTask('download', 'Downloads files via HTTP', function() {
+    var configData = this.data;
+    var done = this.async();
+
+    var downloadFile = function(parsedUrl, outputFilename, cb) {
+      var options = {
+        hostname: parsedUrl.hostname,
+        port: parsedUrl.port,
+        path: parsedUrl.path,
+        method: 'GET'
+      };
+
+      grunt.log.write('downloading ' + url.format(parsedUrl) + ' to ' + outputFilename);
+      var file = fs.createWriteStream(outputFilename);
+
+      var req = http.request(options, function(res) {
+        grunt.log.write(' --> ' + res.statusCode + '\n');
+        //console.log('HEADERS: ' + JSON.stringify(res.headers));
+        //res.setEncoding('utf8');
+        if (res.statusCode == 200) {
+          res.pipe(file);
+          res.on('end', function () {
+            cb();
+          });
+        } else {
+          cb('HTTP result was ' + res.statusCode);
+        }
+      });
+      req.on('error', function(e) {
+          cb('problem with request: ' +  e.message);
+      });
+      req.end();
+    };
+
+    var parsedUrl = url.parse(configData.url, true);
+    var filename = configData.filename;
+    if (fs.statSync(configData.filename).isDirectory()) {
+      filename = path.join(configData.filename, path.basename(parsedUrl.pathname));
+    }
+
+    downloadFile(parsedUrl, filename, function(error) {
+      if (error) {
+        grunt.log.error(error);
+        console.log('error: ' + error);
+        done(false);
+        return;
+      }
+
+      if (configData.manifest) {
+        var manifest = fs.readFileSync(filename, 'utf8');
+        var manifestDir = path.dirname(filename);
+
+        var tasks = [];
+        manifest.split('\n').forEach(function (line, i) {
+          line = line.replace(/^\s+/g, ' ');
+          if (/^CACHE MANIFEST|#|\*$/.test(line) || /^[A-Z]+:$/.test(line) || line.length == 0) {
+            return;
+          }
+
+          var newUrl = clone(parsedUrl);
+          newUrl.search = '';
+          if (line[0] === '/') {
+            newUrl.path = newUrl.pathname = line;
+          } else {
+            newUrl.path = newUrl.pathname = path.join(path.dirname(parsedUrl.pathname), line);
+          }
+
+          var assetFilename = path.join(manifestDir, line);
+          mkdirp.sync(path.dirname(assetFilename));
+
+          tasks.push(function(cb) { downloadFile(newUrl, assetFilename, cb); });
+        });
+
+
+        async.series(tasks, function(error, results) {
+          if (error) {
+            done(false);
+          } else {
+            done(true);
+          }
+        });
+      } else {
+        done(true);
+      }
+
+    });
+
+  });
+
+};
+
+/* vim: set et sw=2 ts=2: */

test/download_test.js

+var grunt = require('grunt');
+
+/*
+  ======== A Handy Little Nodeunit Reference ========
+  https://github.com/caolan/nodeunit
+
+  Test methods:
+    test.expect(numAssertions)
+    test.done()
+  Test assertions:
+    test.ok(value, [message])
+    test.equal(actual, expected, [message])
+    test.notEqual(actual, expected, [message])
+    test.deepEqual(actual, expected, [message])
+    test.notDeepEqual(actual, expected, [message])
+    test.strictEqual(actual, expected, [message])
+    test.notStrictEqual(actual, expected, [message])
+    test.throws(block, [error], [message])
+    test.doesNotThrow(block, [error], [message])
+    test.ifError(value)
+*/
+
+exports['download-manifest'] = {
+  setUp: function(done) {
+    // setup here
+    done();
+  },
+  'helper': function(test) {
+    test.expect(0);
+    //test.equal(grunt.helper('download-manifest'), 'download-manifest!!!', 'should return the correct value.');
+    test.done();
+  }
+};
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.