[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
This commit is contained in:
Yulong Wang 2023-06-09 16:18:53 -07:00 committed by GitHub
parent 3b5a8352c1
commit f274bbb0c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 135 additions and 9 deletions

View file

@ -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) {

View file

@ -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
*/

7
js/common/lib/version.ts Normal file
View file

@ -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';

View file

@ -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;

7
js/node/lib/version.ts Normal file
View file

@ -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';

View file

@ -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"
}

View file

@ -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;

View file

@ -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';

View file

@ -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);

View file

@ -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;

7
js/web/lib/version.ts Normal file
View file

@ -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';

View file

@ -171,7 +171,7 @@ export interface TestRunnerCliArgs {
cudaFlags?: Record<string, unknown>;
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<TestRunnerCliArgs['globalEnvFlags']> {
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:

View file

@ -111,7 +111,7 @@ export declare namespace Test {
cudaFlags?: Record<string, unknown>;
wasmOptions?: InferenceSession.WebAssemblyExecutionProviderOption;
webglOptions?: InferenceSession.WebGLExecutionProviderOption;
globalEnvFlags?: Env;
globalEnvFlags?: Partial<Env>;
}
/**

View file

@ -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
}

View file

@ -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()