Skip to content

Commit 051fb7e

Browse files
Merge pull request #5139 from NativeScript/vladimirov/fix-image-generation-2
fix: generate iOS images when we have their sizes
2 parents 829b836 + 60fc024 commit 051fb7e

File tree

5 files changed

+129
-4
lines changed

5 files changed

+129
-4
lines changed

lib/services/project-data-service.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,15 @@ export class ProjectDataService implements IProjectDataService {
282282
}
283283
});
284284

285-
if (!foundMatchingDefinition && image.filename) {
286-
this.$logger.warn(`Didn't find a matching image definition for file ${path.join(path.basename(dirPath), image.filename)}. This file will be skipped from reources generation.`);
285+
if (!foundMatchingDefinition) {
286+
if (image.height && image.width) {
287+
this.$logger.trace("Missing data for image", image, " in CLI's resource file, but we will try to generate images based on the size from Contents.json");
288+
finalContent.images.push(image);
289+
} else if (image.filename) {
290+
this.$logger.warn(`Didn't find a matching image definition for file ${path.join(path.basename(dirPath), image.filename)}. This file will be skipped from resources generation.`);
291+
} else {
292+
this.$logger.trace(`Unable to detect data for image generation of image`, image);
293+
}
287294
}
288295
});
289296

npm-shrinkwrap.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nativescript",
33
"preferGlobal": true,
4-
"version": "6.2.1",
4+
"version": "6.2.2",
55
"author": "Telerik <[email protected]>",
66
"description": "Command-line interface for building NativeScript projects",
77
"bin": {

resources/assets/image-definitions.json

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
{
22
"ios": {
33
"icons": [
4+
{
5+
"width": 20,
6+
"height": 20,
7+
"directory": "Assets.xcassets/AppIcon.appiconset",
8+
"filename": "[email protected]",
9+
"scale": "3x"
10+
},
11+
{
12+
"width": 20,
13+
"height": 20,
14+
"directory": "Assets.xcassets/AppIcon.appiconset",
15+
"filename": "[email protected]",
16+
"scale": "2x"
17+
},
18+
{
19+
"width": 20,
20+
"height": 20,
21+
"directory": "Assets.xcassets/AppIcon.appiconset",
22+
"filename": "icon-20.png",
23+
"scale": "1x"
24+
},
425
{
526
"width": 60,
627
"height": 60,

test/services/project-data-service.ts

+97
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,103 @@ describe("projectDataService", () => {
366366

367367
assert.deepEqual(assetStructure, { ios: emptyAssetStructure, android: _.merge(_.cloneDeep(emptyAssetStructure), { splashImages: null }) });
368368
});
369+
370+
it("generates iOS resources for files, which are not declared in image-definitions.json, but we have their size from resource's Contents.json", async () => {
371+
const defaultEmptyData: any = {};
372+
defaultEmptyData[CLIENT_NAME_KEY_IN_PROJECT_FILE] = {};
373+
const testInjector = createTestInjector(JSON.stringify(defaultEmptyData));
374+
const fs = testInjector.resolve<IFileSystem>("fs");
375+
fs.exists = () => true;
376+
fs.readJson = (filePath: string): any => {
377+
if (basename(filePath) === AssetConstants.imageDefinitionsFileName) {
378+
return {
379+
android: {},
380+
ios: {
381+
"icons": [
382+
{
383+
"width": 20,
384+
"height": 20,
385+
"directory": "Assets.xcassets/AppIcon.appiconset",
386+
"filename": "[email protected]",
387+
"scale": "3x"
388+
}]
389+
}
390+
};
391+
}
392+
393+
if (basename(filePath) === AssetConstants.iOSResourcesFileName && filePath.indexOf(AssetConstants.iOSIconsDirName) !== -1) {
394+
return {
395+
"images": [
396+
{
397+
"size": "20x20",
398+
"idiom": "iphone",
399+
"filename": "[email protected]",
400+
"scale": "2x"
401+
},
402+
{
403+
"size": "20x20",
404+
"idiom": "iphone",
405+
"filename": "[email protected]",
406+
"scale": "3x"
407+
}
408+
]
409+
};
410+
}
411+
};
412+
413+
const projectDataService = testInjector.resolve<IProjectDataService>("projectDataService");
414+
const assetStructure = await projectDataService.getAssetsStructure({ projectDir: "." });
415+
const emptyAssetStructure: IAssetGroup = {
416+
icons: {
417+
images: []
418+
},
419+
splashBackgrounds: {
420+
images: []
421+
},
422+
splashCenterImages: {
423+
images: []
424+
},
425+
splashImages: {
426+
images: []
427+
}
428+
};
429+
430+
const expectedIOSStructure = _.merge({}, emptyAssetStructure, {
431+
icons: {
432+
"images": [
433+
{
434+
"filename": "[email protected]",
435+
"height": 20,
436+
"idiom": "iphone",
437+
"scale": "2x",
438+
"size": "20x20",
439+
"width": 20
440+
},
441+
{
442+
"filename": "[email protected]",
443+
"height": 20,
444+
"idiom": "iphone",
445+
"overlayImageScale": undefined,
446+
"resizeOperation": undefined,
447+
"rgba": undefined,
448+
"scale": "3x",
449+
"size": "20x20",
450+
"width": 20
451+
}
452+
]
453+
}
454+
});
455+
456+
_.each(assetStructure.ios.icons.images, icon => {
457+
// as path is generated from the current directory, skip it from the validation
458+
delete icon.path;
459+
});
460+
461+
assert.deepEqual(assetStructure, {
462+
ios: expectedIOSStructure,
463+
android: _.merge(_.cloneDeep(emptyAssetStructure), { splashImages: null })
464+
});
465+
});
369466
});
370467

371468
describe("getAppExecutableFiles", () => {

0 commit comments

Comments
 (0)