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

Move build cache and fallback to copying #50

Merged
merged 4 commits into from
May 16, 2022
Merged
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
3 changes: 2 additions & 1 deletion example/basic/view/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

<style>
h1 {
background: red;
background: blue;
padding: 20px;
color: white;
}
</style>
2 changes: 1 addition & 1 deletion internal/bud/bud.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Load(module *gomod.Module) (*Compiler, error) {
}
return &Compiler{
module: module,
bcache: buildcache.Default(),
bcache: buildcache.Default(module),
Env: env,
Stdout: os.Stdout,
Stderr: os.Stderr,
Expand Down
6 changes: 2 additions & 4 deletions internal/buildcache/buildcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import (
"github.com/livebud/bud/package/gomod"
)

func Default() *Cache {
func Default(module *gomod.Module) *Cache {
return &Cache{
// TODO: make this configurable
// TODO: use the user cache, once we have a way to clean up
Dir: filepath.Join(os.TempDir(), "bud", "cache"),
Dir: filepath.Join(module.Directory(), "bud", "cache"),
}
}

Expand Down
18 changes: 9 additions & 9 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ func Parse(args []string) int {

func parse(args []string) error {
// $ bud
bud := new(command.Bud)
bud := command.New()
cli := commander.New("bud")
cli.Flag("chdir", "Change the working directory").Short('C').String(&bud.Dir).Default(".")
cli.Args("args").Strings(&bud.Args)
cli.Run(bud.Run)

{ // $ bud create <app>
cmd := &create.Command{Bud: bud}
cmd := create.New(bud)
cli := cli.Command("create", "create a new project")
cli.Arg("dir").String(&cmd.Dir)
cli.Run(cmd.Run)
}

{ // $ bud run
cmd := &run.Command{Bud: bud}
cmd := run.New(bud)
cli := cli.Command("run", "run the development server")
cli.Flag("embed", "embed the assets").Bool(&bud.Flag.Embed).Default(false)
cli.Flag("hot", "hot reload the frontend").Bool(&bud.Flag.Hot).Default(true)
Expand All @@ -54,7 +54,7 @@ func parse(args []string) error {
}

{ // $ bud build
cmd := &build.Command{Bud: bud}
cmd := build.New(bud)
cli := cli.Command("build", "build the production server")
cli.Flag("embed", "embed the assets").Bool(&bud.Flag.Embed).Default(true)
cli.Flag("hot", "hot reload the frontend").Bool(&bud.Flag.Hot).Default(false)
Expand All @@ -66,7 +66,7 @@ func parse(args []string) error {
cli := cli.Command("tool", "extra tools")

{ // $ bud tool di
cmd := &di.Command{Bud: bud}
cmd := di.New(bud)
cli := cli.Command("di", "dependency injection generator")
cli.Flag("dependency", "generate dependency provider").Short('d').Strings(&cmd.Dependencies)
cli.Flag("external", "mark dependency as external").Short('e').Strings(&cmd.Externals).Optional()
Expand All @@ -78,19 +78,19 @@ func parse(args []string) error {
}

{ // $ bud tool v8
cmd := &v8.Command{Bud: bud}
cmd := v8.New()
cli := cli.Command("v8", "Execute Javascript with V8 from stdin")
cli.Run(cmd.Run)

{ // $ bud tool v8 client
cmd := &v8client.Command{Bud: bud}
cmd := v8client.New()
cli := cli.Command("client", "V8 client used during development")
cli.Run(cmd.Run)
}
}

{ // $ bud tool cache
cmd := &cache.Command{}
cmd := cache.New(bud)
cli := cli.Command("cache", "Manage the build cache")

{ // $ bud tool cache clean
Expand All @@ -101,7 +101,7 @@ func parse(args []string) error {
}

{ // $ bud version
cmd := &version.Command{}
cmd := version.New()
cli := cli.Command("version", "Show package versions")
cli.Arg("key").String(&cmd.Key).Default("")
cli.Run(cmd.Run)
Expand Down
10 changes: 7 additions & 3 deletions internal/command/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ import (
"github.com/livebud/bud/internal/command"
)

func New(bud *command.Bud) *Command {
return &Command{bud}
}

type Command struct {
Bud *command.Bud
bud *command.Bud
}

func (c *Command) Run(ctx context.Context) error {
// Load the compiler
compiler, err := bud.Find(c.Bud.Dir)
compiler, err := bud.Find(c.bud.Dir)
if err != nil {
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
4 changes: 4 additions & 0 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
runtime_bud "github.com/livebud/bud/runtime/bud"
)

func New() *Bud {
return &Bud{}
}

// Bud command
type Bud struct {
Flag runtime_bud.Flag
Expand Down
34 changes: 30 additions & 4 deletions internal/command/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strings"

"golang.org/x/sync/errgroup"

"github.com/livebud/bud/internal/command"
"github.com/livebud/bud/internal/version"
"github.com/livebud/bud/package/gomod"
"github.com/otiai10/copy"
)

func New(bud *command.Bud) *Command {
return &Command{bud: bud}
}

type Command struct {
Bud *command.Bud
bud *command.Bud
Dir string
}

func (c *Command) Run(ctx context.Context) error {
dir := filepath.Join(c.Bud.Dir, c.Dir)
dir := filepath.Join(c.bud.Dir, c.Dir)
// Check if we can write into the directory
if err := checkDir(dir); err != nil {
return err
Expand All @@ -52,7 +58,7 @@ func (c *Command) Run(ctx context.Context) error {
return err
}
// Try moving the temporary build path to the project directory
if err := os.Rename(tmpDir, dir); err != nil {
if err := move(tmpDir, dir); err != nil {
// Can't rename on top of an existing directory
if !errors.Is(err, fs.ErrExist) {
return err
Expand All @@ -63,7 +69,7 @@ func (c *Command) Run(ctx context.Context) error {
return err
}
for _, fi := range fis {
if err := os.Rename(filepath.Join(tmpDir, fi.Name()), filepath.Join(dir, fi.Name())); err != nil {
if err := move(filepath.Join(tmpDir, fi.Name()), filepath.Join(dir, fi.Name())); err != nil {
return err
}
}
Expand Down Expand Up @@ -145,3 +151,23 @@ func findBudModule() (*gomod.Module, error) {
}
return gomod.Find(dir)
}

// Move first tries to rename a directory `from` one location `to` another.
// If `from` is on a different partition than `to`, the underlying os.Rename can
// fail with an "invalid cross-device link" error. If this occurs we'll fallback
// to copying the files over recursively.
func move(from, to string) error {
if err := os.Rename(from, to); err != nil {
// If it's not an invalid cross-device link error, return the error
if !isInvalidCrossLink(err) {
return err
}
// Fallback to copying files recursively
return copy.Copy(from, to)
}
return nil
}

func isInvalidCrossLink(err error) bool {
return strings.Contains(err.Error(), "invalid cross-device link")
}
10 changes: 7 additions & 3 deletions internal/command/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import (
"github.com/livebud/bud/package/socket"
)

func New(bud *command.Bud) *Command {
return &Command{bud: bud}
}

type Command struct {
Bud *command.Bud
bud *command.Bud
Port string
}

Expand All @@ -32,12 +36,12 @@ func (c *Command) Run(ctx context.Context) error {
}
console.Info("Listening on http://" + host + ":" + port)
// Load the compiler
compiler, err := bud.Find(c.Bud.Dir)
compiler, err := bud.Find(c.bud.Dir)
if err != nil {
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
14 changes: 13 additions & 1 deletion internal/command/tool/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ import (
"context"
"os"
"path/filepath"

"github.com/livebud/bud/internal/command"
"github.com/livebud/bud/package/gomod"
)

func New(bud *command.Bud) *Command {
return &Command{bud}
}

type Command struct {
bud *command.Bud
}

func (c *Command) Clean(ctx context.Context) error {
cacheDir := filepath.Join(os.TempDir(), "bud-compiler")
module, err := gomod.Find(c.bud.Dir)
if err != nil {
return err
}
cacheDir := filepath.Join(module.Directory(), "bud", "cache")
return os.RemoveAll(cacheDir)
}
8 changes: 6 additions & 2 deletions internal/command/tool/di/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ import (
"github.com/livebud/bud/package/parser"
)

func New(bud *command.Bud) *Command {
return &Command{bud: bud}
}

type Command struct {
Bud *command.Bud
bud *command.Bud
Target string
Map map[string]string
Dependencies []string
Expand All @@ -25,7 +29,7 @@ type Command struct {
}

func (c *Command) Run(ctx context.Context) error {
module, err := gomod.Find(c.Bud.Dir)
module, err := gomod.Find(c.bud.Dir)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions internal/command/tool/v8/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package client
import (
"context"

"github.com/livebud/bud/internal/command"
"github.com/livebud/bud/package/js/v8server"
)

func New() *Command {
return &Command{}
}

type Command struct {
Bud *command.Bud
}

func (c *Command) Run(ctx context.Context) error {
Expand Down
6 changes: 4 additions & 2 deletions internal/command/tool/v8/v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"os"
"strings"

"github.com/livebud/bud/internal/command"
v8 "github.com/livebud/bud/package/js/v8"
"github.com/mattn/go-isatty"
)

func New() *Command {
return &Command{}
}

type Command struct {
Bud *command.Bud
}

func (c *Command) Run(ctx context.Context) error {
Expand Down
4 changes: 4 additions & 0 deletions internal/command/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/livebud/bud/internal/version"
)

func New() *Command {
return &Command{}
}

type Command struct {
Key string
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/bud/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func New(fsys *overlay.FileSystem, module *gomod.Module) *Project {
return &Project{
fsys: fsys,
module: module,
bcache: buildcache.Default(),
bcache: buildcache.Default(module),
Env: os.Environ(),
Stderr: os.Stderr,
Stdout: os.Stdout,
Expand Down