Skip to content

Commit 957f603

Browse files
Fix local builds when CLI is required as lib
When CLI is required as library, the `$options` dependency is not populated. However the projectChangesService relies on it in order to determine if the project should be prepared/built. When CLI is required as library and you change only the build configuration (debug/release), the project is not rebuilt. However when you use the CLI and try the same, a new full build is triggered. Fix this by parsing required boolean flags to projectChangesService and exclude `$options` from its implementation. This way local builds will work in the same manner both from command line and when required as library. Steps to reproduce the problem: * use CLI as lib * create local build for android in debug configuration * create local build for android in release configuration - you'll notice gradle clean is not called at all and also in the `<project dir>/platforms/android/build/outputs/apks/` there are both debug and release apks. This should never happen when changing configurations.
1 parent 8244231 commit 957f603

8 files changed

+26
-20
lines changed

lib/definitions/project-changes.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ interface IProjectChangesInfo {
1919
changesRequireBuild: boolean;
2020
}
2121

22+
interface IProjectChangesOptions extends IAppFilesUpdaterOptions, IProvision {}
23+
2224
interface IProjectChangesService {
23-
checkForChanges(platform: string, projectData: IProjectData): IProjectChangesInfo;
25+
checkForChanges(platform: string, projectData: IProjectData, buildOptions: IProjectChangesOptions): IProjectChangesInfo;
2426
getPrepareInfo(platform: string, projectData: IProjectData): IPrepareInfo;
2527
savePrepareInfo(platform: string, projectData: IProjectData): void;
2628
getPrepareInfoFilePath(platform: string, projectData: IProjectData): string;

lib/services/android-project-service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
433433
}
434434

