Skip to content

feat: ns typings #5551

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 7 commits into from
Jul 22, 2021
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: 52 additions & 0 deletions docs/man_pages/project/testing/typings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<% if (isJekyll) { %>---
title: ns typings
position: 25
---<% } %>

# ns typings

### Description

Generate iOS & Android typings by default (respecting platform support, so no ios typings generated on windows/linux machines)

### Commands

Usage | Synopsis
---|---
Generate typings to Android | `$ ns typings android [--jar <Jar1> --jar <Jar2>] [--aar <Aar1>] [--copy-to <Path>]`
Generate typings to iOS | `$ ns typings ios [--filter <Filter>] [--copy-to <Path>]`

### Options

* `--jar` - Path to jar file to generate typings for (Supports multiple by passing them individually) (Android only)
* `--aar` - Path to aar file to generate typings for (Currently unsupported) (Android only)
* `--filter` - Regex to filter typings (Currently unsupported) (iOS only)

* `--copy-to` - Copy generated typings to the specified folder

<% if((isConsole && isMacOS) || isHtml) { %>### Arguments
`<Platform>` is the target mobile platform for which you want to generate typings. You can set the following target platforms:
* `android` - Generate typings for android.
* `ios` - Generate typings for iOS.



### Related Commands

Command | Description
----------|----------
[appstore](../../publishing/appstore.html) | Lists applications registered in iTunes Connect.
[appstore upload](../../publishing/appstore-upload.html) | Uploads project to iTunes Connect.
[build android](build-android.html) | Builds the project for Android and produces an APK that you can manually deploy on device or in the native emulator.
[build ios](build-ios.html) | Builds the project for iOS and produces an APP or IPA that you can manually deploy in the iOS Simulator or on device, respectively.
[build](build.html) | Builds the project for the selected target platform and produces an application package that you can manually deploy on device or in the native emulator.
[debug android](debug-android.html) | Debugs your project on a connected Android device or in a native emulator.
[debug ios](debug-ios.html) | Debugs your project on a connected iOS device or in a native emulator.
[debug](debug.html) | Debugs your project on a connected device or in a native emulator.
[deploy](deploy.html) | Builds and deploys the project to a connected physical or virtual device.
[run android](run-android.html) | Runs your project on a connected Android device or in a native Android emulator, if configured.
[run ios](run-ios.html) | Runs your project on a connected iOS device or in the iOS Simulator, if configured.
[test init](test-init.html) | Configures your project for unit testing with a selected framework.
[test android](test-android.html) | Runs the tests in your project on Android devices or native emulators.
[test ios](test-ios.html) | Runs the tests in your project on iOS devices or the iOS Simulator.
<% } %>
1 change: 1 addition & 0 deletions docs/man_pages/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Command | Description
[prepare `<Platform>`](project/configuration/prepare.html) | Copies relevant content from the app directory to the subdirectory for the selected target platform to let you build the project.
[build `<Platform>`](project/testing/build.html) | Builds the project for the selected target platform and produces an application package or an emulator package.
[deploy `<Platform>`](project/testing/deploy.html) | Deploys the project to a connected physical or virtual device.
[typings `<Platform>`](project/testing/typings.html) | Generate iOS & Android typings
[run](project/testing/run.html) | Runs your project on a connected device or in the native emulator, if configured.
[run `<Platform>`](project/testing/run.html) | Runs your project on a connected device or in the native emulator for the specified platform, if configured.
[debug `<Platform>`](project/testing/debug.html) | Debugs your project on a connected physical or virtual device.
Expand Down
1 change: 1 addition & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ injector.requireCommand("platform|update", "./commands/update-platform");
injector.requireCommand("run|*all", "./commands/run");
injector.requireCommand("run|ios", "./commands/run");
injector.requireCommand("run|android", "./commands/run");
injector.requireCommand("typings", "./commands/typings");

injector.requireCommand("preview", "./commands/preview");

Expand Down
128 changes: 128 additions & 0 deletions lib/commands/typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { IOptions } from "../declarations";
import { IChildProcess, IFileSystem } from "../common/declarations";
import { ICommand, ICommandParameter } from "../common/definitions/commands";
import { injector } from "../common/yok";
import { IProjectData } from "../definitions/project";
import * as path from "path";

