Skip to content

Commit d5bfec2

Browse files
chore: Improve code for getting assets to be generated
1 parent b5e3bc9 commit d5bfec2

File tree

5 files changed

+85
-88
lines changed

5 files changed

+85
-88
lines changed

lib/commands/generate-assets.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class GenerateSplashScreensCommand extends GenerateCommandBase implements
4242
super($options, $injector, $projectData, $stringParameterBuilder, $assetsGenerationService);
4343
}
4444

45-
protected async generate(imagePath: string, resourcesPath: string, background?: string): Promise<void> {
45+
protected async generate(imagePath: string, background?: string): Promise<void> {
4646
await this.$assetsGenerationService.generateSplashScreens({ imagePath, background, projectDir: this.$projectData.projectDir });
4747
}
4848
}

lib/declarations.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ interface IOptions extends ICommonOptions, IBundleString, IPlatformTemplate, IEm
463463
liveEdit: boolean;
464464
chrome: boolean;
465465
inspector: boolean; // the counterpart to --chrome
466-
background: string
466+
background: string;
467467
}
468468

469469
interface IEnvOptions {

lib/definitions/project.d.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ interface IAssetItem {
146146
interface IAssetSubGroup {
147147
images: IAssetItem[];
148148
info?: { version: string, author: string };
149+
[imageType: string]: any;
149150
}
150151

151152
interface IAssetGroup {
@@ -159,7 +160,18 @@ interface IAssetGroup {
159160
interface IAssetsStructure {
160161
ios: IAssetGroup;
161162
android: IAssetGroup;
162-
[platform: string]: IAssetGroup;
163+
}
164+
165+
interface IImageDefinitionGroup {
166+
icons: IAssetItem[];
167+
splashBackgrounds: IAssetItem[];
168+
splashCenterImages: IAssetItem[];
169+
splashImages?: IAssetItem[];
170+
}
171+
172+
interface IImageDefinitionsStructure {
173+
ios: IImageDefinitionGroup;
174+
android: IImageDefinitionGroup;
163175
}
164176

165177
/**

lib/services/assets-generation/assets-generation-service.ts

+42-55
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ export const enum Operations {
88
Resize = "resize"
99
}
1010

11-
interface IGenerateImagesData extends ISplashesGenerationData {
12-
propertiesToEnumerate: string[];
13-
}
14-
1511
export class AssetsGenerationService implements IAssetsGenerationService {
16-
private get propertiesToEnumerate(): any {
12+
private get propertiesToEnumerate(): IDictionary<string[]> {
1713
return {
1814
icon: ["icons"],
1915
splash: ["splashBackgrounds", "splashCenterImages", "splashImages"]
@@ -27,65 +23,56 @@ export class AssetsGenerationService implements IAssetsGenerationService {
2723
@exported("assetsGenerationService")
2824
public async generateIcons(resourceGenerationData: IResourceGenerationData): Promise<void> {
2925
this.$logger.info("Generating icons ...");
30-
const generationData = (<IGenerateImagesData>resourceGenerationData);
31-
generationData.propertiesToEnumerate = this.propertiesToEnumerate.icon;
32-
await this.generateImagesForDefinitions(generationData);
26+
await this.generateImagesForDefinitions(resourceGenerationData, this.propertiesToEnumerate.icon);
3327
this.$logger.info("Icons generation completed.");
3428
}
3529

3630
@exported("assetsGenerationService")
3731
public async generateSplashScreens(splashesGenerationData: ISplashesGenerationData): Promise<void> {
3832
this.$logger.info("Generating splash screens ...");
39-
const generationData = (<IGenerateImagesData>splashesGenerationData);
40-
generationData.propertiesToEnumerate = this.propertiesToEnumerate.splash;
41-
await this.generateImagesForDefinitions(generationData);
33+
await this.generateImagesForDefinitions(splashesGenerationData, this.propertiesToEnumerate.splash);
4234
this.$logger.info("Splash screens generation completed.");
4335
}
4436

45-
private async generateImagesForDefinitions(data: IGenerateImagesData): Promise<void> {
46-
data.background = data.background || "white";
47-
const assetsStructure = await this.$projectDataService.getAssetsStructure({ projectDir: data.projectDir });
48-
49-
for (const platform in assetsStructure) {
50-
if (data.platform && platform.toLowerCase() !== data.platform.toLowerCase()) {
51-
continue;
52-
}
53-
54-
const platformAssetsStructure = assetsStructure[platform];
55-
56-
for (const imageTypeKey in platformAssetsStructure) {
57-
if (data.propertiesToEnumerate.indexOf(imageTypeKey) === -1 || !platformAssetsStructure[imageTypeKey]) {
58-
continue;
59-
}
60-
61-
const imageType = platformAssetsStructure[imageTypeKey];
62-
63-
for (const assetItem of imageType.images) {
64-
if (!assetItem.filename) {
65-
continue;
66-
}
67-
68-
const operation = assetItem.resizeOperation || Operations.Resize;
69-
const scale = assetItem.scale || 0.8;
70-
const outputPath = assetItem.path;
71-
72-
switch (operation) {
73-
case Operations.OverlayWith:
74-
const imageResize = Math.round(Math.min(assetItem.width, assetItem.height) * scale);
75-
const image = await this.resize(data.imagePath, imageResize, imageResize);
76-
await this.generateImage(data.background, assetItem.width, assetItem.height, outputPath, image);
77-
break;
78-
case Operations.Blank:
79-
await this.generateImage(data.background, assetItem.width, assetItem.height, outputPath);
80-
break;
81-
case Operations.Resize:
82-
const resizedImage = await this.resize(data.imagePath, assetItem.width, assetItem.height);
83-
resizedImage.write(outputPath);
84-
break;
85-
default:
86-
throw new Error(`Invalid image generation operation: ${operation}`);
87-
}
88-
}
37+
private async generateImagesForDefinitions(generationData: ISplashesGenerationData, propertiesToEnumerate: string[]): Promise<void> {
38+
generationData.background = generationData.background || "white";
39+
const assetsStructure = await this.$projectDataService.getAssetsStructure(generationData);
40+
41+
const assetItems = _(assetsStructure)
42+
.filter((assetGroup: IAssetGroup, platform: string) => {
43+
return !generationData.platform || platform.toLowerCase() === generationData.platform.toLowerCase();
44+
})
45+
.map((assetGroup: IAssetGroup) =>
46+
_.filter(assetGroup, (assetSubGroup: IAssetSubGroup, imageTypeKey: string) =>
47+
propertiesToEnumerate.indexOf(imageTypeKey) !== -1 && !assetSubGroup[imageTypeKey]
48+
)
49+
)
50+
.flatten<IAssetSubGroup>()
51+
.map(assetSubGroup => assetSubGroup.images)
52+
.flatten<IAssetItem>()
53+
.filter(assetItem => !!assetItem.filename)
54+
.value();
55+
56+
for (const assetItem of assetItems) {
57+
const operation = assetItem.resizeOperation || Operations.Resize;
58+
const scale = assetItem.scale || 0.8;
59+
const outputPath = assetItem.path;
60+
61+
switch (operation) {
62+
case Operations.OverlayWith:
63+
const imageResize = Math.round(Math.min(assetItem.width, assetItem.height) * scale);
64+
const image = await this.resize(generationData.imagePath, imageResize, imageResize);
65+
await this.generateImage(generationData.background, assetItem.width, assetItem.height, outputPath, image);
66+
break;
67+
case Operations.Blank:
68+
await this.generateImage(generationData.background, assetItem.width, assetItem.height, outputPath);
69+
break;
70+
case Operations.Resize:
71+
const resizedImage = await this.resize(generationData.imagePath, assetItem.width, assetItem.height);
72+
resizedImage.write(outputPath);
73+
break;
74+
default:
75+
throw new Error(`Invalid image generation operation: ${operation}`);
8976
}
9077
}
9178
}

lib/services/project-data-service.ts

+28-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from "path";
22
import { ProjectData } from "../project-data";
33
import { exported } from "../common/decorators";
4-
import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER, AssetConstants, SRC_DIR, RESOURCES_DIR, MAIN_DIR, CLI_RESOURCES_DIR_NAME } from '../constants';
4+
import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER, AssetConstants, SRC_DIR, RESOURCES_DIR, MAIN_DIR, CLI_RESOURCES_DIR_NAME } from "../constants";
55

66
interface IProjectFileData {
77
projectData: any;
@@ -116,7 +116,7 @@ export class ProjectDataService implements IProjectDataService {
116116
};
117117
}
118118

119-
private getImageDefinitions(): any {
119+
private getImageDefinitions(): IImageDefinitionsStructure {
120120
const pathToImageDefinitions = path.join(__dirname, "..", "..", CLI_RESOURCES_DIR_NAME, AssetConstants.assets, AssetConstants.imageDefinitionsFileName);
121121
const imageDefinitions = this.$fs.readJson(pathToImageDefinitions);
122122

@@ -129,36 +129,34 @@ export class ProjectDataService implements IProjectDataService {
129129

130130
const imageDefinitions = this.getImageDefinitions().ios;
131131

132-
if (content && content.images) {
133-
_.each(content.images, image => {
134-
// In some cases the image may not be available, it will just be described.
135-
// When this happens, the filename will be empty.
136-
// So we'll keep the path empty as well.
137-
if (image.filename) {
138-
image.path = path.join(dirPath, image.filename);
139-
}
132+
_.each(content && content.images, image => {
133+
// In some cases the image may not be available, it will just be described.
134+
// When this happens, the filename will be empty.
135+
// So we'll keep the path empty as well.
136+
if (image.filename) {
137+
image.path = path.join(dirPath, image.filename);
138+
}
140139

141-
if (image.size) {
142-
// size is basically <width>x<height>
143-
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
144-
if (width && height) {
145-
image.width = +width;
146-
image.height = +height;
147-
}
148-
} else {
149-
// Find the image size based on the hardcoded values in the image-definitions.json
150-
_.each(imageDefinitions, (assetSubGroup: IAssetItem[]) => {
151-
_.each(assetSubGroup, assetItem => {
152-
if (assetItem.filename === image.filename && path.basename(assetItem.directory) === path.basename(dirPath)) {
153-
image.width = assetItem.width;
154-
image.height = assetItem.height;
155-
image.size = `${assetItem.width}${AssetConstants.sizeDelimiter}${assetItem.height}`;
156-
}
157-
});
158-
});
140+
if (image.size) {
141+
// size is basically <width>x<height>
142+
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
143+
if (width && height) {
144+
image.width = +width;
145+
image.height = +height;
159146
}
160-
});
161-
}
147+
} else {
148+
// Find the image size based on the hardcoded values in the image-definitions.json
149+
_.each(imageDefinitions, (assetSubGroup: IAssetItem[]) => {
150+
_.each(assetSubGroup, assetItem => {
151+
if (assetItem.filename === image.filename && path.basename(assetItem.directory) === path.basename(dirPath)) {
152+
image.width = assetItem.width;
153+
image.height = assetItem.height;
154+
image.size = `${assetItem.width}${AssetConstants.sizeDelimiter}${assetItem.height}`;
155+
}
156+
});
157+
});
158+
}
159+
});
162160

163161
return content;
164162
}

0 commit comments

Comments
 (0)