435435
public async cleanProject(projectRoot: string, options: string[], projectData: IProjectData): Promise<void> {
436+
// In case options are not passed, we'll use the default ones for cleaning the project.
437+
// In case we do not do this, we'll required android-23 (default in build.gradle) for cleaning the project.
438+
options = options.length === 0 ? this.getBuildOptions({ release: false }, projectData) : options;
439+
436440
options.unshift("clean");
437441

438442
let gradleBin = path.join(projectRoot, "gradlew");

lib/services/local-build-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class LocalBuildService extends EventEmitter {
99

1010
public async build(platform: string, platformBuildOptions: IPlatformBuildData, platformTemplate?: string): Promise<string> {
1111
this.$projectData.initializeProjectData(platformBuildOptions.projectDir);
12-
await this.$platformService.preparePlatform(platform, platformBuildOptions, platformTemplate, this.$projectData, undefined);
12+
await this.$platformService.preparePlatform(platform, platformBuildOptions, platformTemplate, this.$projectData, { provision: platformBuildOptions.provision, sdk: null });
1313
this.$platformService.on(BUILD_OUTPUT_EVENT_NAME, (data: any) => {
1414
data.projectDir = platformBuildOptions.projectDir;
1515
this.emit(BUILD_OUTPUT_EVENT_NAME, data);

lib/services/platform-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export class PlatformService extends EventEmitter implements IPlatformService {
233233
await this.$pluginsService.validate(platformData, projectData);
234234

235235
await this.ensurePlatformInstalled(platform, platformTemplate, projectData, platformSpecificData);
236-
let changesInfo = this.$projectChangesService.checkForChanges(platform, projectData);
236+
let changesInfo = this.$projectChangesService.checkForChanges(platform, projectData, { bundle: appFilesUpdaterOptions.bundle, release: appFilesUpdaterOptions.release, provision: platformSpecificData.provision });
237237

238238
this.$logger.trace("Changes info in prepare platform:", changesInfo);
239239

lib/services/project-changes-service.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,17 @@ export class ProjectChangesService implements IProjectChangesService {
3838
constructor(
3939
private $platformsData: IPlatformsData,
4040
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
41-
private $options: IOptions,
4241
private $fs: IFileSystem) {
4342
}
4443

4544
public get currentChanges(): IProjectChangesInfo {
4645
return this._changesInfo;
4746
}
4847

49-
public checkForChanges(platform: string, projectData: IProjectData): IProjectChangesInfo {
48+
public checkForChanges(platform: string, projectData: IProjectData, buildOptions: IProjectChangesOptions): IProjectChangesInfo {
5049
let platformData = this.$platformsData.getPlatformData(platform, projectData);
5150
this._changesInfo = new ProjectChangesInfo();
52-
if (!this.ensurePrepareInfo(platform, projectData)) {
51+
if (!this.ensurePrepareInfo(platform, projectData, buildOptions)) {
5352
this._newFiles = 0;
5453
this._changesInfo.appFilesChanged = this.containsNewerFiles(projectData.appDirectoryPath, projectData.appResourcesDirectoryPath, projectData);
5554
this._changesInfo.packageChanged = this.filesChanged([path.join(projectData.projectDir, "package.json")]);
@@ -77,7 +76,7 @@ export class ProjectChangesService implements IProjectChangesService {
7776
}
7877
}
7978
if (platform.toLowerCase() === this.$devicePlatformsConstants.iOS.toLowerCase()) {
80-
const nextCommandProvisionUUID = this.$options.provision;
79+
const nextCommandProvisionUUID = buildOptions.provision;
8180
// We should consider reading here the provisioning profile UUID from the xcodeproj and xcconfig.
8281
const prevProvisionUUID = this._prepareInfo.iOSProvisioningProfileUUID;
8382
if (nextCommandProvisionUUID !== prevProvisionUUID) {
@@ -86,13 +85,13 @@ export class ProjectChangesService implements IProjectChangesService {
8685
this._prepareInfo.iOSProvisioningProfileUUID = nextCommandProvisionUUID;
8786
}
8887
}
89-
if (this.$options.bundle !== this._prepareInfo.bundle || this.$options.release !== this._prepareInfo.release) {
88+
if (buildOptions.bundle !== this._prepareInfo.bundle || buildOptions.release !== this._prepareInfo.release) {
9089
this._changesInfo.appFilesChanged = true;
9190
this._changesInfo.appResourcesChanged = true;
9291
this._changesInfo.modulesChanged = true;
9392
this._changesInfo.configChanged = true;
94-
this._prepareInfo.release = this.$options.release;
95-
this._prepareInfo.bundle = this.$options.bundle;
93+
this._prepareInfo.release = buildOptions.release;
94+
this._prepareInfo.bundle = buildOptions.bundle;
9695
}
9796
if (this._changesInfo.packageChanged) {
9897
this._changesInfo.modulesChanged = true;
@@ -134,7 +133,7 @@ export class ProjectChangesService implements IProjectChangesService {
134133
this.$fs.writeJson(prepareInfoFilePath, this._prepareInfo);
135134
}
136135

137-
private ensurePrepareInfo(platform: string, projectData: IProjectData): boolean {
136+
private ensurePrepareInfo(platform: string, projectData: IProjectData, buildOptions: { bundle: boolean, release: boolean, provision: string }): boolean {
138137
this._prepareInfo = this.getPrepareInfo(platform, projectData);
139138
if (this._prepareInfo) {
140139
let platformData = this.$platformsData.getPlatformData(platform, projectData);
@@ -145,8 +144,8 @@ export class ProjectChangesService implements IProjectChangesService {
145144
}
146145
this._prepareInfo = {
147146
time: "",
148-
bundle: this.$options.bundle,
149-
release: this.$options.release,
147+
bundle: buildOptions.bundle,
148+
release: buildOptions.release,
150149
changesRequireBuild: true,
151150
changesRequireBuildTime: null
152151
};

test/nativescript-cli-lib.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ describe("nativescript-cli-lib", () => {
1515
const publicApi: any = {
1616
deviceEmitter: null,
1717
projectService: ["createProject", "isValidNativeScriptProject"],
18-
localBuildService: ["build"]
19-
18+
localBuildService: ["build"],
19+
deviceLogProvider: null
2020
};
2121

2222
const pathToEntryPoint = path.join(__dirname, "..", "lib", "nativescript-cli-lib.js").replace(/\\/g, "\\\\");

test/npm-support.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,12 @@ async function addDependencies(testInjector: IInjector, projectFolder: string, d
187187
}
188188

189189
async function preparePlatform(testInjector: IInjector): Promise<void> {
190-
let platformService: IPlatformService = testInjector.resolve("platformService");
191-
let projectData: IProjectData = testInjector.resolve("projectData");
190+
const platformService: IPlatformService = testInjector.resolve("platformService");
191+
const projectData: IProjectData = testInjector.resolve("projectData");
192192
projectData.initializeProjectData();
193+
const options: IOptions = testInjector.resolve("options");
193194

194-
await platformService.preparePlatform("android", { bundle: false, release: false }, "", projectData, undefined);
195+
await platformService.preparePlatform("android", { bundle: options.bundle, release: options.release }, "", projectData, { provision: options.provision, sdk: options.sdk });
195196
}
196197

197198
describe("Npm support tests", () => {

test/platform-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ describe('Platform Service Tests', () => {
369369

370370
platformService = testInjector.resolve("platformService");
371371
const appFilesUpdaterOptions: IAppFilesUpdaterOptions = { bundle: false, release: release };
372-
await platformService.preparePlatform(platformToTest, appFilesUpdaterOptions, "", projectData, undefined);
372+
await platformService.preparePlatform(platformToTest, appFilesUpdaterOptions, "", projectData, { provision: null, sdk: null });
373373

374374
let test1FileName = platformToTest.toLowerCase() === "ios" ? "test1.js" : "test2.js";
375375
let test2FileName = platformToTest.toLowerCase() === "ios" ? "test2.js" : "test1.js";
@@ -446,7 +446,7 @@ describe('Platform Service Tests', () => {
446446
try {
447447
testInjector.resolve("$logger").warn = (text: string) => warnings += text;
448448
const appFilesUpdaterOptions: IAppFilesUpdaterOptions = { bundle: false, release: false };
449-
await platformService.preparePlatform("android", appFilesUpdaterOptions, "", projectData, undefined);
449+
await platformService.preparePlatform("android", appFilesUpdaterOptions, "", projectData, { provision: null, sdk: null });
450450
} finally {
451451
testInjector.resolve("$logger").warn = oldLoggerWarner;
452452
}

0 commit comments

Comments
 (0)