Skip to content

Commit 06dfc73

Browse files
committed
feat(android): custom buildPath from nativescript.config
It requires a PR on @nativescript/webpack too
1 parent 7c010fe commit 06dfc73

30 files changed

+1382
-1034
lines changed

lib/commands/clean.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
IProjectCleanupResult,
77
IProjectCleanupService,
88
IProjectConfigService,
9+
IProjectData,
910
IProjectService,
1011
} from "../definitions/project";
1112

@@ -85,6 +86,7 @@ export class CleanCommand implements ICommand {
8586
constructor(
8687
private $projectCleanupService: IProjectCleanupService,
8788
private $projectConfigService: IProjectConfigService,
89+
private $projectData: IProjectData,
8890
private $terminalSpinnerService: ITerminalSpinnerService,
8991
private $projectService: IProjectService,
9092
private $prompter: IPrompter,
@@ -109,7 +111,7 @@ export class CleanCommand implements ICommand {
109111

110112
let pathsToClean = [
111113
constants.HOOKS_DIR_NAME,
112-
constants.PLATFORMS_DIR_NAME,
114+
this.$projectData.getBuildRelativeDirectoryPath(),
113115
constants.NODE_MODULES_FOLDER_NAME,
114116
constants.PACKAGE_LOCK_JSON_FILE_NAME,
115117
];

lib/commands/typings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class TypingsCommand implements ICommand {
6666

6767
const dtsGeneratorPath = path.resolve(
6868
this.$projectData.projectDir,
69-
"platforms",
69+
this.$projectData.getBuildRelativeDirectoryPath(),
7070
"android",
7171
"build-tools",
7272
"dts-generator.jar"

lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const BUNDLE_DIR = "bundle";
5858
export const RESOURCES_DIR = "res";
5959
export const CONFIG_NS_FILE_NAME = "nsconfig.json";
6060
export const CONFIG_NS_APP_RESOURCES_ENTRY = "appResourcesPath";
61+
export const CONFIG_NS_BUILD_ENTRY = "buildPath";
6162
export const CONFIG_NS_APP_ENTRY = "appPath";
6263
export const CONFIG_FILE_NAME_DISPLAY = "nativescript.config.(js|ts)";
6364
export const CONFIG_FILE_NAME_JS = "nativescript.config.js";

lib/controllers/migrate-controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export class MigrateController
6666
private $pluginsService: IPluginsService,
6767
private $projectDataService: IProjectDataService,
6868
private $projectConfigService: IProjectConfigService,
69+
private $projectData: IProjectData,
6970
private $options: IOptions,
7071
private $resources: IResourceLoader,
7172
private $injector: IInjector,
@@ -719,7 +720,7 @@ export class MigrateController
719720
private async cleanUpProject(projectData: IProjectData): Promise<void> {
720721
await this.$projectCleanupService.clean([
721722
constants.HOOKS_DIR_NAME,
722-
constants.PLATFORMS_DIR_NAME,
723+
this.$projectData.getBuildRelativeDirectoryPath(),
723724
constants.NODE_MODULES_FOLDER_NAME,
724725
constants.PACKAGE_LOCK_JSON_FILE_NAME,
725726
]);

lib/controllers/platform-controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export class PlatformController implements IPlatformController {
6868
packageToInstall,
6969
addPlatformData
7070
);
71-
71+
const buildPath = projectData.platformsDir;
72+
console.log('buildPath', buildPath, projectData.getBuildRelativeDirectoryPath())
7273
this.$fs.ensureDirectoryExists(
7374
path.join(projectData.platformsDir, platform)
7475
);
@@ -96,6 +97,7 @@ export class PlatformController implements IPlatformController {
9697
commentHeader,
9798
`appPath = ${appPath}`,
9899
`appResourcesPath = ${appResourcesPath}`,
100+
`buildPath = ${buildPath}`,
99101
"",
100102
].join("\n");
101103

lib/controllers/prepare-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export class PrepareController extends EventEmitter {
357357
this.$logger.info(`Chokidar raised event ${event} for ${filePath}.`);
358358
await this.writeRuntimePackageJson(projectData, platformData);
359359
this.emitPrepareEvent({
360-
files: [],
360+
files: [filePath],
361361
staleFiles: [],
362362
hasOnlyHotUpdateFiles: false,
363363
hmrData: null,

lib/controllers/update-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export class UpdateController
296296
private async cleanUpProject(): Promise<void> {
297297
await this.$projectCleanupService.clean([
298298
constants.HOOKS_DIR_NAME,
299-
constants.PLATFORMS_DIR_NAME,
299+
this.$projectDataService.getProjectData().getBuildRelativeDirectoryPath(),
300300
constants.NODE_MODULES_FOLDER_NAME,
301301
constants.PACKAGE_LOCK_JSON_FILE_NAME,
302302
]);

lib/definitions/project.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ interface INsConfig {
153153
main?: string;
154154
appPath?: string;
155155
appResourcesPath?: string;
156+
buildPath?: string;
156157
shared?: boolean;
157158
overridePods?: string;
158159
webpackConfigPath?: string;
@@ -209,6 +210,7 @@ interface IProjectData extends ICreateProjectData {
209210
getAppDirectoryRelativePath(): string;
210211
getAppResourcesDirectoryPath(projectDir?: string): string;
211212
getAppResourcesRelativeDirectoryPath(): string;
213+
getBuildRelativeDirectoryPath(): string;
212214
}
213215

214216
interface IProjectDataService {

lib/project-data.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,14 @@ export class ProjectData implements IProjectData {
164164
this.projectName = this.$projectHelper.sanitizeName(
165165
path.basename(projectDir)
166166
);
167-
this.platformsDir = path.join(projectDir, constants.PLATFORMS_DIR_NAME);
167+
this.nsConfig = nsConfig;
168+
this.platformsDir = path.join(projectDir, this.getBuildRelativeDirectoryPath());
168169
this.projectFilePath = projectFilePath;
169170
this.projectIdentifiers = this.initializeProjectIdentifiers(nsConfig);
170171
this.packageJsonData = packageJsonData;
171172
this.dependencies = packageJsonData.dependencies;
172173
this.devDependencies = packageJsonData.devDependencies;
173174
this.projectType = this.getProjectType();
174-
this.nsConfig = nsConfig;
175175
this.ignoredDependencies = nsConfig?.ignoredNativeDependencies;
176176
this.appDirectoryPath = this.getAppDirectoryPath();
177177
this.appResourcesDirectoryPath = this.getAppResourcesDirectoryPath();
@@ -270,6 +270,17 @@ export class ProjectData implements IProjectData {
270270
return this.resolveToProjectDir(appRelativePath, projectDir);
271271
}
272272

273+
public getBuildRelativeDirectoryPath(): string {
274+
if (
275+
this.nsConfig &&
276+
this.nsConfig[constants.CONFIG_NS_BUILD_ENTRY]
277+
) {
278+
return this.nsConfig[constants.CONFIG_NS_BUILD_ENTRY];
279+
}
280+
281+
return constants.PLATFORMS_DIR_NAME;
282+
}
283+
273284
public getAppDirectoryRelativePath(): string {
274285
if (this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_ENTRY]) {
275286
return this.nsConfig[constants.CONFIG_NS_APP_ENTRY];

lib/services/android-plugin-build-service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
775775
`-PtempBuild=true`,
776776
`-PcompileSdk=android-${pluginBuildSettings.androidToolsInfo.compileSdkVersion}`,
777777
`-PbuildToolsVersion=${pluginBuildSettings.androidToolsInfo.buildToolsVersion}`,
778+
`-PprojectRoot=${this.$projectData.projectDir}`,
779+
`-DprojectRoot=${this.$projectData.projectDir}`, // we need it as a -D to be able to read it from settings.gradle
778780
`-PappPath=${this.$projectData.getAppDirectoryPath()}`,
781+
`-PappBuildPath=${this.$projectData.getBuildRelativeDirectoryPath()}`,
782+
`-DappBuildPath=${this.$projectData.getBuildRelativeDirectoryPath()}`, // we need it as a -D to be able to read it from settings.gradle
779783
`-PappResourcesPath=${this.$projectData.getAppResourcesDirectoryPath()}`,
780784
];
781785
if (pluginBuildSettings.gradleArgs) {

lib/services/android-project-service.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
364364
);
365365
const allGradleTemplateFiles = path.join(gradleTemplatePath, "*");
366366

367-
this.$fs.copyFile(allGradleTemplateFiles, path.join(this.getPlatformData(projectData).projectRoot, 'app'));
367+
this.$fs.copyFile(allGradleTemplateFiles, path.join(this.getPlatformData(projectData).projectRoot));
368368

369369
// TODO: Check if we actually need this and if it should be targetSdk or compileSdk
370370
this.cleanResValues(targetSdkVersion, projectData);
@@ -465,6 +465,13 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
465465
this.getPlatformData(projectData).projectRoot,
466466
"settings.gradle"
467467
);
468+
shell.sed(
469+
"-i",
470+
/def USER_PROJECT_ROOT = \"\$rootDir\/..\/..\/\"/,
471+
'def USER_PROJECT_ROOT = System.getProperties().projectRoot != null ? System.getProperties().projectRoot : "$rootDir/../../../"',
472+
gradleSettingsFilePath
473+
);
474+
468475
shell.sed(
469476
"-i",
470477
/__PROJECT_NAME__/,
@@ -529,6 +536,12 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
529536
projectData.projectIdentifiers.android,
530537
buildGradlePath
531538
);
539+
shell.sed(
540+
"-i",
541+
/project.ext.USER_PROJECT_ROOT = \"\$rootDir\/..\/..\"/,
542+
'project.ext.USER_PROJECT_ROOT = System.getProperties().projectRoot != null ? System.getProperties().projectRoot : "$rootDir/../../../"',
543+
buildGradlePath
544+
);
532545
}
533546

534547
private getProjectNameFromId(projectData: IProjectData): string {
@@ -899,7 +912,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
899912
if (buildData.buildFilterDevicesArch) {
900913
const outputPath = platformData.getBuildOutputPath(deviceDescriptor.buildData);
901914
const apkOutputPath = path.join(outputPath, prepareData.release ? "release" : "debug");
902-
if (!this.$fs.exists(outputPath)) {
915+
if (!this.$fs.exists(apkOutputPath)) {
903916
return;
904917
}
905918
// check if we already build this arch

lib/services/android/gradle-build-args-service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export class GradleBuildArgsService implements IGradleBuildArgsService {
6161
`-PtargetSdk=${toolsInfo.targetSdkVersion}`,
6262
`-PbuildToolsVersion=${toolsInfo.buildToolsVersion}`,
6363
`-PgenerateTypings=${toolsInfo.generateTypings}`,
64+
`-PprojectRoot=${this.$projectData.projectDir}`,
65+
`-DprojectRoot=${this.$projectData.projectDir}`, // we need it as a -D to be able to read it from settings.gradle
66+
`-PappPath=${this.$projectData.getAppDirectoryPath()}`,
67+
`-PappBuildPath=${this.$projectData.getBuildRelativeDirectoryPath()}`,
68+
`-DappBuildPath=${this.$projectData.getBuildRelativeDirectoryPath()}`, // we need it as a -D to be able to read it from settings.gradle
6469
`-PappPath=${this.$projectData.getAppDirectoryPath()}`,
6570
`-PappResourcesPath=${this.$projectData.getAppResourcesDirectoryPath()}`
6671
);

lib/services/plugins-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ export class PluginsService implements IPluginsService {
489489
): IDependencyData[] {
490490
const dependenciesWithFrameworks: any[] = [];
491491
_.each(productionDependencies, (d) => {
492-
const pathToPlatforms = path.join(d.directory, "platforms", platform);
492+
const pathToPlatforms = path.join(d.directory, constants.PLATFORMS_DIR_NAME, platform);
493493
if (this.$fs.exists(pathToPlatforms)) {
494494
const contents = this.$fs.readDirectory(pathToPlatforms);
495495
_.each(contents, (file) => {
@@ -629,7 +629,7 @@ This framework comes from ${dependencyName} plugin, which is installed multiple
629629
);
630630
pluginData.isPlugin = !!cacheData.nativescript;
631631
pluginData.pluginPlatformsFolderPath = (platform: string) =>
632-
path.join(pluginData.fullPath, "platforms", platform.toLowerCase());
632+
path.join(pluginData.fullPath, constants.PLATFORMS_DIR_NAME, platform.toLowerCase());
633633
const data = cacheData.nativescript;
634634

635635
if (pluginData.isPlugin) {

lib/services/webpack/webpack-compiler-service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,13 @@ export class WebpackCompilerService
357357

358358
const appPath = projectData.getAppDirectoryRelativePath();
359359
const appResourcesPath = projectData.getAppResourcesRelativeDirectoryPath();
360+
const buildPath = projectData.getBuildRelativeDirectoryPath();
360361

361362
Object.assign(
362363
envData,
363364
appPath && { appPath },
364365
appResourcesPath && { appResourcesPath },
366+
buildPath && { buildPath },
365367
{
366368
nativescriptLibPath: path.resolve(
367369
__dirname,

test/ios-project-service.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { YarnPackageManager } from "../lib/yarn-package-manager";
3030

3131
import { assert } from "chai";
3232
import { SettingsService } from "../lib/common/test/unit-tests/stubs";
33-
import { BUILD_XCCONFIG_FILE_NAME } from "../lib/constants";
33+
import { BUILD_XCCONFIG_FILE_NAME, PLATFORMS_DIR_NAME } from "../lib/constants";
3434
import {
3535
ProjectDataStub,
3636
TempServiceStub,
@@ -88,7 +88,7 @@ function createTestInjector(
8888
testInjector.register("options", OptionsLib.Options);
8989
testInjector.register("cocoaPodsPlatformManager", CocoaPodsPlatformManager);
9090
const projectData = Object.assign({}, ProjectDataStub, {
91-
platformsDir: join(projectPath, "platforms"),
91+
platformsDir: join(projectPath, PLATFORMS_DIR_NAME),
9292
projectName: projectName,
9393
projectPath: projectPath,
9494
projectFilePath: join(projectPath, "package.json"),
@@ -309,7 +309,7 @@ describe("Cocoapods support", () => {
309309
// fs.writeJson(join(projectPath, "package.json"), packageJsonData);
310310
createPackageJson(testInjector, projectPath, "myProject");
311311

312-
const platformsFolderPath = join(projectPath, "platforms", "ios");
312+
const platformsFolderPath = join(projectPath, PLATFORMS_DIR_NAME, "ios");
313313
fs.createDirectory(platformsFolderPath);
314314

315315
const iOSProjectService = testInjector.resolve("iOSProjectService");
@@ -411,7 +411,7 @@ describe("Cocoapods support", () => {
411411
};
412412
fs.writeJson(join(projectPath, "package.json"), packageJsonData);
413413

414-
const platformsFolderPath = join(projectPath, "platforms", "ios");
414+
const platformsFolderPath = join(projectPath, PLATFORMS_DIR_NAME, "ios");
415415
fs.createDirectory(platformsFolderPath);
416416

417417
const iOSProjectService = testInjector.resolve("iOSProjectService");
@@ -442,7 +442,7 @@ describe("Cocoapods support", () => {
442442
const pluginPath = temp.mkdirSync("pluginDirectory");
443443
const samplePluginPlatformsFolderPath = join(
444444
pluginPath,
445-
"platforms",
445+
PLATFORMS_DIR_NAME,
446446
"ios"
447447
);
448448
const pluginPodfilePath = join(
@@ -521,7 +521,7 @@ describe("Cocoapods support", () => {
521521
};
522522
fs.writeJson(join(projectPath, "package.json"), packageJsonData);
523523

524-
const platformsFolderPath = join(projectPath, "platforms", "ios");
524+
const platformsFolderPath = join(projectPath, PLATFORMS_DIR_NAME, "ios");
525525
fs.createDirectory(platformsFolderPath);
526526

527527
const iOSProjectService = testInjector.resolve("iOSProjectService");
@@ -570,7 +570,7 @@ describe("Cocoapods support", () => {
570570
const pluginPath = temp.mkdirSync("pluginDirectory");
571571
const samplePluginPlatformsFolderPath = join(
572572
pluginPath,
573-
"platforms",
573+
PLATFORMS_DIR_NAME,
574574
"ios"
575575
);
576576
const pluginPodfilePath = join(
@@ -679,7 +679,7 @@ describe("Source code support", () => {
679679
};
680680
fs.writeJson(join(projectPath, "package.json"), packageJsonData);
681681

682-
const platformsFolderPath = join(projectPath, "platforms", "ios");
682+
const platformsFolderPath = join(projectPath, PLATFORMS_DIR_NAME, "ios");
683683
fs.createDirectory(platformsFolderPath);
684684

685685
const xcprojService = testInjector.resolve("xcprojService");
@@ -738,7 +738,7 @@ describe("Source code support", () => {
738738
};
739739
fs.writeJson(join(projectPath, "package.json"), packageJsonData);
740740

741-
const platformsFolderPath = join(projectPath, "platforms", "ios");
741+
const platformsFolderPath = join(projectPath, PLATFORMS_DIR_NAME, "ios");
742742
fs.createDirectory(platformsFolderPath);
743743

744744
const iOSProjectService = testInjector.resolve("iOSProjectService");
@@ -775,7 +775,7 @@ describe("Source code support", () => {
775775
const pluginPath = temp.mkdirSync("pluginDirectory");
776776
const samplePluginPlatformsFolderPath = join(
777777
pluginPath,
778-
"platforms",
778+
PLATFORMS_DIR_NAME,
779779
"ios"
780780
);
781781
files.forEach((file) => {
@@ -820,7 +820,7 @@ describe("Source code support", () => {
820820
const testInjector = createTestInjector(projectPath, projectName, xcode);
821821
const fs: IFileSystem = testInjector.resolve("fs");
822822

823-
const platformsFolderPath = join(projectPath, "platforms", "ios");
823+
const platformsFolderPath = join(projectPath, PLATFORMS_DIR_NAME, "ios");
824824
fs.createDirectory(platformsFolderPath);
825825

826826
const pbxProj = await await getProjectWithoutPlugins(sourceFileNames);
@@ -988,7 +988,7 @@ describe("Static libraries support", () => {
988988
const testInjector = createTestInjector(projectPath, projectName);
989989
const fs: IFileSystem = testInjector.resolve("fs");
990990
const staticLibraryPath = join(
991-
join(temp.mkdirSync("pluginDirectory"), "platforms", "ios")
991+
join(temp.mkdirSync("pluginDirectory"), PLATFORMS_DIR_NAME, "ios")
992992
);
993993
const staticLibraryHeadersPath = join(
994994
staticLibraryPath,

0 commit comments

Comments
 (0)