Skip to content

Commit

Permalink
pass the flags through. embed public files
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmueller committed Mar 27, 2022
1 parent 35b0a7b commit 6552a9b
Show file tree
Hide file tree
Showing 28 changed files with 375 additions and 232 deletions.
6 changes: 3 additions & 3 deletions internal/bud/bud.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"gitlab.com/mnm/bud/package/overlay"
"gitlab.com/mnm/bud/package/parser"
"gitlab.com/mnm/bud/package/trace"
"gitlab.com/mnm/bud/runtime/bud"
)

func defaultEnv(module *gomod.Module) (Env, error) {
Expand Down Expand Up @@ -123,7 +124,7 @@ func (c *Compiler) goBuild(ctx context.Context, module *gomod.Module, outPath st
return nil
}

func (c *Compiler) Compile(ctx context.Context, flag Flag) (p *Project, err error) {
func (c *Compiler) Compile(ctx context.Context, flag *bud.Flag) (p *Project, err error) {
// Start the trace
ctx, span := trace.Start(ctx, "compile project cli")
defer span.End(&err)
Expand All @@ -138,7 +139,7 @@ func (c *Compiler) Compile(ctx context.Context, flag Flag) (p *Project, err erro
// Setup the generators
overlay.FileGenerator("bud/import.go", importfile.New(c.module))
overlay.FileGenerator("bud/.cli/main.go", mainfile.New(c.module))
overlay.FileGenerator("bud/.cli/program/program.go", program.New(injector, c.module))
overlay.FileGenerator("bud/.cli/program/program.go", program.New(flag, injector, c.module))
overlay.FileGenerator("bud/.cli/command/command.go", command.New(overlay, c.module, parser))
overlay.FileGenerator("bud/.cli/generator/generator.go", generator.New(overlay, c.module, parser))
// Sync the generators
Expand All @@ -155,7 +156,6 @@ func (c *Compiler) Compile(ctx context.Context, flag Flag) (p *Project, err erro
}
return &Project{
Module: c.module,
Flag: flag,
Env: c.Env,
Stdout: c.Stdout,
Stderr: c.Stderr,
Expand Down
22 changes: 0 additions & 22 deletions internal/bud/flag.go

This file was deleted.

60 changes: 0 additions & 60 deletions internal/bud/process.go

This file was deleted.

32 changes: 14 additions & 18 deletions internal/bud/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,31 @@ import (
"context"
"io"
"net"
"os/exec"

"gitlab.com/mnm/bud/package/exe"
"gitlab.com/mnm/bud/package/gomod"
"gitlab.com/mnm/bud/package/socket"
"gitlab.com/mnm/bud/runtime/bud"
)

type Project struct {
Module *gomod.Module
Flag Flag
Env Env
Stdout io.Writer
Stderr io.Writer
}

func (p *Project) args(args ...string) []string {
return append(args, p.Flag.List()...)
}

func (p *Project) command(ctx context.Context, args ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, p.Module.Directory("bud", "cli"), args...)
func (p *Project) command(ctx context.Context, args ...string) *exe.Cmd {
cmd := exe.Command(ctx, p.Module.Directory("bud", "cli"), args...)
cmd.Dir = p.Module.Directory()
cmd.Env = p.Env.List()
cmd.Stderr = p.Stderr
cmd.Stdout = p.Stdout
return cmd
}

func (p *Project) Executor(ctx context.Context, args ...string) *exec.Cmd {
return p.command(ctx, p.args(args...)...)
func (p *Project) Executor(ctx context.Context, args ...string) *exe.Cmd {
return p.command(ctx, args...)
}

// Execute a custom command
Expand All @@ -45,42 +41,42 @@ func (p *Project) Execute(ctx context.Context, args ...string) error {
return nil
}

func (p *Project) Builder(ctx context.Context) *exec.Cmd {
return p.command(ctx, p.args("build")...)
func (p *Project) Builder(ctx context.Context) *exe.Cmd {
return p.command(ctx, "build")
}

func (p *Project) Build(ctx context.Context) (*App, error) {
func (p *Project) Build(ctx context.Context) (*bud.App, error) {
cmd := p.Builder(ctx)
if err := cmd.Run(); err != nil {
return nil, err
}
return &App{
return &bud.App{
Module: p.Module,
Env: p.Env.List(),
Stderr: p.Stderr,
Stdout: p.Stdout,
}, nil
}

func (p *Project) Runner(ctx context.Context, listener net.Listener) (*exec.Cmd, error) {
func (p *Project) Runner(ctx context.Context, listener net.Listener) (*exe.Cmd, error) {
// Pass the socket through
files, env, err := socket.Files(listener)
if err != nil {
return nil, err
}
cmd := p.command(ctx, p.args("run")...)
cmd := p.command(ctx, "run")
cmd.Env = append(p.Env.List(), string(env))
cmd.ExtraFiles = append(cmd.ExtraFiles, files...)
return cmd, nil
}

func (p *Project) Run(ctx context.Context, listener net.Listener) (*Process, error) {
func (p *Project) Run(ctx context.Context, listener net.Listener) (*exe.Cmd, error) {
cmd, err := p.Runner(ctx, listener)
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
return &Process{cmd}, nil
return cmd, nil
}
23 changes: 18 additions & 5 deletions internal/budtest/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import (
"github.com/matthewmueller/diff"
"gitlab.com/mnm/bud/internal/bud"
"gitlab.com/mnm/bud/internal/testdir"
"gitlab.com/mnm/bud/package/exe"
"gitlab.com/mnm/bud/package/gomod"
"gitlab.com/mnm/bud/package/modcache"
"gitlab.com/mnm/bud/package/socket"
runtime_bud "gitlab.com/mnm/bud/runtime/bud"
)

func New(dir string) *Compiler {
return &Compiler{
dir: dir,
Flag: bud.Flag{
Flag: runtime_bud.Flag{
Embed: false,
Minify: false,
Hot: true,
Expand Down Expand Up @@ -57,7 +59,7 @@ func New(dir string) *Compiler {

type Compiler struct {
dir string
Flag bud.Flag
Flag runtime_bud.Flag
Files map[string]string // String files (convenient)
BFiles map[string][]byte // Byte files (for images and binaries)
Modules modcache.Modules // name@version[path[data]]
Expand Down Expand Up @@ -95,7 +97,7 @@ func (c *Compiler) Compile(ctx context.Context) (p *Project, err error) {
}
compiler.Env = c.Env
compiler.ModCacheRW = true
project, err := compiler.Compile(ctx, c.Flag)
project, err := compiler.Compile(ctx, &c.Flag)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -194,7 +196,7 @@ func (p *Project) Run(ctx context.Context) (*Server, error) {

type App struct {
module *gomod.Module
app *bud.App
app *runtime_bud.App
}

func (a *App) Exists(paths ...string) error {
Expand Down Expand Up @@ -296,7 +298,7 @@ func (s Stdio) String() string {
}

type Server struct {
process *bud.Process
process *exe.Cmd
listener net.Listener
client *http.Client
}
Expand Down Expand Up @@ -450,6 +452,17 @@ func (r *Response) ExpectHeaders(expect string) error {
return diffHTTP(expect, dump)
}

func (r *Response) ContainsBody(expect string) error {
body, err := io.ReadAll(r.Response.Body)
if err != nil {
return err
}
if strings.Contains(string(body), expect) {
return nil
}
return fmt.Errorf("%s does not contain %q", string(body), expect)
}

func (r *Response) Query(selector string) (*goquery.Selection, error) {
doc, err := goquery.NewDocumentFromReader(r.Body)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/command/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Command) Run(ctx context.Context) error {
return err
}
// Compile the project CLI
project, err := compiler.Compile(ctx, c.Bud.Flag)
project, err := compiler.Compile(ctx, &c.Bud.Flag)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (

"gitlab.com/mnm/bud/internal/bud"
"gitlab.com/mnm/bud/package/trace"
runtime_bud "gitlab.com/mnm/bud/runtime/bud"
)

// Bud command
type Bud struct {
Flag bud.Flag
Flag runtime_bud.Flag
Trace bool
Dir string
Args []string
Expand Down Expand Up @@ -61,7 +62,7 @@ func (c *Bud) Run(ctx context.Context) (err error) {
return err
}
// Compiler the project CLI
project, err := compiler.Compile(ctx, c.Flag)
project, err := compiler.Compile(ctx, &c.Flag)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/command/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Command) Run(ctx context.Context) error {
return err
}
// Compiler the project CLI
project, err := compiler.Compile(ctx, c.Bud.Flag)
project, err := compiler.Compile(ctx, &c.Bud.Flag)
if err != nil {
return err
}
Expand Down
8 changes: 1 addition & 7 deletions internal/generator/command/command.gotext
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,19 @@ type CLI struct {
}

func (c *CLI) Parse(ctx context.Context, args ...string) error {
project := project.New(c.fsys, c.module)
project := bud.New(c.fsys, c.module)
cli := commander.New("cli")

{ // cli run
cmd := &run.Command{Project: project}
cli := cli.Command("run", "run command")
cli.Flag("embed", "embed assets").Bool(&cmd.Flag.Embed).Default(false)
cli.Flag("hot", "hot reload").Bool(&cmd.Flag.Hot).Default(true)
cli.Flag("minify", "minify assets").Bool(&cmd.Flag.Minify).Default(false)
cli.Flag("port", "port to listen to").String(&cmd.Port).Default(":3000")
cli.Run(cmd.Run)
}

{ // cli build
cmd := &build.Command{Project: project}
cli := cli.Command("build", "build command")
cli.Flag("embed", "embed assets").Bool(&cmd.Flag.Embed).Default(false)
cli.Flag("hot", "hot reload").Bool(&cmd.Flag.Hot).Default(true)
cli.Flag("minify", "minify assets").Bool(&cmd.Flag.Minify).Default(false)
cli.Run(cmd.Run)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/generator/command/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (p *parser) Parse(ctx context.Context) (state *State, err error) {
p.imports.AddNamed("commander", "gitlab.com/mnm/bud/package/commander")
// p.imports.AddNamed("command", "gitlab.com/mnm/bud/runtime/command")
p.imports.AddNamed("gomod", "gitlab.com/mnm/bud/package/gomod")
p.imports.AddNamed("project", "gitlab.com/mnm/bud/runtime/project")
p.imports.AddNamed("bud", "gitlab.com/mnm/bud/runtime/bud")
p.imports.AddNamed("run", "gitlab.com/mnm/bud/runtime/command/run")
p.imports.AddNamed("build", "gitlab.com/mnm/bud/runtime/command/build")
p.imports.AddNamed("generator", p.module.Import("bud/.cli/generator"))
Expand Down
Loading

0 comments on commit 6552a9b

Please sign in to comment.