Skip to content
Open
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
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
| cargo | Cargo.toml | Cargo.lock |
| carthage | Cartfile, Cartfile.private | Cartfile.resolved |
| chef | metadata.rb, metadata.json, Berksfile | |
| clojars | project.clj | |
| clojars | project.clj, deps.edn, bb.edn | |
| cocoapods | Podfile, *.podspec | Podfile.lock |
| composer | composer.json | composer.lock |
| conan | conanfile.txt, conanfile.py | conan.lock |
Expand Down Expand Up @@ -142,6 +142,28 @@ 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.

### Clojure deps.edn output

`deps.edn` and babashka's `bb.edn` are read as EDN, so comments, commas,
`#_` discards, tagged literals and metadata are handled. Values nested more
than 100 levels deep are a parse error. Top-level `:deps` are runtime
dependencies under a `deps/<lib>` location. In `:aliases`, the `:extra-deps`,
`:replace-deps` and `:deps` of each alias are reported under
`aliases/<alias>/<key>/<lib>`. An alias whose name contains `test` gives test
scope, `build` gives build scope and any other alias gives development scope.
`:override-deps` and `:default-deps` only adjust versions, so they are not
reported.

A `:mvn/version` coordinate keeps its version. A `$classifier` suffix on the
lib is dropped from `Name`, while the location keeps it. Git coordinates use
the `git` source kind with `:git/sha` (or the older `:sha`) in `Ref`,
`:git/tag` in `Tag` and `:deps/root` in `Rel`. When `:git/url` is absent, the
URL is inferred from an `io.github`, `io.gitlab` or `io.bitbucket` lib (and
their `com.` and `org.` forms) as tools.deps does. `:local/root` uses the
`path` source kind. A coordinate with none of these keys is skipped. In
`bb.edn`, `:pods` are babashka pods rather than libraries, so they are not
reported.

### Erlang output

`rebar.config` and `*.app.src` are read as Erlang terms, the way
Expand Down
43 changes: 43 additions & 0 deletions clojure_deps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package manifests

import (
"os"
"testing"
)

func TestClojureDepsEDNParse(t *testing.T) {
content, err := os.ReadFile("testdata/clojure/deps.edn")
if err != nil {
t.Fatal(err)
}
got, err := Parse("deps.edn", content)
if err != nil {
t.Fatal(err)
}
if got.Ecosystem != "clojars" || got.Kind != Manifest || len(got.Dependencies) != 13 {
t.Fatalf("got %s %s with %d dependencies", got.Ecosystem, got.Kind, len(got.Dependencies))
}
if dep := got.Dependencies[0]; dep.Name != "org.clojure/clojure" || dep.PURL != "pkg:clojars/org.clojure%2Fclojure" {
t.Errorf("first dependency = %+v", dep)
}
if dep := got.Dependencies[4]; dep.Source.Kind != SourceGit || dep.Source.Value != "https://github.com/clojure/tools.build.git" {
t.Errorf("git dependency = %+v", dep)
}
if declaration := got.Declarations[8]; declaration.Scope != Test || declaration.PURL != "pkg:clojars/lambdaisland%2Fkaocha" {
t.Errorf("alias declaration = %+v", declaration)
}
}

func TestClojureBBEDNParse(t *testing.T) {
content, err := os.ReadFile("testdata/clojure/bb.edn")
if err != nil {
t.Fatal(err)
}
got, err := Parse("bb.edn", content)
if err != nil {
t.Fatal(err)
}
if got.Ecosystem != "clojars" || got.Kind != Manifest || len(got.Dependencies) != 2 {
t.Fatalf("got %s %s with %d dependencies", got.Ecosystem, got.Kind, len(got.Dependencies))
}
}
155 changes: 155 additions & 0 deletions internal/clojure/deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package clojure

