Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit

  • Loading branch information...
commit c308bd18bf423d0adbf07133408fe34fa290ca62 0 parents
Hubert Boma Manilla authored November 29, 2012
22  LICENSE-MIT
... ...
@@ -0,0 +1,22 @@
  1
+Copyright (c) 2012 Hubert Boma Manilla
  2
+
  3
+Permission is hereby granted, free of charge, to any person
  4
+obtaining a copy of this software and associated documentation
  5
+files (the "Software"), to deal in the Software without
  6
+restriction, including without limitation the rights to use,
  7
+copy, modify, merge, publish, distribute, sublicense, and/or sell
  8
+copies of the Software, and to permit persons to whom the
  9
+Software is furnished to do so, subject to the following
  10
+conditions:
  11
+
  12
+The above copyright notice and this permission notice shall be
  13
+included in all copies or substantial portions of the Software.
  14
+
  15
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  17
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22
+OTHER DEALINGS IN THE SOFTWARE.
28  README.md
Source Rendered
... ...
@@ -0,0 +1,28 @@
  1
+# grunt-regex-replace
  2
+
  3
+Grunt plugin to search and replace text content of files based on regular expression patterns
  4
+
  5
+## Getting Started
  6
+Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-regex-replace`
  7
+
  8
+Then add this line to your project's `grunt.js` gruntfile:
  9
+
  10
+```javascript
  11
+grunt.loadNpmTasks('grunt-regex-replace');
  12
