Skip to content

Commit

Permalink
the first version of grunt-manifest-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
bzhang committed Aug 23, 2013
1 parent cfae789 commit d48c9a2
Show file tree
Hide file tree
Showing 7 changed files with 1,154 additions and 1 deletion.
56 changes: 56 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,56 @@
/*
* grunt-combopage
* https://github.com/ginano/grunt-combopage
*
* Copyright (c) 2013 ginano
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
// Configuration to be run (and then tested).
manifestGenerator: {
test: {
options:{
//is cache all the html files in source files
//{Boolean}
//default:true
includeHTML:true,
//is cache all the <img src=""/> images in the html files in source files
//{Boolean}
//default:true
includeHtmlImage:true,
//is cache all the style files imported by the html
//{Boolean}
//default:true
includeCSS:true,
//is cache all the background-images in the css contents, which were used by the html files
//{Boolean}
//default:true
includeCssImage:true,
//is cache all the js files in the html files
//{Boolean}
//default:true
includeJS:true,
//all the files above but the fllowwing files.
//{Array} the item could be writen as regexpress.
//default:[]
excludeFiles:['/\.png$/']
},
files: {
'test.manifest': ['test/test.html']
}
}
}
});

// Actually load this plugin's task(s).
grunt.loadTasks('tasks');

// By default, lint and run all tests.
grunt.registerTask('default', ['manifestGenerator']);
};
77 changes: 76 additions & 1 deletion README.md
@@ -1,4 +1,79 @@
grunt-manifest-generator
========================

this tool will scan all the html files, and get all the images, css, js, html to generator the manifest file.
>this tool will scan all the html files, and get all the images, css, js, html to generator the manifest file.
## Getting Started
This plugin requires Grunt `~0.4.1`

If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

```shell
npm install grunt-manifest-generator
```

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

```js
grunt.loadNpmTasks('grunt-manifest-generator');
```

You can run the testcase. change you path to ./node_modeules/grunt-manifest-generator/, and run grunt. You could see the result file ./output/test.manifest file was created! The ./Gruntfile.js has the base function of this task, so you could use it as that. If you have any question, you can contact me at sina weibo: http://weibo.com/ginano
## The "manifest-generator" task

### Overview
In your project's Gruntfile, add a section named `manifestGenerator` to the data object passed into `grunt.initConfig()`.

```js
'use strict';

module.exports = function(grunt) {
grunt.initConfig({
// Configuration to be run (and then tested).
manifestGenerator:{
test: {
options:{
//is cache all the html files in source files
//{Boolean}
//default:true
includeHTML:true,
//is cache all the images tags or inline style with background-images in the html files in source files
//{Boolean}
//default:true
includeHtmlImage:true,
//is cache all the style files imported by the html
//{Boolean}
//default:true
includeCSS:true,
//is cache all the background-images in the css contents, which were used by the html files
//{Boolean}
//default:true
includeCssImage:true,
//is cache all the js files in the html files
//{Boolean}
//default:true
includeJS:true,
//all the files above but the fllowwing files.
//{Array} the item could be writen as regexpress.
//default:[]
excludeFiles:['/\.png$/']
},
files: {
//the task will scan all the source files, and generate 'test.manifest' file as the cache setting.
'test.manifest': ['test/test.html','test/index.html']
}
}
}
});
// Actually load this plugin's task(s).
grunt.loadNpmTasks('grunt-manifest-generator');
grunt.registerTask('grunt', ['manifestGenerator:test']);
};
```


## 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](http://gruntjs.com/).

## Release History
2013-8-23 0.1.0 create the plugin
42 changes: 42 additions & 0 deletions package.json
@@ -0,0 +1,42 @@
{
"name": "grunt-manifest-generator",
"description": "this tool will scan all the html files, and get all the images, css, js, html to generator the manifest file.",
"version": "0.1.0",
"homepage": "https://github.com/ginano/grunt-manifest-generator",
"author": {
"name": "ginano",
"email": "www@ginano.net",
"url": "http://www.ginano.net"
},
"repository": {
"type": "git",
"url": "git://github.com/ginano/grunt-manifest-generator.git"
},
"bugs": {
"url": "https://github.com/ginano/grunt-manifest-generator/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/ginano/grunt-manifest-generator/blob/master/LICENSE-MIT"
}
],
"main": "Gruntfile.js",
"engines": {
"node": ">= 0.8.0"
},
"scripts": {
"test": "grunt test"
},
"dependencies": {
},
"peerDependencies": {
"grunt": "~0.4.1"
},
"keywords": [
"gruntplugin",
"manifest",
"generator",
"combo"
]
}
154 changes: 154 additions & 0 deletions tasks/lib/util.js
@@ -0,0 +1,154 @@
'use strict';

var Path = require('path');
var Url = require('url');
var Http = require('http');

exports.init = function(grunt) {
var File = grunt.file;
/**
* output the log to the console
* @param {String} str
* @param {String} type 'log'/'error'
* @return {undefined}
*/
function log(str, type) {
type = type == 'error' ? 'error' : 'writeln';
grunt.log[type](str);
}
/**
* check the str is or isnot url, such as http:// or //www.baidu.com
* @param {String} str
* @return {Boolean}
*/
function isUrl(str) {
return /^(\S+:)?\/\//i.test(str.trim());
}
/**
* check the type of obj is function
* @param {Object} obj
* @return {Boolean}
*/
function isFunction(obj){
return 'function' === typeof obj;
}
/**
* get the file content of url, when the content has been gotten, it will call the function of callback
* please pay attention that the function is asynchronous
* @param {String} url
* @param {Function} callback
* @param {Function} errorcall
* @return {undefined}
*/
function getContent(url, callback, errorcall) {
var _content = '';
//from net
if (isUrl(url)) {
Http.get(url, function(res) {
res.on('data', function(data) {
_content += data;
})
.on('end', function() {
if (isFunction(callback)) {
callback(_content);
return;
}
log("Got response: " + _content);
});
}).on('error', function(e) {
if (isFunction(errorcall)) {
e.url = url;
errorcall(e);
return;
}
log("Got error from [" + url + "]:\n" + e.message, 'error');
});
} else {
setTimeout(function() {
try {
//should clear the query string
_content = File.read(url.replace(/[?#]\S+$/i,''));
if (isFunction(callback)) {
callback(_content);
return;
}
log("Got response: " + _content);
} catch (e) {
if (isFunction(errorcall)) {
e.url = url;
errorcall(e);
return;
}
log("Got error from [" + url + "]:\n" + e.message, 'error');
}
}, 0);
}
}
/**
* get the absoulte path of file relativePath, which relatived to targetFilePath
* @param {String} targetFilePath
* @param {String} relativePath
* @return {String}
*/
function getAbsolutePath(targetFilePath, relativePath) {
relativePath = relativePath.trim();
if (isUrl(relativePath)) {
return relativePath;
}
if (isUrl(targetFilePath)) {
return Url.resolve(targetFilePath, relativePath);
}
return Path.normalize(Path.dirname(targetFilePath) + '/' + relativePath);
}
/**
* get the relative path of file sourcePath, which relatived to targetFilePath
* @param {[type]} targetFilePath
* @param {[type]} sourcePath
* @return {[type]}
*/
function getRelativePath(targetFilePath, sourcePath) {
sourcePath = sourcePath.trim();
if (isUrl(sourcePath)) {
return sourcePath;
}
return Path.relative(Path.dirname(targetFilePath), sourcePath).replace(/\\/g, '/');
}
/**
* are all the List items have completed?
* @param {Array} checkList
* @param {Function} callback if all have done, call this function.
* @return {Boolean}
*/
function checkAllDone(checkList, callback){
var l;
if(checkList.__isDone){
return;
}
if(checkList instanceof Array){
l= checkList.length;
while(l--){
if(!checkList[l].isDone){
return;
}
}
} else {
for(l in checkList){
if(!checkList[l].isDone){
return;
}
}
}
//has done, may be the array is nevery use
checkList.__isDone=true;
callback();
}
return {
log: log,
isUrl: isUrl,
isFunction: isFunction,
getContent: getContent,
getAbsolutePath: getAbsolutePath,
getRelativePath: getRelativePath,
checkAllDone: checkAllDone
};
};

0 comments on commit d48c9a2

Please sign in to comment.