// A plugin's manifest (`lecodes-plugin.json`, 2.x): what a host needs to BUILD the plugin in — the // type that registers each half, what a half asks of its platform (usage strings, entitlements, // packages, prebuilt binaries, Gradle dependencies, bundler resources). What the plugin DOES is not // here: the channels, their names and their types are the contract's (contract.d.ts), the folders // are the convention (`sdk/`, `ios/`, `android/`, `web/`). // // A platform is a key of the manifest, ALWAYS: an object where the plugin has a half, `null` where // it has none. A missing key is refused — "not supported" is a statement, not an omission. // // Desktop plugins are another system (a prebuilt library behind a C ABI) with a manifest of their // own shape; nothing of it is read here. // // A HALF FOLDER of a project (`implements: ""`) has no contract and no sdk/ of its // own: it implements the contract of a first-party plugin the SDK carries, for the platforms it has // a half of — app.json names it for those (`"ar": { "web": "./plugins/ar-8thwall" }`), the // first-party halves stay everywhere else. import { existsSync, readFileSync, statSync } from "node:fs" import { isAbsolute, join } from "node:path" import { firstPartyContractFile, firstPartyIds } from "./firstParty" export const MANIFEST_FILE = "lecodes-plugin.json" export const CONTRACT_FILE = "contract.d.ts" /** A SwiftPM dependency the iOS half links. */ export type IosPackage = { url: string, exact?: string, from?: string, products: string[] } /** What a web half asks of its bundler: a file of a package, handed over as a URL of a script that * stands alone (`worker-url`), as a URL (`url`) or as its text (`text`). */ export type WebResource = { module: string, as: "worker-url" | "url" | "text" } export type PluginManifest = { id: string name: string version: string /** The SDK the plugin is written for, as a range of its ONE version (`^2.0.0`): the major is the * contract between a bundle, the runtime and a half. */ sdk: string description?: string /** A HALF FOLDER: the first-party plugin whose contract the folder implements (it has no * contract.d.ts and no sdk/ of its own — the app's side is the SDK's wrapper). */ implements?: string ios: { /** The Swift module the half is built as in a shell. Default: `LeCodes` + the id in PascalCase. */ module?: string /** The type with `static func register(in: LeCodesEngine)`. */ register: string /** Keys of the app's Info.plist: a text (a usage string), or a list of texts (`UIBackgroundModes`). */ infoPlist?: Record entitlements?: Record packages?: IosPackage[] /** Prebuilt xcframeworks the half links, by their paths in the plugin * (`binaries/ios/QrAnchorFFI.xcframework`): folders of the plugin, never under ios/ — SwiftPM * takes a binary as a target of its own, beside the sources. Each is a binary target named after * its folder (`binaryName`), which the half's target depends on; a name is one in a package * (two plugins may not carry one alike). */ binaries?: string[] } | null android: { /** The Gradle module the half is built as in a shell. Default: `lecodes-` + the id. */ module?: string /** The object with `fun register(engine: LecodesEngine, context: Context)`, by its full name. */ register: string /** Maven coordinates; a `platform:` prefix is a BoM. */ dependencies?: string[] /** App-level Gradle plugins the shell applies (`com.google.gms.google-services`). */ gradlePlugins?: string[] } | null web: { /** The module that exports `register(host, resources?)`, relative to the plugin. */ register: string /** npm packages the half imports, as a package.json's dependencies. */ dependencies?: Record /** The half's bundler resources, by the name `register` receives them under. */ resources?: Record /** A folder of the plugin (relative to it) put beside the built module AS IS, names unhashed: a * runtime the half loads by its own url (`new URL("/x.js", import.meta.url)`), which * fetches its files relative to itself. A project's half; the first-party halves are built in * Web Lite's page with `resources`. */ static?: string } | null } export class ManifestError extends Error { constructor(readonly file: string, readonly problems: string[]) { super(`${file}:\n${problems.map(p => ` ${p}`).join("\n")}`) } } const ID = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/ const VERSION = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/ const RANGE = /^[\^~]?\d+\.\d+\.\d+$/ const PLATFORMS = ["ios", "android", "web"] as const const KNOWN = new Set(["id", "name", "version", "sdk", "description", "implements", ...PLATFORMS]) const GONE: Record = { provides: "the channels are the contract's (contract.d.ts)", js: "the wrapper is found by convention (sdk/)", desktop: "desktop plugins are another system, with a manifest of their own", } const BRANCH: Record<(typeof PLATFORMS)[number], Set> = { ios: new Set(["module", "register", "infoPlist", "entitlements", "packages", "binaries"]), android: new Set(["module", "register", "dependencies", "gradlePlugins"]), web: new Set(["register", "dependencies", "resources", "static"]), } const isObject = (v: unknown): v is Record => typeof v === "object" && v !== null && !Array.isArray(v) const isFolder = (path: string): boolean => existsSync(path) && statSync(path).isDirectory() export const pascal = (id: string): string => id.split("-").map(w => w.charAt(0).toUpperCase() + w.slice(1)).join("") const XCFRAMEWORK = ".xcframework" /** The binary target of an `ios.binaries` path: its folder's name without `.xcframework` * (`binaries/ios/QrAnchorFFI.xcframework` → `QrAnchorFFI`). */ export const binaryName = (path: string): string => path.split(/[\\/]/).pop()!.slice(0, -XCFRAMEWORK.length) /** The manifest of the plugin in `dir` (its folder's name is its id). Every problem of the file is * reported at once. */ export const readManifest = (dir: string, id: string): PluginManifest => { const file = join(dir, MANIFEST_FILE) let raw: unknown try { raw = JSON.parse(readFileSync(file, "utf8").replace(/^/, "")) } catch (e) { throw new ManifestError(file, [`not JSON: ${e instanceof Error ? e.message : String(e)}`]) } if (!isObject(raw)) throw new ManifestError(file, ["a manifest is an object"]) const problems: string[] = [] const text = (key: string, of: Record = raw, where = ""): string | undefined => { const v = of[key] if (typeof v === "string" && v.trim() !== "") return v problems.push(`${where}${key}: ${v === undefined ? "missing" : "a text"}`) return undefined } for (const key of Object.keys(raw)) { if (GONE[key]) problems.push(`${key}: gone in 2.x — ${GONE[key]}`) else if (!KNOWN.has(key)) problems.push(`${key}: not a key of a manifest`) } if (text("id") !== undefined && raw.id !== id) problems.push(`id: "${String(raw.id)}" — the plugin's folder is "${id}", and the folder is the id`) if (!ID.test(id)) problems.push(`id: "${id}" — lower-case words joined by "-"`) text("name") const version = text("version") if (version !== undefined && !VERSION.test(version)) problems.push(`version: "${version}" — a version is 2.0.0`) const sdk = text("sdk") if (sdk !== undefined && !RANGE.test(sdk)) problems.push(`sdk: "${sdk}" — a range of the SDK's version: ^2.0.0`) if (raw.description !== undefined && typeof raw.description !== "string") problems.push("description: a text") if (raw.implements !== undefined) { const impl = raw.implements if (typeof impl !== "string" || !ID.test(impl)) problems.push(`implements: the id of the first-party plugin whose contract the folder implements ("ar")`) else if (!firstPartyContractFile(impl)) problems.push(`implements: "${impl}" is no first-party plugin of this SDK (${firstPartyIds().join(", ") || "it carries none"})`) if (existsSync(join(dir, CONTRACT_FILE))) problems.push(`implements: the folder implements another plugin's contract — it has no ${CONTRACT_FILE} of its own`) if (isFolder(join(dir, "sdk"))) problems.push("implements: the folder implements another plugin — its app side is the SDK's, it has no sdk/ of its own") } for (const platform of PLATFORMS) { const branch = raw[platform] const folder = isFolder(join(dir, platform)) if (branch === undefined) { problems.push(`${platform}: missing — an object, or null where the plugin has no ${platform} half`); continue } if (branch === null) { if (folder) problems.push(`${platform}: null, and the plugin has a ${platform}/ folder`) continue } if (!isObject(branch)) { problems.push(`${platform}: an object, or null`); continue } if (!folder) problems.push(`${platform}: the plugin has no ${platform}/ folder — say null`) for (const key of Object.keys(branch)) { if (key === "sources" || key === "minSdk") problems.push(`${platform}.${key}: gone in 2.x — ${key === "sources" ? `the half is the ${platform}/ folder` : "the SDK a plugin needs is `sdk`, one for every platform"}`) else if (!BRANCH[platform].has(key)) problems.push(`${platform}.${key}: not a key of a manifest`) } const register = text("register", branch, `${platform}.`) if (platform === "android" && register !== undefined && !/^[A-Za-z_][\w]*(\.[A-Za-z_][\w]*)+$/.test(register)) { problems.push(`android.register: "${register}" — the object's full name (com.example.plugin.Plugin)`) } if (platform === "ios" && register !== undefined && !/^[A-Za-z_]\w*$/.test(register)) problems.push(`ios.register: "${register}" — a type's name`) if (platform === "web" && register !== undefined) { if (!register.startsWith("web/")) problems.push(`web.register: "${register}" — a module of the web/ folder`) else if (!existsSync(join(dir, register))) problems.push(`web.register: ${register} is not there`) } } const ios = isObject(raw.ios) ? raw.ios : null if (ios) { for (const key of ["infoPlist"] as const) { const v = ios[key] const text = (x: unknown): boolean => typeof x === "string" if (v !== undefined && (!isObject(v) || Object.values(v).some(x => !text(x) && !(Array.isArray(x) && x.length > 0 && x.every(text))))) problems.push(`ios.${key}: a key → a text, or a list of texts`) } if (ios.entitlements !== undefined && !isObject(ios.entitlements)) problems.push("ios.entitlements: an object") if (ios.packages !== undefined) { if (!Array.isArray(ios.packages)) problems.push("ios.packages: a list") else ios.packages.forEach((p, i) => { if (!isObject(p) || typeof p.url !== "string" || !Array.isArray(p.products) || p.products.length === 0 || p.products.some(x => typeof x !== "string")) { problems.push(`ios.packages[${i}]: { url, exact | from, products }`) } else if ((typeof p.exact === "string") === (typeof p.from === "string")) { problems.push(`ios.packages[${i}]: one of exact and from`) } }) } if (ios.binaries !== undefined) { if (!Array.isArray(ios.binaries)) problems.push(`ios.binaries: a list of the plugin's xcframeworks, by their paths in it ("binaries/ios/X.xcframework")`) else { const named = new Map() ios.binaries.forEach((path, i) => { const segments = typeof path === "string" ? path.split(/[\\/]/) : [] if (typeof path !== "string" || path.trim() === "" || isAbsolute(path) || segments.some(s => s === ".." || s === "." || s === "")) { problems.push(`ios.binaries[${i}]: a folder of the plugin, by its path in it ("binaries/ios/X.xcframework")`) } else if (!path.endsWith(XCFRAMEWORK)) problems.push(`ios.binaries[${i}]: "${path}" — an ${XCFRAMEWORK} folder`) else if (segments[0] === "ios") problems.push(`ios.binaries[${i}]: ${path} is in ios/, the half's sources — a binary stands beside them ("binaries/ios/X.xcframework")`) else if (!/^[A-Za-z_]\w*$/.test(binaryName(path))) problems.push(`ios.binaries[${i}]: "${path}" — the folder's name is its binary target's: a Swift name before ${XCFRAMEWORK}`) else if (!isFolder(join(dir, path))) problems.push(`ios.binaries[${i}]: ${path} is not a folder of the plugin`) else if (named.has(binaryName(path))) problems.push(`ios.binaries[${i}]: ${path} is named "${binaryName(path)}" like ${named.get(binaryName(path))} — a name is one binary target`) else named.set(binaryName(path), path) }) } } } const android = isObject(raw.android) ? raw.android : null if (android) { for (const key of ["dependencies", "gradlePlugins"] as const) { const v = android[key] if (v !== undefined && (!Array.isArray(v) || v.some(x => typeof x !== "string"))) problems.push(`android.${key}: a list of texts`) } } const web = isObject(raw.web) ? raw.web : null if (web) { const deps = web.dependencies if (deps !== undefined && (!isObject(deps) || Object.values(deps).some(x => typeof x !== "string"))) problems.push("web.dependencies: a package → its range") const resources = web.resources if (resources !== undefined) { if (!isObject(resources)) problems.push("web.resources: a name → { module, as }") else for (const [name, r] of Object.entries(resources)) { if (!/^[A-Za-z_]\w*$/.test(name)) problems.push(`web.resources.${name}: a name the half reads it under`) if (!isObject(r) || typeof r.module !== "string" || (r.as !== "worker-url" && r.as !== "url" && r.as !== "text")) { problems.push(`web.resources.${name}: { module, as: "worker-url" | "url" | "text" }`) } } } const folder = web.static if (folder !== undefined) { const segments = typeof folder === "string" ? folder.split(/[\\/]/) : [] if (typeof folder !== "string" || folder.trim() === "" || isAbsolute(folder) || segments.some((s) => s === ".." || s === "." || s === "")) { problems.push(`web.static: a folder of the plugin, by its path in it ("xr-runtime")`) } else if (!isFolder(join(dir, folder))) problems.push(`web.static: ${folder} is not a folder of the plugin`) else if (folder === "index.js") problems.push(`web.static: "index.js" is the built module's name`) } } if (problems.length) throw new ManifestError(file, problems) return raw as unknown as PluginManifest } /** The manifest, or null where the plugin has none yet (a contract is where a plugin starts). */ export const readManifestIfAny = (dir: string, id: string): PluginManifest | null => existsSync(join(dir, MANIFEST_FILE)) ? readManifest(dir, id) : null