1
- import * as path from "path" ;
2
1
import * as Jimp from "jimp" ;
3
2
import * as Color from "color" ;
4
- import { Icons , SplashScreens , Operations } from "./image-definitions" ;
5
3
import { exported } from "../../common/decorators" ;
6
4
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
+
7
15
export class AssetsGenerationService implements IAssetsGenerationService {
16
+ private get propertiesToEnumerate ( ) : any {
17
+ return {
18
+ icon : [ "icons" ] ,
19
+ splash : [ "splashBackgrounds" , "splashCenterImages" , "splashImages" ]
20
+ } ;
21
+ }
22
+
8
23
constructor ( private $logger : ILogger ,
9
- private $androidResourcesMigrationService : IAndroidResourcesMigrationService ) {
24
+ private $projectDataService : IProjectDataService ) {
10
25
}
11
26
12
27
@exported ( "assetsGenerationService" )
13
28
public async generateIcons ( resourceGenerationData : IResourceGenerationData ) : Promise < void > {
14
29
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 ) ;
16
33
this . $logger . info ( "Icons generation completed." ) ;
17
34
}
18
35
19
36
@exported ( "assetsGenerationService" )
20
37
public async generateSplashScreens ( splashesGenerationData : ISplashesGenerationData ) : Promise < void > {
21
38
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 ) ;
23
42
this . $logger . info ( "Splash screens generation completed." ) ;
24
43
}
25
44
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
+ }
52
89
}
53
90
}
54
91
}
55
92
56
- private async resize ( imagePath : string , width : number , height : number ) : Promise < Jimp > {
93
+ private async resize ( imagePath : string , width : number , height : number ) : Promise < Jimp > {
57
94
const image = await Jimp . read ( imagePath ) ;
58
95
return image . scaleToFit ( width , height ) ;
59
96
}
60
97
61
98
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.
63
100
const J = < any > Jimp ;
64
101
const backgroundColor = this . getRgbaNumber ( background ) ;
65
102
let image = new J ( width , height , backgroundColor ) ;
@@ -73,11 +110,7 @@ export class AssetsGenerationService implements IAssetsGenerationService {
73
110
image . write ( outputPath ) ;
74
111
}
75
112
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 {
81
114
const color = new Color ( colorString ) ;
82
115
const colorRgb = color . rgb ( ) ;
83
116
const alpha = Math . round ( colorRgb . alpha ( ) * 255 ) ;
0 commit comments