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 escaping characters in variable values (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza committed Mar 6, 2018
1 parent ca02c41 commit 1152554
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Parameters include:
- **Token prefix**: the prefix of the tokens to search in the target files.
- **Token suffix**: the suffix of the tokens to search in the target files.
- **Empty value**: the variable value that will be replaced with an empty string.
- **Escape character**: the escape character to use when escaping characters in the variable values.
- **Characters to escape**: characters in variable values to escape before replacing tokens.

## Tips
If you want to use tokens in XML based configuration files to be replaced during deployment and also have those files usable for local development you can combine the [Replace Tokens task](https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens) with the [XDT tranform task](https://marketplace.visualstudio.com/items?itemName=qetza.xdttransform):
Expand All @@ -31,8 +33,11 @@ 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 2.3.0**
- Add support to escape characters in variable values ([#52](https://github.com/qetza/vsts-replacetokens-task/issues/52))

**New in 2.2.1**
- Fix issue with backslash in default target files value on mac ([#50](https://github.com/qetza/vsts-replacetokens-task/issues/50)
- Fix issue with backslash in default target files value on mac ([#50](https://github.com/qetza/vsts-replacetokens-task/issues/50))

**New in 2.2.0**
- Fix issue on file not found when using network paths ([#40](https://github.com/qetza/vsts-replacetokens-task/issues/40), [#41](https://github.com/qetza/vsts-replacetokens-task/issues/41)).
Expand Down
20 changes: 18 additions & 2 deletions task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ var getEncoding = function (filePath: string): string {
}
}

var replaceTokensInFile = function (filePath: string, regex: RegExp, encoding: string, keepToken: boolean, actionOnMissing: string, writeBOM: boolean, emptyValue: string): void {
var replaceTokensInFile = function (
filePath: string,
regex: RegExp,
encoding: string,
keepToken: boolean,
actionOnMissing: string,
writeBOM: boolean,
emptyValue: string,
escapeChar: string,
charsToEscape: string): void {
console.log('replacing tokens in: ' + filePath);

// ensure encoding
Expand Down Expand Up @@ -118,6 +127,11 @@ var replaceTokensInFile = function (filePath: string, regex: RegExp, encoding: s
else if (emptyValue && value === emptyValue)
value = '';

if (escapeChar && charsToEscape)
for (var c of charsToEscape)
// split and join to avoid regex and escaping escape char
value = value.split(c).join(escapeChar + c);

return value;
});

Expand All @@ -136,6 +150,8 @@ async function run() {
let actionOnMissing: string = tl.getInput('actionOnMissing', true);
let writeBOM: boolean = tl.getBoolInput('writeBOM', true);
let emptyValue: string = tl.getInput('emptyValue', false);
let escapeChar: string = tl.getInput('escapeChar', false);
let charsToEscape: string = tl.getInput('charsToEscape', false);

let targetFiles: string[] = [];
tl.getDelimitedInput('targetFiles', '\n', true).forEach((x: string) => {
Expand All @@ -159,7 +175,7 @@ async function run() {
return;
}

replaceTokensInFile(filePath, regex, encoding, keepToken, actionOnMissing, writeBOM, emptyValue);
replaceTokensInFile(filePath, regex, encoding, keepToken, actionOnMissing, writeBOM, emptyValue, escapeChar, charsToEscape);
});
}
catch (err)
Expand Down
18 changes: 16 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": 2,
"Minor": 2,
"Patch": 1
"Minor": 3,
"Patch": 0
},
"minimumAgentVersion": "2.105.0",
"groups": [
Expand Down Expand Up @@ -119,6 +119,20 @@
"groupName": "advanced",
"required": false,
"helpMarkDown": "The variable value which will be replaced by an empty string."
},
{
"name": "escapeChar",
"type": "string",
"label": "Escape character",
"groupName": "advanced",
"helpMarkDown": "The escape character to use when escaping characters in the variable values."
},
{
"name": "charsToEscape",
"type": "string",
"label": "Characters to escape",
"groupName": "advanced",
"helpMarkDown": "Characters in variable values to escape before replacing tokens."
}
],
"instanceNameFormat": "Replace tokens in $(targetFiles)",
Expand Down
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": "2.2.1",
"version": "2.3.0",
"public": true,
"publisher": "qetza",
"targets": [
Expand Down

0 comments on commit 1152554

Please sign in to comment.