import (
"errors"
"strings"

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

func init() {
core.Register("clojars", core.Manifest, &depsEDNParser{}, core.ExactMatch("deps.edn", "bb.edn"))
}

// depsEDNParser parses the deps and alias deps of deps.edn and bb.edn files.
type depsEDNParser struct{}

// aliasDepKeys are the alias keys that add or replace dependencies.
var aliasDepKeys = []string{"extra-deps", "replace-deps", "deps"}

// gitHosts infers a repository URL from a lib such as io.github.user/repo.
var gitHosts = map[string]string{
"io.github": "https://github.com/",
"com.github": "https://github.com/",
"io.gitlab": "https://gitlab.com/",
"com.gitlab": "https://gitlab.com/",
"io.bitbucket": "https://bitbucket.org/",
"org.bitbucket": "https://bitbucket.org/",
}

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

func (p *depsEDNParser) Parse(filename string, content []byte) (*core.Result, error) {
root, err := parseEDN(string(content))
if err != nil {
return nil, &core.ParseError{Filename: filename, Err: err}
}
if root.kind != ednMap {
return nil, &core.ParseError{Filename: filename, Err: errors.New("top-level value is not a map")}
}

walker := &depsWalker{result: &core.Result{}, locations: make(map[string]int)}
if deps, ok := root.get("deps"); ok {
walker.deps(deps, "deps", core.Runtime)
}
if aliases, ok := root.get("aliases"); ok && aliases.kind == ednMap {
for index := 0; index+1 < len(aliases.items); index += 2 {
name, alias := aliases.items[index], aliases.items[index+1]
if name.kind != ednKeyword {
continue
}
for _, key := range aliasDepKeys {
if deps, ok := alias.get(key); ok {
walker.deps(deps, "aliases/"+name.text+"/"+key, aliasScope(name.text))
}
}
}
}
return walker.result, nil
}

// aliasScope maps an alias name such as test or build to a scope.
func aliasScope(alias string) core.Scope {
name := strings.ToLower(alias)
switch {
case strings.Contains(name, "test"):
return core.Test
case name == "build" || strings.HasPrefix(name, "build/"):
return core.Build
}
return core.Development
}

func (w *depsWalker) deps(deps ednValue, prefix string, scope core.Scope) {
if deps.kind != ednMap {
return
}
for index := 0; index+1 < len(deps.items); index += 2 {
lib, coord := deps.items[index], deps.items[index+1]
if lib.kind != ednSymbol {
continue
}
dep, ok := depsCoordinate(lib.text, coord)
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: scope,
Direct: true,
Location: core.NextLocation(w.locations, prefix+"/"+lib.text),
Source: dep.Source,
})
}
}

// depsCoordinate reads a Maven, git or local coordinate for lib.
func depsCoordinate(lib string, coord ednValue) (core.Dependency, bool) {
if coord.kind != ednMap {
return core.Dependency{}, false
}
// A $classifier suffix selects an artifact of the same library.
name, _, _ := strings.Cut(lib, "$")
dep := core.Dependency{Name: name}

if version := coord.str("mvn/version"); version != "" {
dep.Version = version
return dep, true
}
if root := coord.str("local/root"); root != "" {
dep.Source = core.Source{Kind: core.SourcePath, Value: root}
return dep, true
}

url := coord.str("git/url")
sha := firstNonEmpty(coord.str("git/sha"), coord.str("sha"))
tag := firstNonEmpty(coord.str("git/tag"), coord.str("tag"))
if url == "" && sha == "" && tag == "" {
return core.Dependency{}, false
}
if url == "" {
url = inferGitURL(name)
}
dep.Source = core.Source{Kind: core.SourceGit, Value: url, Tag: tag, Ref: sha, Rel: coord.str("deps/root")}
return dep, true
}

// inferGitURL builds the repository URL tools.deps infers for a lib.
func inferGitURL(lib string) string {
group, repo, ok := strings.Cut(lib, "/")
if !ok {
return ""
}
for prefix, host := range gitHosts {
if user, ok := strings.CutPrefix(group, prefix+"."); ok && user != "" {
return host + user + "/" + repo + ".git"
}
}
return ""
}

func firstNonEmpty(values ...string) string {
for _, value := range values {
if value != "" {
return value
}
}
return ""
}
143 changes: 143 additions & 0 deletions internal/clojure/deps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package clojure

