Add Wails project metadata reader

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Leon Mika 2026-05-02 10:00:21 +10:00
parent 5c72dd2a97
commit 338d1fd5b7
7 changed files with 111 additions and 1 deletions

4
go.mod
View file

@ -1,3 +1,5 @@
module github.com/leonmika/wails-release
go 1.24.3
go 1.25.0
require golang.org/x/mod v0.35.0 // indirect

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=
golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=

51
internal/wails/project.go Normal file
View file

@ -0,0 +1,51 @@
package wails
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"golang.org/x/mod/modfile"
)
// Project is the subset of wails.json we care about.
type Project struct {
Name string `json:"name"`
}
// ReadProject parses wails.json from dir.
func ReadProject(dir string) (*Project, error) {
path := filepath.Join(dir, "wails.json")
b, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read %s: %w", path, err)
}
var p Project
if err := json.Unmarshal(b, &p); err != nil {
return nil, fmt.Errorf("parse %s: %w", path, err)
}
if p.Name == "" {
return nil, fmt.Errorf("%s: name field is empty", path)
}
return &p, nil
}
// ProjectWailsVersion returns the require'd Wails version from go.mod.
func ProjectWailsVersion(dir string) (string, error) {
path := filepath.Join(dir, "go.mod")
b, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("read %s: %w", path, err)
}
mf, err := modfile.Parse(path, b, nil)
if err != nil {
return "", fmt.Errorf("parse %s: %w", path, err)
}
for _, r := range mf.Require {
if r.Mod.Path == "github.com/wailsapp/wails/v2" {
return r.Mod.Version, nil
}
}
return "", fmt.Errorf("%s: no require for github.com/wailsapp/wails/v2", path)
}

View file

@ -0,0 +1,41 @@
package wails_test
import (
"testing"
"github.com/leonmika/wails-release/internal/wails"
)
func TestReadProject_ReturnsAppName(t *testing.T) {
p, err := wails.ReadProject("testdata/sample")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if p.Name != "SampleApp" {
t.Fatalf("name: got %q want SampleApp", p.Name)
}
}
func TestReadProject_MissingWailsJSONErrors(t *testing.T) {
_, err := wails.ReadProject("testdata/missing")
if err == nil {
t.Fatal("expected error for missing wails.json")
}
}
func TestProjectWailsVersion_FromGoMod(t *testing.T) {
v, err := wails.ProjectWailsVersion("testdata/sample")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if v != "v2.11.0" {
t.Fatalf("got %q, want v2.11.0", v)
}
}
func TestProjectWailsVersion_NoWailsDependencyErrors(t *testing.T) {
_, err := wails.ProjectWailsVersion("testdata/no-wails")
if err == nil {
t.Fatal("expected error for project with no wails dep")
}
}

View file

@ -0,0 +1,3 @@
module example.com/sample
go 1.22

5
internal/wails/testdata/sample/go.mod vendored Normal file
View file

@ -0,0 +1,5 @@
module example.com/sample
go 1.22
require github.com/wailsapp/wails/v2 v2.11.0

View file

@ -0,0 +1,6 @@
{
"name": "SampleApp",
"outputfilename": "SampleApp",
"frontend:install": "npm install",
"frontend:build": "npm run build"
}