Skip to content

Commit

Permalink
Fix prepare for android when building with webpack (#2852)
Browse files Browse the repository at this point in the history
When switching between release, debug and bundle builds, the android
build artifacts should be cleaned.

fixes NativeScript/android#759
  • Loading branch information
sis0k0 authored May 29, 2017
1 parent 756af30 commit c2d844d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
30 changes: 21 additions & 9 deletions lib/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,9 @@ export class PlatformService extends EventEmitter implements IPlatformService {
this.$logger.trace("Changes info in prepare platform:", changesInfo);

if (changesInfo.hasChanges) {
// android build artifacts need to be cleaned up when switching from release to debug builds
if (platform.toLowerCase() === "android") {
let previousPrepareInfo = this.$projectChangesService.getPrepareInfo(platform, projectData);
// clean up prepared plugins when not building for release
if (previousPrepareInfo && previousPrepareInfo.release !== appFilesUpdaterOptions.release) {
await platformData.platformProjectService.cleanProject(platformData.projectRoot, projectData);
}
}

await this.cleanProject(platform, appFilesUpdaterOptions, platformData, projectData);
await this.preparePlatformCore(platform, appFilesUpdaterOptions, projectData, platformSpecificData, changesInfo, filesToSync);

this.$projectChangesService.savePrepareInfo(platform, projectData);
} else {
this.$logger.out("Skipping prepare.");
Expand All @@ -275,6 +268,25 @@ export class PlatformService extends EventEmitter implements IPlatformService {
}
}

private async cleanProject(platform: string, appFilesUpdaterOptions: IAppFilesUpdaterOptions, platformData: IPlatformData, projectData: IProjectData): Promise<void> {
// android build artifacts need to be cleaned up
// when switching between debug, release and webpack builds
if (platform.toLowerCase() !== "android") {
return;
}

const previousPrepareInfo = this.$projectChangesService.getPrepareInfo(platform, projectData);
if (!previousPrepareInfo) {
return;
}

const { release: previousWasRelease, bundle: previousWasBundle } = previousPrepareInfo;
const { release: currentIsRelease, bundle: currentIsBundle } = appFilesUpdaterOptions;
if ((previousWasRelease !== currentIsRelease) || (previousWasBundle !== currentIsBundle)) {
await platformData.platformProjectService.cleanProject(platformData.projectRoot, projectData);
}
}

/* Hooks are expected to use "filesToSync" parameter, as to give plugin authors additional information about the sync process.*/
@helpers.hook('prepare')
private async preparePlatformCore(platform: string, appFilesUpdaterOptions: IAppFilesUpdaterOptions, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, changesInfo?: IProjectChangesInfo, filesToSync?: Array<String>): Promise<void> {
Expand Down
5 changes: 3 additions & 2 deletions test/npm-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ async function setupProject(dependencies?: any): Promise<any> {
ensureConfigurationFileInAppResources: (): any => null,
interpolateConfigurationFile: (): void => undefined,
isPlatformPrepared: (projectRoot: string) => false,
validatePlugins: (projectData: IProjectData) => Promise.resolve()
validatePlugins: (projectData: IProjectData) => Promise.resolve(),
cleanProject: () => Promise.resolve(),
}
};
};
Expand Down Expand Up @@ -316,7 +317,7 @@ describe("Flatten npm modules tests", () => {
let gulpJshint = path.join(tnsModulesFolderPath, "gulp-jshint");
assert.isFalse(fs.exists(gulpJshint));

// Get all gulp dependencies
// Get all gulp dependencies
let gulpJsonContent = fs.readJson(path.join(projectFolder, nodeModulesFolderName, "gulp", packageJsonName));
_.each(_.keys(gulpJsonContent.dependencies), dependency => {
assert.isFalse(fs.exists(path.join(tnsModulesFolderPath, dependency)));
Expand Down

0 comments on commit c2d844d

Please sign in to comment.