Skip to content

fix(assets): Fix asset genration #3485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/common
Submodule common updated 0 files
2 changes: 1 addition & 1 deletion lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ interface IAssetItem {
height: number;
filename: string;
directory: string;
scale: number;
scale: string;
idiom: string;
resizeOperation?: string;
}
Expand Down
18 changes: 13 additions & 5 deletions lib/services/assets-generation/assets-generation-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Jimp from "jimp";
import * as Color from "color";
import { exported } from "../../common/decorators";
import { AssetConstants } from "../../constants";

export const enum Operations {
OverlayWith = "overlayWith",
Expand Down Expand Up @@ -39,12 +40,12 @@ export class AssetsGenerationService implements IAssetsGenerationService {
const assetsStructure = await this.$projectDataService.getAssetsStructure(generationData);

const assetItems = _(assetsStructure)
.filter((assetGroup: IAssetGroup, platform: string) => {
return !generationData.platform || platform.toLowerCase() === generationData.platform.toLowerCase();
})
.filter((assetGroup: IAssetGroup, platform: string) =>
!generationData.platform || platform.toLowerCase() === generationData.platform.toLowerCase()
)
.map((assetGroup: IAssetGroup) =>
_.filter(assetGroup, (assetSubGroup: IAssetSubGroup, imageTypeKey: string) =>
propertiesToEnumerate.indexOf(imageTypeKey) !== -1 && !assetSubGroup[imageTypeKey]
assetSubGroup && propertiesToEnumerate.indexOf(imageTypeKey) !== -1 && assetSubGroup[imageTypeKey]
)
)
.flatten<IAssetSubGroup>()
Expand All @@ -55,7 +56,14 @@ export class AssetsGenerationService implements IAssetsGenerationService {

for (const assetItem of assetItems) {
const operation = assetItem.resizeOperation || Operations.Resize;
const scale = assetItem.scale || 0.8;
let tempScale: number = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case when assetItem.scale is a number, scale will always be 0.8 and assetItem.scale will be disregarded. Is it the desired behaviour?

If not it should be let tempScale = assetItem.scale

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks , I've fixed it in the code below

if (assetItem.scale && !_.isNumber(assetItem.scale)) {
const splittedElements = `${assetItem.scale}`.split(AssetConstants.sizeDelimiter);
tempScale = splittedElements && splittedElements.length && splittedElements[0] && +splittedElements[0];
}

const scale = tempScale || 0.8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scale must be applied to all resize operations.


const outputPath = assetItem.path;

switch (operation) {
Expand Down
43 changes: 25 additions & 18 deletions lib/services/project-data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,32 @@ export class ProjectDataService implements IProjectDataService {
image.path = path.join(dirPath, image.filename);
}

if (image.size) {
// size is basically <width>x<height>
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
if (width && height) {
image.width = +width;
image.height = +height;
}
} else {
// Find the image size based on the hardcoded values in the image-definitions.json
_.each(imageDefinitions, (assetSubGroup: IAssetItem[]) => {
_.each(assetSubGroup, assetItem => {
if (assetItem.filename === image.filename && path.basename(assetItem.directory) === path.basename(dirPath)) {
image.width = assetItem.width;
image.height = assetItem.height;
image.size = `${assetItem.width}${AssetConstants.sizeDelimiter}${assetItem.height}`;
// Find the image size based on the hardcoded values in the image-definitions.json
_.each(imageDefinitions, (assetSubGroup: IAssetItem[]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion (not a merge stopper), not sure if it is working correctly.

_(imageDefinitions)
				.filter((assetSubGroup: IAssetItem[]) => _.find(assetSubGroup, assetElement => assetElement.filename === image.filename && path.basename(assetElement.directory) === path.basename(dirPath)))
				.map((assetItem: IAssetItem) => {
					if (image.size) {
						// size is basically <width>x<height>
						const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
						if (width && height) {
							image.width = +width;
							image.height = +height;
						}
					}

					if (!image.width || !image.height) {
						image.width = assetItem.width;
						image.height = assetItem.height;
						image.size = image.size || `${assetItem.width}${AssetConstants.sizeDelimiter}${assetItem.height}`;
					}
				});

const assetItem = _.find(assetSubGroup, assetElement =>
assetElement.filename === image.filename && path.basename(assetElement.directory) === path.basename(dirPath)
);

if (assetItem) {
if (image.size) {
// size is basically <width>x<height>
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
if (width && height) {
image.width = +width;
image.height = +height;
}
});
});
}
}

if (!image.width || !image.height) {
image.width = assetItem.width;
image.height = assetItem.height;
image.size = image.size || `${assetItem.width}${AssetConstants.sizeDelimiter}${assetItem.height}`;
}

// break each
return false;
}
});
});

return content;
Expand Down