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

Refactor architecture #347

Merged
merged 8 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
bud generate working with new budfs
  • Loading branch information
matthewmueller committed Dec 30, 2022
commit 797659e26f313d2d71de90257b1f0ec0d50431f4
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ example.scratch.watch:

example.hn:
@ # (cd example/hn && npm link ../../livebud)
@ go run main.go --log=debug -C example/hn generate bud/internal/generator bud/cmd/afs bud/afs
@ go run main.go -log=debug -C example/hn generate

example.hn.embed:
@ (cd example/hn && npm link ../../livebud)
Expand Down
2 changes: 1 addition & 1 deletion framework/afs/afs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (l *loader) Load() (state *State, err error) {
l.imports.AddNamed("commander", "github.com/livebud/bud/package/commander")
l.imports.AddNamed("afsrt", "github.com/livebud/bud/framework/afs/afsrt")
l.imports.AddNamed("console", "github.com/livebud/bud/package/log/console")
l.imports.AddNamed("budfs", "github.com/livebud/bud/internal/budfs")
l.imports.AddNamed("genfs", "github.com/livebud/bud/package/genfs")
l.imports.AddNamed("parser", "github.com/livebud/bud/package/parser")
state.Imports = l.imports.List()
return state, nil
Expand Down
7 changes: 5 additions & 2 deletions framework/afs/afs.gotext
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ func (c *Command) Serve(ctx context.Context) (err error) {
if err != nil {
return err
}
gfs := afsrt.GenFS(module, log)
gfs, err := afsrt.GenFS(module, log)
if err != nil {
return err
}
parser := parser.New(gfs, module)
injector := di.New(gfs, log, module, parser)
_ = injector
Expand All @@ -68,7 +71,7 @@ func (c *Command) Serve(ctx context.Context) (err error) {
{{- if $.Provider.Variable "context.Context" }}ctx,{{ end }}
{{- if $.Provider.Variable "github.com/livebud/bud/package/di.*Injector" }}injector,{{ end }}
{{- if $.Provider.Variable "github.com/livebud/bud/framework.*Flag" }}&c.Flag,{{ end }}
{{- if $.Provider.Variable "github.com/livebud/bud/internal/genfs.*FileSystem" }}gfs,{{ end }}
{{- if $.Provider.Variable "github.com/livebud/bud/package/genfs.*FileSystem" }}gfs,{{ 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 }}
{{- if $.Provider.Variable "github.com/livebud/bud/package/parser.*Parser" }}parser,{{ end }}
Expand Down
10 changes: 7 additions & 3 deletions framework/afs/afsrt/appfsrt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ func Logger(level string) (log.Log, error) {
return logger, nil
}

// BudFS creates a new filesystem
func BudFS(module *gomod.Module, log log.Log) *genfs.FileSystem {
return genfs.New(dag.Discard, module, log)
// GenFS creates a new filesystem
func GenFS(module *gomod.Module, log log.Log) (*genfs.FileSystem, error) {
cache, err := dag.Load(module, module.Directory("bud/bud.db"))
if err != nil {
return nil, err
}
return genfs.New(cache, module, log), nil
}

// Serve the remote filesystem
Expand Down
1 change: 0 additions & 1 deletion framework/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,6 @@ func TestSameNestedName(t *testing.T) {

"/users"
`)
is.NoErr(app.Close())
res, err = app.Get("/admins/10/users")
is.NoErr(err)
res.Diff(`
Expand Down
11 changes: 10 additions & 1 deletion framework/framework.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package framework

import "io"
import (
"io"

"github.com/livebud/bud/internal/pubsub"
"github.com/livebud/bud/package/socket"
)

// Flag is used by many of the framework generators
type Flag struct {
Expand All @@ -14,4 +19,8 @@ type Flag struct {
Stdout io.Writer
Stderr io.Writer
Env []string
// Currently passed in only for testing
BudLn socket.Listener // Can be nil
WebLn socket.Listener // Can be nil
Bus pubsub.Client // Can be nil
}
1 change: 0 additions & 1 deletion framework/generator2/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type Generator struct {

// GenerateFile connects to the remotefs and mounts the remote directory.
func (g *Generator) GenerateFile(fsys genfs.FS, file *genfs.File) error {
fmt.Println("loading...", file.Target())
state, err := g.Load(fsys)
if err != nil {
return fmt.Errorf("framework/generator: unable to load. %w", err)
Expand Down
14 changes: 14 additions & 0 deletions framework/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/livebud/bud/framework"
"github.com/livebud/bud/package/budfs"
"github.com/livebud/bud/package/genfs"
"github.com/livebud/bud/package/gomod"

"github.com/livebud/bud/internal/gotemplate"
Expand Down Expand Up @@ -45,3 +46,16 @@ func (g *Generator) GenerateFileOld(fsys budfs.FS, file *budfs.File) error {
file.Data = code
return nil
}

func (g *Generator) GenerateFile(fsys genfs.FS, file *genfs.File) error {
state, err := Load(fsys, g.flag)
if err != nil {
return err
}
code, err := Generate(state)
if err != nil {
return err
}
file.Data = code
return nil
}
56 changes: 56 additions & 0 deletions framework/view/dom/dom.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/livebud/bud/package/budfs"
"github.com/livebud/bud/package/genfs"

esbuild "github.com/evanw/esbuild/pkg/api"
"github.com/livebud/bud/framework/transform/transformrt"
Expand Down Expand Up @@ -161,6 +162,61 @@ func (c *Generator) GenerateFileOld(fsys budfs.FS, file *budfs.File) error {
return nil
}

// ServeFile generates a single file, used in development
func (c *Generator) ServeFile(fsys genfs.FS, file *genfs.File) error {
// If the name starts with node_modules, trim it to allow esbuild to do
// the resolving. e.g. node_modules/livebud => livebud
entryPoint := trimEntrypoint(file.Target())
// Check that the entrypoint exists, ignoring generated files to avoid
// infinite recursion
if !strings.HasPrefix(entryPoint, "bud/") {
if _, err := fs.Stat(fsys, entryPoint); err != nil {
return err
}
}
// Run esbuild
result := esbuild.Build(esbuild.BuildOptions{
EntryPoints: []string{entryPoint},
AbsWorkingDir: c.module.Directory(),
Format: esbuild.FormatESModule,
Platform: esbuild.PlatformBrowser,
// Add "import" condition to support svelte/internal
// https://esbuild.github.io/api/#how-conditions-work
Conditions: []string{"browser", "default", "import"},
Metafile: true,
Bundle: true,
Plugins: append([]esbuild.Plugin{
domPlugin(fsys, c.module),
domExternalizePlugin(),
}, c.transformer.DOM.Plugins()...),
})
if len(result.Errors) > 0 {
msgs := esbuild.FormatMessages(result.Errors, esbuild.FormatMessagesOptions{
Color: true,
Kind: esbuild.ErrorMessage,
TerminalWidth: 80,
})
return fmt.Errorf(strings.Join(msgs, "\n"))
}
// if err := esmeta.Link2(dfs, result.Metafile); err != nil {
// return nil, err
// }
code := result.OutputFiles[0].Contents
// Replace require statements and updates the path on imports
code = replaceDependencyPaths(code)
file.Data = code
// Link the dependencies
metafile, err := esmeta.Parse(result.Metafile)
if err != nil {
return err
}
// Watch the dependencies for changes
if err := fsys.Watch(metafile.Dependencies()...); err != nil {
return err
}
return nil
}

func toEntry(path string) string {
dir, base := filepath.Split(path)
return filepath.Join(dir, "_"+base) + ".js"
Expand Down
44 changes: 44 additions & 0 deletions framework/view/nodemodules/nodemodules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
esbuild "github.com/evanw/esbuild/pkg/api"
"github.com/livebud/bud/internal/esmeta"
"github.com/livebud/bud/package/budfs"
"github.com/livebud/bud/package/genfs"
"github.com/livebud/bud/package/gomod"
)

Expand Down Expand Up @@ -64,6 +65,49 @@ func (g *Generator) GenerateFileOld(fsys budfs.FS, file *budfs.File) error {
return nil
}

// ServeFile serves node modules on demand
func (g *Generator) ServeFile(fsys genfs.FS, file *genfs.File) error {
// If the name starts with node_modules, trim it to allow esbuild to do
// the resolving. e.g. node_modules/timeago.js => timeago.js
entryPoint := trimEntrypoint(file.Target())
result := esbuild.Build(esbuild.BuildOptions{
EntryPoints: []string{entryPoint},
AbsWorkingDir: g.module.Directory(),
Format: esbuild.FormatESModule,
Platform: esbuild.PlatformBrowser,
// Add "import" condition to support svelte/internal
// https://esbuild.github.io/api/#how-conditions-work
Conditions: []string{"browser", "default", "import"},
Metafile: true,
Bundle: true,
Plugins: []esbuild.Plugin{
domExternalizePlugin(),
},
})
if len(result.Errors) > 0 {
msgs := esbuild.FormatMessages(result.Errors, esbuild.FormatMessagesOptions{
Color: true,
Kind: esbuild.ErrorMessage,
TerminalWidth: 80,
})
return fmt.Errorf(strings.Join(msgs, "\n"))
}
content := result.OutputFiles[0].Contents
// Replace require statements and updates the path on imports
code := replaceDependencyPaths(content)
file.Data = code
// Link the dependencies
metafile, err := esmeta.Parse(result.Metafile)
if err != nil {
return err
}
// Watch the dependencies for changes
if err := fsys.Watch(metafile.Dependencies()...); err != nil {
return err
}
return nil
}

// Transforms the dom file imports into including the "__LIVEBUD_EXTERNAL__:" prefix
// TODO: dedupe with dom
func domExternalizePlugin() esbuild.Plugin {
Expand Down
2 changes: 1 addition & 1 deletion framework/web/webrt/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func shutdown(ctx context.Context, server *http.Server) <-chan error {
go func() {
<-ctx.Done()
// Wait for one more interrupt to force an immediate shutdown
forceCtx := sig.Trap(ctx, os.Interrupt)
forceCtx := sig.Trap(context.Background(), os.Interrupt)
if err := server.Shutdown(forceCtx); err != nil {
shutdown <- err
}
Expand Down
21 changes: 0 additions & 21 deletions internal/budfs/afs.go

This file was deleted.

Loading