1- import * as path from "path" ;
21import * as Jimp from "jimp" ;
32import * as Color from "color" ;
4- import { Icons , SplashScreens , Operations } from "./image-definitions" ;
53import { exported } from "../../common/decorators" ;
64
5+ export const enum Operations {
6+ OverlayWith = "overlayWith" ,
7+ Blank = "blank" ,
8+ Resize = "resize"
9+ }
10+
11+ interface IGenerateImagesData extends ISplashesGenerationData {
12+ propertiesToEnumerate : string [ ] ;
13+ }
14+
715export class AssetsGenerationService implements IAssetsGenerationService {
16+ private get propertiesToEnumerate ( ) : any {
17+ return {
18+ icon : [ "icons" ] ,
19+ splash : [ "splashBackgrounds" , "splashCenterImages" , "splashImages" ]
20+ } ;
21+ }
22+
823 constructor ( private $logger : ILogger ,
9- private $androidResourcesMigrationService : IAndroidResourcesMigrationService ) {
24+ private $projectDataService : IProjectDataService ) {
1025 }
1126
1227 @exported ( "assetsGenerationService" )
1328 public async generateIcons ( resourceGenerationData : IResourceGenerationData ) : Promise < void > {
1429 this . $logger . info ( "Generating icons ..." ) ;
15- await this . generateImagesForDefinitions ( resourceGenerationData . imagePath , resourceGenerationData . resourcesPath , Icons , resourceGenerationData . platform ) ;
30+ const generationData = ( < IGenerateImagesData > resourceGenerationData ) ;
31+ generationData . propertiesToEnumerate = this . propertiesToEnumerate . icon ;
32+ await this . generateImagesForDefinitions ( generationData ) ;
1633 this . $logger . info ( "Icons generation completed." ) ;
1734 }
1835
1936 @exported ( "assetsGenerationService" )
2037 public async generateSplashScreens ( splashesGenerationData : ISplashesGenerationData ) : Promise < void > {
2138 this . $logger . info ( "Generating splash screens ..." ) ;
22- await this . generateImagesForDefinitions ( splashesGenerationData . imagePath , splashesGenerationData . resourcesPath , SplashScreens , splashesGenerationData . platform , splashesGenerationData . background ) ;
39+ const generationData = ( < IGenerateImagesData > splashesGenerationData ) ;
40+ generationData . propertiesToEnumerate = this . propertiesToEnumerate . splash ;
41+ await this . generateImagesForDefinitions ( generationData ) ;
2342 this . $logger . info ( "Splash screens generation completed." ) ;
2443 }
2544
26- private async generateImagesForDefinitions ( imagePath : string , resourcesPath : string , definitions : any [ ] , platform ?: string , background : string = "white" ) : Promise < void > {
27- const hasMigrated = this . $androidResourcesMigrationService . hasMigrated ( resourcesPath ) ;
28-
29- const filteredDefinitions = platform ? _ . filter ( definitions , definition => _ . toLower ( definition . platform ) === _ . toLower ( platform ) ) : definitions ;
30-
31- for ( const definition of filteredDefinitions ) {
32- const operation = definition . operation || Operations . Resize ;
33- const scale = definition . scale || 0.8 ;
34- const path = hasMigrated ? definition . path : ( definition . pathBeforeMigration || definition . path ) ;
35- const outputPath = this . convertToAbsolutePath ( resourcesPath , path ) ;
36-
37- switch ( operation ) {
38- case Operations . OverlayWith :
39- const imageResize = Math . round ( Math . min ( definition . width , definition . height ) * scale ) ;
40- const image = await this . resize ( imagePath , imageResize , imageResize ) ;
41- await this . generateImage ( background , definition . width , definition . height , outputPath , image ) ;
42- break ;
43- case Operations . Blank :
44- await this . generateImage ( background , definition . width , definition . height , outputPath ) ;
45- break ;
46- case Operations . Resize :
47- const resizedImage = await this . resize ( imagePath , definition . width , definition . height ) ;
48- resizedImage . write ( outputPath ) ;
49- break ;
50- default :
51- throw new Error ( `Invalid image generation operation: ${ operation } ` ) ;
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+ }
5289 }
5390 }
5491 }
5592
56- private async resize ( imagePath : string , width : number , height : number ) : Promise < Jimp > {
93+ private async resize ( imagePath : string , width : number , height : number ) : Promise < Jimp > {
5794 const image = await Jimp . read ( imagePath ) ;
5895 return image . scaleToFit ( width , height ) ;
5996 }
6097
6198 private generateImage ( background : string , width : number , height : number , outputPath : string , overlayImage ?: Jimp ) : void {
62- //Typescript declarations for Jimp are not updated to define the constructor with backgroundColor so we workaround it by casting it to <any> for this case only.
99+ // Typescript declarations for Jimp are not updated to define the constructor with backgroundColor so we workaround it by casting it to <any> for this case only.
63100 const J = < any > Jimp ;
64101 const backgroundColor = this . getRgbaNumber ( background ) ;
65102 let image = new J ( width , height , backgroundColor ) ;
@@ -73,11 +110,7 @@ export class AssetsGenerationService implements IAssetsGenerationService {
73110 image . write ( outputPath ) ;
74111 }
75112
76- private convertToAbsolutePath ( resourcesPath : string , resourcePath : string ) {
77- return path . isAbsolute ( resourcePath ) ? resourcePath : path . join ( resourcesPath , resourcePath ) ;
78- }
79-
80- private getRgbaNumber ( colorString : string ) : number {
113+ private getRgbaNumber ( colorString : string ) : number {
81114 const color = new Color ( colorString ) ;
82115 const colorRgb = color . rgb ( ) ;
83116 const alpha = Math . round ( colorRgb . alpha ( ) * 255 ) ;
0 commit comments