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

Commit

Permalink
Add summary to logs (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza committed Mar 2, 2020
1 parent edfadd1 commit d36c6fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ 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.4.0**
- Add summary in logs with number of tokens found and replaced ([#126](https://github.com/qetza/vsts-replacetokens-task/issues/126)).

**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)).
- Fix negative pattern support ([#127](https://github.com/qetza/vsts-replacetokens-task/issues/127)).

**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
29 changes: 26 additions & 3 deletions task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ class Logger implements ILogger {
}
}

class Counter {
public Tokens: number = 0;
public Replaced: number = 0;
public Files: number = 0;
}

var logger: ILogger = new NullLogger();
var globalCounters: Counter = new Counter();

var mapEncoding = function (encoding: string): string {
switch (encoding)
Expand Down Expand Up @@ -203,8 +210,12 @@ var replaceTokensInFile = function (
logger.debug(' using encoding: ' + encoding);

// read file and replace tokens
let localCounter: Counter = new Counter();

let content: string = iconv.decode(fs.readFileSync(filePath), encoding);
content = content.replace(regex, (match, name) => {
++localCounter.Tokens;

let value: string = tl.getVariable(name);

if (!value)
Expand All @@ -229,8 +240,13 @@ var replaceTokensInFile = function (
logger.debug(message);
}
}
else if (options.emptyValue && value === options.emptyValue)
value = '';
else
{
++localCounter.Replaced;

if (options.emptyValue && value === options.emptyValue)
value = '';
}

let escapeType: string = options.escapeType;
if (escapeType === 'auto')
Expand Down Expand Up @@ -306,8 +322,12 @@ var replaceTokensInFile = function (
};
mkdirSyncRecursive(path.dirname(path.resolve(outputPath)));

// write file
// write file & log
fs.writeFileSync(outputPath, iconv.encode(content, encoding, { addBOM: options.writeBOM, stripBOM: null, defaultEncoding: null }));
logger.info(' ' + localCounter.Replaced + ' tokens replaced out of ' + localCounter.Tokens);

globalCounters.Tokens += localCounter.Tokens;
globalCounters.Replaced += localCounter.Replaced;
}

var mapLogLevel = function (level: string): LogLevel {
Expand Down Expand Up @@ -418,8 +438,11 @@ async function run() {
}

replaceTokensInFile(filePath, outputPath, regex, options);
++globalCounters.Files;
});
});

logger.info('replaced ' + globalCounters.Replaced + ' tokens out of ' + globalCounters.Tokens + ' in ' + globalCounters.Files + ' file(s).');
}
catch (err)
{
Expand Down
4 changes: 2 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": 3,
"Patch": 1
"Minor": 4,
"Patch": 0
},
"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.1",
"version": "3.4.0",
"public": true,
"publisher": "qetza",
"targets": [
Expand Down

0 comments on commit d36c6fe

Please sign in to comment.