Skip to content

refactor: introduce shouldprepare hook #3399

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 1 commit into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 16 additions & 2 deletions lib/definitions/platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ interface IPlatformService extends NodeJS.EventEmitter {
*/
shouldInstall(device: Mobile.IDevice, projectData: IProjectData, outputPath?: string): Promise<boolean>;

/**
* Determines whether the project should undergo the prepare process.
* @param {IShouldPrepareInfo} shouldPrepareInfo Options needed to decide whether to prepare.
* @returns {Promise<boolean>} true indicates that the project should be prepared.
*/
shouldPrepare(shouldPrepareInfo: IShouldPrepareInfo): Promise<boolean>

/**
* Installs the application on specified device.
* When finishes, saves .nsbuildinfo in application root folder to indicate the prepare that was used to build the app.
Expand Down Expand Up @@ -314,11 +321,18 @@ interface IPreparePlatformJSInfo extends IPreparePlatformCoreInfo, ICopyAppFiles
projectFilesConfig?: IProjectFilesConfig;
}

interface IPreparePlatformCoreInfo extends IPreparePlatformInfoBase {
platformSpecificData: IPlatformSpecificData
interface IShouldPrepareInfo extends IOptionalProjectChangesInfoComposition {
platformInfo: IPreparePlatformInfo;
}

interface IOptionalProjectChangesInfoComposition {
changesInfo?: IProjectChangesInfo;
}

interface IPreparePlatformCoreInfo extends IPreparePlatformInfoBase, IOptionalProjectChangesInfoComposition {
platformSpecificData: IPlatformSpecificData
}

interface IPreparePlatformInfo extends IPreparePlatformInfoBase, IPlatformConfig, IPlatformTemplate, ISkipNativeCheckOptional { }

interface IPlatformConfig {
Expand Down
20 changes: 16 additions & 4 deletions lib/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,25 @@ export class PlatformService extends EventEmitter implements IPlatformService {
return _.filter(this.$platformsData.platformsNames, p => { return this.isPlatformPrepared(p, projectData); });
}

public async preparePlatform(platformInfo: IPreparePlatformInfo): Promise<boolean> {
@helpers.hook('shouldPrepare')
public async shouldPrepare(shouldPrepareInfo: IShouldPrepareInfo): Promise<boolean> {
shouldPrepareInfo.changesInfo = shouldPrepareInfo.changesInfo || await this.getChangesInfo(shouldPrepareInfo.platformInfo);
const requiresNativePrepare = (!shouldPrepareInfo.platformInfo.nativePrepare || !shouldPrepareInfo.platformInfo.nativePrepare.skipNativePrepare) && shouldPrepareInfo.changesInfo.nativePlatformStatus === constants.NativePlatformStatus.requiresPrepare;

return shouldPrepareInfo.changesInfo.hasChanges || requiresNativePrepare;
}

private async getChangesInfo(platformInfo: IPreparePlatformInfo): Promise<IProjectChangesInfo> {
const platformData = this.$platformsData.getPlatformData(platformInfo.platform, platformInfo.projectData);

const changesInfo = await this.initialPrepare(platformInfo.platform, platformData, platformInfo.appFilesUpdaterOptions, platformInfo.platformTemplate, platformInfo.projectData, platformInfo.config, platformInfo.nativePrepare, platformInfo);
const requiresNativePrepare = (!platformInfo.nativePrepare || !platformInfo.nativePrepare.skipNativePrepare) && changesInfo.nativePlatformStatus === constants.NativePlatformStatus.requiresPrepare;
return this.initialPrepare(platformInfo.platform, platformData, platformInfo.appFilesUpdaterOptions, platformInfo.platformTemplate, platformInfo.projectData, platformInfo.config, platformInfo.nativePrepare, platformInfo);
}

public async preparePlatform(platformInfo: IPreparePlatformInfo): Promise<boolean> {
const changesInfo = await this.getChangesInfo(platformInfo);
const shouldPrepare = await this.shouldPrepare({ platformInfo, changesInfo });

if (changesInfo.hasChanges || requiresNativePrepare) {
if (shouldPrepare) {
// Always clear up the app directory in platforms if `--bundle` value has changed in between builds or is passed in general
// this is done as user has full control over what goes in platforms when `--bundle` is passed
// and we may end up with duplicate symbols which would fail the build
Expand Down
3 changes: 3 additions & 0 deletions test/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ export class CommandsService implements ICommandsService {
}

export class PlatformServiceStub extends EventEmitter implements IPlatformService {
public shouldPrepare(): Promise<boolean> {
return Promise.resolve(true);
}

public validateOptions(): Promise<boolean> {
return Promise.resolve(true);
Expand Down