From f274bbb0c8121f47b1101327edbe9b145b0fcda8 Mon Sep 17 00:00:00 2001 From: Yulong Wang <7679871+fs-eire@users.noreply.github.com> Date: Fri, 9 Jun 2023 16:18:53 -0700 Subject: [PATCH] [js] add API that allows to get package version (#16207) ### Description Add an API for users to get version of current package. example usage: ```js import { env } from 'onnxruntime-node'; console.log(env.versions.node); // output "1.16.0" ``` ```js import { env } from 'onnxruntime-web'; console.log(env.versions.web); // output "1.16.0" console.log(env.versions.common); // output "1.16.0" console.log(env.versions.node); // output "undefined" ``` #16156 --- js/common/lib/env-impl.ts | 2 ++ js/common/lib/env.ts | 11 +++++++ js/common/lib/version.ts | 7 ++++ js/node/lib/index.ts | 5 ++- js/node/lib/version.ts | 7 ++++ js/package.json | 4 ++- js/react_native/lib/index.ts | 5 ++- js/react_native/lib/version.ts | 7 ++++ js/scripts/update-version.ts | 33 +++++++++++++++++++ js/web/lib/index.ts | 5 ++- js/web/lib/version.ts | 7 ++++ js/web/script/test-runner-cli-args.ts | 8 ++--- js/web/test/test-types.ts | 2 +- .../ci_build/github/js/pack-npm-packages.ps1 | 33 +++++++++++++++++++ tools/python/update_version.py | 8 +++++ 15 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 js/common/lib/version.ts create mode 100644 js/node/lib/version.ts create mode 100644 js/react_native/lib/version.ts create mode 100644 js/scripts/update-version.ts create mode 100644 js/web/lib/version.ts diff --git a/js/common/lib/env-impl.ts b/js/common/lib/env-impl.ts index ebc494cebe..4d6c663096 100644 --- a/js/common/lib/env-impl.ts +++ b/js/common/lib/env-impl.ts @@ -2,6 +2,7 @@ // Licensed under the MIT License. import {Env} from './env'; +import {version} from './version'; type LogLevelType = Env['logLevel']; @@ -11,6 +12,7 @@ export const env: Env = { wasm: {} as Env.WebAssemblyFlags, webgl: {} as Env.WebGLFlags, webgpu: {} as Env.WebGpuFlags, + versions: {common: version}, set logLevel(value: LogLevelType) { if (value === undefined) { diff --git a/js/common/lib/env.ts b/js/common/lib/env.ts index c58f1cf936..7bd91cf5ad 100644 --- a/js/common/lib/env.ts +++ b/js/common/lib/env.ts @@ -106,6 +106,17 @@ export interface Env { */ debug?: boolean; + /** + * Get version of the current package. + */ + readonly versions: { + common: string; + web?: string; + node?: string; + // eslint-disable-next-line @typescript-eslint/naming-convention + 'react-native'?: string; + }; + /** * Represent a set of flags for WebAssembly */ diff --git a/js/common/lib/version.ts b/js/common/lib/version.ts new file mode 100644 index 0000000000..8f597765eb --- /dev/null +++ b/js/common/lib/version.ts @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// This file is generated by /js/scripts/update-version.ts +// Do not modify file content manually. + +export const version = '1.16.0'; diff --git a/js/node/lib/index.ts b/js/node/lib/index.ts index e455f69a8f..fbea822da9 100644 --- a/js/node/lib/index.ts +++ b/js/node/lib/index.ts @@ -2,7 +2,10 @@ // Licensed under the MIT License. export * from 'onnxruntime-common'; -import {registerBackend} from 'onnxruntime-common'; +import {registerBackend, env} from 'onnxruntime-common'; import {onnxruntimeBackend} from './backend'; +import {version} from './version'; registerBackend('cpu', onnxruntimeBackend, 100); + +env.versions.node = version; diff --git a/js/node/lib/version.ts b/js/node/lib/version.ts new file mode 100644 index 0000000000..8f597765eb --- /dev/null +++ b/js/node/lib/version.ts @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// This file is generated by /js/scripts/update-version.ts +// Do not modify file content manually. + +export const version = '1.16.0'; diff --git a/js/package.json b/js/package.json index 0d57a1d86b..b542530399 100644 --- a/js/package.json +++ b/js/package.json @@ -26,9 +26,11 @@ "worker-loader": "^3.0.8" }, "scripts": { + "prepare": "tsc --build scripts", "lint": "eslint . --ext .ts --ext .tsx", "format": "clang-format --glob=\"{scripts/**/*.ts,common/lib/**/*.ts,node/{lib,script,test}/**/*.ts,node/src/**/*.{cc,h},web/{lib,script,test}/**/*.ts,react_native/{android,example,ios,lib}/**/*.{ts,mm,java}}\" --style=file -i", - "prepare-node-tests": "tsc --build scripts && node ./scripts/prepare-onnx-node-tests" + "prepare-node-tests": "node ./scripts/prepare-onnx-node-tests", + "update-version": "node ./scripts/update-version" }, "license": "MIT" } diff --git a/js/react_native/lib/index.ts b/js/react_native/lib/index.ts index 957b22aab0..a20c5c6a46 100644 --- a/js/react_native/lib/index.ts +++ b/js/react_native/lib/index.ts @@ -2,7 +2,10 @@ // Licensed under the MIT License. export * from 'onnxruntime-common'; -import {registerBackend} from 'onnxruntime-common'; +import {registerBackend, env} from 'onnxruntime-common'; import {onnxruntimeBackend} from './backend'; +import {version} from './version'; registerBackend('cpu', onnxruntimeBackend, 1); + +env.versions['react-native'] = version; diff --git a/js/react_native/lib/version.ts b/js/react_native/lib/version.ts new file mode 100644 index 0000000000..8f597765eb --- /dev/null +++ b/js/react_native/lib/version.ts @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// This file is generated by /js/scripts/update-version.ts +// Do not modify file content manually. + +export const version = '1.16.0'; diff --git a/js/scripts/update-version.ts b/js/scripts/update-version.ts new file mode 100644 index 0000000000..df6a9ea334 --- /dev/null +++ b/js/scripts/update-version.ts @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// This script update source file "version.ts" under the following folders: +// /js/${arg0}/lib/version.ts +// +// version data is read from file /js/${arg0}/package.json + +import fs from 'fs-extra'; +import path from 'path'; + +const packageName = process.argv[2]; +if (['common', 'web', 'node', 'react_native'].indexOf(packageName) === -1) { + throw new Error('expect arg0 to be one of: common,web,node,react_native'); +} + +const PACKAGE_JSON_FILE = path.join(__dirname, '..', packageName, 'package.json'); +const version = JSON.parse(fs.readFileSync(PACKAGE_JSON_FILE).toString()).version; + +if (typeof version !== 'string') { + throw new Error(`failed to parse "version" from file: ${PACKAGE_JSON_FILE}`); +} + +const FILE_CONTENT = `// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// This file is generated by /js/scripts/update-version.ts +// Do not modify file content manually. + +export const version = ${JSON.stringify(version)}; +`; + +fs.writeFileSync(path.join(__dirname, '..', packageName, 'lib', 'version.ts'), FILE_CONTENT); diff --git a/js/web/lib/index.ts b/js/web/lib/index.ts index e3851755a3..e3f2cf7300 100644 --- a/js/web/lib/index.ts +++ b/js/web/lib/index.ts @@ -7,7 +7,8 @@ // So we import code inside the if-clause to allow terser remove the code safely. export * from 'onnxruntime-common'; -import {registerBackend} from 'onnxruntime-common'; +import {registerBackend, env} from 'onnxruntime-common'; +import {version} from './version'; if (!BUILD_DEFS.DISABLE_WEBGL) { const onnxjsBackend = require('./backend-onnxjs').onnxjsBackend; @@ -24,3 +25,5 @@ if (!BUILD_DEFS.DISABLE_WASM) { registerBackend('xnnpack', wasmBackend, 9); registerBackend('webnn', wasmBackend, 9); } + +env.versions.web = version; diff --git a/js/web/lib/version.ts b/js/web/lib/version.ts new file mode 100644 index 0000000000..8f597765eb --- /dev/null +++ b/js/web/lib/version.ts @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// This file is generated by /js/scripts/update-version.ts +// Do not modify file content manually. + +export const version = '1.16.0'; diff --git a/js/web/script/test-runner-cli-args.ts b/js/web/script/test-runner-cli-args.ts index e1cc5891f0..7e3d29414a 100644 --- a/js/web/script/test-runner-cli-args.ts +++ b/js/web/script/test-runner-cli-args.ts @@ -171,7 +171,7 @@ export interface TestRunnerCliArgs { cudaFlags?: Record; wasmOptions?: InferenceSession.WebAssemblyExecutionProviderOption; webglOptions?: InferenceSession.WebGLExecutionProviderOption; - globalEnvFlags?: Env; + globalEnvFlags?: Test.Options['globalEnvFlags']; noSandbox?: boolean; } @@ -327,7 +327,7 @@ function parseWebgpuFlags(args: minimist.ParsedArgs): Env.WebGpuFlags { return {profilingMode}; } -function parseGlobalEnvFlags(args: minimist.ParsedArgs): Env { +function parseGlobalEnvFlags(args: minimist.ParsedArgs): NonNullable { const wasm = parseWasmFlags(args); const webgl = parseWebglFlags(args); const webgpu = parseWebgpuFlags(args); @@ -382,9 +382,9 @@ export function parseTestRunnerCliArgs(cmdlineArgs: string[]): TestRunnerCliArgs const globalEnvFlags = parseGlobalEnvFlags(args); - if (backend.includes('webnn') && !globalEnvFlags.wasm.proxy) { + if (backend.includes('webnn') && !globalEnvFlags.wasm!.proxy) { // Backend webnn is restricted in the dedicated worker. - globalEnvFlags.wasm.proxy = true; + globalEnvFlags.wasm!.proxy = true; } // Options: diff --git a/js/web/test/test-types.ts b/js/web/test/test-types.ts index 966b1e704a..e6afdcafd7 100644 --- a/js/web/test/test-types.ts +++ b/js/web/test/test-types.ts @@ -111,7 +111,7 @@ export declare namespace Test { cudaFlags?: Record; wasmOptions?: InferenceSession.WebAssemblyExecutionProviderOption; webglOptions?: InferenceSession.WebGLExecutionProviderOption; - globalEnvFlags?: Env; + globalEnvFlags?: Partial; } /** diff --git a/tools/ci_build/github/js/pack-npm-packages.ps1 b/tools/ci_build/github/js/pack-npm-packages.ps1 index fb14d456c3..f9bd11ebe7 100644 --- a/tools/ci_build/github/js/pack-npm-packages.ps1 +++ b/tools/ci_build/github/js/pack-npm-packages.ps1 @@ -85,6 +85,11 @@ if ($MODE -eq "dev") { pushd $JS_COMMON_DIR npm version --allow-same-version $ort_common_latest_version echo $($version_number.commit) | Out-File -Encoding ascii -NoNewline -FilePath ./__commit.txt + # update version.ts of common + pushd .. + npm run update-version common + npm run format + popd npm pack popd @@ -147,6 +152,13 @@ if ($MODE -eq "dev") { pushd $JS_COMMON_DIR npm version --allow-same-version $($version_number.version) # file __commit.txt is already generated + + # update version.ts of common + pushd .. + npm run update-version common + npm run format + popd + npm pack popd } @@ -155,6 +167,13 @@ if ($MODE -eq "dev") { pushd $JS_TARGET_DIR npm version --allow-same-version $($version_number.version) echo $($version_number.commit) | Out-File -Encoding ascii -NoNewline -FilePath ./__commit.txt + + # update version.ts of TARGET + pushd .. + npm run update-version $TARGET + npm run format + popd + npm pack popd } elseif ($MODE -eq "release") { @@ -171,11 +190,25 @@ if ($MODE -eq "dev") { pushd $JS_COMMON_DIR npm version --allow-same-version $($version_number.version) + + # update version.ts of common + pushd .. + npm run update-version common + npm run format + popd + npm pack popd pushd $JS_TARGET_DIR npm version --allow-same-version $($version_number.version) + + # update version.ts of TARGET + pushd .. + npm run update-version $TARGET + npm run format + popd + npm pack popd } diff --git a/tools/python/update_version.py b/tools/python/update_version.py index 187a6c47e3..f6e7acb646 100755 --- a/tools/python/update_version.py +++ b/tools/python/update_version.py @@ -111,6 +111,14 @@ def update_version(): run(["npm", "version", version], cwd=os.path.join(js_root, "react_native")) run(["yarn", "upgrade", "onnxruntime-common"], cwd=os.path.join(js_root, "react_native")) + # upgrade version.ts in each package + run(["npm", "ci"], cwd=js_root) + run(["npm", "run", "update-version", "common"], cwd=js_root) + run(["npm", "run", "update-version", "node"], cwd=js_root) + run(["npm", "run", "update-version", "web"], cwd=js_root) + run(["npm", "run", "update-version", "react_native"], cwd=js_root) + run(["npm", "run", "format"], cwd=js_root) + if __name__ == "__main__": update_version()