Skip to content

Commit e0ed7f2

Browse files
authored
fix: write project settings to gradle.properties (#5640)
this fixes running the platforms/android project in Android Studio
1 parent 74908cd commit e0ed7f2

File tree

4 files changed

+104
-11
lines changed

4 files changed

+104
-11
lines changed

lib/controllers/platform-controller.ts

+46-6
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ export class PlatformController implements IPlatformController {
2525
private $packageInstallationManager: IPackageInstallationManager,
2626
private $projectDataService: IProjectDataService,
2727
private $platformsDataService: IPlatformsDataService,
28-
private $projectChangesService: IProjectChangesService
28+
private $projectChangesService: IProjectChangesService,
29+
private $mobileHelper: Mobile.IMobileHelper
2930
) {}
3031

31-
public async addPlatform(addPlatformData: IAddPlatformData): Promise<void> {
32+
public async addPlatform(
33+
addPlatformData: IAddPlatformData,
34+
projectData?: IProjectData
35+
): Promise<void> {
3236
const [platform, version] = addPlatformData.platform
3337
.toLowerCase()
3438
.split("@");
35-
const projectData = this.$projectDataService.getProjectData(
39+
projectData ??= this.$projectDataService.getProjectData(
3640
addPlatformData.projectDir
3741
);
3842
const platformData = this.$platformsDataService.getPlatformData(
@@ -68,18 +72,54 @@ export class PlatformController implements IPlatformController {
6872
this.$fs.ensureDirectoryExists(
6973
path.join(projectData.platformsDir, platform)
7074
);
75+
76+
if (this.$mobileHelper.isAndroidPlatform(platform)) {
77+
const gradlePropertiesPath = path.resolve(
78+
platformData.projectRoot,
79+
"gradle.properties"
80+
);
81+
const commentHeader = "# App configuration";
82+
const appPath = projectData.getAppDirectoryRelativePath();
83+
const appResourcesPath = projectData.getAppResourcesRelativeDirectoryPath();
84+
85+
let gradlePropertiesContents = "";
86+
if (this.$fs.exists(gradlePropertiesPath)) {
87+
gradlePropertiesContents = this.$fs
88+
.readFile(gradlePropertiesPath)
89+
.toString();
90+
}
91+
92+
if (!gradlePropertiesContents.includes(commentHeader)) {
93+
const dataToWrite = [
94+
"",
95+
"",
96+
commentHeader,
97+
`appPath = ${appPath}`,
98+
`appResourcesPath = ${appResourcesPath}`,
99+
"",
100+
].join("\n");
101+
102+
gradlePropertiesContents += dataToWrite;
103+
104+
this.$logger.trace("Updated gradle.properties with project data...");
105+
this.$fs.writeFile(gradlePropertiesPath, gradlePropertiesContents);
106+
}
107+
}
71108
this.$logger.info(
72109
`Platform ${platform} successfully added. v${installedPlatformVersion}`
73110
);
74111
}
75112

76113
public async addPlatformIfNeeded(
77-
addPlatformData: IAddPlatformData
114+
addPlatformData: IAddPlatformData,
115+
projectData?: IProjectData
78116
): Promise<void> {
79117
const [platform] = addPlatformData.platform.toLowerCase().split("@");
80-
const projectData = this.$projectDataService.getProjectData(
118+
119+
projectData ??= this.$projectDataService.getProjectData(
81120
addPlatformData.projectDir
82121
);
122+
83123
const platformData = this.$platformsDataService.getPlatformData(
84124
platform,
85125
projectData
@@ -91,7 +131,7 @@ export class PlatformController implements IPlatformController {
91131
addPlatformData.nativePrepare
92132
);
93133
if (shouldAddPlatform) {
94-
await this.addPlatform(addPlatformData);
134+
await this.addPlatform(addPlatformData, projectData);
95135
}
96136
}
97137

lib/controllers/prepare-controller.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ export class PrepareController extends EventEmitter {
127127
prepareData: IPrepareData,
128128
projectData: IProjectData
129129
): Promise<IPrepareResultData> {
130-
await this.$platformController.addPlatformIfNeeded(prepareData);
130+
await this.$platformController.addPlatformIfNeeded(
131+
prepareData,
132+
projectData
133+
);
131134
await this.trackRuntimeVersion(prepareData.platform, projectData);
132135

133136
this.$logger.info("Preparing project...");
@@ -375,7 +378,10 @@ export class PrepareController extends EventEmitter {
375378
projectData: IProjectData
376379
): Promise<string[]> {
377380
const dependencies = this.$nodeModulesDependenciesBuilder
378-
.getProductionDependencies(projectData.projectDir, projectData.ignoredDependencies)
381+
.getProductionDependencies(
382+
projectData.projectDir,
383+
projectData.ignoredDependencies
384+
)
379385
.filter((dep) => dep.nativescript);
380386
const pluginsNativeDirectories = dependencies.map((dep) =>
381387
path.join(

lib/definitions/platform.d.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ interface IPrepareNodeModulesData {
7878
}
7979

8080
interface INodeModulesDependenciesBuilder {
81-
getProductionDependencies(projectPath: string, ignore?: string[]): IDependencyData[];
81+
getProductionDependencies(
82+
projectPath: string,
83+
ignore?: string[]
84+
): IDependencyData[];
8285
}
8386

8487
interface IBuildInfo {
@@ -118,8 +121,14 @@ interface IAddPlatformData extends IControllerDataBase {
118121
}
119122

120123
interface IPlatformController {
121-
addPlatform(addPlatformData: IAddPlatformData): Promise<void>;
122-
addPlatformIfNeeded(addPlatformData: IAddPlatformData): Promise<void>;
124+
addPlatform(
125+
addPlatformData: IAddPlatformData,
126+
projectData?: IProjectData
127+
): Promise<void>;
128+
addPlatformIfNeeded(
129+
addPlatformData: IAddPlatformData,
130+
projectData?: IProjectData
131+
): Promise<void>;
123132
}
124133

125134
interface IAddPlatformService {

test/controllers/add-platform-controller.ts

+38
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { PackageManager } from "../../lib/package-manager";
99
import { NodePackageManager } from "../../lib/node-package-manager";
1010
import { YarnPackageManager } from "../../lib/yarn-package-manager";
1111
import { PnpmPackageManager } from "../../lib/pnpm-package-manager";
12+
import { MobileHelper } from "../../lib/common/mobile/mobile-helper";
1213

1314
let actualMessage: string = null;
1415
const latestFrameworkVersion = "5.3.1";
@@ -33,6 +34,7 @@ function createInjector(data?: { latestFrameworkVersion: string }) {
3334
getSettingValue: async (settingName: string): Promise<void> => undefined,
3435
});
3536
injector.register("tempService", TempServiceStub);
37+
injector.register("mobileHelper", MobileHelper);
3638

3739
const logger = injector.resolve("logger");
3840
logger.info = (message: string) => (actualMessage = message);
@@ -43,7 +45,15 @@ function createInjector(data?: { latestFrameworkVersion: string }) {
4345
packageInstallationManager.getLatestCompatibleVersion = async () => version;
4446

4547
const fs = injector.resolve("fs");
48+
const exists: Function = fs.exists.bind(fs);
4649
fs.readJson = () => ({ version });
50+
fs.exists = (path: string) => {
51+
if (path.includes("gradle.properties")) {
52+
return false;
53+
}
54+
return exists(path);
55+
};
56+
fs.writeFile = () => {};
4757

4858
return injector;
4959
}
@@ -149,4 +159,32 @@ describe("PlatformController", () => {
149159
// assert.deepStrictEqual(extractedPackageFromPacote, expectedPackageToAdd);
150160
// });
151161
});
162+
163+
it(`should update gradle.properties after adding android platform`, async () => {
164+
const injector = createInjector();
165+
const fs = injector.resolve("fs");
166+
167+
let writeFileCalled = false;
168+
let writeFileContents = "";
169+
fs.writeFile = (path: string, contents: string) => {
170+
if (path.includes("gradle.properties")) {
171+
writeFileCalled = true;
172+
writeFileContents = contents;
173+
}
174+
};
175+
176+
const platformController: PlatformController = injector.resolve(
177+
"platformController"
178+
);
179+
await platformController.addPlatform({
180+
projectDir,
181+
platform: "android",
182+
});
183+
184+
assert(writeFileCalled, "expected to write gradle.properties");
185+
assert(
186+
writeFileContents.includes("# App configuration"),
187+
"expected gradle.properties to have the project data written to it"
188+
);
189+
});
152190
});

0 commit comments

Comments
 (0)