forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
128 lines (121 loc) · 3.37 KB
/
build.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// +build !test,!release
package main
import (
"encoding/json"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/ansel1/merry"
"github.com/kr/binarydist"
"github.com/ulikunitz/xz"
)
func init() {
CLITopics = append(CLITopics, Topics{
{
Name: "build",
Description: "These commands are used to build the CLI. They are not intended to be used otherwise.",
Commands: Commands{
{
Command: "plugins",
Description: "installs core plugins",
Run: buildPlugins,
},
{
Command: "manifest",
Description: "builds manifest.json to upload to S3",
Flags: []Flag{
{Name: "dir", Char: "d", Required: true, HasValue: true},
{Name: "version", Char: "v", Required: true, HasValue: true},
{Name: "channel", Char: "c", Required: true, HasValue: true},
{Name: "targets", Char: "t", Required: true, HasValue: true},
},
Run: buildManifest,
},
{
Command: "bsdiff",
Description: "generates bsdiff patch",
Flags: []Flag{
{Name: "new", HasValue: true, Required: true},
{Name: "channel", HasValue: true, Required: true},
{Name: "target", HasValue: true, Required: true},
{Name: "out", HasValue: true, Required: true},
},
Run: buildBsdiff,
},
},
},
}...)
}
func buildPlugins(ctx *Context) {
pjson := struct {
Dependencies map[string]string `json:"dependencies"`
}{}
must(readJSON(&pjson, filepath.Join(CorePlugins.Path, "package.json")))
plugins := []string{}
for name, v := range pjson.Dependencies {
plugins = append(plugins, name+"@"+v)
}
must(CorePlugins.installPackages(plugins...))
for _, plugin := range plugins {
s := strings.SplitN(plugin, "@", 2)
plugin, err := CorePlugins.ParsePlugin(s[0], s[1])
must(err)
CorePlugins.addToCache(plugin)
}
}
func buildManifest(ctx *Context) {
manifest := &Manifest{
ReleasedAt: time.Now().UTC().Format("2006-01-02T15:04:05Z0700"),
Version: ctx.Flags["version"].(string),
Channel: ctx.Flags["channel"].(string),
Builds: map[string]*Build{},
}
files := strings.Split(ctx.Flags["targets"].(string), ",")
re := regexp.MustCompile(`heroku-v\d+\.\d+\.\d+-\w+-(\w+)-(\w+)\.tar\..z`)
for _, file := range files {
filename := filepath.Base(file)
info := re.FindStringSubmatch(filename)
f, err := os.Open(file)
must(err)
fi, err := f.Stat()
must(err)
os := info[1]
arch := info[2]
sha256, err := fileSha256(file)
must(err)
manifest.Builds[os+"-"+arch] = &Build{
URL: "https://cli-assets.heroku.com/branches/" + manifest.Channel + "/" + manifest.Version + "/" + filename,
Sha256: sha256,
Bytes: fi.Size(),
}
}
data, err := json.MarshalIndent(manifest, "", " ")
must(err)
Println(string(data))
}
func buildBsdiff(ctx *Context) {
channel := ctx.Flags["channel"].(string)
newpath := ctx.Flags["new"].(string)
target := ctx.Flags["target"].(string)
out := ctx.Flags["out"].(string)
patch, err := os.Create(out)
must(err)
newf, err := os.Open(newpath)
must(err)
defer newf.Close()
new, err := xz.NewReader(newf)
must(err)
manifest := GetUpdateManifest(channel, "")
build := manifest.Builds[target]
old, sha, err := downloadXZ(build.URL, "")
must(err)
Errf("downloading %s\n", build.URL)
defer patch.Close()
Errf("bsdiffing %s\n", out)
must(binarydist.Diff(old, new, patch))
if sha() != build.Sha256 {
must(merry.Errorf("SHA mismatch"))
}
}