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

Commit

Permalink
Fix negative pattern (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza committed Dec 9, 2019
1 parent 3f20bb0 commit edfadd1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ Parameters include (in parenthesis the yaml name):
> - `*.tokenized.config => *.config` will replace tokens in all _{filename}.tokenized.config_ target files and save the result in _{filename}.config_.
> - `**\*.tokenized.config => c:\tmp\*.config` will replace tokens in all _{filename}.tokenized.config_ target files and save the result in _c:\tmp\\{filename}.config_.
>
> Only the wildcard _*_ in the target file name will be used for replacement in the output.
> Relative paths in the output pattern are relative to the target file path.
> Only the wildcard _*_ in the target file name will be used for replacement in the output.\
> Relative paths in the output pattern are relative to the target file path.\
>
> **Negative pattern**\
> If you want to use negative pattern in target file, use a semi-colon `;` to separate the including pattern and the negative patterns. When using output syntax, only the wildcard in the first pattern will be used for generating the output path.
> - `**\*.tokenized.config;!**\dev\*.config => c:\tmp\*.config` will replace tokens in all _{filename}.tokenized.config_ target files except those under a _dev_ directory and save the result in _c:\tmp\\{filename}.config_.

- **Files encoding** (encoding): the files encoding used for reading and writing. The 'auto' value will determine the encoding based on the Byte Order Mark (BOM) if present; otherwise it will use ascii.
- **Write unicode BOM** (writeBOM): if checked writes an unicode Byte Order Mark (BOM).
- **Escape type** (escapeType): specify how to escape variable values. Value `auto` uses the file extension (`.json` and `.xml`) to determine the escaping and `none` as fallback.
Expand All @@ -57,6 +62,10 @@ 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.3.1**
- **Breaking change**: If you were using negative pattern you need to use the semi colon `;` as a separator instead of new-line in _Target files_.
- Fix negative pattern support ([#127](https://github.com/qetza/vsts-replacetokens-task/issues/122)).

**New in 3.3.0**
- Add support for custom output file and wildcard support ([#114](https://github.com/qetza/vsts-replacetokens-task/issues/114)).

Expand Down
20 changes: 10 additions & 10 deletions task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface Options {

interface Rule {
isInputWildcard: boolean,
inputPattern: string,
inputPatterns: string[],
isOutputRelative: boolean,
outputPattern: string
}
Expand Down Expand Up @@ -193,14 +193,14 @@ var replaceTokensInFile = function (
logger.info('replacing tokens in: ' + filePath);

if (filePath !== outputPath)
logger.info('output in: ' + outputPath);
logger.info(' output in: ' + outputPath);

// ensure encoding
let encoding: string = options.encoding;
if (options.encoding === ENCODING_AUTO)
encoding = getEncoding(filePath);

logger.debug('using encoding: ' + encoding);
logger.debug(' using encoding: ' + encoding);

// read file and replace tokens
let content: string = iconv.decode(fs.readFileSync(filePath), encoding);
Expand All @@ -214,7 +214,7 @@ var replaceTokensInFile = function (
else
value = '';

let message: string = 'variable not found: ' + name;
let message: string = ' variable not found: ' + name;
switch (options.actionOnMissing)
{
case ACTION_WARN:
Expand Down Expand Up @@ -251,7 +251,7 @@ var replaceTokensInFile = function (
}

// log value before escaping to show raw value and avoid secret leaks (escaped secrets are not replaced by ***)
logger.debug(name + ': ' + value);
logger.debug(' ' + name + ': ' + value);

switch (escapeType) {
case 'json':
Expand Down Expand Up @@ -302,7 +302,7 @@ var replaceTokensInFile = function (
mkdirSyncRecursive(path.dirname(p));

fs.mkdirSync(p);
logger.debug('created folder: ' + p);
logger.debug(' created folder: ' + p);
};
mkdirSyncRecursive(path.dirname(path.resolve(outputPath)));

Expand Down Expand Up @@ -361,12 +361,12 @@ async function run() {
let ruleParts: string[] = line.split('=>');
let rule: Rule = {
isInputWildcard: false,
inputPattern: normalize(ruleParts[0].trim()),
inputPatterns: normalize(ruleParts[0].trim()).split(';'),
isOutputRelative: false,
outputPattern: null
};

rule.isInputWildcard = path.basename(rule.inputPattern).indexOf('*') != -1;
rule.isInputWildcard = path.basename(rule.inputPatterns[0]).indexOf('*') != -1;

if (ruleParts.length > 1)
{
Expand All @@ -385,7 +385,7 @@ async function run() {

// process files
rules.forEach(rule => {
tl.findMatch(root, rule.inputPattern).forEach(filePath => {
tl.findMatch(root, rule.inputPatterns).forEach(filePath => {
if (tl.stats(filePath).isDirectory())
{
return;
Expand All @@ -405,7 +405,7 @@ async function run() {

if (rule.isInputWildcard)
{
let inputBasename: string = path.basename(rule.inputPattern);
let inputBasename: string = path.basename(rule.inputPatterns[0]);
let inputWildcardIndex = inputBasename.indexOf('*');
let fileBasename: string = path.basename(filePath);
let token: string = fileBasename.substring(inputWildcardIndex, fileBasename.length - (inputBasename.length - inputWildcardIndex -1));
Expand Down
2 changes: 1 addition & 1 deletion task/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 3,
"Minor": 3,
"Patch": 0
"Patch": 1
},
"instanceNameFormat": "Replace tokens in $(targetFiles)",
"minimumAgentVersion": "2.105.0",
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": "3.3.0",
"version": "3.3.1",
"public": true,
"publisher": "qetza",
"targets": [
Expand Down

0 comments on commit edfadd1

Please sign in to comment.