Skip to content

Commit

Permalink
Checking in work in progress on grunt 4 rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
yoniholmes committed Feb 17, 2013
1 parent 7d86b29 commit 8bd2e88
Show file tree
Hide file tree
Showing 20 changed files with 301 additions and 754 deletions.
73 changes: 73 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,73 @@
/*
* grunt-text-replace
* https://github.com/Yoni/grunt-4-test
*
* Copyright (c) 2013 Jonathan Holmes
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'tasks/*.js',
'<%= nodeunit.tests %>',
],
options: {
jshintrc: '.jshintrc',
},
},

// Before generating any new files, remove any previously-created files.
clean: {
tests: ['tmp'],
},

// Configuration to be run (and then tested).
replace: {
default_options: {
options: {
},
files: {
'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123'],
},
},
custom_options: {
options: {
separator: ': ',
punctuation: ' !!!',
},
files: {
'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123'],
},
},
},

// Unit tests.
nodeunit: {
tests: ['test/*-test.js'],
},

});

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

// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');

// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean', 'replace', 'nodeunit']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);

};
2 changes: 1 addition & 1 deletion LICENSE-MIT
@@ -1,4 +1,4 @@
Copyright (c) 2012 Jonathan Holmes
Copyright (c) 2013 Jonathan Holmes

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
228 changes: 69 additions & 159 deletions README.md
@@ -1,185 +1,95 @@
# grunt-text-replace [!['Build status'][travis_image_url]][travis_page_url]
# grunt-text-replace

[travis_image_url]: https://api.travis-ci.org/yoniholmes/grunt-text-replace.png
[travis_page_url]: https://travis-ci.org/yoniholmes/grunt-text-replace
> FOOOOO
## Getting Started
_If you haven't used [grunt][] before, be sure to check out the [Getting Started][] guide._


General purpose text replacement for grunt.

Allows you to replace text in files using strings, regexs or functions.


## Installation
In your project's [gruntfile][getting_started] directory, run:
From the same directory as your project's [Gruntfile][Getting Started] and [package.json][], install this plugin with the following command:

```bash
npm install grunt-text-replace
npm install grunt-text-replace --save-dev
```

Then add this line to your project's [gruntfile][getting_started]:
Once that's done, add this line to your project's Gruntfile:

```javascript
```js
grunt.loadNpmTasks('grunt-text-replace');
```

[grunt]: http://gruntjs.com/
[getting_started]: https://github.com/gruntjs/grunt/blob/master/docs/getting_started.md


## Usage


```javascript
replace: {
example: {
src: ['text/*.txt'], // source files array (supports minimatch)
dest: 'build/text/', // destination directory or file
replacements: [{
from: 'Red', // string replacement
to: 'Blue'
}, {
from: /(f|F)(o{2,100})/g, // regex replacement ('Fooo' to 'Mooo')
to: 'M$2'
}, {
from: 'Foo',
to: function (matchedWord) { // callback replacement
return matchedWord + ' Bar';
}
}]
}
}
```
If the plugin has been installed correctly, running `grunt --help` at the command line should list the newly-installed plugin's task or tasks. In addition, the plugin should be listed in package.json as a `devDependency`, which ensures that it will be installed whenever the `npm install` command is run.

Here's another example using [grunt.template][grunt.template], and overwriting
original source files:

```javascript
replace: {
another_example: {
src: ['build/*.html'],
overwrite: true, // overwrite matched source files
replacements: [{
from: /[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}/g,
to: "<%= grunt.template.today('dd/mm/yyyy') %>"
}]
}
}
[grunt]: http://gruntjs.com/
[Getting Started]: https://github.com/gruntjs/grunt/blob/devel/docs/getting_started.md
[package.json]: https://npmjs.org/doc/json.html

## The "text_replace" task

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

```js
grunt.initConfig({
text_replace: {
options: {
// Task-specific options go here.
},
your_target: {
// Target-specific file lists and/or options go here.
},
},
})
```

### Options

#### options.separator
Type: `String`
Default value: `', '`

## API reference


### replace

*replace* is the top level task that goes in your `grunt.initConfig({})`. It is
a [multi-task][multitask], meaning that it must contain targets, which you can
name anything you like.

[multitask]: https://github.com/gruntjs/grunt/blob/master/docs/api.md#gruntregistermultitask


### src

*src* is an array of source files to be replaced, and is required.
It supports [minimatch][minimatch] paths.

[minimatch]: https://github.com/isaacs/minimatch


### dest

*dest* is the destination for files to be replaced, and can refer to either a:

- file: `'path/output.txt'`
- directory: `'path/'`

grunt-text-replace will throw an error if multiple source files are mapped to
a single file.


A string value that is used to do something with whatever.

### overwrite
#### options.punctuation
Type: `String`
Default value: `'.'`

*overwrite* is used if all you need to do is overwrite existing files.
To use it, omit *dest*, otherwise
grunt-text-replace will throw an error. You can only use one or the other.
A string value that is used to do something else with whatever else.

### Usage Examples

### replacements
#### Default Options
In this example, the default options are used to do something with whatever. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result would be `Testing, 1 2 3.`

*replacements* is an array of *from* and *to* replacements. See the
[examples](#usage) above.


### from

*from* is the old text that you'd like replace. It can be a:

- plain string: `'Red'` *matches all instances of 'Red' in file*
- regular expression object: `/Red/g` *same as above*


### to

*to* is the replacement. It can be a:

- plain string
- string containing a [grunt.template][grunt.template]
- string containing regex variables `$1`, `$2`, etc
- combination of the above
- function where the return value will be used as the replacement text (supports
[grunt.template][grunt.template])

#### function
Where *to* is a function, the function receives 4 parameters:

1. **matchedWord**: the matched word
2. **index**: an integer representing point where word was found in a text
3. **fullText**: the full original text
4. **regexMatches**: an array containing all regex matches, empty if none
defined or found.


```javascript
// Where the original source file text is: "Hello world"

replacements: [{
from: /wor(ld)/g,
to: function (matchedWord, index, fullText, regexMatches) {
// matchedWord: "world"
// index: 6
// fullText: "Hello world"
// regexMatches: ["ld"]
return 'planet'; //
}
}]

// The new text will now be: "Hello planet"
```js
grunt.initConfig({
text_replace: {
options: {},
files: {
'dest/default_options': ['src/testing', 'src/123'],
},
},
})
```

#### Custom Options
In this example, custom options are used to do something else with whatever else. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result in this case would be `Testing: 1 2 3 !!!`

```js
grunt.initConfig({
text_replace: {
options: {
separator: ': ',
punctuation: ' !!!',
},
files: {
'dest/default_options': ['src/testing', 'src/123'],
},
},
})
```

[grunt.template]: https://github.com/gruntjs/grunt/blob/master/docs/api_template.md

## 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][].

## Release History
- v0.2.10 - 2012/12/21. Minor internal refactor to better support globally installed instances of grunt on some systems.
- v0.2.9 - 2012/11/26. Fixed issue where overwrite: true was not working where multiple src files were defined.
- v0.2.7 - 2012/11/25. Fixed issue where replacing a string globally would fail
if regex characters were present in string. This is no longer a problem.
- v0.2.5 - 2012/11/23. Function replacements now support grunt.template.
- v0.2.0 - 2012/11/21. Added tests, refactored internals, strings now replace
globally within a file, updated documentation.
- v0.1.0 - 2012/11/12. Initial release.

Patch releases will generally remain undocumented in this release history.
I'll do so if there's enough reason for it, such as a functionality tweak, or
significant bug fix. For more detail see the source.



## License
Copyright (c) 2012 Jonathan Holmes
Licensed under the MIT license.
_(Nothing yet)_
2 changes: 0 additions & 2 deletions bin/grunt-text-replace

This file was deleted.

0 comments on commit 8bd2e88

Please sign in to comment.