Skip to content
Merged
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
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func main() {
| hackage | *.cabal, stack.yaml, cabal.project | stack.yaml.lock, cabal.config, cabal.project.freeze |
| haxelib | haxelib.json | |
| helm | Chart.yaml, requirements.yaml | Chart.lock, requirements.lock |
| hex | mix.exs, gleam.toml | mix.lock, rebar.lock |
| hex | mix.exs, gleam.toml, rebar.config, *.app.src | mix.lock, rebar.lock |
| ips | *.p5m | |
| julia | Project.toml, REQUIRE | Manifest.toml |
| lean | lakefile.toml, lakefile.lean | lake-manifest.json |
Expand Down Expand Up @@ -124,6 +124,31 @@ and `source` constraints carry no version and are skipped. Entries inside
`if`, `elif` and `else` blocks keep the condition in `Marker`, for example
`!(os(windows)) && impl(ghc >= 9.6)`, without evaluating it.

### Erlang output

`rebar.config` and `*.app.src` are read as Erlang terms, the way
`file:consult/1` reads them, so comments, quoted atoms, binaries and maps are
handled. A file that is not valid term syntax, such as one using macros, is a
parse error, as is one with terms nested more than 100 levels deep.

`rebar.config` reports top-level `deps` with runtime scope under a
`deps/<app>` location. `deps` inside `profiles` use test scope for the `test`
profile, runtime scope for `prod` and development scope for any other profile.
`plugins` and `project_plugins` use build scope wherever they appear. A version
string is kept as written, such as `"~> 3.9"`. `{App, {pkg, Name}}` reports the
Hex package `Name` while the location keeps `App`. Git sources keep a `branch`,
`tag` or `ref` in `Branch`, `Tag` or `Ref`, while `git_subdir` keeps its
directory in `Rel`. Other version control sources use the `url` kind. A rebar2
version regex next to a source, such as `".*"`, is not a version and is dropped.

`*.app.src` sets `Name` and `Version` from the application name and `vsn`, as
well as `Licenses` from `licenses`. A `vsn` of `git` or a command tuple is not
a version, so it is left empty. Entries in `applications` and
`included_applications` are runtime dependencies without versions, while those
in `optional_applications` have optional scope. Applications that ship with
Erlang/OTP or Elixir, such as `kernel`, `stdlib` and `logger`, are not packages,
so they are skipped.

### vcpkg CONTROL output

Classic vcpkg ports declare their dependencies in `ports/<port>/CONTROL`. The
Expand Down Expand Up @@ -601,6 +626,8 @@ unexpected type is treated as absent rather than failing the parse. `.nuspec`
fills `Summary` and `Description` verbatim, `Keywords` from `tags` split on
white space, `Homepage` from `projectUrl` plus `Readme` from `readme`, with the
`repository` URL, `licenseUrl` and `iconUrl` as `Links`.
`*.app.src` fills `Description` from `description`, with each `links` entry as
a `Links` item under its own label.

### Registry

Expand Down
100 changes: 100 additions & 0 deletions internal/rebar/appsrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package rebar

import (
"github.com/git-pkgs/manifests/internal/core"
)

func init() {
core.Register("hex", core.Manifest, &appSrcParser{}, core.SuffixMatch(".app.src"))
}

// appSrcParser parses OTP application resource files (*.app.src).
type appSrcParser struct{}

// otpApplications ships with Erlang/OTP or Elixir, so it is not a package.
var otpApplications = map[string]bool{
"asn1": true, "common_test": true, "compiler": true, "crypto": true, "debugger": true,
"dialyzer": true, "diameter": true, "edoc": true, "eldap": true, "erl_docgen": true,
"erl_interface": true, "erts": true, "et": true, "eunit": true, "ftp": true,
"hipe": true, "inets": true, "jinterface": true, "kernel": true, "megaco": true,
"mnesia": true, "observer": true, "odbc": true, "os_mon": true, "otp_mibs": true,
"parsetools": true, "public_key": true, "reltool": true, "runtime_tools": true,
"sasl": true, "snmp": true, "ssh": true, "ssl": true, "stdlib": true,
"syntax_tools": true, "tftp": true, "tools": true, "wx": true, "xmerl": true,
"elixir": true, "eex": true, "ex_unit": true, "iex": true, "logger": true, "mix": true,
}

func (p *appSrcParser) Parse(filename string, content []byte) (*core.Result, error) {
terms, err := parseErlangTerms(string(content))
if err != nil {
return nil, &core.ParseError{Filename: filename, Err: err}
}
result := &core.Result{}
if len(terms) == 0 {
return result, nil
}
application, ok := terms[0].tagged("application")
if !ok || len(application) != 2 || application[0].kind != termAtom {
return result, nil
}
result.Name = application[0].value
properties := application[1]

if vsn, ok := property(properties, "vsn"); ok && vsn.kind == termString && vsn.value != "git" {
result.Version = vsn.value
}
if description, ok := property(properties, "description"); ok && description.kind == termString {
value := description.value
result.Metadata.Description = &value
}
if licenses, ok := property(properties, "licenses"); ok {
for _, license := range licenses.items {
if license.kind == termString && license.value != "" {
result.Licenses = append(result.Licenses, license.value)
}
}
}
if links, ok := property(properties, "links"); ok {
for _, link := range links.items {
if link.kind == termTuple && len(link.items) == 2 && link.items[0].kind == termString && link.items[1].kind == termString {
result.Metadata.Links = append(result.Metadata.Links, core.Link{Label: link.items[0].value, URL: link.items[1].value})
}
}
}

// OTP 24 requires optional applications to also be listed in applications.
optional := make(map[string]bool)
if list, ok := property(properties, "optional_applications"); ok {
for _, app := range list.items {
if app.kind == termAtom {
optional[app.value] = true
}
}
}
locations := make(map[string]int)
seen := make(map[string]bool)
for _, field := range []string{"applications", "included_applications", "optional_applications"} {
list, ok := property(properties, field)
if !ok {
continue
}
for _, app := range list.items {
if app.kind != termAtom || otpApplications[app.value] || seen[app.value] {
continue
}
seen[app.value] = true
scope := core.Runtime
if optional[app.value] {
scope = core.Optional
}
result.Dependencies = append(result.Dependencies, core.Dependency{Name: app.value, Scope: scope, Direct: true})
result.Declarations = append(result.Declarations, core.Declaration{
Name: app.value,
Scope: scope,
Direct: true,
Location: core.NextLocation(locations, field+"/"+app.value),
})
}
}
return result, nil
}
88 changes: 88 additions & 0 deletions internal/rebar/appsrc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package rebar

import (
"os"
"reflect"
"testing"

"github.com/git-pkgs/manifests/internal/core"
)

func TestAppSrc(t *testing.T) {
content, err := os.ReadFile("../../testdata/rebar/myapp.app.src")
if err != nil {
t.Fatalf("failed to read fixture: %v", err)
}
res, err := (&appSrcParser{}).Parse("myapp.app.src", content)
if err != nil {
t.Fatalf("Parse failed: %v", err)
}

if res.Name != "myapp" || res.Version != "0.3.1" {
t.Errorf("package = %q %q", res.Name, res.Version)
}
if !reflect.DeepEqual(res.Licenses, []string{"Apache-2.0", "MIT"}) {
t.Errorf("licenses = %v", res.Licenses)
}
if res.Metadata.Description == nil || *res.Metadata.Description != "An example OTP application" {
t.Errorf("description = %v", res.Metadata.Description)
}
wantLinks := []core.Link{
{Label: "GitHub", URL: "https://github.com/example/myapp"},
{Label: "Changelog", URL: "https://github.com/example/myapp/blob/main/CHANGELOG.md"},
}
if !reflect.DeepEqual(res.Metadata.Links, wantLinks) {
t.Errorf("links = %+v", res.Metadata.Links)
}

want := []core.Dependency{
{Name: "cowboy", Scope: core.Runtime, Direct: true},
{Name: "jsx", Scope: core.Runtime, Direct: true},
{Name: "telemetry", Scope: core.Optional, Direct: true},
{Name: "worker_pool", Scope: core.Runtime, Direct: true},
}
if !reflect.DeepEqual(res.Dependencies, want) {
t.Errorf("dependencies = %+v", res.Dependencies)
}
var locations []string
for _, declaration := range res.Declarations {
locations = append(locations, declaration.Location)
}
wantLocations := []string{"applications/cowboy", "applications/jsx", "applications/telemetry", "included_applications/worker_pool"}
if !reflect.DeepEqual(locations, wantLocations) {
t.Errorf("locations = %v", locations)
}
}

func TestAppSrcEdgeCases(t *testing.T) {
tests := []struct {
name string
content string
app string
version string
deps int
}{
{"git version", `{application, a, [{vsn, "git"}, {applications, [kernel, stdlib, logger, elixir]}]}.`, "a", "", 0},
{"command version", `{application, b, [{vsn, {cmd, "echo 1"}}, {applications, [dep]}]}.`, "b", "", 1},
{"only optional", `{application, c, [{optional_applications, [opt]}]}.`, "c", "", 1},
{"not an application", `{other, d, []}.`, "", "", 0},
{"empty", "% nothing here\n", "", "", 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := (&appSrcParser{}).Parse("x.app.src", []byte(tt.content))
if err != nil {
t.Fatal(err)
}
if res.Name != tt.app || res.Version != tt.version || len(res.Dependencies) != tt.deps {
t.Errorf("result = %q %q %+v", res.Name, res.Version, res.Dependencies)
}
})
}
}