export class TypingsCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];
constructor(
private $logger: ILogger,
private $options: IOptions,
private $fs: IFileSystem,
private $projectData: IProjectData,
private $mobileHelper: Mobile.IMobileHelper,
private $childProcess: IChildProcess
) {}

public async execute(args: string[]): Promise<void> {
const platform = args[0];

if (this.$mobileHelper.isAndroidPlatform(platform)) {
await this.handleAndroidTypings();
}

if (this.$mobileHelper.isiOSPlatform(platform)) {
await this.handleiOSTypings();
}
let typingsFolder = "./typings";
if (this.$options.copyTo) {
this.$fs.copyFile(
path.resolve(this.$projectData.projectDir, "typings"),
this.$options.copyTo
);
typingsFolder = this.$options.copyTo;
}
this.$logger.info(
"Typing have been generated in the following directory:",
typingsFolder
);
}

public async canExecute(args: string[]): Promise<boolean> {
const platform = args[0];
this.$mobileHelper.validatePlatformName(platform);
return true;
}

private async handleAndroidTypings() {
if (this.$options.aar) {
return this.$logger.warn(`Open the .aar archive
Extract the classes.jar and any dependencies it may have inside libs/
Rename classes.jar if necessary

ns typings android --jar classes.jar --jar dependency-of-classes-jar.jar
`);
} else if (!this.$options.jar) {
return this.$logger.warn(
"No .jar file specified. Please specify a .jar file with --jar <Jar>."
);
}

this.$fs.ensureDirectoryExists(
path.resolve(this.$projectData.projectDir, "typings", "android")
);

const dtsGeneratorPath = path.resolve(
this.$projectData.projectDir,
"platforms",
"android",
"build-tools",
"dts-generator.jar"
);
if (!this.$fs.exists(dtsGeneratorPath)) {
this.$logger.warn("No platforms folder found, preparing project now...");
await this.$childProcess.spawnFromEvent(
"ns",
["prepare", "android"],
"exit",
{ stdio: "inherit" }
);
}

if (this.$options.jar) {
const jars: string[] =
typeof this.$options.jar === "string"
? [this.$options.jar]
: this.$options.jar;
await this.$childProcess.spawnFromEvent(
"java",
[
"-jar",
dtsGeneratorPath,
"-input",
...jars,
"-output",
path.resolve(this.$projectData.projectDir, "typings", "android"),
],
"exit",
{ stdio: "inherit" }
);
}
}

private async handleiOSTypings() {
if (this.$options.filter !== undefined) {
this.$logger.warn("--filter flag is not supported yet.");
}

this.$fs.ensureDirectoryExists(
path.resolve(this.$projectData.projectDir, "typings", "ios")
);

await this.$childProcess.spawnFromEvent("ns", ["build", "ios"], "exit", {
env: {
...process.env,
TNS_TYPESCRIPT_DECLARATIONS_PATH: path.resolve(
this.$projectData.projectDir,
"typings",
"ios"
),
},
stdio: "inherit",
});
}
}

injector.registerCommand("typings", TypingsCommand);
9 changes: 8 additions & 1 deletion lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,12 @@ interface IAndroidBundleOptions {
aab: boolean;
}

interface ITypingsOptions {
jar: string;
aar: string;
filter: string;
}

interface IOptions
extends IRelease,
IDeviceIdentifier,
Expand All @@ -598,7 +604,8 @@ interface IOptions
IPort,
IEnvOptions,
IPluginSeedOptions,
IGenerateOptions {
IGenerateOptions,
ITypingsOptions {
argv: IYargArgv;
validateOptions(
commandSpecificDashedOptions?: IDictionary<IDashedOption>,
Expand Down
3 changes: 3 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ export class Options {
hasSensitiveValue: true,
},
appleSessionBase64: { type: OptionType.String, hasSensitiveValue: true },
jar: { type: OptionType.String, hasSensitiveValue: true },
aar: { type: OptionType.String, hasSensitiveValue: true },
filter: { type: OptionType.String, hasSensitiveValue: true },
};
}

Expand Down