diff --git a/bin/build-native.sh b/bin/build-native.sh new file mode 100644 index 00000000..f4743b99 --- /dev/null +++ b/bin/build-native.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +set -euo pipefail + +driver_repo=$(pwd) +kernel_repo=${DATABRICKS_SQL_KERNEL_REPO:-../../databricks-sql-kernel} +napi_dir="${kernel_repo}/napi" +napi_major=$( + cargo metadata --format-version 1 --locked --manifest-path "${napi_dir}/Cargo.toml" | + node -e ' + const metadata = JSON.parse(require("fs").readFileSync(0, "utf8")); + const rootId = metadata.resolve.root ?? metadata.workspace_members[0]; + const root = metadata.resolve.nodes.find(({ id }) => id === rootId); + const napiId = root?.deps.find(({ name }) => name === "napi")?.pkg; + const version = metadata.packages.find(({ id }) => id === napiId)?.version; + process.stdout.write(version?.split(".")[0] ?? ""); + ' +) + +# napi-rs v2 and v3 derive macros expect different CLI environment variables. +case "${napi_major}" in + 2) + cli=(npx --yes @napi-rs/cli@2.18.4) + ;; + 3) + cli=(npx --yes --package @napi-rs/cli@3.8.2 napi) + ;; + *) + echo "Unsupported napi-rs major version: ${napi_major:-unknown}" >&2 + exit 1 + ;; +esac + +build_profile=${BUILD_PROFILE---release} + +cd "${napi_dir}" +if [[ -n "${build_profile//[[:space:]]/}" ]]; then + read -r -a build_profile_args <<< "${build_profile}" + "${cli[@]}" build --platform "${build_profile_args[@]}" +else + "${cli[@]}" build --platform +fi +cp index.* "${driver_repo}/native/kernel/" diff --git a/package.json b/package.json index 2ec01635..d321e802 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "test": "nyc --report-dir=${NYC_REPORT_DIR:-coverage_unit} mocha --config tests/unit/.mocharc.js", "update-version": "node bin/update-version.js && prettier --write ./lib/version.ts", "build": "npm run update-version && tsc --project tsconfig.build.json", - "build:native": "bash -c 'cd ${DATABRICKS_SQL_KERNEL_REPO:-../../databricks-sql-kernel}/napi && npx --yes @napi-rs/cli@2.18.4 build --platform ${BUILD_PROFILE:---release} && cp index.* $OLDPWD/native/kernel/'", + "build:native": "bash bin/build-native.sh", "prepack": "test -f native/kernel/index.js || { echo 'ERROR: native/kernel/index.js (napi-rs router) is missing — the published tarball would fail to load kernel. It is committed to git; run `npm run build:native` if you removed it.' >&2; exit 1; }", "watch": "tsc --project tsconfig.build.json --watch", "type-check": "tsc --noEmit",