github.com/jacobsoderblom/buffalo@v0.11.0/buffalo/cmd/build/transform_main.go (about)

     1  package build
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/gobuffalo/plush"
    10  	"github.com/pkg/errors"
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  func (b *Builder) transformMain() error {
    15  	logrus.Debug("transforming main() to originalMain()")
    16  
    17  	return b.transform("main.go", func(body []byte, w io.Writer) error {
    18  		body = bytes.Replace(body, []byte("func main()"), []byte("func originalMain()"), 1)
    19  		_, err := w.Write(body)
    20  		if err != nil {
    21  			return errors.WithStack(err)
    22  		}
    23  		return nil
    24  	})
    25  }
    26  
    27  func (b *Builder) createBuildMain() error {
    28  	ctx := plush.NewContext()
    29  	ctx.Set("opts", b.Options)
    30  
    31  	t, err := templates.MustString("main.go.tmpl")
    32  	if err != nil {
    33  		return errors.WithStack(err)
    34  	}
    35  
    36  	s, err := plush.Render(t, ctx)
    37  	if err != nil {
    38  		return errors.WithStack(err)
    39  	}
    40  
    41  	bbm := filepath.Join(b.Root, "buffalo_build_main.go")
    42  	logrus.Debugf("creating %s", bbm)
    43  	f, err := os.Create(bbm)
    44  	if err != nil {
    45  		return errors.WithStack(err)
    46  	}
    47  	defer f.Close()
    48  	b.cleanups = append(b.cleanups, func() error {
    49  		return os.RemoveAll(bbm)
    50  	})
    51  	f.WriteString(s)
    52  	return nil
    53  }