Skip to content

Commit

Permalink
Add flags for platform directories. #389
Browse files Browse the repository at this point in the history
  • Loading branch information
mlynch committed Oct 12, 2022
1 parent 2c965f1 commit abf5031
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ npx capacitor-assets generate --iconBackgroundColor '#eeeeee' --iconBackgroundCo

Where the provided flags are:

- `--ios-project` - the path to the iOS project (default `ios/App`)
- `--android-project` - the path to the Android project (default `android`)
- `--iconBackgroundColor` - the background color (hex value) used when generating icon layers for light mode (default `#ffffff`)
- `--iconBackgroundColorDark` - the background color (hex value) used when generating icon layers for dark mode (where supported) (default `#111111`)
- `--splashBackgroundColor` - the background color (hex value) used when generating splash screens (default `#ffffff`)
- `--splashBackgroundColorDark` - the background color (hex value) used when generating splash screens for dark mode (where supported) (default `#111111`)
- `--logoSplashTargetWidth` - A specific width to set the logo to when generating splash screens from a single logo file (not used by default, logo is scaled as percentage of splash instead, see `--logoSplashScale`)
- `--logoSplashScale` - the scale multiplier to apply to the logo when generating splash screens from a single logo file (default: `0.2`)
- `--ios` - explicitly run iOS asset generation. Using a platform flag makes the platform list exclusive.
- `--android` - explicitly run Android asset generation. Using a platform flag makes the platform list exclusive.
- `--pwa` - explicitly run Android asset generation. Using a platform flag makes the platform list exclusive.

### Usage - Custom Mode

Expand Down
14 changes: 7 additions & 7 deletions src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export async function loadContext(projectRootPath?: string): Promise<Context> {

let project: Project;
try {
project = await loadProject(projectRootPath, (argv.assetPath as string) ?? 'assets');
project = await loadProject(argv, projectRootPath, (argv.assetPath as string) ?? 'assets');
} catch (e) {
throw new Error(`Unable to load Capacitor project: ${(e as any).message}`);
throw new Error(`Unable to load project: ${(e as any).message}`);
}

return {
Expand All @@ -42,21 +42,21 @@ export function setArguments(ctx: Context, args: any) {
process.env.VERBOSE = '' + !!args.verbose;
}

async function loadProject(projectRootPath?: string, projectAssetPath?: string): Promise<Project> {
const config = await loadMobileProjectConfig();
async function loadProject(args: any, projectRootPath?: string, projectAssetPath?: string): Promise<Project> {
const config = await loadMobileProjectConfig(args);
const project = new Project(projectRootPath, config, projectAssetPath);
await project.load();
return project;
}

// TODO: Use the config loading stuff from @capacitor/configure
function loadMobileProjectConfig(): MobileProjectConfig {
function loadMobileProjectConfig(args: any): MobileProjectConfig {
return {
ios: {
path: 'ios/App',
path: args.iosProject ?? 'ios/App',
},
android: {
path: 'android',
path: args.androidProject ?? 'android',
},
};
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export function runProgram(ctx: Context) {
'--android-flavor <name>',
'Android product flavor name where generated assets will be created. Defaults to "main".'
)
.option('--ios-project <dir>', 'Path to iOS project (defaults to "ios/App")')
.option('--android-project <dir>', 'Path to Android project (defaults to "android")')
/*
.option(
'--pwaTags',
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export class AndroidAssetGenerator extends AssetGenerator {
const resPath = this.getResPath(project);
const destRound = join(resPath, `mipmap-${template.density}`, 'ic_launcher_round.png');

// This pipeline is trick, but we need two separate pipelines
// This pipeline is tricky, but we need two separate pipelines
// per https://github.com/lovell/sharp/issues/2378#issuecomment-864132578
const resized = await sharp(asset.path).resize(template.width, template.height).toBuffer();
const composited = await sharp(resized)
Expand Down
8 changes: 8 additions & 0 deletions src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export class Project extends MobileProject {
this.detectAssetDir(projectRoot);
}

async androidExists() {
return this.config.android?.path && (await pathExists(this.config.android?.path));
}

async iosExists() {
return this.config.ios?.path && (await pathExists(this.config.ios?.path));
}

async detectAssetDir(projectRoot: string) {
if (this.assetPath === 'assets' && !(await pathExists(this.assetDir))) {
this.assetDir = join(projectRoot, 'resources');
Expand Down
48 changes: 33 additions & 15 deletions src/tasks/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from '../ctx';
import * as c from '../colors';
import { error, log } from '../util/log';
import { error, log, logger, warn } from '../util/log';
import { IosAssetGenerator } from '../platforms/ios';
import { PwaAssetGenerator } from '../platforms/pwa';
import { AndroidAssetGenerator } from '../platforms/android';
Expand Down Expand Up @@ -38,32 +38,50 @@ export async function run(ctx: Context): Promise<OutputAsset[]> {
platforms.push('pwa');
}

if (!ctx.args.silent) {
log(`Generating assets for ${platforms.map((p) => c.strong(c.success(p))).join(', ')}`);
}
await verifyPlatformFolders(/* mut */ platforms, ctx.project);

const generators = getGenerators(ctx, platforms);
if (platforms.length > 0) {
if (!ctx.args.silent) {
log(`Generating assets for ${platforms.map((p) => c.strong(c.success(p))).join(', ')}`);
}

const generated = await generateAssets(assets, generators, ctx.project);
const generators = getGenerators(ctx, platforms);

if (!ctx.args.silent) {
logGenerated(generated);
}
const generated = await generateAssets(assets, generators, ctx.project);

/*
if (!ctx.args.silent && platforms.indexOf('pwa') >= 0 && ctx.args.pwaTags) {
PwaAssetGenerator.logInstructions(generated);
}
*/
if (!ctx.args.silent) {
logGenerated(generated);
}

/*
if (!ctx.args.silent && platforms.indexOf('pwa') >= 0 && ctx.args.pwaTags) {
PwaAssetGenerator.logInstructions(generated);
}
*/

return generated;
return generated;
} else {
logger.warn('No platforms found, exiting');
return [];
}
} catch (e) {
error('Unable to generate assets', (e as Error).message);
error(e);
}
return [];
}

async function verifyPlatformFolders(platforms: string[], project: Project) {
if (platforms.indexOf('ios') >= 0 && !(await project.iosExists())) {
platforms.splice(platforms.indexOf('ios'), 1);
logger.warn(`iOS platform not found at ${project.config.ios?.path || ''}, skipping iOS generation`);
}
if (platforms.indexOf('android') >= 0 && !(await project.androidExists())) {
platforms.splice(platforms.indexOf('android'), 1);
logger.warn(`Android platform not found at ${project.config.android?.path || ''}, skipping android generation`);
}
}

async function generateAssets(assets: Assets, generators: AssetGenerator[], project: Project) {
const generated: OutputAsset[] = [];

Expand Down

0 comments on commit abf5031

Please sign in to comment.