Main: Add method to embed and fetch git sha

This allows for users to track which version of YALS is compiled.

Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com>
This commit is contained in:
kingbri
2025-03-02 22:58:28 -05:00
parent de7b2c11d3
commit f310af595b
6 changed files with 61 additions and 5 deletions

View File

@@ -73,7 +73,7 @@ jobs:
export CMAKE_CUDA_ARCHITECTURES="75;86;89;120"
fi
deno task bindings
deno task compile
deno task build
chmod +x YALS
- name: Copy CUDA libraries
run: |
@@ -183,7 +183,7 @@ jobs:
$env:CMAKE_CUDA_ARCHITECTURES="75;86;89;120"
}
deno task bindings-win
deno task compile
deno task build
- name: Copy vcruntime
run: |
cp "C:\Windows\System32\vcruntime140.dll" lib

8
.gitignore vendored
View File

@@ -45,3 +45,11 @@ templates/*
!templates/place_your_templates_here.txt
!templates/alpaca.jinja
!templates/chatml.jinja
# Compiled binaries and embedded assets
gitSha.txt
YALS.exe
YALS
# Markdown
.obsidian/

View File

@@ -9,3 +9,27 @@ export function asyncDefer(callback: () => Promise<void>): AsyncDisposable {
[Symbol.asyncDispose]: async () => await callback(),
};
}
export async function getCommitSha() {
const cmd = new Deno.Command("git", {
args: ["rev-parse", "--short", "HEAD"],
});
try {
const { stdout } = await cmd.output();
const sha = new TextDecoder().decode(stdout).trim();
return sha;
} catch (error) {
console.error(`Failed to get commit SHA: ${error}`);
return undefined;
}
}
export async function getYalsVersion() {
try {
return Deno.readTextFileSync(`${import.meta.dirname}/gitSha.txt`)
.trim();
} catch {
return await getCommitSha();
}
}

View File

@@ -2,9 +2,11 @@
"tasks": {
"dev": "deno run -A --watch main.ts",
"start": "deno run --allow-read --allow-write=api_tokens.yml --allow-env --allow-sys --allow-net --allow-ffi main.ts",
"compile": "deno compile --allow-read --allow-write=api_tokens.yml --allow-env --allow-sys --allow-net --allow-ffi main.ts",
"bindings": "cd bindings && ./bindings.sh",
"bindings-win": "cd bindings && powershell -ExecutionPolicy Bypass -File bindings.ps1"
"bindings-win": "cd bindings && powershell -ExecutionPolicy Bypass -File bindings.ps1",
"generate-sha": "deno run --allow-run --allow-write=gitSha.txt generateGitSha.ts",
"compile": "deno compile --allow-read --allow-write=api_tokens.yml --allow-env --allow-sys --allow-net --allow-ffi --include=gitSha.txt main.ts",
"build": "deno task generate-sha && deno task compile"
},
"imports": {
"@/": "./",

12
generateGitSha.ts Normal file
View File

@@ -0,0 +1,12 @@
import { getCommitSha } from "@/common/utils.ts";
if (import.meta.main) {
const sha = await getCommitSha();
if (sha) {
await Deno.writeTextFile("gitSha.txt", sha);
console.log(`Successfully wrote Git SHA (${sha}) to gitSha.txt.`);
} else {
console.log("Failed to write Git SHA due to the errors above.");
}
}

12
main.ts
View File

@@ -2,12 +2,22 @@ import { createApi } from "@/api/server.ts";
import { loadAuthKeys } from "@/common/auth.ts";
import { parseArgs } from "@/common/args.ts";
import { config, loadConfig } from "@/common/config.ts";
import { setupLogger } from "@/common/logging.ts";
import { logger, setupLogger } from "@/common/logging.ts";
import { loadModel } from "@/common/modelContainer.ts";
import { getYalsVersion } from "@/common/utils.ts";
if (import.meta.main) {
await setupLogger();
// Use Promise resolution to avoid nested try/catch
const version = await getYalsVersion();
if (version) {
logger.info(`Using YALS commit ${version}`);
} else {
logger.info("Could not find YALS commit version. Launching anyway.");
}
// Parse CLI args
const { args, usage } = parseArgs();