Skip to content

Commit 91e03a1

Browse files
Merge pull request #295 from Microsoft/users/stfrance/resourcefiles
Allow multiple resource files
2 parents 2ea9157 + 954f052 commit 91e03a1

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

node/internal.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function _setErrStream(errStream): void {
6666
//-----------------------------------------------------
6767

6868
let _locStringCache: { [key: string]: string } = {};
69-
let _resourceFile: string;
69+
let _resourceFiles: { [key: string]: string } = {};
7070
let _libResourceFileLoaded: boolean = false;
7171
let _resourceCulture: string = 'en-US';
7272

@@ -147,27 +147,27 @@ function _loadLocStrings(resourceFile: string, culture: string): { [key: string]
147147
*/
148148
export function _setResourcePath(path: string): void {
149149
if (process.env['TASKLIB_INPROC_UNITS']) {
150-
_resourceFile = null;
150+
_resourceFiles = {};
151151
_libResourceFileLoaded = false;
152152
_locStringCache = {};
153153
_resourceCulture = 'en-US';
154154
}
155155

156-
if (!_resourceFile) {
156+
if (!_resourceFiles[path]) {
157157
_checkPath(path, 'resource file path');
158-
_resourceFile = path;
159-
_debug('set resource file to: ' + _resourceFile);
158+
_resourceFiles[path] = path;
159+
_debug('adding resource file: ' + path);
160160

161161
_resourceCulture = _getVariable('system.culture') || _resourceCulture;
162-
var locStrs = _loadLocStrings(_resourceFile, _resourceCulture);
162+
var locStrs: { [key: string]: string; } = _loadLocStrings(path, _resourceCulture);
163163
for (var key in locStrs) {
164164
//cache loc string
165165
_locStringCache[key] = locStrs[key];
166166
}
167167

168168
}
169169
else {
170-
_warning(_loc('LIB_ResourceFileAlreadySet', _resourceFile));
170+
_warning(_loc('LIB_ResourceFileAlreadySet', path));
171171
}
172172
}
173173

@@ -196,7 +196,7 @@ export function _loc(key: string, ...param: any[]): string {
196196
locString = _locStringCache[key];
197197
}
198198
else {
199-
if (!_resourceFile) {
199+
if (Object.keys(_resourceFiles).length <= 0) {
200200
_warning(_loc('LIB_ResourceFileNotSet', key));
201201
}
202202
else {

node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vsts-task-lib",
3-
"version": "2.1.1",
3+
"version": "2.2.0",
44
"description": "VSTS Task SDK",
55
"main": "./task.js",
66
"typings": "./task.d.ts",

node/test/loctests.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import testutil = require('./testutil');
1313

1414
describe('Loc Tests', function () {
1515

16-
before(function (done) {
16+
beforeEach(function (done) {
1717
try {
1818
testutil.initialize();
1919
}
@@ -72,6 +72,54 @@ describe('Loc Tests', function () {
7272

7373
done();
7474
})
75+
it('gets loc string from second loc resources.json', function (done) {
76+
this.timeout(1000);
77+
78+
// Don't reset values each time we call setResourcesPath for this test.
79+
process.env['TASKLIB_INPROC_UNITS'] = '';
80+
81+
// Arrange
82+
var tempFolder = path.join(testutil.getTestTemp(), 'loc-str-from-loc-res-json2');
83+
shell.mkdir('-p', tempFolder);
84+
85+
// Create first task.json and resources file
86+
var jsonStr = "{\"messages\": {\"key6\" : \"string for key 6.\"}}";
87+
var jsonPath = path.join(tempFolder, 'task.json');
88+
fs.writeFileSync(jsonPath, jsonStr);
89+
90+
var tempLocFolder = path.join(tempFolder, 'Strings', 'resources.resjson', 'zh-CN');
91+
shell.mkdir('-p', tempLocFolder);
92+
var locJsonStr = "{\"loc.messages.key6\" : \"loc cn-string for key 6.\"}";
93+
var locJsonPath = path.join(tempLocFolder, 'resources.resjson');
94+
fs.writeFileSync(locJsonPath, locJsonStr);
95+
96+
// Create second task.json and resources file
97+
var nestedLocFolder = path.join(tempFolder, 'nested');
98+
shell.mkdir('-p', nestedLocFolder);
99+
100+
var jsonStr2 = "{\"messages\": {\"keySecondFile\" : \"string for keySecondFile.\"}}";
101+
var jsonPath2 = path.join(nestedLocFolder, 'task.json');
102+
fs.writeFileSync(jsonPath2, jsonStr2);
103+
104+
var tempLocFolder2 = path.join(nestedLocFolder, 'Strings', 'resources.resjson', 'zh-CN');
105+
shell.mkdir('-p', tempLocFolder2);
106+
var locJsonStr2 = "{\"loc.messages.keySecondFile\" : \"loc cn-string for keySecondFile.\"}";
107+
var locJsonPath2 = path.join(tempLocFolder2, 'resources.resjson');
108+
fs.writeFileSync(locJsonPath2, locJsonStr2);
109+
110+
process.env['SYSTEM_CULTURE'] = 'ZH-cn'; // Lib should handle casing differences for culture.
111+
112+
// Act
113+
tl.setResourcePath(jsonPath);
114+
tl.setResourcePath(jsonPath2);
115+
116+
// Assert
117+
assert.equal(tl.loc('key6'), 'loc cn-string for key 6.', 'string not found for key.');
118+
assert.equal(tl.loc('keySecondFile'), 'loc cn-string for keySecondFile.', 'string not found for keySecondFile.');
119+
120+
done();
121+
})
122+
75123
it('fallback to current string if culture resources.resjson not found', function (done) {
76124
this.timeout(1000);
77125

0 commit comments

Comments
 (0)