Skip to content

Commit

Permalink
Fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanovit committed Mar 14, 2018
1 parent d48cff2 commit 18398b8
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 55 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
// "args": [ "livesync", "android", "--path", "cliapp"]
// "args": [ "livesync", "android", "--watch", "--path", "cliapp"],
"args": [ "resources", "generate", "icons", "./test/image-generation-test.png", "--path", "cliapp" ],
// "args": [ "generate-splashscreens", "./test/image-generation-test.png", "--path", "cliapp", "--background", "black" ],
"autoAttachChildProcesses": true
// "args": [ "resources", "generate", "splashes", "./test/image-generation-test.png", "--path", "cliapp", "--background", "#8000ff" ],
},
{
// in case you want to debug a single test, modify it's code to be `it.only(...` instead of `it(...`
Expand Down
2 changes: 1 addition & 1 deletion lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,4 @@ $injector.require("nativescriptCloudExtensionService", "./services/nativescript-

$injector.requireCommand("resources|generate|icons", "./commands/generate-assets")
$injector.requireCommand("resources|generate|splashes", "./commands/generate-assets")
$injector.requirePublic("assetsGenerationService", "./services/assets-generation/assets-generation-service");
$injector.requirePublic("assetsGenerationService", "./services/assets-generation/assets-generation-service");
8 changes: 4 additions & 4 deletions lib/commands/generate-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export abstract class GenerateCommandBase implements ICommand {
public async execute(args: string[]): Promise<void> {
const [ imagePath ] = args;
const resourcesPath = this.$projectData.getAppResourcesDirectoryPath();
await this.generate(imagePath, resourcesPath, this.$options.background)
await this.generate(imagePath, resourcesPath, this.$options.background);
}

protected abstract generate(imagePath: string, resourcesPath: string, background?: string): Promise<void>
protected abstract generate(imagePath: string, resourcesPath: string, background?: string): Promise<void>;
}

export class GenerateIconsCommand extends GenerateCommandBase implements ICommand {
Expand All @@ -28,7 +28,7 @@ export class GenerateIconsCommand extends GenerateCommandBase implements IComman
}

protected async generate(imagePath: string, resourcesPath: string, background?: string): Promise<void> {
await this.$assetsGenerationService.generateIcons(imagePath, resourcesPath);
await this.$assetsGenerationService.generateIcons({ imagePath, resourcesPath });
}
}

Expand All @@ -43,7 +43,7 @@ export class GenerateSplashScreensCommand extends GenerateCommandBase implements
}

protected async generate(imagePath: string, resourcesPath: string, background?: string): Promise<void> {
await this.$assetsGenerationService.generateSplashScreens(imagePath, resourcesPath, background);
await this.$assetsGenerationService.generateSplashScreens({ imagePath, resourcesPath, background });
}
}

Expand Down
35 changes: 28 additions & 7 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,25 +802,46 @@ interface INativescriptCloudExtensionService {
install(): Promise<IExtensionData>;
}

/**
* Describes the basic data needed for resource generation
*/
interface IResourceGenerationData {
/**
* @param {string} imagePath Path to the image that will be used for generation
*/
imagePath: string,
/**
* @param {string} resourcesPath Path to the app resources
*/
resourcesPath: string
}

/**
* Describes the data needed for splash screens generation
*/
interface ISplashesGenerationData extends IResourceGenerationData {
/**
* @param {string} background Background color that will be used for background. Defaults to #FFFFFF
*/
background?: string
}


/**
* Describes service used for assets generation
*/
interface IAssetsGenerationService {
/**
* Generate icons for iOS and Android
* @param {string} imagePath Path to the image that will be used for generation
* @param {string} resourcesPath Path to the app resources
* @param {IResourceGenerationData} iconsGenerationData Provides the data needed for icons generation
* @returns {Promise<void>}
*/
generateIcons(imagePath: string, resourcesPath: string): Promise<void>;
generateIcons(iconsGenerationData: IResourceGenerationData): Promise<void>;

/**
* Generate splash screens for iOS and Android
* @param {string} imagePath Path to the image that will be used for generation
* @param {string} resourcesPath Path to the app resources
* @param {string} background Background color that will be used for background. Defaults to #FFFFFF
* @param {ISplashesGenerationData} splashesGenerationData Provides the data needed for splash screens generation
* @returns {Promise<void>}
*/
generateSplashScreens(imagePath: string, resourcesPath: string, background?: string): Promise<void>;
generateSplashScreens(splashesGenerationData: ISplashesGenerationData): Promise<void>;
}
22 changes: 13 additions & 9 deletions lib/services/assets-generation/assets-generation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@ import { Icons, SplashScreens, Operations } from "./image-definitions"
import { exported } from "../../common/decorators";