import (
"os"
"reflect"
"testing"

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

func parseDepsFixture(t *testing.T, name string) *core.Result {
t.Helper()
content, err := os.ReadFile("../../testdata/clojure/" + name)
if err != nil {
t.Fatalf("failed to read fixture: %v", err)
}
res, err := (&depsEDNParser{}).Parse(name, content)
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
return res
}

func TestDepsEDN(t *testing.T) {
res := parseDepsFixture(t, "deps.edn")

dep := func(name, version string, scope core.Scope, source core.Source) core.Dependency {
return core.Dependency{Name: name, Version: version, Scope: scope, Direct: true, Source: source}
}
git := func(url, tag, sha, root string) core.Source {
return core.Source{Kind: core.SourceGit, Value: url, Tag: tag, Ref: sha, Rel: root}
}
want := []core.Dependency{
dep("org.clojure/clojure", "1.11.1", core.Runtime, core.Source{}),
dep("ring/ring-core", "1.10.0", core.Runtime, core.Source{}),
dep("cheshire", "5.12.0", core.Runtime, core.Source{}),
dep("org.lwjgl/lwjgl", "3.3.3", core.Runtime, core.Source{}),
dep("io.github.clojure/tools.build", "", core.Runtime, git("https://github.com/clojure/tools.build.git", "v0.9.6", "8e78bcc", "")),
dep("com.example/forked", "", core.Runtime, git("https://git.example.com/forked.git", "", "0123456789abcdef0123456789abcdef01234567", "lib")),
dep("legacy/gitdep", "", core.Runtime, git("https://github.com/legacy/gitdep.git", "", "abc123", "")),
dep("my/local-lib", "", core.Runtime, core.Source{Kind: core.SourcePath, Value: "../local-lib"}),
dep("lambdaisland/kaocha", "1.87.1366", core.Test, core.Source{}),
dep("io.github.cognitect-labs/test-runner", "", core.Test, git("https://github.com/cognitect-labs/test-runner.git", "v0.5.1", "dfb30dd", "")),
dep("nrepl/nrepl", "1.1.0", core.Development, core.Source{}),
dep("io.github.clojure/tools.build", "0.9.6", core.Build, core.Source{}),
dep("com.github.liquidz/antq", "2.8.1185", core.Development, core.Source{}),
}
if !reflect.DeepEqual(res.Dependencies, want) {
t.Errorf("dependencies = %+v", res.Dependencies)
}

if len(res.Declarations) != len(want) {
t.Fatalf("declarations = %+v", res.Declarations)
}
wantLocations := map[int]string{
0: "deps/org.clojure/clojure",
3: "deps/org.lwjgl/lwjgl$natives-linux",
8: "aliases/test/extra-deps/lambdaisland/kaocha",
11: "aliases/build/deps/io.github.clojure/tools.build",
12: "aliases/outdated/replace-deps/com.github.liquidz/antq",
}
for index, location := range wantLocations {
if got := res.Declarations[index].Location; got != location {
t.Errorf("declaration %d location = %q, want %q", index, got, location)
}
}
}

func TestBBEDN(t *testing.T) {
res := parseDepsFixture(t, "bb.edn")
want := []core.Dependency{
{Name: "medley/medley", Version: "1.4.0", Scope: core.Runtime, Direct: true},
{
Name: "io.github.babashka/fs", Scope: core.Runtime, Direct: true,
Source: core.Source{Kind: core.SourceGit, Value: "https://github.com/babashka/fs.git", Ref: "a8d22b3"},
},
}
if !reflect.DeepEqual(res.Dependencies, want) {
t.Errorf("dependencies = %+v", res.Dependencies)
}
}

func TestDepsEDNEdgeCases(t *testing.T) {
content := `{:deps {"string/lib" {:mvn/version "1"}
not-a-map "1.0"
unknown.host/repo {:git/sha "abc"}
ok/lib {:mvn/version "2.0"}}
:aliases {"string-alias" {:extra-deps {skipped/lib {:mvn/version "1"}}}
:empty {}
:test/unit {:extra-deps {unit/lib {:mvn/version "3"}}}}}`
res, err := (&depsEDNParser{}).Parse("deps.edn", []byte(content))
if err != nil {
t.Fatal(err)
}
want := []core.Dependency{
{Name: "unknown.host/repo", Scope: core.Runtime, Direct: true, Source: core.Source{Kind: core.SourceGit, Ref: "abc"}},
{Name: "ok/lib", Version: "2.0", Scope: core.Runtime, Direct: true},
{Name: "unit/lib", Version: "3", Scope: core.Test, Direct: true},
}
if !reflect.DeepEqual(res.Dependencies, want) {
t.Errorf("dependencies = %+v", res.Dependencies)
}
}

func TestDepsEDNInvalid(t *testing.T) {
for _, content := range []string{"[:not :a :map]", "{:deps {a/b", ""} {
if _, err := (&depsEDNParser{}).Parse("deps.edn", []byte(content)); err == nil {
t.Errorf("expected a parse error for %q", content)
}
}
}

func TestAliasScope(t *testing.T) {
for alias, want := range map[string]core.Scope{
"test": core.Test,
"test/unit": core.Test,
"kaocha-test": core.Test,
"build": core.Build,
"build/jar": core.Build,
"dev": core.Development,
"rebuild": core.Development,
} {
if got := aliasScope(alias); got != want {
t.Errorf("aliasScope(%q) = %q, want %q", alias, got, want)
}
}
}

func TestInferGitURL(t *testing.T) {
for lib, want := range map[string]string{
"io.github.user/repo": "https://github.com/user/repo.git",
"com.github.user/repo": "https://github.com/user/repo.git",
"io.gitlab.group/proj": "https://gitlab.com/group/proj.git",
"org.bitbucket.team/x": "https://bitbucket.org/team/x.git",
"io.github/repo": "",
"unqualified": "",
"org.example.user/repo": "",
} {
if got := inferGitURL(lib); got != want {
t.Errorf("inferGitURL(%q) = %q, want %q", lib, got, want)
}
}
}
Loading