Skip to content

Commit 350a098

Browse files
committed
✨ Add library entry for other Blockbench tools
- Lets animated-java-gametest reuse the headless driver instead of copying it - Environment name keeps each tool's Blockbench profile separate
1 parent 81fc728 commit 350a098

6 files changed

Lines changed: 64 additions & 19 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ bun run build # bundles src/ -> dist/index.cjs (committed; CI verifies it is
129129
`dist/index.cjs` is committed because a composite action runs straight from the
130130
checked-out repo. Rebuild it whenever `src/` changes.
131131

132+
Other tools can drive Blockbench the same way by importing from
133+
`animated-java-ci/lib` (see [`src/lib.ts`](src/lib.ts)). Bun runs it straight
134+
from source, so it needs no build.
135+
132136
## License
133137

134138
MIT

‎dist/index.cjs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11546,7 +11546,7 @@ function oneLine(message) {
1154611546

1154711547
// src/blockbench.ts
1154811548
var delay = (ms) => new Promise((resolve2) => setTimeout(resolve2, ms));
11549-
var ENVIRONMENT = "animated-java-ci";
11549+
var DEFAULT_ENVIRONMENT = "animated-java-ci";
1155011550
function getFreePort() {
1155111551
return new Promise((resolve2, reject) => {
1155211552
const server = (0, import_node_net.createServer)();
@@ -11567,12 +11567,12 @@ function numericSemver(a, b2) {
1156711567
}
1156811568
return 0;
1156911569
}
11570-
async function ensurePortable(eb, blockbenchVersion) {
11571-
if (await eb.environmentExists(ENVIRONMENT) !== "env") {
11570+
async function ensurePortable(eb, environment, blockbenchVersion) {
11571+
if (await eb.environmentExists(environment) !== "env") {
1157211572
info(`Provisioning Blockbench (${blockbenchVersion}) via envbench...`);
1157311573
let lastPercent = -1;
1157411574
await eb.createEnvironment(
11575-
ENVIRONMENT,
11575+
environment,
1157611576
{ blockbenchVersion, force: true },
1157711577
{
1157811578
onDownloadStart: (version2) => info(`Downloading Blockbench ${version2}`),
@@ -11586,7 +11586,7 @@ async function ensurePortable(eb, blockbenchVersion) {
1158611586
}
1158711587
);
1158811588
}
11589-
const env = await eb.getEnvironment(ENVIRONMENT);
11589+
const env = await eb.getEnvironment(environment);
1159011590
let version = "";
1159111591
try {
1159211592
version = await eb.resolveVersion(env.blockbench_version);
@@ -11604,21 +11604,21 @@ async function ensurePortable(eb, blockbenchVersion) {
1160411604
}
1160511605
return portable;
1160611606
}
11607-
async function launchBlockbench(blockbenchVersion) {
11607+
async function launchBlockbench(blockbenchVersion, environment = DEFAULT_ENVIRONMENT) {
1160811608
const eb = new se();
1160911609
await eb.ensureStorageFolder();
11610-
const userDataDir = (0, import_node_path2.join)(eb.storageDir, ENVIRONMENT);
11610+
const userDataDir = (0, import_node_path2.join)(eb.storageDir, environment);
1161111611
await killByUserData(userDataDir);
1161211612
for (const lock of ["SingletonLock", "SingletonSocket", "SingletonCookie"]) {
1161311613
try {
1161411614
(0, import_node_fs3.rmSync)((0, import_node_path2.join)(userDataDir, lock), { force: true });
1161511615
} catch {
1161611616
}
1161711617
}
11618-
await ensurePortable(eb, blockbenchVersion);
11618+
await ensurePortable(eb, environment, blockbenchVersion);
1161911619
grantFsPermission(userDataDir, "animated_java");
1162011620
const debugPort = await getFreePort();
11621-
const child = await eb.launch(ENVIRONMENT, {
11621+
const child = await eb.launch(environment, {
1162211622
extraArgs: [
1162311623
`--remote-debugging-port=${debugPort}`,
1162411624
"--remote-allow-origins=*",

‎package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"url": "https://github.com/Animated-Java/animated-java-ci"
1111
},
1212
"type": "module",
13+
"exports": {
14+
"./lib": "./src/lib.ts"
15+
},
1316
"scripts": {
1417
"build": "esbuild src/main.ts --bundle --platform=node --target=node20 --format=cjs --outfile=dist/index.cjs --banner:js=\"/* Generated by \\`bun run build\\` - edit src/ instead. */\"",
1518
"typecheck": "tsc --noEmit",

‎src/blockbench.ts‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import * as log from './log'
99

1010
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
1111

12-
/** Fixed envbench environment name - one isolated Blockbench install for the action. */
13-
const ENVIRONMENT = 'animated-java-ci'
12+
/** Default envbench environment name - one isolated Blockbench install for the action. */
13+
const DEFAULT_ENVIRONMENT = 'animated-java-ci'
1414

1515
export interface RunningBlockbench {
1616
debugPort: number
@@ -46,12 +46,16 @@ function numericSemver(a: string, b: string): number {
4646
* Ensure the isolated Blockbench environment exists (downloading Blockbench on
4747
* first use) and return the path to its portable executable.
4848
*/
49-
async function ensurePortable(eb: Envbench, blockbenchVersion: string): Promise<string> {
50-
if ((await eb.environmentExists(ENVIRONMENT)) !== 'env') {
49+
async function ensurePortable(
50+
eb: Envbench,
51+
environment: string,
52+
blockbenchVersion: string
53+
): Promise<string> {
54+
if ((await eb.environmentExists(environment)) !== 'env') {
5155
log.info(`Provisioning Blockbench (${blockbenchVersion}) via envbench...`)
5256
let lastPercent = -1
5357
await eb.createEnvironment(
54-
ENVIRONMENT,
58+
environment,
5559
{ blockbenchVersion: blockbenchVersion as NamedBlockbenchVersion, force: true },
5660
{
5761
onDownloadStart: version => log.info(`Downloading Blockbench ${version}`),
@@ -66,7 +70,7 @@ async function ensurePortable(eb: Envbench, blockbenchVersion: string): Promise<
6670
)
6771
}
6872

69-
const env = await eb.getEnvironment(ENVIRONMENT)
73+
const env = await eb.getEnvironment(environment)
7074
let version: ResolvedBlockbenchVersion | '' = ''
7175
try {
7276
version = await eb.resolveVersion(env.blockbench_version)
@@ -91,11 +95,16 @@ async function ensurePortable(eb: Envbench, blockbenchVersion: string): Promise<
9195
* Launch Blockbench with the Chrome DevTools Protocol enabled and wait until its
9296
* endpoint answers. The caller is expected to already be running under
9397
* `xvfb-run` (the action wrapper does this) so no window appears.
98+
*
99+
* `environment` names the isolated envbench install, so separate tools don't share one.
94100
*/
95-
export async function launchBlockbench(blockbenchVersion: string): Promise<RunningBlockbench> {
101+
export async function launchBlockbench(
102+
blockbenchVersion: string,
103+
environment = DEFAULT_ENVIRONMENT
104+
): Promise<RunningBlockbench> {
96105
const eb = new Envbench()
97106
await eb.ensureStorageFolder()
98-
const userDataDir = join(eb.storageDir, ENVIRONMENT)
107+
const userDataDir = join(eb.storageDir, environment)
99108

100109
// Clear a stale Chromium singleton lock a previously hard-killed run may have left.
101110
await killByUserData(userDataDir)
@@ -107,11 +116,11 @@ export async function launchBlockbench(blockbenchVersion: string): Promise<Runni
107116
}
108117
}
109118

110-
await ensurePortable(eb, blockbenchVersion)
119+
await ensurePortable(eb, environment, blockbenchVersion)
111120
grantFsPermission(userDataDir, 'animated_java')
112121
const debugPort = await getFreePort()
113122

114-
const child = await eb.launch(ENVIRONMENT, {
123+
const child = await eb.launch(environment, {
115124
extraArgs: [
116125
`--remote-debugging-port=${debugPort}`,
117126
'--remote-allow-origins=*',

‎src/lib.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Library entry for other tools that drive Blockbench headlessly
3+
* (e.g. animated-java-gametest). The action itself starts at `main.ts`.
4+
*/
5+
export { launchBlockbench, type RunningBlockbench } from './blockbench'
6+
export { RendererBridge } from './cdp'
7+
export {
8+
type ExportOptions,
9+
type ExportResult,
10+
type LoadResult,
11+
type PluginLoadResult,
12+
type RendererBlueprintSettings,
13+
rendererExport,
14+
rendererLoadBlueprint,
15+
rendererLoadPlugin,
16+
} from './renderer'

‎test/lib.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import * as lib from '../src/lib'
3+
4+
// animated-java-gametest imports these; a rename here breaks it silently otherwise.
5+
describe('lib', () => {
6+
it('exports the Blockbench driver', () => {
7+
expect(typeof lib.launchBlockbench).toBe('function')
8+
expect(typeof lib.RendererBridge.attach).toBe('function')
9+
expect(typeof lib.rendererLoadPlugin).toBe('function')
10+
expect(typeof lib.rendererLoadBlueprint).toBe('function')
11+
expect(typeof lib.rendererExport).toBe('function')
12+
})
13+
})

0 commit comments

Comments
 (0)