github.com/bscott/buffalo@v0.11.1/buffalo/cmd/build/apkg.go (about) 1 package build 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "os" 8 "path/filepath" 9 10 "github.com/gobuffalo/plush" 11 "github.com/pkg/errors" 12 "github.com/sirupsen/logrus" 13 ) 14 15 func (b *Builder) prepAPackage() error { 16 a := filepath.Join(b.Root, "a") 17 logrus.Debugf("preparing %s", a) 18 err := os.MkdirAll(a, 0766) 19 if err != nil { 20 return errors.WithStack(err) 21 } 22 23 infl := filepath.Join(b.Root, "inflections.json") 24 if _, err := os.Stat(infl); err == nil { 25 logrus.Debugf("preparing %s", infl) 26 // there's an inflection file we need to copy it over 27 fa, err := os.Open(infl) 28 if err != nil { 29 return errors.WithStack(err) 30 } 31 defer fa.Close() 32 fb, err := os.Create(filepath.Join(b.Root, "a", "inflections.json")) 33 if err != nil { 34 return errors.WithStack(err) 35 } 36 defer fb.Close() 37 _, err = io.Copy(fb, fa) 38 if err != nil { 39 return errors.WithStack(err) 40 } 41 } 42 43 b.cleanups = append(b.cleanups, func() error { 44 return os.RemoveAll(a) 45 }) 46 return nil 47 } 48 49 func (b *Builder) buildAInit() error { 50 a := filepath.Join(b.Root, "a", "a.go") 51 logrus.Debugf("generating %s", a) 52 f, err := os.Create(a) 53 if err != nil { 54 return errors.WithStack(err) 55 } 56 defer f.Close() 57 58 ctx := plush.NewContext() 59 ctx.Set("opts", b.Options) 60 61 t, err := templates.MustString("a.go.tmpl") 62 if err != nil { 63 return errors.WithStack(err) 64 } 65 66 s, err := plush.Render(t, ctx) 67 if err != nil { 68 return errors.WithStack(err) 69 } 70 71 _, err = f.WriteString(s) 72 if err != nil { 73 return errors.WithStack(err) 74 } 75 return nil 76 } 77 78 func (b *Builder) buildADatabase() error { 79 ad := filepath.Join(b.Root, "a", "database.go") 80 logrus.Debugf("generating %s", ad) 81 82 dgo, err := os.Create(ad) 83 if err != nil { 84 return errors.WithStack(err) 85 } 86 defer dgo.Close() 87 88 bb := &bytes.Buffer{} 89 if b.WithPop { 90 // copy the database.yml file to the migrations folder so it's available through packr 91 os.MkdirAll(filepath.Join(b.Root, "migrations"), 0755) 92 d, err := os.Open("database.yml") 93 if err != nil { 94 return errors.WithStack(err) 95 } 96 defer d.Close() 97 _, err = io.Copy(bb, d) 98 if err != nil { 99 return errors.WithStack(err) 100 } 101 if bytes.Contains(bb.Bytes(), []byte("sqlite")) { 102 b.Tags = append(b.Tags, "sqlite") 103 if !b.Static { 104 logrus.Debug("you are building a SQLite application, please consider using the `--static` flag to compile a static binary") 105 } 106 } 107 } 108 dgo.WriteString("package a\n") 109 dgo.WriteString(fmt.Sprintf("var DB_CONFIG = `%s`", bb.String())) 110 return nil 111 }