diff --git a/scripts/lib/manifest-packages.mjs b/scripts/lib/manifest-packages.mjs index 78937bc5f..f2bc3e973 100644 --- a/scripts/lib/manifest-packages.mjs +++ b/scripts/lib/manifest-packages.mjs @@ -13,7 +13,8 @@ * * Nothing here runs a build tool. The files are parsed as text. */ -import { execFileSync } from 'node:child_process' +import { execFileSync, spawnSync } from 'node:child_process' +import { fileURLToPath } from 'node:url' /** A read-only view of one repository at one revision. */ export function repositoryAt(checkout, revision) { @@ -68,6 +69,18 @@ function newestTag(tags, prefix) { } const READERS = { + /** OTP application terms are data, parsed natively without evaluating code. */ + erlang(repo) { + const manifests = repo.files().filter((path) => /^(?:apps\/[^/]+\/)?src\/[^/]+\.app\.src$/.test(path)) + if (!manifests.length) return { distributions: [], units: [] } + const input = JSON.stringify(manifests.map((manifest) => ({ manifest, text: repo.read(manifest) }))) + if (Buffer.byteLength(input) > 1048576) throw new Error('Invalid Erlang application manifest: input exceeds 1 MiB') + const result = spawnSync('escript', [fileURLToPath(new URL('./read-erlang-apps.escript', import.meta.url))], { + input, encoding: 'utf8', timeout: 5000, maxBuffer: 1048576, + }) + if (result.error || result.status !== 0) throw new Error(`Invalid Erlang application manifest: ${result.error?.message ?? result.stderr}`) + return { distributions: JSON.parse(result.stdout), units: [] } + }, /** `[project] name` and `version`. */ py(repo, { repository, manifest = 'pyproject.toml' }) { const project = section(repo.read(manifest), 'project') @@ -224,7 +237,7 @@ const READERS = { /** Which reader a port uses: a wrapper port reads its parent's repository. */ const READER_FOR = { py: 'py', ruby: 'ruby', lua: 'lua', powershell: 'powershell', julia: 'julia', ts: 'ts', rs: 'rs', go: 'go', - java: 'java', kotlin: 'java', scala: 'java', csharp: 'dotnet', fsharp: 'dotnet', cxx: 'cxx', swift: 'swift', elixir: 'elixir', + java: 'java', kotlin: 'java', scala: 'java', csharp: 'dotnet', fsharp: 'dotnet', cxx: 'cxx', swift: 'swift', elixir: 'elixir', erlang: 'erlang', } /** diff --git a/scripts/lib/read-erlang-apps.escript b/scripts/lib/read-erlang-apps.escript new file mode 100644 index 000000000..7abfb5528 --- /dev/null +++ b/scripts/lib/read-erlang-apps.escript @@ -0,0 +1,30 @@ +#!/usr/bin/env escript +%%! +S 2:2 +%% Read constant .app.src terms; never evaluate application source. +main([]) -> + "29" = erlang:system_info(otp_release), + ok = io:setopts(standard_io, [{encoding, unicode}]), + Inputs = json:decode(unicode:characters_to_binary(read([]))), + io:put_chars(json:encode([application(Input) || Input <- Inputs])). + +read(Parts) -> + case io:get_chars(standard_io, "", 65536) of + eof -> lists:append(lists:reverse(Parts)); + {error, Reason} -> error({manifest_read, Reason}); + Characters -> read([Characters | Parts]) + end. + +application(#{<<"manifest">> := Path, <<"text">> := Source}) -> + {ok, Tokens, _} = erl_scan:string(unicode:characters_to_list(Source)), + {ok, {application, Name, Properties}} = erl_parse:parse_term(Tokens), + true = is_atom(Name), + true = is_list(Properties), + [Version] = [Value || {vsn, Value} <- Properties], + true = is_list(Version) andalso Version =/= [], + true = lists:all(fun erlang:is_integer/1, Version), + VersionText = unicode:characters_to_binary(Version), + true = is_binary(VersionText), + true = byte_size(VersionText) > 0, + NameText = atom_to_binary(Name), + NameText = filename:basename(Path, <<".app.src">>), + #{name => NameText, version => VersionText, manifest => Path}. diff --git a/site/test/manifest-packages-erlang.outer.test.ts b/site/test/manifest-packages-erlang.outer.test.ts new file mode 100644 index 000000000..2d8826704 --- /dev/null +++ b/site/test/manifest-packages-erlang.outer.test.ts @@ -0,0 +1,45 @@ +import { spawnSync } from 'node:child_process' +import { describe, expect, it } from 'vitest' +import { readPort } from '../../scripts/lib/manifest-packages.mjs' + +const runtime = spawnSync('erl', ['+S', '2:2', '-noshell', '-eval', 'io:put_chars(erlang:system_info(otp_release)), halt().'], { encoding: 'utf8', timeout: 5000, maxBuffer: 1024 }) +const available = runtime.status === 0 && runtime.stdout === '29' +const ci = !!process.env.CI && !['0', 'false'].includes(process.env.CI) +const repo = (files: Record) => ({ revision: 'fixture', + files: () => Object.keys(files), read: (path: string) => files[path]!, + has: (path: string) => path in files, tags: () => [], +}) + +it('requires OTP 29 on CI for native application manifests', () => { + if (ci && !available) throw new Error('Install OTP 29 for Erlang manifest checks') +}) + +// Outer: parses real application terms with the selected OTP runtime. +describe.skipIf(!available)('Erlang application manifests', () => { + it('reads source-installed umbrella apps without inventing a package for assets', () => { + const files = { + 'apps/libtmux_erlang/src/libtmux_erlang.app.src': '% {vsn,"wrong"}\n{application, libtmux_erlang, [{description,"{vsn, wrong}"}, {vsn,"0.0.0+development"}]}.', + 'apps/libtmux_erlang_workspace/src/libtmux_erlang_workspace.app.src': '{application, libtmux_erlang_workspace, [{vsn,"0.0.0+development"}]}.', + 'apps/libtmux_erlang_mcp/README.md': 'Artwork only.', + '_build/default/lib/old/src/old.app.src': '{application, old, [{vsn,"9"}]}.', + } + expect(readPort('erlang', repo(files))).toEqual({ distributions: [ + { name: 'libtmux_erlang', version: '0.0.0+development', manifest: 'apps/libtmux_erlang/src/libtmux_erlang.app.src' }, + { name: 'libtmux_erlang_workspace', version: '0.0.0+development', manifest: 'apps/libtmux_erlang_workspace/src/libtmux_erlang_workspace.app.src' }, + ], units: [] }) + }) + + it('rejects ambiguous metadata, mismatched app names and executable expressions', () => { + for (const term of [ + '{application, probe, [{vsn,"1"},{vsn,"2"}]}.', + '{application, another, [{vsn,"1"}]}.', + '{application, probe, [{vsn, atom_to_list(probe)}]}.', + '{application, probe, [{vsn, [[]]}]}.', + '{application, probe, [{vsn, [[],[]]}]}.', + '{application, probe, [{vsn, ["1"]}]}.', + '{application, probe, [{vsn, [<<"1">>]}]}.', + ]) { + expect(() => readPort('erlang', repo({ 'src/probe.app.src': term }))).toThrow('Invalid Erlang application manifest') + } + }) +})