Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"make-pdf": "./make-pdf/dist/pdf"
},
"scripts": {
"build": "bun run vendor:xterm && bun run gen:skill-docs --host all; bun build --compile browse/src/cli.ts --outfile browse/dist/browse && bun build --compile browse/src/find-browse.ts --outfile browse/dist/find-browse && bun build --compile design/src/cli.ts --outfile design/dist/design && bun build --compile make-pdf/src/cli.ts --outfile make-pdf/dist/pdf && bun build --compile bin/gstack-global-discover.ts --outfile bin/gstack-global-discover && bash browse/scripts/build-node-server.sh && git rev-parse HEAD > browse/dist/.version && git rev-parse HEAD > design/dist/.version && git rev-parse HEAD > make-pdf/dist/.version && chmod +x browse/dist/browse browse/dist/find-browse design/dist/design make-pdf/dist/pdf bin/gstack-global-discover && (rm -f .*.bun-build || true)",
"build": "bun run vendor:xterm && bun run gen:skill-docs --host all; bun build --compile browse/src/cli.ts --outfile browse/dist/browse && bun build --compile browse/src/find-browse.ts --outfile browse/dist/find-browse && bun build --compile design/src/cli.ts --outfile design/dist/design && bun build --compile make-pdf/src/cli.ts --outfile make-pdf/dist/pdf && bun build --compile bin/gstack-global-discover.ts --outfile bin/gstack-global-discover && bash browse/scripts/build-node-server.sh && bash scripts/write-build-version.sh && chmod +x browse/dist/browse browse/dist/find-browse design/dist/design make-pdf/dist/pdf bin/gstack-global-discover && (rm -f .*.bun-build || true)",
"vendor:xterm": "mkdir -p extension/lib && cp node_modules/xterm/lib/xterm.js extension/lib/xterm.js && cp node_modules/xterm/css/xterm.css extension/lib/xterm.css && cp node_modules/xterm-addon-fit/lib/xterm-addon-fit.js extension/lib/xterm-addon-fit.js",
"dev:make-pdf": "bun run make-pdf/src/cli.ts",
"dev:design": "bun run design/src/cli.ts",
Expand Down
12 changes: 12 additions & 0 deletions scripts/write-build-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"

VERSION="$(git rev-parse --verify HEAD 2>/dev/null || printf unknown)"

mkdir -p browse/dist design/dist make-pdf/dist
printf '%s\n' "$VERSION" > browse/dist/.version
printf '%s\n' "$VERSION" > design/dist/.version
printf '%s\n' "$VERSION" > make-pdf/dist/.version
86 changes: 86 additions & 0 deletions test/write-build-version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { describe, test, expect } from 'bun:test';
import { spawnSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';

const ROOT = path.resolve(import.meta.dir, '..');
const SCRIPT = path.join(ROOT, 'scripts', 'write-build-version.sh');

function copyScriptToRepo(repo: string) {
const scriptDir = path.join(repo, 'scripts');
fs.mkdirSync(scriptDir, { recursive: true });
fs.copyFileSync(SCRIPT, path.join(scriptDir, 'write-build-version.sh'));
}

function readVersions(repo: string) {
return [
fs.readFileSync(path.join(repo, 'browse', 'dist', '.version'), 'utf8').trim(),
fs.readFileSync(path.join(repo, 'design', 'dist', '.version'), 'utf8').trim(),
fs.readFileSync(path.join(repo, 'make-pdf', 'dist', '.version'), 'utf8').trim(),
];
}

describe('write-build-version.sh', () => {
test('writes unknown in a git repo before the first commit exists', () => {
const repo = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-unborn-head-'));
try {
spawnSync('git', ['init', '-q'], { cwd: repo, stdio: 'pipe' });
copyScriptToRepo(repo);

const result = spawnSync('bash', ['scripts/write-build-version.sh'], {
cwd: repo,
stdio: 'pipe',
});

expect(result.status).toBe(0);
expect(readVersions(repo)).toEqual(['unknown', 'unknown', 'unknown']);
} finally {
fs.rmSync(repo, { recursive: true, force: true });
}
});

test('writes unknown when git metadata is not present', () => {
const repo = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-no-git-'));
try {
copyScriptToRepo(repo);

const result = spawnSync('bash', ['scripts/write-build-version.sh'], {
cwd: repo,
stdio: 'pipe',
});

expect(result.status).toBe(0);
expect(readVersions(repo)).toEqual(['unknown', 'unknown', 'unknown']);
} finally {
fs.rmSync(repo, { recursive: true, force: true });
}
});

test('writes the current commit sha when HEAD resolves', () => {
const repo = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-valid-head-'));
try {
spawnSync('git', ['init', '-q'], { cwd: repo, stdio: 'pipe' });
spawnSync('git', ['config', 'user.email', 'test@example.com'], { cwd: repo, stdio: 'pipe' });
spawnSync('git', ['config', 'user.name', 'Test User'], { cwd: repo, stdio: 'pipe' });
fs.writeFileSync(path.join(repo, 'README.md'), '# test\n');
spawnSync('git', ['add', 'README.md'], { cwd: repo, stdio: 'pipe' });
spawnSync('git', ['commit', '-q', '-m', 'init'], { cwd: repo, stdio: 'pipe' });
copyScriptToRepo(repo);

const expected = spawnSync('git', ['rev-parse', '--verify', 'HEAD'], {
cwd: repo,
stdio: 'pipe',
}).stdout.toString().trim();
const result = spawnSync('bash', ['scripts/write-build-version.sh'], {
cwd: repo,
stdio: 'pipe',
});

expect(result.status).toBe(0);
expect(readVersions(repo)).toEqual([expected, expected, expected]);
} finally {
fs.rmSync(repo, { recursive: true, force: true });
}
});
});