+```
  13
+
  14
+[grunt]: http://gruntjs.com/
  15
+[getting_started]: https://github.com/gruntjs/grunt/blob/master/docs/getting_started.md
  16
+
  17
+## Documentation
  18
+_(Coming soon)_
  19
+
  20
+## Contributing
  21
+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].
  22
+
  23
+## Release History
  24
+_(Nothing yet)_
  25
+
  26
+## License
  27
+Copyright (c) 2012 Hubert Boma Manilla  
  28
+Licensed under the MIT license.
2  bin/grunt-regex-replace
... ...
@@ -0,0 +1,2 @@
  1
+#!/usr/bin/env node
  2
+require('grunt').npmTasks('grunt-regex-replace').cli();
40  grunt.js
... ...
@@ -0,0 +1,40 @@
  1
+module.exports = function(grunt) {
  2
+
  3
+  // Project configuration.
  4
+  grunt.initConfig({
  5
+    test: {
  6
+      files: ['test/**/*.js']
  7
+    },
  8
+    lint: {
  9
+      files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js']
  10
+    },
  11
+    watch: {
  12
+      files: '<config:lint.files>',
  13
+      tasks: 'default'
  14
+    },
  15
+    jshint: {
  16
+      options: {
  17
+        curly: true,
  18
+        eqeqeq: true,
  19
+        immed: true,
  20
+        latedef: true,
  21
+        newcap: true,
  22
+        noarg: true,
  23
+        sub: true,
  24
+        undef: true,
  25
+        boss: true,
  26
+        eqnull: true,
  27
+        node: true,
  28
+        es5: true
  29
+      },
  30
+      globals: {}
  31
+    }
  32
+  });
  33
+
  34
+  // Load local tasks.
  35
+  grunt.loadTasks('tasks');
  36
+
  37
+  // Default task.
  38
+  grunt.registerTask('default', 'lint test');
  39
+
  40
+};
37  package.json
... ...
@@ -0,0 +1,37 @@
  1
+{
  2
+  "name": "grunt-regex-replace",
  3
+  "description": "Grunt plugin to search and replace text content of files based on regular expression patterns",
  4
+  "version": "0.1.0",
  5
+  "homepage": "https://github.com/bomsy/grunt-regex-replace",
  6
+  "author": {
  7
+    "name": "Hubert Boma Manilla",
  8
+    "email": "b4bomsy@gmail.com"
  9
+  },
  10
+  "repository": {
  11
+    "type": "git",
  12
+    "url": "git://github.com/bomsy/grunt-regex-replace.git"
  13
+  },
  14
+  "bugs": {
  15
+    "url": "https://github.com/bomsy/grunt-regex-replace/issues"
  16
+  },
  17
+  "licenses": [
  18
+    {
  19
+      "type": "MIT",
  20
+      "url": "https://github.com/bomsy/grunt-regex-replace/blob/master/LICENSE-MIT"
  21
+    }
  22
+  ],
  23
+  "main": "grunt.js",
  24
+  "bin": "bin/grunt-regex-replace",
  25
+  "engines": {
  26
+    "node": "*"
  27
+  },
  28
+  "scripts": {
  29
+    "test": "grunt test"
  30
+  },
  31
+  "devDependencies": {
  32
+    "grunt": "~0.3.17"
  33
+  },
  34
+  "keywords": [
  35
+    "gruntplugin"
  36
+  ]
  37
+}
61  tasks/regex-replace.js
... ...
@@ -0,0 +1,61 @@
  1
+/*
  2
+ * grunt-regex-replace
  3
+ * https://github.com/bomsy/grunt-regex-replace
  4
+ *
  5
+ * Copyright (c) 2012 Hubert Boma Manilla
  6
+ * Licensed under the MIT license.
  7
+ */
  8
+
  9
+module.exports = function(grunt) {
  10
+  'use strict'
  11
+  // Please see the grunt documentation for more information regarding task and
  12
+  // helper creation: https://github.com/gruntjs/grunt/blob/master/docs/toc.md
  13
+
  14
+  // ==========================================================================
  15
+  // TASKS
  16
+  // ==========================================================================
  17
+  ;
  18
+  grunt.registerMultiTask('regex-replace', 'find & replace content of a file based regex patterns', function(){
  19
+    var files = grunt.file.expandFiles(this.file.src),
  20
+      actions = this.data.actions,
  21
+      arrString = "[object Array]",
  22
+      toString = Object.prototype.toString,
  23
+      GLOBAL = 'g',
  24
+      options = null,
  25
+      updatedContent;
  26
+      for(var i = 0; i< files.length; i++){
  27
+        if(toString.call(actions) === arrString){
  28
+          updatedContent = grunt.file.read(files[i]);
  29
+          for(var j = 0; j < actions.length; j++){
  30
+            options = actions[j].flags;
  31
+            if(typeof options === 'undefined'){
  32
+              options = GLOBAL;
  33
+            }
  34
+            if(typeof actions[j].search !== 'string' || typeof actions[j].replace !== 'string' || typeof options !== 'string' ){
  35
+              grunt.warn('An error occured while processing (Invalid type passed for \'search\' or \'replace\' of \'flags\', only strings accepted.)' );
  36
+            }
  37
+            updatedContent = grunt.helper('regex-replace', updatedContent, grunt.template.process(actions[j].search), grunt.template.process(actions[j].replace), options, j);
  38
+          }
  39
+          grunt.file.write(files[i], updatedContent);
  40
+          if(this.errorCount){
  41
+            return false;
  42
+          } 
  43
+          grunt.log.writeln('File \'' + files[i] + '\' replace complete.');
  44
+        }
  45
+      }
  46
+  });
  47
+  // ==========================================================================
  48
+  // HELPERS
  49
+  // ==========================================================================
  50
+
  51
+ grunt.registerHelper('regex-replace', function(src, regex, substr, options, index){
  52
+    //takes the sr content and changes the
  53
+    var regExp = new RegExp(regex , options),
  54
+      updatedSrc;
  55
+    updatedSrc = String(src).replace(regExp, substr);
  56
+    index = typeof index === 'undefined' ? '' : index;
  57
+    grunt.log.writeln(index + 1 + ' action(s) completed.');
  58
+    return updatedSrc;
  59
+  });
  60
+
  61
+};
34  test/regex-replace_test.js
... ...
@@ -0,0 +1,34 @@
  1
+var grunt = require('grunt');
  2
+
  3
+/*
  4
+  ======== A Handy Little Nodeunit Reference ========
  5
+  https://github.com/caolan/nodeunit
  6
+
  7
+  Test methods:
  8
+    test.expect(numAssertions)
  9
+    test.done()
  10
+  Test assertions:
  11
+    test.ok(value, [message])
  12
+    test.equal(actual, expected, [message])
  13
+    test.notEqual(actual, expected, [message])
  14
+    test.deepEqual(actual, expected, [message])
  15
+    test.notDeepEqual(actual, expected, [message])
  16
+    test.strictEqual(actual, expected, [message])
  17
+    test.notStrictEqual(actual, expected, [message])
  18
+    test.throws(block, [error], [message])
  19
+    test.doesNotThrow(block, [error], [message])
  20
+    test.ifError(value)
  21
+*/
  22
+
  23
+exports['regex-replace'] = {
  24
+  setUp: function(done) {
  25
+    // setup here
  26
+    done();
  27
+  },
  28
+  'helper': function(test) {
  29
+    test.expect(1);
  30
+    // tests here
  31
+    test.equal(grunt.helper('regex-replace'), 'regex-replace!!!', 'should return the correct value.');
  32
+    test.done();
  33
+  }
  34
+};

0 notes on commit c308bd1

Please sign in to comment.
Something went wrong with that request. Please try again.