Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
Add support for legacy pattern (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza committed Apr 17, 2020
1 parent f60ddbe commit 8c08a00
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Parameters include (in parenthesis the yaml name):
- **Keep token** (keepToken): if checked tokens with missing variables will not be replaced by empty string.
- **Token prefix** (tokenPrefix): the prefix of the tokens to search in the target files.
- **Token suffix** (tokenSuffix): the suffix of the tokens to search in the target files.
- **Use legacy pattern** (useLegacyPattern): if checked whitespaces between the token prefix/suffix and the variable name are not ignored.
- **Empty value** (emptyValue): the variable value that will be replaced with an empty string.
- **Variable files (JSON)** (variableFiles): the absolute or relative comma or newline-separated paths to the files containing additional variables. Wildcards can be used (eg: `vars\**\*.json` for all _.json_ files in all sub folders of _vars_). Variables declared in files overrides variables defined in the pipeline.
- **Variable separator** (variableSeparator): the separtor to use in variable names for nested objects and arrays in variable files. Example: `{ 'My': { 'Value': ['Hello World!'] } }` will create a variable _My.Value.0_ with the value _Hello World!_.
Expand All @@ -68,6 +69,9 @@ If you want to use tokens in XML based configuration files to be replaced during
- replace tokens in your updated configuration file

## Release notes
**New in 3.6.0**
- Add parameter _Use legacy pattern_ with default value to `false`.

**New in 3.5.2**
- Fix issue when token prefix present but not as a token prefix ([#149](https://github.com/qetza/vsts-replacetokens-task/issues/149)).

Expand Down
7 changes: 6 additions & 1 deletion task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ async function run() {
let root: string = tl.getPathInput('rootDirectory', false, true);
let tokenPrefix: string = tl.getInput('tokenPrefix', true);
let tokenSuffix: string = tl.getInput('tokenSuffix', true);
let useLegacyPattern: boolean = tl.getBoolInput('useLegacyPattern', true);
let options: Options = {
encoding: mapEncoding(tl.getInput('encoding', true)),
keepToken: tl.getBoolInput('keepToken', true),
Expand Down Expand Up @@ -504,7 +505,10 @@ async function run() {
// initialize task
let escapedTokenPrefix: string = tokenPrefix.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
let escapedTokenSuffix: string = tokenSuffix.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
let regex: RegExp = new RegExp(escapedTokenPrefix + '\\s*((?:(?!' + escapedTokenPrefix + ')(?!\\s*' + escapedTokenSuffix + ').)*)\\s*' + escapedTokenSuffix, 'gm');
let pattern = useLegacyPattern
? escapedTokenPrefix + '((?:(?!' + escapedTokenSuffix + ').)*)' + escapedTokenSuffix
: escapedTokenPrefix + '\\s*((?:(?!' + escapedTokenPrefix + ')(?!\\s*' + escapedTokenSuffix + ').)*)\\s*' + escapedTokenSuffix;
let regex: RegExp = new RegExp(pattern, 'gm');
logger.debug('pattern: ' + regex.source);

// set telemetry data
Expand All @@ -527,6 +531,7 @@ async function run() {
telemetryEvent.variableSeparator = variableSeparator;
telemetryEvent.verbosity = options.verbosity;
telemetryEvent.writeBOM = options.writeBOM;
telemetryEvent.useLegacyPattern = useLegacyPattern;

// process files
rules.forEach(rule => {
Expand Down
13 changes: 11 additions & 2 deletions task/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"author": "Guillaume Rouchon",
"version": {
"Major": 3,
"Minor": 5,
"Patch": 2
"Minor": 6,
"Patch": 0
},
"instanceNameFormat": "Replace tokens in $(targetFiles)",
"minimumAgentVersion": "2.105.0",
Expand Down Expand Up @@ -154,6 +154,15 @@
"required": true,
"helpMarkDown": "The suffix of the tokens to search in the target files."
},
{
"name": "useLegacyPattern",
"type": "boolean",
"label": "Use legacy pattern",
"defaultValue": "false",
"groupName": "advanced",
"required": true,
"helpMarkDown": "If checked whitespaces between the token prefix/suffix and the variable name are not ignored."
},
{
"name": "emptyValue",
"type": "string",
Expand Down
4 changes: 3 additions & 1 deletion task/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export default function trackEvent(event: TelemetryEvent, proxyUrl?: string): st
duration: event.duration,
tokenReplaced: event.tokenReplaced,
tokenFound: event.tokenFound,
fileProcessed: event.fileProcessed
fileProcessed: event.fileProcessed,
useLegacyPattern: event.useLegacyPattern
}
}
}
Expand Down Expand Up @@ -156,4 +157,5 @@ export interface TelemetryEvent {
tokenReplaced: number;
tokenFound: number;
fileProcessed: number;
useLegacyPattern: boolean;
}
2 changes: 1 addition & 1 deletion vss-extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifestVersion": 1,
"id": "replacetokens",
"name": "Replace Tokens",
"version": "3.5.2",
"version": "3.6.0",
"public": true,
"publisher": "qetza",
"targets": [
Expand Down

0 comments on commit 8c08a00

Please sign in to comment.