diff --git a/docs/man_pages/project/testing/typings.md b/docs/man_pages/project/testing/typings.md new file mode 100644 index 0000000000..ab14676511 --- /dev/null +++ b/docs/man_pages/project/testing/typings.md @@ -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 --jar ] [--aar ] [--copy-to ]` +Generate typings to iOS | `$ ns typings ios [--filter ] [--copy-to ]` + +### 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 +`` 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. +<% } %> diff --git a/docs/man_pages/start.md b/docs/man_pages/start.md index 2ba51499b5..b051aac65d 100644 --- a/docs/man_pages/start.md +++ b/docs/man_pages/start.md @@ -41,6 +41,7 @@ Command | Description [prepare ``](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 ``](project/testing/build.html) | Builds the project for the selected target platform and produces an application package or an emulator package. [deploy ``](project/testing/deploy.html) | Deploys the project to a connected physical or virtual device. +[typings ``](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 ``](project/testing/run.html) | Runs your project on a connected device or in the native emulator for the specified platform, if configured. [debug ``](project/testing/debug.html) | Debugs your project on a connected physical or virtual device. diff --git a/lib/bootstrap.ts b/lib/bootstrap.ts index 6eb120a8de..a29826aa35 100644 --- a/lib/bootstrap.ts +++ b/lib/bootstrap.ts @@ -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"); diff --git a/lib/commands/typings.ts b/lib/commands/typings.ts new file mode 100644 index 0000000000..c416716a67 --- /dev/null +++ b/lib/commands/typings.ts @@ -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 { + 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 { + 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 ." + ); + } + + 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); diff --git a/lib/declarations.d.ts b/lib/declarations.d.ts index 043c288813..67aeacc1af 100644 --- a/lib/declarations.d.ts +++ b/lib/declarations.d.ts @@ -579,6 +579,12 @@ interface IAndroidBundleOptions { aab: boolean; } +interface ITypingsOptions { + jar: string; + aar: string; + filter: string; +} + interface IOptions extends IRelease, IDeviceIdentifier, @@ -598,7 +604,8 @@ interface IOptions IPort, IEnvOptions, IPluginSeedOptions, - IGenerateOptions { + IGenerateOptions, + ITypingsOptions { argv: IYargArgv; validateOptions( commandSpecificDashedOptions?: IDictionary, diff --git a/lib/options.ts b/lib/options.ts index eb65ff4122..2c7fdee082 100644 --- a/lib/options.ts +++ b/lib/options.ts @@ -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 }, }; }