Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

framework/view: experiment to extract out svelte into a swappable "viewer" #225

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
38 changes: 24 additions & 14 deletions framework/app/app.gotext
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,24 @@ func run(ctx context.Context, args ...string) int {

// Parse the arguments
func parse(ctx context.Context, args ...string) error {
cmd := &App{
Flag: new(framework.Flag),
}
cli := commander.New("bud")
app := new(App)
cli.Flag("listen", "address to listen to").String(&app.Listen).Default(":3000")
cli.Flag("log", "filter logs with a pattern").Short('L').String(&app.Log).Default("info")
cli.Run(app.Run)
cli.Flag("chdir", "change the working directory").Short('C').String(&cmd.Dir).Default(".")
cli.Flag("embed", "embed assets").Bool(&cmd.Flag.Embed).Default(false)
cli.Flag("hot", "hot reloading").Bool(&cmd.Flag.Hot).Default(true)
cli.Flag("minify", "minify assets").Bool(&cmd.Flag.Minify).Default(false)
cli.Flag("listen", "address to listen to").String(&cmd.Listen).Default(":3000")
cli.Flag("log", "filter logs with a pattern").Short('L').String(&cmd.Log).Default("info")
cli.Run(cmd.Run)
return cli.Parse(ctx, args)
}

// App command
type App struct {
Dir string
Flag *framework.Flag
Listen string
Log string
}
Expand Down Expand Up @@ -68,22 +76,24 @@ func (a *App) Run(ctx context.Context) error {
{{- end }}
{{- if $.Provider.Variable "github.com/livebud/bud/package/gomod.*Module" }}
// Load the module dependency
{{- if $.Flag.Embed }}
module, err := gomod.Parse("go.mod", []byte("module e"))
if err != nil {
return err
}
{{- else }}
module, err := gomod.Find(".")
if err != nil {
return err
var module *gomod.Module
if a.Flag.Embed {
module, err = gomod.Parse("go.mod", []byte("module e"))
if err != nil {
return err
}
} else {
module, err = gomod.Find(a.Dir)
if err != nil {
return err
}
}
{{- end }}
{{- end }}
// Load the web server
webServer, err := loadWeb(
{{/* Order matters. Ordered by package name (e.g. budhttp > context) */}}
{{- if $.Provider.Variable "github.com/livebud/bud/package/budhttp.Client" }}budClient,{{ end }}
{{- if $.Provider.Variable "github.com/livebud/bud/framework.*Flag" }}a.Flag,{{ end }}
{{- if $.Provider.Variable "context.Context" }}ctx,{{ end }}
{{- if $.Provider.Variable "github.com/livebud/bud/package/gomod.*Module" }}module,{{ end }}
{{- if $.Provider.Variable "github.com/livebud/bud/package/log.Log" }}log,{{ end }}
Expand Down
4 changes: 3 additions & 1 deletion framework/app/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (l *loader) Load() (state *State, err error) {
l.imports.AddNamed("budhttp", "github.com/livebud/bud/package/budhttp")
l.imports.Add(l.module.Import("bud/internal/web"))
state.Provider = l.loadProvider()
state.Flag = l.flag
state.Imports = l.imports.List()
return state, nil
}
Expand All @@ -57,11 +56,13 @@ func (l *loader) loadProvider() *di.Provider {
publicFS := di.ToType("github.com/livebud/bud/framework/public/publicrt", "FS")
viewFS := di.ToType("github.com/livebud/bud/framework/view/viewrt", "FS")
transpilerFS := di.ToType("github.com/livebud/bud/runtime/transpiler", "FS")
viewerPages := di.ToType(`github.com/livebud/bud/package/viewer`, `Pages`)
fn := &di.Function{
Name: "loadWeb",
Imports: l.imports,
Target: l.module.Import("bud", "program"),
Params: []*di.Param{
{Import: "github.com/livebud/bud/framework", Type: "*Flag"},
{Import: "github.com/livebud/bud/package/log", Type: "Log"},
{Import: "github.com/livebud/bud/package/gomod", Type: "*Module"},
{Import: "github.com/livebud/bud/package/budhttp", Type: "Client"},
Expand All @@ -77,6 +78,7 @@ func (l *loader) loadProvider() *di.Provider {
publicFS: di.ToType("github.com/livebud/bud/runtime/transpiler", "*Proxy"),
viewFS: di.ToType("github.com/livebud/bud/package/remotefs", "*Client"),
jsVM: di.ToType("github.com/livebud/bud/package/budhttp", "Client"),
viewerPages: di.ToType(l.module.Import("bud/internal/web/view"), "Pages"),
},
}
if l.flag.Embed {
Expand Down
2 changes: 0 additions & 2 deletions framework/app/state.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package app

import (
"github.com/livebud/bud/framework"
"github.com/livebud/bud/internal/imports"
"github.com/livebud/bud/package/di"
)

type State struct {
Imports []*imports.Import
Provider *di.Provider
Flag *framework.Flag
}
2 changes: 2 additions & 0 deletions framework/view/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func (l *loader) Load() (state *State, err error) {
l.imports.AddNamed("router", "github.com/livebud/bud/package/router")
l.imports.AddNamed("virtual", "github.com/livebud/bud/package/virtual")
l.imports.AddNamed("viewrt", "github.com/livebud/bud/framework/view/viewrt")
l.imports.AddNamed("svelte", "github.com/livebud/bud/package/viewer/svelte")
l.imports.AddNamed("viewer", "github.com/livebud/bud/package/viewer")
state.Imports = l.imports.List()
return state, nil
}
8 changes: 7 additions & 1 deletion framework/view/view.gotext
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import (
)
{{- end }}

func NewHandler(handler *viewrt.Handler) *Handler {
func NewPages() Pages {
return Pages{}
}

type Pages = viewer.Pages

func NewHandler(handler *viewrt.Handler, svelte *svelte.Viewer) *Handler {
return &Handler{handler}
}

Expand Down
3 changes: 2 additions & 1 deletion framework/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
func TestHello(t *testing.T) {
is := is.New(t)
ctx := context.Background()
dir := t.TempDir()
is.NoErr(os.RemoveAll("_tmp"))
dir := "_tmp"
td := testdir.New(dir)
td.Files["controller/controller.go"] = `
package controller
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/keegancsmith/rpc v1.3.0
github.com/lithammer/dedent v1.1.0
github.com/livebud/bud-test-plugin v0.0.9
github.com/livebud/js v0.0.0-20221112072017-b9e63b92aad5
github.com/livebud/transpiler v0.0.3
github.com/matthewmueller/diff v0.0.0-20220104030700-cb2fe910d90c
github.com/matthewmueller/gotext v0.0.0-20210424201144-265ed61725ac
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/livebud/bud-test-nested-plugin v0.0.5 h1:MpPp20Gng0F+Kvl+L9kttu6nsJIY
github.com/livebud/bud-test-nested-plugin v0.0.5/go.mod h1:M3QujkGG4ggZ6h75t5zF8MEJFrLTwa2USeIYHQdO2YQ=
github.com/livebud/bud-test-plugin v0.0.9 h1:JmS4aj+NV52RUroteLs+ld6rcbkBwio7p9qPNutTsqM=
github.com/livebud/bud-test-plugin v0.0.9/go.mod h1:GTxMZ8W4BIyGIOgAA4hvPHMDDTkaZtfcuhnOcSu3y8M=
github.com/livebud/js v0.0.0-20221112072017-b9e63b92aad5 h1:7fYJMOnT4WrnjRhJ8B6HiJBGcDVFd9jam0205gn9y1k=
github.com/livebud/js v0.0.0-20221112072017-b9e63b92aad5/go.mod h1:TDkks+mVAlB96mxcbIAftAxpGteCxoBYGVF1JThjcLk=
github.com/livebud/transpiler v0.0.3 h1:OFKPsmfTOywBDoOE/ZFMVjAupk/xVXFlXoAgZNRhh5Q=
github.com/livebud/transpiler v0.0.3/go.mod h1:vQYMN//Y2cnM55tw0lOmLGbEETugP7alxTyhQHzNdTI=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
Expand Down
125 changes: 125 additions & 0 deletions package/di/di_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,131 @@ func TestHTTPRequest(t *testing.T) {
})
}

func TestMapParamAlias(t *testing.T) {
t.Skip("aliasing to a map is not implemented yet")
runTest(t, Test{
Function: &di.Function{
Name: "Load",
Target: "app.com/gen/web",
Params: []*di.Param{},
Results: []di.Dependency{
di.ToType("app.com/web", "*Controller"),
},
Aliases: di.Aliases{
di.ToType("app.com/viewer", "Pages"): di.ToType("app.com/viewer/svelte", "Pages"),
},
},
Expect: `
&web.Controller{Pool: &postgres.Pool{
Path: "/foo",
}}
`,
Files: map[string]string{
"go.mod": goMod,
"main.go": mainGo,
"web/web.go": `
package web
import "app.com/viewer/svelte"
type Controller struct {
Svelte *svelte.Viewer
}
`,
"viewer/viewer.go": `
package viewer
type Pages = map[string]string
`,
"viewer/svelte/svelte.go": `
package svelte
import "app.com/viewer"
func New(pages Pages) *Viewer {
return &Viewer{pages}
}
type Pages = viewer.Pages
func NewPages() Pages {
return viewer.Pages{}
}
type Viewer struct {
pages Pages
}
`,
},
})
}

func TestTypeSpec(t *testing.T) {
runTest(t, Test{
Function: &di.Function{
Name: "Load",
Target: "app.com/gen/web",
Params: []*di.Param{},
Results: []di.Dependency{
di.ToType("app.com/web", "*Controller"),
},
Aliases: di.Aliases{},
},
Expect: `
&web.Controller{
Actions: web.Actions{
"hello": "world",
},
URL: web.URL("hello"),
}
`,
Files: map[string]string{
"go.mod": goMod,
"main.go": mainGo,
"web/web.go": `
package web
func NewActions() Actions {
return Actions{"hello": "world"}
}
type Actions map[string]string
// type Actions2 map[string]string
func NewURL() URL {
return URL("hello")
}
type URL string
type Controller struct {
Actions Actions
URL URL
}
`,
},
})
}

func TestInterfaceErrorUnclear(t *testing.T) {
runTest(t, Test{
Function: &di.Function{
Name: "Load",
Target: "app.com/gen/web",
Params: []*di.Param{},
Results: []di.Dependency{
di.ToType("app.com/web", "*Controller"),
},
Aliases: di.Aliases{},
},
Expect: `di: unclear how to provide 'app.com/js'.VM`,
Files: map[string]string{
"go.mod": goMod,
"main.go": mainGo,
"js/js.go": `
package js
type VM interface {
Eval(expr string) (string, error)
}
`,
"web/web.go": `
package web
import "app.com/js"
type Controller struct {
VM js.VM
}
`,
},
})
}

// TODO: figure out how to test imports as inputs

// IDEA: consider renaming Target to Import
Expand Down
2 changes: 1 addition & 1 deletion package/di/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (i *Injector) Find(currModule *gomod.Module, dep Dependency) (Declaration,
}).Debug("di: found struct declaration")
return decl, nil
}
// Lastly, look through the type aliases
// Look through the type aliases
for _, alias := range pkg.Aliases() {
decl, err := tryTypeAlias(alias, dep.TypeName())
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions package/di/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ func tryFunction(fn *parser.Function, importPath, dataType string) (*function, e
}
def, err := param.Definition()
if err != nil {
importPath, err := parser.ImportPath(pt)
if err != nil {
return nil, err
importPath, err2 := parser.ImportPath(pt)
if err2 != nil {
return nil, err2
}
return nil, fmt.Errorf("di: unable to find definition for param %q.%s in %q.%s. %w", importPath, parser.Unqualify(pt).String(), importPath, dataType, err)
}
Expand Down Expand Up @@ -154,9 +154,9 @@ func tryFunction(fn *parser.Function, importPath, dataType string) (*function, e
}
def, err := result.Definition()
if err != nil {
importPath, err := parser.ImportPath(rt)
if err != nil {
return nil, err
importPath, err2 := parser.ImportPath(rt)
if err2 != nil {
return nil, err2
}
return nil, fmt.Errorf("di: unable to find definition for result %q.%s in %q.%s. %w", importPath, parser.Unqualify(rt).String(), importPath, dataType, err)
}
Expand Down Expand Up @@ -186,7 +186,7 @@ type function struct {
var _ Declaration = (*function)(nil)

func (fn *function) ID() string {
return `'` + fn.Import + `'.` + fn.Name
return getID(fn.Import, fn.Name)
}

// Dependencies are the values that the funcDecl depends on to run
Expand Down
Loading