spacedrive/scripts/utils/patchTauri.mjs
Ericson "Fogo" Soares 7c90bcb95b
[ENG-1479] AI Prototype (#1845)
* First draft on image labeling

* Fixing execution providers for other OSs

* Better error handling and shutdown

* Working with shallow media processor

* bruh

* Fix warnings

* Now hooked to media processor job

* Link desktop app with libonnxruntime to avoid TLS error during startup

* Be able to change models on runtime
Revert to use labels table instead of tags

* A bug on a model-less inference

* Show AI labels on Inspector
 - Change yolo inference to use half precision
 - Add labels api to core

* Remove LD_PRELOAD

* Fix race condition on model executor shutdown

* Don't load all images in memory moron

* Embeed yolo model in prod build
 - Change yolo model path to new one relative to executable

* Disable volume watcher on linux, it was crashing the app
 - Invalidate labels when they are updated

* Rust fmt

* Minor changes

* Gate onnxruntime linking to the ai-models feature

* Add build script to sd-server to handle onnxruntime linking workaround

* Move AI stuff to its own crate and normalize deps

* Rust fmt

* Don't regenerate labels unless asked to

* Now blazingly fast

* Bad merge

* Fix

* Fix

* Add backend logic to download extra yolo models

* Add models api route
 - Add api call to get available model version
 - Add api call to change the model version

* Improve new model download logic
 - Add frontend to change image labeler model

* Fix new model downloader

* Fix model select width

* invalidate labels count after media_processor generates a new output

* Rename AI crate and first draft on download notifications

* fix types

---------

Co-authored-by: Vítor Vasconcellos <vasconcellos.dev@gmail.com>
Co-authored-by: Brendan Allan <brendonovich@outlook.com>
2023-12-19 09:28:57 +00:00

143 lines
4.1 KiB
JavaScript

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'
import { linuxLibs, windowsDLLs } from './shared.mjs'
const exec = promisify(_exec)
const __debug = env.NODE_ENV === 'debug'
/**
* @param {string} nativeDeps
* @returns {Promise<string?>}
*/
export async function tauriUpdaterKey(nativeDeps) {
if (env.TAURI_PRIVATE_KEY) return null
// 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')
}
env.TAURI_PRIVATE_KEY = privateKey
env.TAURI_KEY_PASSWORD = ''
return publicKey
}
/**
* @param {string} root
* @param {string} nativeDeps
* @param {string[]} targets
* @param {string[]} bundles
* @param {string[]} args
* @returns {Promise<string[]>}
*/
export async function patchTauri(root, nativeDeps, targets, bundles, args) {
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 = {
tauri: {
bundle: {
macOS: { minimumSystemVersion: '' },
resources: {},
},
updater: /** @type {{ pubkey?: string }} */ ({}),
},
}
if (osType === 'Linux') {
tauriPatch.tauri.bundle.resources = await linuxLibs(nativeDeps)
} else if (osType === 'Windows_NT') {
tauriPatch.tauri.bundle.resources = {
...(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')
const tauriConfig = await fs
.readFile(path.join(tauriRoot, 'tauri.conf.json'), 'utf-8')
.then(JSON.parse)
if (bundles.length === 0) {
const defaultBundles = tauriConfig.tauri?.bundle?.targets
if (Array.isArray(defaultBundles)) bundles.push(...defaultBundles)
if (bundles.length === 0) bundles.push('all')
}
if (args[0] === 'build') {
if (tauriConfig?.tauri?.updater?.active) {
const pubKey = await tauriUpdaterKey(nativeDeps)
if (pubKey != null) tauriPatch.tauri.updater.pubkey = pubKey
}
}
if (osType === 'Darwin') {
const macOSArm64MinimumVersion = '11.0'
let macOSMinimumVersion = tauriConfig?.tauri?.bundle?.macOS?.minimumSystemVersion
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
tauriPatch.tauri.bundle.macOS.minimumSystemVersion = macOSMinimumVersion
} 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
return [tauriPatchConf]
}