Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extended on the work done by Scotty Eckenthal (@scottyeck):
- extended the test coverage, including some edge cases - `options.replaceContent` now is the `options.replace` setting itself: that option now accepts the search string as it was before by `options.replaceContent`. - augmented the replace abilities of `grunt-banner`: + `options.position`: when set to `replace`, this *implies* `options.replace: true` unless that option has explicitly been set by the user already (see below) + when replacement fails, i.e. no existing banner could be spotted, then `grunt-banner` falls back to regular non-replacement behaviour. In short: `grunt-banner` will always either *replace* or *add* a banner! This fixes the edge condition in the previous work where `position: replace` would *require* a pre-existing banner or else *fail to add* a banner. + `options.replace` parameter now can be: + `false` (default) - do not look for existing banners; simply add the banner at the specified position (top/bottom). + `true` - 'smart' replace mode: use the built-in 'smart' locate-and-mark scanner to dig out the existing banners (more on the rules what maketh a banner below) + (string) - replace any part of the source code which matches this *implicit regex*. This means most strings are matched as-is, but do not get mistaken about this: dot `.`, star `*` et al will not be the *literal characters* you might have expected, but are treated as regex operators! E.g. `replace: "/* blurb */"` will **not** work as a literal string, as the stars `*` in there will make it match lines like `// blurb //` but **will not** match an actual C-style comment line `/* blurb */`; you will need to specify the proper regex for that instead: `replace: "\/\* blurb \*\/"`. + (RegExp) - a rexexp instance to match against. Otherwise the same as for the (string) type value above. + (function) - provide your own callback method to locate and mark the input. The interface for the callback function is: ``` function (fileContents, newBanner, insertPositionMarker, src, options) ``` which should *return* the marked `fileContents`, i.e. the `fileContents` with all banner eligible for replacement removed and replaced by a simple `insertPositionMarker` string (see below). Your locate-and-match callback may insert multiple markers or none: `grunt-banner` will check how many markers you injected and either replace them when one(1) or more markers are seen, or revert to its basic `top|bottom` position-based banner *insertion* process when zero markers are found in your returned result. The callback function parameters: + `fileContents` (string) - the contents of the `src` file. + `newBanner` (string) - the new banner to be inserted by `grunt-banner`. This (and the `options` parameter, see below) allows you to customize `grunt-banner` behaviour to an extreme degree, even providing your own custom *replacer* entirely: simply return your processed result with a single marker and reduce the `options.banner` to an empty string. But I digress... + `insertPositionMarker` (string) - the insert marker. Currently this is the Unicode `REPLACEMENT CHARACTER` character, i.e. `\uFFFD`. We *assume* your original file content does not contain this marker already. + `src` (string) - the path to the file being processed. + `options` (object reference) - a *reference* to the current `options` object as used by `grunt-banner`. This **is not** the same as the options object you provided through your `Gruntfile`; this is a reference to the updated/augmented clone of that original as used by `grunt-banner` internally. Though the following coding practice should be frowned upon as 'side effects' are generally undesirable, you *can* tweak the `options.banner` value to suit your custom needs, for example. **Tread with great care when you are about to *edit* this object! The fact that you *can* doesn't mean you *should* fiddle with it!** ## The default locate-and-mark functionality The default locate-and-mark process, invoked when you specify the `replace: true` option or `position: "replace"` without any `replace:` value to go with that one, is set up to locate copyright comment chunks in either C or C++ style format, i.e. surrounded by `/*....*/` or single- or multiline `//` comment chunks. The process will inspect each comment chunk which start at the **left edge** (hence we ignore all *indented* comment chunks!) and which span *entire* lines, hence ruling out any comments which are leading or trailing source code statements *on the same line*. The last restriction placed on any 'old' banner to replace is that it **must** have the (case-**in**sensitive) word `Copyright` in there somewhere. And that word **must** be followed by a bit of non-whitespace blurb on the same line: generally a year, a name or both suffices to satisfy this last condition. Any such 'banner' block is marked for replacement in its entirety. **Warning Note**: The replacer *does not* check if the *new* banner also includes the `Copyright` phrase, hence multiple applications of `grunt-banner` may lead to the later rounds of `grunt-banner` application *adding* the shiny new banner at the top (or bottom) of the sourcefile!
- Loading branch information