export class AssetsGenerationService implements IAssetsGenerationService {
constructor(private $logger: ILogger) {
constructor(private $logger: ILogger,
private $androidResourcesMigrationService: IAndroidResourcesMigrationService) {
}

@exported("assetsGenerationService")
public async generateIcons(imagePath: string, resourcesPath: string): Promise<void> {
public async generateIcons(resourceGenerationData: IResourceGenerationData): Promise<void> {
this.$logger.info("Generating icons ...");
await this.generateImagesForDefinitions(imagePath, resourcesPath, Icons)
await this.generateImagesForDefinitions(resourceGenerationData.imagePath, resourceGenerationData.resourcesPath, Icons)
this.$logger.info("Icons generation completed.");
}

@exported("assetsGenerationService")
public async generateSplashScreens(imagePath: string, resourcesPath: string, background?: string): Promise<void> {
public async generateSplashScreens(splashesGenerationData: ISplashesGenerationData): Promise<void> {
this.$logger.info("Generating splash screens ...");
await this.generateImagesForDefinitions(imagePath, resourcesPath, SplashScreens, background)
await this.generateImagesForDefinitions(splashesGenerationData.imagePath, splashesGenerationData.resourcesPath, SplashScreens, splashesGenerationData.background)
this.$logger.info("Splash screens generation completed.");
}

private async generateImagesForDefinitions(imagePath: string, resourcesPath: string, definitions: any[], background: string = "white") : Promise<void> {
const hasMigrated = this.$androidResourcesMigrationService.hasMigrated(resourcesPath);

for (let definition of definitions) {
const operation = definition.operation || Operations.Resize;
const scale = definition.scale || 0.8;
const outputPath = this.convertToAbsolutePath(resourcesPath, definition.path);
const path = hasMigrated ? definition.path : (definition.pathBeforeMigration || definition.path)
const outputPath = this.convertToAbsolutePath(resourcesPath, path);

switch (operation) {
case Operations.OverlayWith:
Expand All @@ -47,14 +51,14 @@ export class AssetsGenerationService implements IAssetsGenerationService {
}
}

private async resize(imagePath: string, width: number, height: number) {
private async resize(imagePath: string, width: number, height: number) : Promise<Jimp> {
const image = await Jimp.read(imagePath);
return image.scaleToFit(width, height);
}

private generateImage(background: string, width: number, height: number, outputPath: string, overlayImage?: Jimp) {
private generateImage(background: string, width: number, height: number, outputPath: string, overlayImage?: Jimp): void {
//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.
let J = <any>Jimp;
const J = <any>Jimp;
const backgroundColor = this.getRgbaNumber(background);
let image = new J(width, height, backgroundColor);

Expand Down
59 changes: 41 additions & 18 deletions lib/services/assets-generation/image-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,46 @@ export const Icons = [
"height":114,
"path":"iOS/Assets.xcassets/AppIcon.appiconset/icon-57@2x.png"
},
{
"width":1024,
"height":1024,
"path":"iOS/Assets.xcassets/AppIcon.appiconset/icon-1024.png"
},
{
"width":72,
"height":72,
"path":"Android/drawable-hdpi/icon.png"
"path":"Android/src/main/res/drawable-hdpi/icon.png",
"pathBeforeMigration":"Android/drawable-hdpi/icon.png"
},
{
"width":36,
"height":36,
"path":"Android/drawable-ldpi/icon.png"
"path":"Android/src/main/res/drawable-ldpi/icon.png",
"pathBeforeMigration":"Android/drawable-ldpi/icon.png"
},
{
"width":48,
"height":48,
"path":"Android/drawable-mdpi/icon.png"
"path":"Android/src/main/res/drawable-mdpi/icon.png",
"pathBeforeMigration":"Android/drawable-mdpi/icon.png"
},
{
"width":96,
"height":96,
"path":"Android/drawable-xhdpi/icon.png"
"path":"Android/src/main/res/drawable-xhdpi/icon.png",
"pathBeforeMigration":"Android/drawable-xhdpi/icon.png"
},
{
"width":144,
"height":144,
"path":"Android/drawable-xxhdpi/icon.png"
"path":"Android/src/main/res/drawable-xxhdpi/icon.png",
"pathBeforeMigration":"Android/drawable-xxhdpi/icon.png"
},
{
"width":345,
"height":345,
"path":"Android/drawable-xxxhdpi/icon.png"
"path":"Android/src/main/res/drawable-xxxhdpi/icon.png",
"pathBeforeMigration":"Android/drawable-xxxhdpi/icon.png"
}
]

Expand Down Expand Up @@ -238,68 +249,80 @@ export const SplashScreens = [
{
"width":576,
"height":768,
"path":"Android/drawable-hdpi/background.png",
"path":"Android/src/main/res/drawable-hdpi/background.png",
"pathBeforeMigration":"Android/drawable-hdpi/background.png",
"operation": "blank"
},
{
"width":288,
"height":384,
"path":"Android/drawable-ldpi/background.png",
"path":"Android/src/main/res/drawable-ldpi/background.png",
"pathBeforeMigration":"Android/drawable-ldpi/background.png",
"operation": "blank"
},
{
"width":384,
"height":512,
"path":"Android/drawable-mdpi/background.png",
"path":"Android/src/main/res/drawable-mdpi/background.png",
"pathBeforeMigration":"Android/drawable-mdpi/background.png",
"operation": "blank"
},
{
"width":768,
"height":1024,
"path":"Android/drawable-xhdpi/background.png",
"path":"Android/src/main/res/drawable-xhdpi/background.png",
"pathBeforeMigration":"Android/drawable-xhdpi/background.png",
"operation": "blank"
},
{
"width":1154,
"height":1536,
"path":"Android/drawable-xxhdpi/background.png",
"path":"Android/src/main/res/drawable-xxhdpi/background.png",
"pathBeforeMigration":"Android/drawable-xxhdpi/background.png",
"operation": "blank"
},
{
"width":1536,
"height":2048,
"path":"Android/drawable-xxxhdpi/background.png",
"path":"Android/src/main/res/drawable-xxxhdpi/background.png",
"pathBeforeMigration":"Android/drawable-xxxhdpi/background.png",
"operation": "blank"
},
{
"width":476,
"height":476,
"path":"Android/drawable-hdpi/logo.png"
"path":"Android/src/main/res/drawable-hdpi/logo.png",
"pathBeforeMigration":"Android/drawable-hdpi/logo.png"
},
{
"width":188,
"height":188,
"path":"Android/drawable-ldpi/logo.png"
"path":"Android/src/main/res/drawable-ldpi/logo.png",
"pathBeforeMigration":"Android/drawable-ldpi/logo.png"
},
{
"width":284,
"height":284,
"path":"Android/drawable-mdpi/logo.png"
"path":"Android/src/main/res/drawable-mdpi/logo.png",
"pathBeforeMigration":"Android/drawable-mdpi/logo.png"
},
{
"width":668,
"height":668,
"path":"Android/drawable-xhdpi/logo.png"
"path":"Android/src/main/res/drawable-xhdpi/logo.png",
"pathBeforeMigration":"Android/drawable-xhdpi/logo.png"
},
{
"width":1024,
"height":1024,
"path":"Android/drawable-xxhdpi/logo.png"
"path":"Android/src/main/res/drawable-xxhdpi/logo.png",
"pathBeforeMigration":"Android/drawable-xxhdpi/logo.png"
},
{
"width":1024,
"height":1024,
"path":"Android/drawable-xxxhdpi/logo.png"
"path":"Android/src/main/res/drawable-xxxhdpi/logo.png",
"pathBeforeMigration":"Android/drawable-xxxhdpi/logo.png"
}
]

Expand Down

0 comments on commit 18398b8

Please sign in to comment.