-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.go
74 lines (48 loc) · 2.04 KB
/
binary.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
package compiler
import (
_ "embed"
"encoding/json"
"os"
"path/filepath"
"github.com/bastean/bingo/pkg/context/binary/domain/aggregate"
"github.com/bastean/bingo/pkg/context/binary/domain/builder"
"github.com/bastean/bingo/pkg/context/binary/domain/service"
"github.com/bastean/bingo/pkg/context/binary/domain/valueObject"
)
//go:generate rsync -a --delete ../../domain/model app
//go:generate touch app/config/config.json
//go:generate bash -c "cd app && go mod tidy && go mod vendor"
//go:generate rm -rf embed
//go:generate mkdir embed
//go:generate bash -c "cd app && tar -czf ../embed/app.tar.gz *"
const dirTempName = "temp"
const dirTempBuildName = "build-*"
const fileCompressedAppName = "app.tar.gz"
const fileEntrypointName = "main.go"
const fileConfigurationName = "config.json"
//go:embed embed/app.tar.gz
var compressedAppEmbedded []byte
type Binary struct{}
func (binary *Binary) Build(root *aggregate.Root) *valueObject.Filepath {
os.Mkdir(dirTempName, os.ModePerm)
dirTemp, err := os.MkdirTemp(dirTempName, dirTempBuildName)
service.FailOnError(err, "failed to create temp directory")
compressedAppFile := filepath.Join(dirTemp, fileCompressedAppName)
err = os.WriteFile(compressedAppFile, compressedAppEmbedded, os.ModePerm)
service.FailOnError(err, "failed to write embed compressed app")
service.RunCommand("tar", "-C", dirTemp, "-xf", compressedAppFile)
service.RunCommand("rm", "-f", compressedAppFile)
rootJson, err := json.Marshal(root)
service.FailOnError(err, "failed to encoding json")
configFile := filepath.Join(dirTemp, "config", fileConfigurationName)
err = os.WriteFile(configFile, rootJson, os.ModePerm)
service.FailOnError(err, "failed to write configuration file")
output := filepath.Join("build", root.Command.Name)
// TODO!(build): GOOS & GOARCH
service.RunCommand("go", "build", "-C", dirTemp, "-mod", "vendor", "-o", output, fileEntrypointName)
filePath := filepath.Join(dirTemp, output)
return valueObject.NewFilepath(filePath)
}
func NewBinaryBuilder() builder.Builder {
return new(Binary)
}