Permalink
Browse files

Added grunt tasks

  • Loading branch information...
1 parent 0a52af1 commit 7e907eabede11c34522c1bf4c707fddceef78e98 @pdanis pdanis committed Aug 30, 2012
Showing with 205 additions and 1 deletion.
  1. +0 −1 .gitignore
  2. +35 −0 build/tasks/clean.js
  3. +98 −0 build/tasks/copy.js
  4. +72 −0 build/tasks/helpers.js
View
@@ -1,2 +1 @@
node_modules
-build
View
@@ -0,0 +1,35 @@
+/**
+ * Task: clean
+ * Description: Clear files and folders.
+ * Dependencies: rimraf
+ * Contributor: @tbranyen
+ */
+
+module.exports = function(grunt) {
+ "use strict";
+
+ grunt.registerMultiTask("clean", "Clear files and folders", function() {
+ var options = grunt.helper("options", this);
+
+ grunt.verbose.writeflags(options, "Options");
+
+ var paths = grunt.file.expand(this.file.src);
+
+ paths.forEach(function(path) {
+ grunt.helper("clean", path);
+ });
+ });
+
+ grunt.registerHelper("clean", function(path) {
+ grunt.log.write('Cleaning "' + path + '"...');
+
+ try {
+ require("rimraf").sync(path);
+ grunt.log.ok();
+ } catch (e) {
+ grunt.log.error();
+ grunt.verbose.error(e);
+ grunt.fail.warn("Clean operation failed.");
+ }
+ });
+};
View
@@ -0,0 +1,98 @@
+/**
+ * Task: copy
+ * Description: Copy files into another directory
+ * Contributor: @ctalkington
+ */
+
+module.exports = function(grunt) {
+ "use strict";
+
+ var path = require("path");
+
+ // TODO: ditch this when grunt v0.4 is released
+ grunt.util = grunt.util || grunt.utils;
+
+ var _ = grunt.util._;
+ var kindOf = grunt.util.kindOf;
+
+ var findBasePath = function(srcFiles) {
+ var basePaths = [];
+ var dirName;
+
+ srcFiles.forEach(function(srcFile) {
+ dirName = path.dirname(srcFile);
+ dirName = path.normalize(dirName);
+
+ basePaths.push(dirName.split(path.sep));
+ });
+
+ basePaths = _.intersection.apply([], basePaths);
+
+ return path.join.apply(path, basePaths);
+ };
+
+ grunt.registerMultiTask("copy", "Copy files into another directory.", function() {
+ var options = grunt.helper("options", this, {
+ basePath: false,
+ flatten: false,
+ processName: false,
+ processContent: false,
+ processContentExclude: []
+ });
+
+ // TODO: ditch this when grunt v0.4 is released
+ this.files = this.files || grunt.helper("normalizeMultiTaskFiles", this.data, this.target);
+
+ var copyOptions = {
+ process: options.processContent,
+ noProcess: options.processContentExclude
+ };
+
+ if (options.basePath) {
+ options.basePath = path.normalize(options.basePath);
+ options.basePath = _(options.basePath).trim(path.sep);
+ }
+
+ grunt.verbose.writeflags(options, "Options");
+
+ var srcFiles;
+
+ var basePath;
+ var filename;
+ var relative;
+ var destFile;
+
+ this.files.forEach(function(file) {
+ srcFiles = grunt.file.expandFiles(file.src);
+
+ basePath = options.basePath || findBasePath(srcFiles);
+
+ grunt.log.write("Copying file(s)" + ' to "' + file.dest + '"...');
+
+ srcFiles.forEach(function(srcFile) {
+ filename = path.basename(srcFile);
+ relative = path.dirname(srcFile);
+ relative = path.normalize(relative);
+
+ if (options.flatten) {
+ relative = "";
+ } else if (basePath && basePath.length > 1) {
+ relative = _(relative).chain().strRight(basePath).trim(path.sep).value();
+ }
+
+ if (options.processName && kindOf(options.processName) === "function") {
+ filename = options.processName(filename);
+ }
+
+ // make paths outside grunts working dir relative
+ relative = relative.replace(/\.\.(\/|\\)/g, "");
+
+ destFile = path.join(file.dest, relative, filename);
+
+ grunt.file.copy(srcFile, destFile, copyOptions);
+ });
+
+ grunt.log.ok();
+ });
+ });
+};
@@ -0,0 +1,72 @@
+/**
+ * Grunt Contrib Helpers
+ * Description: help make things consistent across tasks.
+ * Contributor: @tkellen
+ */
+
+module.exports = function(grunt) {
+ "use strict";
+
+ // TODO: ditch this when grunt v0.4 is released
+ grunt.util = grunt.util || grunt.utils;
+
+ // Helper for consistent options key access across contrib tasks.
+ grunt.registerHelper("options", function(data, defaults) {
+ var _ = grunt.util._;
+ var namespace = data.nameArgs.split(":");
+ var task = grunt.config(_.flatten([namespace, "options"]));
+ var global_subtask = namespace.length > 1 ? grunt.config(_.flatten(["options", namespace])) : {};
+ var global = grunt.config(["options", namespace[0]]);
+
+ return _.defaults({}, task, global_subtask, global, defaults || {});
+ });
+
+ // TODO: ditch this when grunt v0.4 is released
+ // Temporary helper for normalizing files object
+ grunt.registerHelper("normalizeMultiTaskFiles", function(data, target) {
+ var prop, obj;
+ var files = [];
+ if (grunt.util.kindOf(data) === 'object') {
+ if ('src' in data || 'dest' in data) {
+ obj = {};
+ if ('src' in data) { obj.src = data.src; }
+ if ('dest' in data) { obj.dest = data.dest; }
+ files.push(obj);
+ } else if (grunt.util.kindOf(data.files) === 'object') {
+ for (prop in data.files) {
+ files.push({src: data.files[prop], dest: prop});
+ }
+ } else if (Array.isArray(data.files)) {
+ data.files.forEach(function(obj) {
+ var prop;
+ if ('src' in obj || 'dest' in obj) {
+ files.push(obj);
+ } else {
+ for (prop in obj) {
+ files.push({src: obj[prop], dest: prop});
+ }
+ }
+ });
+ }
+ } else {
+ files.push({src: data, dest: target});
+ }
+
+ // Process each normalized file object as a template.
+ files.forEach(function(obj) {
+ // Process src as a template (recursively, if necessary).
+ if ('src' in obj) {
+ obj.src = grunt.util.recurse(obj.src, function(src) {
+ if (typeof src !== 'string') { return src; }
+ return grunt.template.process(src);
+ });
+ }
+ if ('dest' in obj) {
+ // Process dest as a template.
+ obj.dest = grunt.template.process(obj.dest);
+ }
+ });
+
+ return files;
+ });
+};

0 comments on commit 7e907ea

Please sign in to comment.