Skip to content

fix: write project settings to gradle.properties #5640

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 3, 2022
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
52 changes: 46 additions & 6 deletions lib/controllers/platform-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ export class PlatformController implements IPlatformController {
private $packageInstallationManager: IPackageInstallationManager,
private $projectDataService: IProjectDataService,
private $platformsDataService: IPlatformsDataService,
private $projectChangesService: IProjectChangesService
private $projectChangesService: IProjectChangesService,
private $mobileHelper: Mobile.IMobileHelper
) {}

public async addPlatform(addPlatformData: IAddPlatformData): Promise<void> {
public async addPlatform(
addPlatformData: IAddPlatformData,
projectData?: IProjectData
): Promise<void> {
const [platform, version] = addPlatformData.platform
.toLowerCase()
.split("@");
const projectData = this.$projectDataService.getProjectData(
projectData ??= this.$projectDataService.getProjectData(
addPlatformData.projectDir
);
const platformData = this.$platformsDataService.getPlatformData(
Expand Down Expand Up @@ -68,18 +72,54 @@ export class PlatformController implements IPlatformController {
this.$fs.ensureDirectoryExists(
path.join(projectData.platformsDir, platform)
);

if (this.$mobileHelper.isAndroidPlatform(platform)) {
const gradlePropertiesPath = path.resolve(
platformData.projectRoot,
"gradle.properties"
);
const commentHeader = "# App configuration";
const appPath = projectData.getAppDirectoryRelativePath();
const appResourcesPath = projectData.getAppResourcesRelativeDirectoryPath();

let gradlePropertiesContents = "";
if (this.$fs.exists(gradlePropertiesPath)) {
gradlePropertiesContents = this.$fs
.readFile(gradlePropertiesPath)
.toString();
}

if (!gradlePropertiesContents.includes(commentHeader)) {
const dataToWrite = [
"",
"",
commentHeader,
`appPath = ${appPath}`,
`appResourcesPath = ${appResourcesPath}`,
"",
].join("\n");

gradlePropertiesContents += dataToWrite;

this.$logger.trace("Updated gradle.properties with project data...");
this.$fs.writeFile(gradlePropertiesPath, gradlePropertiesContents);
}
}
this.$logger.info(
`Platform ${platform} successfully added. v${installedPlatformVersion}`
);
}

public async addPlatformIfNeeded(
addPlatformData: IAddPlatformData
addPlatformData: IAddPlatformData,
projectData?: IProjectData
): Promise<void> {
const [platform] = addPlatformData.platform.toLowerCase().split("@");
const projectData = this.$projectDataService.getProjectData(

projectData ??= this.$projectDataService.getProjectData(
addPlatformData.projectDir
);

const platformData = this.$platformsDataService.getPlatformData(
platform,
projectData
Expand All @@ -91,7 +131,7 @@ export class PlatformController implements IPlatformController {
addPlatformData.nativePrepare
);
if (shouldAddPlatform) {
await this.addPlatform(addPlatformData);
await this.addPlatform(addPlatformData, projectData);
}
}

Expand Down
10 changes: 8 additions & 2 deletions lib/controllers/prepare-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ export class PrepareController extends EventEmitter {
prepareData: IPrepareData,
projectData: IProjectData
): Promise<IPrepareResultData> {
await this.$platformController.addPlatformIfNeeded(prepareData);
await this.$platformController.addPlatformIfNeeded(
prepareData,
projectData
);
await this.trackRuntimeVersion(prepareData.platform, projectData);

this.$logger.info("Preparing project...");
Expand Down Expand Up @@ -375,7 +378,10 @@ export class PrepareController extends EventEmitter {
projectData: IProjectData
): Promise<string[]> {
const dependencies = this.$nodeModulesDependenciesBuilder
.getProductionDependencies(projectData.projectDir, projectData.ignoredDependencies)
.getProductionDependencies(
projectData.projectDir,
projectData.ignoredDependencies
)
.filter((dep) => dep.nativescript);
const pluginsNativeDirectories = dependencies.map((dep) =>
path.join(
Expand Down
15 changes: 12 additions & 3 deletions lib/definitions/platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ interface IPrepareNodeModulesData {
}

interface INodeModulesDependenciesBuilder {
getProductionDependencies(projectPath: string, ignore?: string[]): IDependencyData[];
getProductionDependencies(
projectPath: string,
ignore?: string[]
): IDependencyData[];
}

interface IBuildInfo {
Expand Down Expand Up @@ -118,8 +121,14 @@ interface IAddPlatformData extends IControllerDataBase {
}

interface IPlatformController {
addPlatform(addPlatformData: IAddPlatformData): Promise<void>;
addPlatformIfNeeded(addPlatformData: IAddPlatformData): Promise<void>;
addPlatform(
addPlatformData: IAddPlatformData,
projectData?: IProjectData
): Promise<void>;
addPlatformIfNeeded(
addPlatformData: IAddPlatformData,
projectData?: IProjectData
): Promise<void>;
}

interface IAddPlatformService {
Expand Down
38 changes: 38 additions & 0 deletions test/controllers/add-platform-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PackageManager } from "../../lib/package-manager";
import { NodePackageManager } from "../../lib/node-package-manager";
import { YarnPackageManager } from "../../lib/yarn-package-manager";
import { PnpmPackageManager } from "../../lib/pnpm-package-manager";
import { MobileHelper } from "../../lib/common/mobile/mobile-helper";

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

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

const fs = injector.resolve("fs");
const exists: Function = fs.exists.bind(fs);
fs.readJson = () => ({ version });
fs.exists = (path: string) => {
if (path.includes("gradle.properties")) {
return false;
}
return exists(path);
};
fs.writeFile = () => {};

return injector;
}
Expand Down Expand Up @@ -149,4 +159,32 @@ describe("PlatformController", () => {
// assert.deepStrictEqual(extractedPackageFromPacote, expectedPackageToAdd);
// });
});

it(`should update gradle.properties after adding android platform`, async () => {
const injector = createInjector();
const fs = injector.resolve("fs");

let writeFileCalled = false;
let writeFileContents = "";
fs.writeFile = (path: string, contents: string) => {
if (path.includes("gradle.properties")) {
writeFileCalled = true;
writeFileContents = contents;
}
};

const platformController: PlatformController = injector.resolve(
"platformController"
);
await platformController.addPlatform({
projectDir,
platform: "android",
});

assert(writeFileCalled, "expected to write gradle.properties");
assert(
writeFileContents.includes("# App configuration"),
"expected gradle.properties to have the project data written to it"
);
});
});