Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
adds --clean-assets flag (#1586)
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Feb 10, 2019
1 parent 16895ff commit 55f304c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
3 changes: 3 additions & 0 deletions buffalo/cmd/build.go
Expand Up @@ -72,6 +72,8 @@ var xbuildCmd = &cobra.Command{
opts.TemplateValidators = append(opts.TemplateValidators, build.PlushValidator, build.GoTemplateValidator)
}

clean := build.Cleanup(opts)
defer clean(run)
run.WithNew(build.New(opts))
return run.Run()
},
Expand All @@ -90,6 +92,7 @@ func init() {
xbuildCmd.Flags().BoolVarP(&buildOptions.Debug, "deprecated-verbose", "d", false, "[deprecated] use -v instead")
xbuildCmd.Flags().BoolVar(&buildOptions.DryRun, "dry-run", false, "runs the build 'dry'")
xbuildCmd.Flags().BoolVar(&buildOptions.SkipTemplateValidation, "skip-template-validation", false, "skip validating templates")
xbuildCmd.Flags().BoolVar(&buildOptions.CleanAssets, "clean-assets", false, "will delete public/assets before calling webpack")
xbuildCmd.Flags().StringVarP(&buildOptions.Environment, "environment", "", "development", "set the environment for the binary")
xbuildCmd.Flags().StringVar(&buildOptions.Mod, "mod", "", "-mod flag for go build")
}
Expand Down
9 changes: 8 additions & 1 deletion genny/assets/webpack/webpack.go
Expand Up @@ -3,6 +3,7 @@ package webpack
import (
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/gobuffalo/genny"
Expand All @@ -12,7 +13,13 @@ import (
)

// BinPath is the path to the local install of webpack
var BinPath = filepath.Join("node_modules", ".bin", "webpack")
var BinPath = func() string {
s := filepath.Join("node_modules", ".bin", "webpack")
if runtime.GOOS == "windows" {
s += ".cmd"
}
return s
}()

// Templates used for generating webpack
// (exported mostly for the "fix" command)
Expand Down
7 changes: 7 additions & 0 deletions genny/build/assets.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"io/ioutil"
"os/exec"
"path/filepath"

"github.com/gobuffalo/buffalo/genny/assets/webpack"
"github.com/gobuffalo/envy"
Expand All @@ -21,6 +22,12 @@ func assets(opts *Options) (*genny.Generator, error) {
}

if opts.App.WithWebpack {
if opts.CleanAssets {
g.RunFn(func(r *genny.Runner) error {
r.Delete(filepath.Join(opts.App.Root, "public", "assets"))
return nil
})
}
g.RunFn(func(r *genny.Runner) error {
r.Logger.Debugf("setting NODE_ENV = %s", opts.Environment)
return envy.MustSet("NODE_ENV", opts.Environment)
Expand Down
11 changes: 5 additions & 6 deletions genny/build/build.go
Expand Up @@ -35,10 +35,6 @@ func New(opts *Options) (*genny.Generator, error) {
// validate templates
g.RunFn(ValidateTemplates(templateWalker(opts.App), opts.TemplateValidators))

g.RunFn(func(r *genny.Runner) error {
return jam.Pack(jam.PackOptions{})
})

// rename main() to originalMain()
g.RunFn(transformMain(opts))

Expand Down Expand Up @@ -79,15 +75,18 @@ func New(opts *Options) (*genny.Generator, error) {
}
g.Merge(dg)

g.RunFn(func(r *genny.Runner) error {
return jam.Pack(jam.PackOptions{})
})

// create the final go build command
c, err := buildCmd(opts)
if err != nil {
return g, errors.WithStack(err)
}
g.Command(c)

// clean up everything!
g.RunFn(cleanup(opts))
g.RunFn(Cleanup(opts))

g.Event(EvtBuildStop, events.Payload{"opts": opts})
return g, nil
Expand Down
10 changes: 9 additions & 1 deletion genny/build/cleanup.go
Expand Up @@ -2,14 +2,17 @@ package build

import (
"os"
"os/exec"
"path/filepath"

"github.com/gobuffalo/envy"
"github.com/gobuffalo/genny"
"github.com/gobuffalo/packr/v2/jam"
"github.com/pkg/errors"
)

func cleanup(opts *Options) genny.RunFn {
// Cleanup all of the generated files
func Cleanup(opts *Options) genny.RunFn {
return func(r *genny.Runner) error {
defer os.RemoveAll(filepath.Join(opts.Root, "a"))
if err := jam.Clean(); err != nil {
Expand All @@ -34,6 +37,11 @@ func cleanup(opts *Options) genny.RunFn {
return errors.WithStack(err)
}
}
if envy.Mods() {
if err := r.Exec(exec.Command(genny.GoBin(), "mod", "tidy")); err != nil {
return errors.WithStack(err)
}
}
return nil
}
}
8 changes: 5 additions & 3 deletions genny/build/options.go
Expand Up @@ -17,7 +17,9 @@ type Options struct {
// a) git sha of last commit or
// b) to time.RFC3339 of BuildTime
BuildVersion string `json:"build_version,omitempty"`
WithAssets bool `json:"with_assets,omitempty"`
// CleanAssets will remove the public/assets folder build compiling
CleanAssets bool `json:"clean_assets"`
WithAssets bool `json:"with_assets,omitempty"`
// places ./public/assets into ./bin/assets.zip.
// requires WithAssets = true
ExtractAssets bool `json:"extract_assets,omitempty"`
Expand All @@ -37,8 +39,8 @@ type Options struct {
// Empty by default
TemplateValidators []TemplateValidator `json:"-"`
// Mod is the -mod flag
Mod string
rollback *sync.Map
Mod string `json:"mod"`
rollback *sync.Map `json:"rollback"`
}

// Validate that options are usuable
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -42,6 +42,6 @@ require (
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190208162236-193df9c0f06f
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4
golang.org/x/tools v0.0.0-20190206221403-44bcb96178d3
golang.org/x/tools v0.0.0-20190208222737-3744606dbb67
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc
)
3 changes: 2 additions & 1 deletion go.sum
Expand Up @@ -591,8 +591,9 @@ golang.org/x/tools v0.0.0-20190118193359-16909d206f00/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190122202912-9c309ee22fab/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190124004107-78ee07aa9465/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190131142011-8dbcc66f33bb/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190206221403-44bcb96178d3 h1:M9mD7d4inzK0+YbTneZEs9Y+q1B1zLv8YxJDJ6hFgnY=
golang.org/x/tools v0.0.0-20190206221403-44bcb96178d3/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190208222737-3744606dbb67 h1:bPP/rGuN1LUM0eaEwo6vnP6OfIWJzJBulzGUiKLjjSY=
golang.org/x/tools v0.0.0-20190208222737-3744606dbb67/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
Expand Down

0 comments on commit 55f304c

Please sign in to comment.