func TestAppSrcInvalid(t *testing.T) {
if _, err := (&appSrcParser{}).Parse("x.app.src", []byte("{application, a, [")); err == nil {
t.Error("expected a parse error")
}
}
155 changes: 155 additions & 0 deletions internal/rebar/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package rebar

import (
"github.com/git-pkgs/manifests/internal/core"
)

func init() {
core.Register("hex", core.Manifest, &rebarConfigParser{}, core.ExactMatch("rebar.config"))
}

// rebarConfigParser parses the deps and plugins of rebar.config files.
type rebarConfigParser struct{}

type rebarConfigWalker struct {
result *core.Result
locations map[string]int
}

func (p *rebarConfigParser) Parse(filename string, content []byte) (*core.Result, error) {
terms, err := parseErlangTerms(string(content))
if err != nil {
return nil, &core.ParseError{Filename: filename, Err: err}
}
walker := &rebarConfigWalker{result: &core.Result{}, locations: make(map[string]int)}
walker.options(terms, "", core.Runtime)
return walker.result, nil
}

// options reads deps, plugins and profiles from a list of config entries.
func (w *rebarConfigWalker) options(entries []erlTerm, prefix string, scope core.Scope) {
for _, entry := range entries {
if entry.kind != termTuple || len(entry.items) != 2 || entry.items[0].kind != termAtom {
continue
}
key, value := entry.items[0].value, entry.items[1]
switch key {
case "deps":
w.deps(value, prefix+"deps", scope)
case "plugins", "project_plugins":
w.deps(value, prefix+key, core.Build)
case "profiles":
if prefix != "" || value.kind != termList {
continue
}
for _, profile := range value.items {
if profile.kind != termTuple || len(profile.items) != 2 || profile.items[0].kind != termAtom || profile.items[1].kind != termList {
continue
}
name := profile.items[0].value
w.options(profile.items[1].items, "profiles/"+name+"/", profileScope(name))
}
}
}
}

