Skip to content

feat: add android embedding and merge main #5776

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 3 commits into from
May 28, 2024
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
18 changes: 11 additions & 7 deletions lib/controllers/prepare-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
TrackActionNames,
WEBPACK_COMPILATION_COMPLETE,
} from "../constants";
import { IWatchIgnoreListService } from "../declarations";
import { IOptions, IWatchIgnoreListService } from "../declarations";
import {
INodeModulesDependenciesBuilder,
IPlatformController,
Expand Down Expand Up @@ -59,6 +59,7 @@ export class PrepareController extends EventEmitter {
public $hooksService: IHooksService,
private $fs: IFileSystem,
private $logger: ILogger,
private $options: IOptions,
private $mobileHelper: Mobile.IMobileHelper,
private $nodeModulesDependenciesBuilder: INodeModulesDependenciesBuilder,
private $platformsDataService: IPlatformsDataService,
Expand Down Expand Up @@ -134,10 +135,13 @@ export class PrepareController extends EventEmitter {
projectData: IProjectData
): Promise<IPrepareResultData> {
await this.$projectService.ensureAppResourcesExist(projectData.projectDir);
await this.$platformController.addPlatformIfNeeded(
prepareData,
projectData
);
if (!this.$options.nativeHost) {
await this.$platformController.addPlatformIfNeeded(
prepareData,
projectData
);
}

await this.trackRuntimeVersion(prepareData.platform, projectData);

this.$logger.info("Preparing project...");
Expand Down Expand Up @@ -479,9 +483,9 @@ export class PrepareController extends EventEmitter {
console.log("!!!!! VM: proj root: " + platformData.projectRoot);
packagePath = path.join(
platformData.projectRoot,
"app",
this.$options.nativeHostModule,
"src",
"main",
this.$options.nativeHost ? "nativescript" : "main",
"assets",
"app",
"package.json"
Expand Down
12 changes: 7 additions & 5 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,16 +578,18 @@ interface IAndroidBundleOptions {
aab: boolean;
}

interface IAndroidOptions {
gradlePath: string;
gradleArgs: string;
interface IEmbedOptions {
nativeHost: string;
nativeHostModule: string;
}

interface IIOSOptions {
nativeHost: string;
interface IAndroidOptions extends IEmbedOptions {
gradlePath: string;
gradleArgs: string;
}

interface IIOSOptions extends IEmbedOptions {}

interface ITypingsOptions {
jar: string;
aar: string;
Expand Down
12 changes: 12 additions & 0 deletions lib/helpers/platform-command-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export class PlatformCommandHelper implements IPlatformCommandHelper {
frameworkPath: string,
nativeHost?: string
): Promise<void> {
if (this.$options.nativeHost) {
this.$logger.info("Ignoring platform add becuase of --android-host flag");
return;
}

const platformsDir = projectData.platformsDir;
this.$fs.ensureDirectoryExists(platformsDir);

Expand Down Expand Up @@ -86,6 +91,13 @@ export class PlatformCommandHelper implements IPlatformCommandHelper {
platforms: string[],
projectData: IProjectData
): Promise<void> {
if (this.$options.nativeHost) {
this.$logger.info(
"Ignoring platform remove becuase of --android-host flag"
);
return;
}

for (const platform of platforms) {
this.$platformValidationService.validatePlatformInstalled(
platform,
Expand Down
6 changes: 6 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ISettingsService,
} from "./common/declarations";
import { injector } from "./common/yok";
import { APP_FOLDER_NAME } from "./constants";
export class Options {
private static DASHED_OPTION_REGEX = /(.+?)([A-Z])(.*)/;
private static NONDASHED_OPTION_REGEX = /(.+?)[-]([a-zA-Z])(.*)/;
Expand Down Expand Up @@ -228,6 +229,11 @@ export class Options {
gradlePath: { type: OptionType.String, hasSensitiveValue: false },
gradleArgs: { type: OptionType.String, hasSensitiveValue: false },
nativeHost: { type: OptionType.String, hasSensitiveValue: false },
nativeHostModule: {
type: OptionType.String,
hasSensitiveValue: false,
default: APP_FOLDER_NAME,
},
aab: { type: OptionType.Boolean, hasSensitiveValue: false },
performance: { type: OptionType.Object, hasSensitiveValue: true },
appleApplicationSpecificPassword: {
Expand Down
2 changes: 1 addition & 1 deletion lib/providers/project-files-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class ProjectFilesProvider extends ProjectFilesProviderBase {
);
mappedFilePath = path.join(
platformData.appDestinationDirectoryPath,
constants.APP_FOLDER_NAME,
this.$options.nativeHostModule,
relativePath
);
}
Expand Down
31 changes: 26 additions & 5 deletions lib/services/android-plugin-build-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
INodePackageManager,
IAndroidToolsInfo,
IWatchIgnoreListService,
IOptions,
} from "../declarations";
import { IPlatformsDataService } from "../definitions/platform";
import { IProjectData, IProjectDataService } from "../definitions/project";
Expand All @@ -37,6 +38,7 @@ import { IInjector } from "../common/definitions/yok";
import { injector } from "../common/yok";
import * as _ from "lodash";
import { resolvePackageJSONPath } from "@rigor789/resolve-package-path";
import { cwd } from "process";

export class AndroidPluginBuildService implements IAndroidPluginBuildService {
private get $platformsDataService(): IPlatformsDataService {
Expand All @@ -47,6 +49,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
private $fs: IFileSystem,
private $childProcess: IChildProcess,
private $hostInfo: IHostInfo,
private $options: IOptions,
private $androidToolsInfo: IAndroidToolsInfo,
private $logger: ILogger,
private $packageManager: INodePackageManager,
Expand Down Expand Up @@ -803,6 +806,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
`-PappPath=${this.$projectData.getAppDirectoryPath()}`,
`-PappResourcesPath=${this.$projectData.getAppResourcesDirectoryPath()}`,
];

if (pluginBuildSettings.gradleArgs) {
localArgs.push(pluginBuildSettings.gradleArgs);
}
Expand All @@ -811,12 +815,29 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
localArgs.push("--quiet");
}

const opts: any = {
cwd: pluginBuildSettings.pluginDir,
stdio: "inherit",
shell: this.$hostInfo.isWindows,
};

if (this.$options.nativeHost) {
opts.env = {
USER_PROJECT_PLATFORMS_ANDROID: path.resolve(
cwd(),
this.$options.nativeHost
), // TODO: couldn't `nativeHost` have an absolute path already?
...process.env, // TODO: any other way to pass automatically the current process.env?
};
}

try {
await this.$childProcess.spawnFromEvent(gradlew, localArgs, "close", {
cwd: pluginBuildSettings.pluginDir,
stdio: "inherit",
shell: this.$hostInfo.isWindows,
});
await this.$childProcess.spawnFromEvent(
gradlew,
localArgs,
"close",
opts
);
} catch (err) {
this.$errors.fail(
`Failed to build plugin ${pluginBuildSettings.pluginName} : \n${err}`
Expand Down
22 changes: 11 additions & 11 deletions lib/services/android-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,21 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject

const appDestinationDirectoryArr = [
projectRoot,
constants.APP_FOLDER_NAME,
this.$options.nativeHostModule,
constants.SRC_DIR,
constants.MAIN_DIR,
constants.ASSETS_DIR,
];
const configurationsDirectoryArr = [
projectRoot,
constants.APP_FOLDER_NAME,
this.$options.nativeHostModule,
constants.SRC_DIR,
constants.MAIN_DIR,
constants.MANIFEST_FILE_NAME,
];
const deviceBuildOutputArr = [
projectRoot,
constants.APP_FOLDER_NAME,
this.$options.nativeHostModule,
constants.BUILD_DIR,
constants.OUTPUTS_DIR,
constants.APK_DIR,
Expand All @@ -210,7 +210,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
if (buildOptions.androidBundle) {
return path.join(
projectRoot,
constants.APP_FOLDER_NAME,
this.$options.nativeHostModule,
constants.BUILD_DIR,
constants.OUTPUTS_DIR,
constants.BUNDLE_DIR
Expand All @@ -229,8 +229,8 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
if (buildOptions.androidBundle) {
return {
packageNames: [
`${constants.APP_FOLDER_NAME}${constants.AAB_EXTENSION_NAME}`,
`${constants.APP_FOLDER_NAME}-${buildMode}${constants.AAB_EXTENSION_NAME}`,
`${this.$options.nativeHostModule}${constants.AAB_EXTENSION_NAME}`,
`${this.$options.nativeHostModule}-${buildMode}${constants.AAB_EXTENSION_NAME}`,
],
};
}
Expand All @@ -240,11 +240,11 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
`${packageName}-${buildMode}${constants.APK_EXTENSION_NAME}`,
`${projectData.projectName}-${buildMode}${constants.APK_EXTENSION_NAME}`,
`${projectData.projectName}${constants.APK_EXTENSION_NAME}`,
`${constants.APP_FOLDER_NAME}-${buildMode}${constants.APK_EXTENSION_NAME}`,
`${this.$options.nativeHostModule}-${buildMode}${constants.APK_EXTENSION_NAME}`,
],
regexes: [
new RegExp(
`(${packageName}|${constants.APP_FOLDER_NAME})-.*-(${Configurations.Debug}|${Configurations.Release})(-unsigned)?${constants.APK_EXTENSION_NAME}`,
`(${packageName}|${this.$options.nativeHostModule})-.*-(${Configurations.Debug}|${Configurations.Release})(-unsigned)?${constants.APK_EXTENSION_NAME}`,
"i"
),
],
Expand Down Expand Up @@ -570,7 +570,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
return this.$fs.exists(
path.join(
this.getPlatformData(projectData).appDestinationDirectoryPath,
constants.APP_FOLDER_NAME
this.$options.nativeHostModule
)
);
}
Expand Down Expand Up @@ -879,7 +879,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
projectData: IProjectData
): string {
const resourcePath: string[] = [
constants.APP_FOLDER_NAME,
this.$options.nativeHostModule,
constants.SRC_DIR,
constants.MAIN_DIR,
constants.RESOURCES_DIR,
Expand All @@ -895,7 +895,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
projectData: IProjectData
): string {
const resourcePath: string[] = [
constants.APP_FOLDER_NAME,
this.$options.nativeHostModule,
constants.SRC_DIR,
];

Expand Down
11 changes: 8 additions & 3 deletions lib/services/files-hash-service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { executeActionByChunks } from "../common/helpers";
import { DEFAULT_CHUNK_SIZE } from "../common/constants";
import { APP_FOLDER_NAME, HASHES_FILE_NAME } from "../constants";
import { HASHES_FILE_NAME } from "../constants";
import * as path from "path";
import * as _ from "lodash";
import { IFilesHashService } from "../definitions/files-hash-service";
import { IPlatformData } from "../definitions/platform";
import { IFileSystem, IStringDictionary } from "../common/declarations";
import { injector } from "../common/yok";
import { IOptions } from "../declarations";

export class FilesHashService implements IFilesHashService {
constructor(private $fs: IFileSystem, private $logger: ILogger) {}
constructor(
private $fs: IFileSystem,
private $logger: ILogger,
private $options: IOptions
) {}

public async generateHashes(files: string[]): Promise<IStringDictionary> {
const result: IStringDictionary = {};
Expand Down Expand Up @@ -37,7 +42,7 @@ export class FilesHashService implements IFilesHashService {
): Promise<IStringDictionary> {
const appFilesPath = path.join(
platformData.appDestinationDirectoryPath,
APP_FOLDER_NAME
this.$options.nativeHostModule
);
const files = this.$fs.enumerateFilesInDirectorySync(appFilesPath);
const hashes = await this.generateHashes(files);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AndroidDeviceLiveSyncServiceBase } from "./android-device-livesync-service-base";
import { APP_FOLDER_NAME } from "../../constants";
import { LiveSyncPaths } from "../../common/constants";
import { AndroidLivesyncTool } from "./android-livesync-tool";
import * as path from "path";
Expand All @@ -19,7 +18,8 @@ export class AndroidDeviceSocketsLiveSyncService
extends AndroidDeviceLiveSyncServiceBase
implements
IAndroidNativeScriptDeviceLiveSyncService,
INativeScriptDeviceLiveSyncService {
INativeScriptDeviceLiveSyncService
{
private livesyncTool: IAndroidLivesyncTool;
private static STATUS_UPDATE_INTERVAL = 10000;
private static MINIMAL_VERSION_LONG_LIVING_CONNECTION = "0.2.0";
Expand Down Expand Up @@ -279,7 +279,7 @@ export class AndroidDeviceSocketsLiveSyncService
);
const projectFilesPath = path.join(
platformData.appDestinationDirectoryPath,
APP_FOLDER_NAME
this.$options.nativeHostModule
);
if (!this.livesyncTool.hasConnection()) {
await this.livesyncTool.connect({
Expand Down
10 changes: 7 additions & 3 deletions lib/services/livesync/android-livesync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ import {
} from "../../common/declarations";
import { IInjector } from "../../common/definitions/yok";
import { injector } from "../../common/yok";
import { IOptions } from "../../declarations";

export class AndroidLiveSyncService
extends PlatformLiveSyncServiceBase
implements IPlatformLiveSyncService {
implements IPlatformLiveSyncService
{
private static MIN_SOCKETS_LIVESYNC_RUNTIME_VERSION = "4.2.0-2018-07-20-02";
constructor(
protected $platformsDataService: IPlatformsDataService,
protected $projectFilesManager: IProjectFilesManager,
private $injector: IInjector,
$devicePathProvider: IDevicePathProvider,
$fs: IFileSystem,
$logger: ILogger
$logger: ILogger,
$options: IOptions
) {
super(
$fs,
$logger,
$platformsDataService,
$projectFilesManager,
$devicePathProvider
$devicePathProvider,
$options
);
}

Expand Down
Loading