Skip to content

Commit

Permalink
v6: Fix win32 paths in sources (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza committed Mar 15, 2024
1 parent 3d47a23 commit 6349de2
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## 5.0.3
Task 6.0.3
- Fix minimum agent requirement to `2.206.1` ([#13](https://github.com/qetza/replacetokens-task/issues/13)).
- Fix paths in sources incompatible with `fast-glob` syntax on win32 ([#16](https://github.com/qetza/replacetokens-task/issues/16)).

## 5.0.2
Task 6.0.2
Expand Down
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -10,7 +10,8 @@ Please refer to the [release page](https://github.com/qetza/replacetokens-task/r
The task was completely rewritten to use the npm package [@qetza/replacetokens](https://www.npmjs.com/package/@qetza/replacetokens) and be more similar with the new [ReplaceTokens GitHub Actions](https://github.com/marketplace/actions/replacetokens):
- support only node 16 (mininum agent version 2.206.1)
- updated to [fast-glob](https://github.com/mrmlnc/fast-glob) for glob pattern
- renamed input _targetFiles_ to _sources_
- renamed input _targetFiles_ to _sources_
- migrated to `fast-glob` for glob patterns causing syntax changes (must use forward slash (`/`) for directory separator whatever the OS)
- removed support for comma-separated paths in _targetFiles_
- renamed _encoding_ value `win1252` to `windows1252`
- renamed _escapeType_ to _escape_
Expand Down Expand Up @@ -57,7 +58,8 @@ The task was completely rewritten to use the npm package [@qetza/replacetokens](
# A multiline list of files to replace tokens in.
# Each line supports:
# - multiple glob patterns separated by a semi-colon ';' using fast-glob syntax
# (you must always use forward slash '/' as a directory separator)
# (you must always use forward slash '/' as a directory separator, on win32 will
# automatically replace backslash with forward slash)
# - outputing the result in another file adding the output path after an arrow '=>'
# (if the output path is a relative path, it will be relative to the input file)
# - wildcard replacement in the output file name using an asterix '*' in the input
Expand Down
1 change: 1 addition & 0 deletions tasks/ReplaceTokensV6/CHANGELOG.md
@@ -1,6 +1,7 @@
# Changelog
## 6.0.3
- Fix minimum agent requirement to `2.206.1` ([#13](https://github.com/qetza/replacetokens-task/issues/13)).
- Fix paths in sources incompatible with `fast-glob` syntax on win32 ([#16](https://github.com/qetza/replacetokens-task/issues/16)).

## 6.0.2
- Add aliases for renamed inputs to ease upgrade ([#11](https://github.com/qetza/replacetokens-task/issues/11)).
Expand Down
6 changes: 4 additions & 2 deletions tasks/ReplaceTokensV6/README.md
Expand Up @@ -10,7 +10,8 @@ Please refer to the [release page](https://github.com/qetza/replacetokens-task/r
The task was completely rewritten to use the npm package [@qetza/replacetokens](https://www.npmjs.com/package/@qetza/replacetokens) and be more similar with the new [ReplaceTokens GitHub Actions](https://github.com/marketplace/actions/replacetokens):
- support only node 16 (mininum agent version 2.206.1)
- updated to [fast-glob](https://github.com/mrmlnc/fast-glob) for glob pattern
- renamed input _targetFiles_ to _sources_
- renamed input _targetFiles_ to _sources_
- migrated to `fast-glob` for glob patterns causing syntax changes (must use forward slash (`/`) for directory separator whatever the OS)
- removed support for comma-separated paths in _targetFiles_
- renamed _encoding_ value `win1252` to `windows1252`
- renamed _escapeType_ to _escape_
Expand Down Expand Up @@ -57,7 +58,8 @@ The task was completely rewritten to use the npm package [@qetza/replacetokens](
# A multiline list of files to replace tokens in.
# Each line supports:
# - multiple glob patterns separated by a semi-colon ';' using fast-glob syntax
# (you must always use forward slash '/' as a directory separator)
# (you must always use forward slash '/' as a directory separator, on win32 will
# automatically replace backslash with forward slash)
# - outputing the result in another file adding the output path after an arrow '=>'
# (if the output path is a relative path, it will be relative to the input file)
# - wildcard replacement in the output file name using an asterix '*' in the input
Expand Down
19 changes: 16 additions & 3 deletions tasks/ReplaceTokensV6/index.ts
Expand Up @@ -6,6 +6,7 @@ import * as rt from '@qetza/replacetokens';
import stripJsonComments from './strip-json-comments';
import * as telemetry from './telemetry';
import { SpanStatusCode } from '@opentelemetry/api';
import * as os from 'os';

async function run() {
const _debug = console.debug;
Expand All @@ -32,9 +33,7 @@ async function run() {

try {
// read and validate inputs
const sources = tl.getDelimitedInput('targetFiles', /\r?\n/);
if (sources.length === 0) throw new Error('Input required: sources');

const sources = getSources();
const options: rt.Options = {
addBOM: tl.getBoolInput('writeBOM'),
encoding: tl.getInput('encoding') || rt.Encodings.Auto,
Expand Down Expand Up @@ -189,6 +188,20 @@ async function run() {
}
}

var getSources = function (): string[] {
const sources = tl.getDelimitedInput('targetFiles', /\r?\n/);
if (sources.length === 0) throw new Error('Input required: sources');

// make sources compatible with fast-glob on win32
if (os.platform() === 'win32') {
for (var i in sources) {
sources[i] = sources[i].replace(/\\/g, '/').replace(/\/\/+/g, '/');
}
}

return sources;
};

var getChoiceInput = function (name: string, choices: string[], alias?: string): string {
alias = alias || name;
const input = tl.getInput(name)?.trim();
Expand Down
24 changes: 23 additions & 1 deletion tasks/ReplaceTokensV6/tests/L0.ts
@@ -1,10 +1,10 @@
import * as path from 'path';
import * as ttm from 'azure-pipelines-task-lib/mock-test';
import * as os from 'os';

require('chai').should();

const data = path.join(__dirname, '..', '..', 'tests', '_data');
const tmp = path.join(__dirname, '_tmp');

describe('ReplaceTokens v6 L0 suite', function () {
this.timeout(10000);
Expand Down Expand Up @@ -288,6 +288,28 @@ describe('ReplaceTokens v6 L0 suite', function () {
}
});

it('sources: normalize', async () => {
// arrange
const tp = path.join(__dirname, 'L0_Run.js');
const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);

process.env['__sources__'] = 'D:\\a\\1\\s/test.json';

// act
await tr.runAsync();

// assert
runValidations(() => {
tr.succeeded.should.be.true;

if (os.platform() === 'win32') {
tr.stdout.should.include('sources: ["D:/a/1/s/test.json"]');
} else {
tr.stdout.should.include('sources: ["D:\\\\a\\\\1\\\\s/test.json"]');
}
}, tr);
});

it('addBOM', async () => {
// arrange
const tp = path.join(__dirname, 'L0_Run.js');
Expand Down

0 comments on commit 6349de2

Please sign in to comment.