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