2023-10-10 07:30:56 +00:00
|
|
|
import { exec as _exec } from 'node:child_process'
|
|
|
|
import * as fs from 'node:fs/promises'
|
|
|
|
import * as os from 'node:os'
|
|
|
|
import * as path from 'node:path'
|
|
|
|
import { env } from 'node:process'
|
|
|
|
import { promisify } from 'node:util'
|
|
|
|
|
|
|
|
import * as semver from 'semver'
|
|
|
|
|
2023-12-19 09:28:57 +00:00
|
|
|
import { linuxLibs, windowsDLLs } from './shared.mjs'
|
2023-10-10 07:30:56 +00:00
|
|
|
|
|
|
|
const exec = promisify(_exec)
|
|
|
|
const __debug = env.NODE_ENV === 'debug'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} nativeDeps
|
|
|
|
* @returns {Promise<string?>}
|
|
|
|
*/
|
|
|
|
export async function tauriUpdaterKey(nativeDeps) {
|
2024-05-07 08:36:50 +00:00
|
|
|
if (env.TAURI_SIGNING_PRIVATE_KEY) return null
|
2023-10-10 07:30:56 +00:00
|
|
|
|
|
|
|
// pnpm exec tauri signer generate -w
|
|
|
|
const privateKeyPath = path.join(nativeDeps, 'tauri.key')
|
|
|
|
const publicKeyPath = path.join(nativeDeps, 'tauri.key.pub')
|
|
|
|
const readKeys = () =>
|
|
|
|
Promise.all([
|
|
|
|
fs.readFile(publicKeyPath, { encoding: 'utf-8' }),
|
|
|
|
fs.readFile(privateKeyPath, { encoding: 'utf-8' }),
|
|
|
|
])
|
|
|
|
|
|
|
|
let privateKey, publicKey
|
|
|
|
try {
|
|
|
|
;[publicKey, privateKey] = await readKeys()
|
|
|
|
if (!(publicKey && privateKey)) throw new Error('Empty keys')
|
|
|
|
} catch (err) {
|
|
|
|
if (__debug) {
|
|
|
|
console.warn('Failed to read tauri updater keys')
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const quote = os.type() === 'Windows_NT' ? '"' : "'"
|
|
|
|
await exec(`pnpm exec tauri signer generate --ci -w ${quote}${privateKeyPath}${quote}`)
|
|
|
|
;[publicKey, privateKey] = await readKeys()
|
|
|
|
if (!(publicKey && privateKey)) throw new Error('Empty keys')
|
|
|
|
}
|
|
|
|
|
2024-05-07 08:36:50 +00:00
|
|
|
env.TAURI_SIGNING_PRIVATE_KEY = privateKey
|
|
|
|
env.TAURI_SIGNING_PRIVATE_KEY_PASSWORD = ''
|
2023-10-10 07:30:56 +00:00
|
|
|
return publicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} root
|
|
|
|
* @param {string} nativeDeps
|
2023-11-17 19:20:14 +00:00
|
|
|
* @param {string[]} targets
|
|
|
|
* @param {string[]} bundles
|
2023-10-10 07:30:56 +00:00
|
|
|
* @param {string[]} args
|
|
|
|
* @returns {Promise<string[]>}
|
|
|
|
*/
|
2023-11-17 19:20:14 +00:00
|
|
|
export async function patchTauri(root, nativeDeps, targets, bundles, args) {
|
2023-10-10 07:30:56 +00:00
|
|
|
if (args.findIndex(e => e === '-c' || e === '--config') !== -1) {
|
|
|
|
throw new Error('Custom tauri build config is not supported.')
|
|
|
|
}
|
|
|
|
|
|
|
|
const osType = os.type()
|
|
|
|
const tauriPatch = {
|
2024-05-07 08:36:50 +00:00
|
|
|
build: {
|
|
|
|
features: /** @type {string[]} */ ([]),
|
|
|
|
},
|
|
|
|
bundle: {
|
|
|
|
macOS: { minimumSystemVersion: '' },
|
|
|
|
resources: {},
|
|
|
|
},
|
|
|
|
plugins: {
|
2023-10-10 07:30:56 +00:00
|
|
|
updater: /** @type {{ pubkey?: string }} */ ({}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-19 09:28:57 +00:00
|
|
|
if (osType === 'Linux') {
|
2024-05-07 08:36:50 +00:00
|
|
|
tauriPatch.bundle.resources = await linuxLibs(nativeDeps)
|
2023-12-19 09:28:57 +00:00
|
|
|
} else if (osType === 'Windows_NT') {
|
2024-05-07 08:36:50 +00:00
|
|
|
tauriPatch.bundle.resources = {
|
2023-12-19 09:28:57 +00:00
|
|
|
...(await windowsDLLs(nativeDeps)),
|
|
|
|
[path.join(nativeDeps, 'models', 'yolov8s.onnx')]: './models/yolov8s.onnx',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Location for desktop app tauri code
|
|
|
|
const tauriRoot = path.join(root, 'apps', 'desktop', 'src-tauri')
|
2023-10-10 07:30:56 +00:00
|
|
|
const tauriConfig = await fs
|
|
|
|
.readFile(path.join(tauriRoot, 'tauri.conf.json'), 'utf-8')
|
|
|
|
.then(JSON.parse)
|
|
|
|
|
2023-11-17 19:20:14 +00:00
|
|
|
if (bundles.length === 0) {
|
2024-05-07 08:36:50 +00:00
|
|
|
const defaultBundles = tauriConfig?.bundle?.targets
|
2023-11-17 19:20:14 +00:00
|
|
|
if (Array.isArray(defaultBundles)) bundles.push(...defaultBundles)
|
|
|
|
if (bundles.length === 0) bundles.push('all')
|
|
|
|
}
|
|
|
|
|
2024-05-07 08:36:50 +00:00
|
|
|
switch (args[0]) {
|
|
|
|
case 'dev':
|
|
|
|
tauriPatch.build.features.push('devtools')
|
|
|
|
break
|
|
|
|
case 'build': {
|
|
|
|
if (tauriConfig?.plugins?.updater?.active) {
|
|
|
|
const pubKey = await tauriUpdaterKey(nativeDeps)
|
|
|
|
if (pubKey != null) tauriPatch.plugins.updater.pubkey = pubKey
|
|
|
|
}
|
|
|
|
break
|
2023-10-10 07:30:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (osType === 'Darwin') {
|
2023-11-17 19:20:14 +00:00
|
|
|
const macOSArm64MinimumVersion = '11.0'
|
2023-10-10 07:30:56 +00:00
|
|
|
|
2024-05-07 08:36:50 +00:00
|
|
|
let macOSMinimumVersion = tauriConfig?.bundle?.macOS?.minimumSystemVersion
|
2023-10-10 07:30:56 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
(targets.includes('aarch64-apple-darwin') ||
|
|
|
|
(targets.length === 0 && process.arch === 'arm64')) &&
|
|
|
|
(macOSMinimumVersion == null ||
|
|
|
|
semver.lt(
|
|
|
|
/** @type {import('semver').SemVer} */ (semver.coerce(macOSMinimumVersion)),
|
|
|
|
/** @type {import('semver').SemVer} */ (
|
|
|
|
semver.coerce(macOSArm64MinimumVersion)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
) {
|
|
|
|
macOSMinimumVersion = macOSArm64MinimumVersion
|
|
|
|
console.log(
|
|
|
|
`aarch64-apple-darwin target detected, setting minimum system version to ${macOSMinimumVersion}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (macOSMinimumVersion) {
|
|
|
|
env.MACOSX_DEPLOYMENT_TARGET = macOSMinimumVersion
|
2024-05-07 08:36:50 +00:00
|
|
|
tauriPatch.bundle.macOS.minimumSystemVersion = macOSMinimumVersion
|
2023-10-10 07:30:56 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('No minimum macOS version detected, please review tauri.conf.json')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const tauriPatchConf = path.join(tauriRoot, 'tauri.conf.patch.json')
|
|
|
|
await fs.writeFile(tauriPatchConf, JSON.stringify(tauriPatch, null, 2))
|
|
|
|
|
|
|
|
// Modify args to load patched tauri config
|
|
|
|
args.splice(1, 0, '-c', tauriPatchConf)
|
|
|
|
|
|
|
|
// Files to be removed
|
2023-12-19 09:28:57 +00:00
|
|
|
return [tauriPatchConf]
|
2023-10-10 07:30:56 +00:00
|
|
|
}
|