Compare commits

...

2 Commits

Author SHA1 Message Date
Alex Dunmow
7ff326ef25 feat(sdk): ModFile struct, ParseModFull, and tests
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-01 22:45:26 +08:00
Alex Dunmow
a5caf2d9e7 chore(sdk): add deps for plugin.mod parser and ninja CLI
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-01 22:44:55 +08:00
4 changed files with 152 additions and 2 deletions

2
go.mod
View File

@ -4,6 +4,7 @@ go 1.26
require (
connectrpc.com/connect v1.19.2
github.com/BurntSushi/toml v1.6.0
github.com/a-h/templ v0.3.1001
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.9.2
@ -11,6 +12,7 @@ require (
)
require (
github.com/google/go-cmp v0.7.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
golang.org/x/text v0.36.0 // indirect

6
go.sum
View File

@ -1,12 +1,14 @@
connectrpc.com/connect v1.19.2 h1:McQ83FGdzL+t60peksi0gXC7MQ/iLKgLduAnThbM0mo=
connectrpc.com/connect v1.19.2/go.mod h1:tN20fjdGlewnSFeZxLKb0xwIZ6ozc3OQs2hTXy4du9w=
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/a-h/templ v0.3.1001 h1:yHDTgexACdJttyiyamcTHXr2QkIeVF1MukLy44EAhMY=
github.com/a-h/templ v0.3.1001/go.mod h1:oCZcnKRf5jjsGpf2yELzQfodLphd2mwecwG4Crk5HBo=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=

45
plugin/mod.go Normal file
View File

@ -0,0 +1,45 @@
package plugin
import (
tomlpkg "github.com/BurntSushi/toml"
)
type ModFile struct {
Plugin ModPlugin `toml:"plugin"`
Compatibility *ModCompat `toml:"compatibility"`
Requires []ModRequirement `toml:"requires"`
}
type ModPlugin struct {
Name string `toml:"name"`
Scope string `toml:"scope"`
Version string `toml:"version"`
}
type ModCompat struct {
BlockCore string `toml:"block_core"`
}
type ModRequirement struct {
Name string `toml:"name"`
Version string `toml:"version"`
}
func ParseModFull(b []byte) (*ModFile, error) {
var m ModFile
if err := tomlpkg.Unmarshal(b, &m); err != nil {
return nil, err
}
return &m, nil
}
func (m *ModFile) Coords() string {
if m == nil {
return ""
}
scope := m.Plugin.Scope
if scope == "" {
return m.Plugin.Name + "@" + m.Plugin.Version
}
return "@" + scope + "/" + m.Plugin.Name + "@" + m.Plugin.Version
}

101
plugin/mod_test.go Normal file
View File

@ -0,0 +1,101 @@
package plugin
import (
"testing"
)
func TestParseModFull_BasicFields(t *testing.T) {
src := []byte(`
[plugin]
name = "smartblock"
scope = "blockninja"
version = "1.4.2"
`)
m, err := ParseModFull(src)
if err != nil {
t.Fatalf("ParseModFull err: %v", err)
}
if m.Plugin.Name != "smartblock" {
t.Errorf("Name = %q, want smartblock", m.Plugin.Name)
}
if m.Plugin.Scope != "blockninja" {
t.Errorf("Scope = %q, want blockninja", m.Plugin.Scope)
}
if m.Plugin.Version != "1.4.2" {
t.Errorf("Version = %q, want 1.4.2", m.Plugin.Version)
}
if got := m.Coords(); got != "@blockninja/smartblock@1.4.2" {
t.Errorf("Coords() = %q", got)
}
}
func TestParseModFull_BackCompatNoScope(t *testing.T) {
src := []byte(`
[plugin]
name = "legacy"
version = "0.1.0"
`)
m, err := ParseModFull(src)
if err != nil {
t.Fatalf("ParseModFull err: %v", err)
}
if m.Plugin.Scope != "" {
t.Errorf("Scope should be empty, got %q", m.Plugin.Scope)
}
if got := m.Coords(); got != "legacy@0.1.0" {
t.Errorf("Coords() = %q, want legacy@0.1.0", got)
}
}
func TestParseModFull_InvalidTOML(t *testing.T) {
_, err := ParseModFull([]byte("not valid toml = ="))
if err == nil {
t.Fatal("expected parse error")
}
}
func TestParseModFull_EmptyInput(t *testing.T) {
m, err := ParseModFull(nil)
if err != nil {
t.Fatalf("nil input err: %v", err)
}
if m.Plugin.Name != "" {
t.Errorf("Name should be empty")
}
}
func TestParseModFull_RequiresAndCompat(t *testing.T) {
src := []byte(`
[plugin]
name = "symposium"
scope = "blockninja"
version = "0.2.0"
[compatibility]
block_core = ">=1.5 <2.0"
[[requires]]
name = "@blockninja/smartblock"
version = ">=1.0 <2.0"
[[requires]]
name = "@blockninja/gotham"
version = ">=1.2"
`)
m, err := ParseModFull(src)
if err != nil {
t.Fatalf("ParseModFull err: %v", err)
}
if m.Compatibility == nil || m.Compatibility.BlockCore != ">=1.5 <2.0" {
t.Errorf("Compat = %+v", m.Compatibility)
}
if len(m.Requires) != 2 {
t.Fatalf("Requires len = %d, want 2", len(m.Requires))
}
if m.Requires[0].Name != "@blockninja/smartblock" {
t.Errorf("Requires[0].Name = %q", m.Requires[0].Name)
}
if m.Requires[1].Version != ">=1.2" {
t.Errorf("Requires[1].Version = %q", m.Requires[1].Version)
}
}