diff --git a/config/bsconfig.base.jsonc b/config/bsconfig.base.jsonc index 92824b39..a07ed054 100644 --- a/config/bsconfig.base.jsonc +++ b/config/bsconfig.base.jsonc @@ -88,6 +88,7 @@ "@rokucommunity/bslint", "../tools/bs-plugins/jobsystem-plugin.ts", "../tools/bs-plugins/manifest-edit-plugin.ts", + "../tools/bs-plugins/odc-plugin.ts", "../tools/bs-plugins/component-includes-plugin.ts", "../tools/bs-plugins/bindings-plugin.ts", "../tools/bs-plugins/oninit-plugin.ts", diff --git a/tools/bs-plugins/odc-plugin.ts b/tools/bs-plugins/odc-plugin.ts new file mode 100644 index 00000000..1b438643 --- /dev/null +++ b/tools/bs-plugins/odc-plugin.ts @@ -0,0 +1,86 @@ +// Plugin to inject the roku-test-automation On-Device Component (ODC) into the app. +// When included in a bsconfig, this plugin: +// 1. Replaces the `vscode_rdb_on_device_component_entry` magic comment in staged Main.brs +// with RTA_OnDeviceComponent creation (same injection point VS Code uses) +// 2. Copies RTA device component files into the staging directory +// +// Gating: this plugin is a no-op unless ALL of the following are true: +// - `options.debug` is set (i.e. this is a `--debug` build — dev or e2e, never prod). +// - The staged tree contains `source/Main.brs` (i.e. this is an app build, not a lib build). +// - That Main.brs contains the `' vscode_rdb_on_device_component_entry` magic comment. +// This makes it safe to enable globally in `config/bsconfig.base.jsonc` — prod builds +// drop the ODC because they're not `--debug`, and the lib build is skipped because it +// has no Main.brs. +import { + CompilerPlugin, + FileObj, + ProgramBuilder, +} from 'brighterscript'; +import path from 'path'; +import fs from 'fs'; + +function copyDirSync(src: string, dest: string): void { + fs.mkdirSync(dest, { recursive: true }); + for (const entry of fs.readdirSync(src, { withFileTypes: true })) { + const srcPath = path.join(src, entry.name); + const destPath = path.join(dest, entry.name); + if (entry.isDirectory()) { + copyDirSync(srcPath, destPath); + } else { + fs.copyFileSync(srcPath, destPath); + } + } +} + +const ODC_MAGIC_COMMENT = `' vscode_rdb_on_device_component_entry`; +const ODC_REPLACEMENT = `if true = CreateObject("roAppInfo").IsDev() then m.vscode_rdb_odc_node = createObject("roSGNode", "RTA_OnDeviceComponent") ' RDB OnDeviceComponent`; + +export class OdcPlugin implements CompilerPlugin { + public name = 'OdcPlugin'; + + afterPublish(builder: ProgramBuilder, _files: FileObj[]) { + const logger = builder.logger; + const stagingDir = builder.options.stagingDir!; + + // @ts-ignore - `debug` is a valid bsc option; the type may not surface it. + const debug = !!builder.options.debug; + if (!debug) { + logger.info(this.name, 'Skipping ODC injection — not a --debug build.'); + return; + } + + const mainBrsPath = path.join(stagingDir, 'source', 'Main.brs'); + if (!fs.existsSync(mainBrsPath)) { + // Library / non-app build: no Main.brs to inject into. + logger.info(this.name, `Skipping ODC injection — no source/Main.brs in ${stagingDir}.`); + return; + } + + const original = fs.readFileSync(mainBrsPath, 'utf-8'); + if (!original.includes(ODC_MAGIC_COMMENT)) { + logger.warn(this.name, 'Magic comment not found in Main.brs — RTA_OnDeviceComponent not injected.'); + return; + } + + // 1. Inject RTA_OnDeviceComponent via the magic comment in staged Main.brs. + fs.writeFileSync(mainBrsPath, original.replace(ODC_MAGIC_COMMENT, ODC_REPLACEMENT)); + logger.info(this.name, 'Injected RTA_OnDeviceComponent into Main.brs'); + + // 2. Copy RTA device files into staging. + const rtaPackageDir = path.dirname( + require.resolve('roku-test-automation/package.json') + ); + const rtaDevicePath = path.join(rtaPackageDir, 'device'); + + if (fs.existsSync(rtaDevicePath)) { + logger.info(this.name, `Copying RTA device files from ${rtaDevicePath} to ${stagingDir}`); + copyDirSync(rtaDevicePath, stagingDir); + } else { + logger.error(this.name, `RTA device files not found at ${rtaDevicePath}`); + } + } +} + +export default () => { + return new OdcPlugin(); +};