func profileScope(profile string) core.Scope {
switch profile {
case "test":
return core.Test
case "prod":
return core.Runtime
}
return core.Development
}

func (w *rebarConfigWalker) deps(list erlTerm, prefix string, scope core.Scope) {
if list.kind != termList {
return
}
for _, entry := range list.items {
app, dep, ok := rebarDependency(entry)
if !ok {
continue
}
dep.Scope = scope
dep.Direct = true
w.result.Dependencies = append(w.result.Dependencies, dep)
w.result.Declarations = append(w.result.Declarations, core.Declaration{
Name: dep.Name,
Version: dep.Version,
Scope: dep.Scope,
Direct: true,
Location: core.NextLocation(w.locations, prefix+"/"+app),
Source: dep.Source,
})
}
}

// rebarDependency reads a deps entry and returns its application name.
func rebarDependency(entry erlTerm) (string, core.Dependency, bool) {
if entry.kind == termAtom {
return entry.value, core.Dependency{Name: entry.value}, true
}
if entry.kind != termTuple || len(entry.items) < 2 || entry.items[0].kind != termAtom {
return "", core.Dependency{}, false
}
app := entry.items[0].value
dep := core.Dependency{Name: app}
rest := entry.items[1:]
if rest[0].kind == termString {
dep.Version = rest[0].value
rest = rest[1:]
}
if len(rest) == 0 {
return app, dep, true
}
if pkg, ok := rest[0].tagged("pkg"); ok {
// {pkg, Name} installs the Hex package Name under the app name.
if len(pkg) == 0 || pkg[0].kind != termAtom {
return "", core.Dependency{}, false
}
dep.Name = pkg[0].value
return app, dep, true
}
source, ok := rebarSource(rest[0])
if !ok {
return "", core.Dependency{}, false
}
// A version next to a source is a rebar2 regex such as ".*", not a version.
dep.Version = ""
dep.Source = source
return app, dep, true
}

// rebarSource reads {git, URL, Spec}, {git_subdir, URL, Spec, Dir} or {hg, URL, Spec}.
func rebarSource(term erlTerm) (core.Source, bool) {
if term.kind != termTuple || len(term.items) < 2 || term.items[0].kind != termAtom || term.items[1].kind != termString {
return core.Source{}, false
}
kind := term.items[0].value
source := core.Source{Kind: core.SourceURL, Value: term.items[1].value}
if kind == "git" || kind == "git_subdir" {
source.Kind = core.SourceGit
}
if len(term.items) > 2 {
spec := term.items[2]
switch {
case spec.kind == termString:
source.Ref = spec.value
case spec.kind == termTuple && len(spec.items) == 2 && spec.items[1].kind == termString:
switch {
case spec.items[0].isAtom("branch"):
source.Branch = spec.items[1].value
case spec.items[0].isAtom("tag"):
source.Tag = spec.items[1].value
case spec.items[0].isAtom("ref"):
source.Ref = spec.items[1].value
}
}
}
if kind == "git_subdir" && len(term.items) > 3 && term.items[3].kind == termString {
source.Rel = term.items[3].value
}
return source, true
}
Loading