Skip to content
Closed
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
17 changes: 15 additions & 2 deletions scripts/lib/manifest-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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',
}

/**
Expand Down
30 changes: 30 additions & 0 deletions scripts/lib/read-erlang-apps.escript
Original file line number Diff line number Diff line change
@@ -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}.
45 changes: 45 additions & 0 deletions site/test/manifest-packages-erlang.outer.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>) => ({ 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')
}
})
})
Loading