-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
281 lines (235 loc) · 7.38 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"context"
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/v42/github"
"golang.org/x/oauth2"
"github.com/mholt/archiver/v3"
log "github.com/sirupsen/logrus"
)
// application's configuration
type Configuration struct {
Owner string
Repository string
Tag string
Amd64Substring string
Arm64Substring string
Compressed bool
GithubToken string
Overwrite bool
UniversalIdentifer string
}
const (
EXTRACT_PREFIX = "extract"
DOWNLOAD_PREFIX = "download"
FAT_PREFIX = "fat"
)
// create universal binary using the given macOS binaries
// for amd64 and arm64
func CreateUniveralBinary(cfg *Configuration) error {
log.Info("Starting process...")
log.Debugf("Repository: %s/%s", cfg.Owner, cfg.Repository)
log.Debugf("Tag: %s", cfg.Tag)
log.Debugf("Amd64 Regex: %s", cfg.Amd64Substring)
log.Debugf("Arm64 Regex: %s", cfg.Arm64Substring)
log.Debugf("Compressed?: %t", cfg.Compressed)
release, err := cfg.GetRelease()
if err != nil {
return err
}
log.Info("Finding relevant Github release")
amd64Asset, arm64Asset, err := cfg.FilterArtifacts(release.Assets)
if err != nil {
return err
}
amd64Path, err := cfg.DownloadAndGetPath(amd64Asset)
if err != nil {
return err
}
arm64Path, err := cfg.DownloadAndGetPath(arm64Asset)
if err != nil {
return err
}
log.Info("Successfully downloaded release assets")
log.Debugf("The path to the amd64 binary is: %s", amd64Path)
log.Debugf("The path to the arm64 binary is: %s", arm64Path)
fatPath, err := cfg.Combine(amd64Path, arm64Path, *amd64Asset.Name)
if err != nil {
return err
}
log.Info("Combined assets into universal binary")
log.Debugf("The path to the fat binary is: %s", fatPath)
err = cfg.UploadAsset(release, fatPath)
if err != nil {
return err
}
log.Info("Uploaded universal binary to Github")
return nil
}
// Get the github release with the given tag name.
// If the given tag is `latest`, return the latest github release
func (cfg *Configuration) GetRelease() (*github.RepositoryRelease, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: cfg.GithubToken,
})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
var release *github.RepositoryRelease
var err error
if cfg.Tag == "latest" {
log.Info("Finding the latest release")
release, _, err = client.Repositories.GetLatestRelease(ctx, cfg.Owner, cfg.Repository)
} else {
log.Info("Finding the release with tag ", cfg.Tag)
release, _, err = client.Repositories.GetReleaseByTag(ctx, cfg.Owner, cfg.Repository, cfg.Tag)
}
if err != nil {
return nil, err
}
return release, nil
}
// Filter the available assets for the given release to find the macOS specific assets
func (cfg *Configuration) FilterArtifacts(assets []*github.ReleaseAsset) (*github.ReleaseAsset, *github.ReleaseAsset, error) {
log.Debug("Filtering artifacts")
var amd64Asset, arm64Asset *github.ReleaseAsset
for _, asset := range assets {
assetName := asset.GetName()
if strings.Contains(assetName, cfg.Amd64Substring) {
amd64Asset = asset
}
if strings.Contains(assetName, cfg.Arm64Substring) {
arm64Asset = asset
}
}
if amd64Asset == nil || arm64Asset == nil {
return nil, nil, errors.New("could not find needed artifacts from github release")
}
return amd64Asset, arm64Asset, nil
}
// Download the Github asset to the local filesystem and return the path for the binary.
// If the asset is compressed, uncompress and return the specific path to the binary
func (cfg *Configuration) DownloadAndGetPath(asset *github.ReleaseAsset) (string, error) {
name := asset.GetName()
url := asset.GetBrowserDownloadURL()
log.Debugf("Asset Name: %s", name)
log.Debugf("Asset Download URL: %s", url)
currentWorkingDir, err := os.Getwd()
if err != nil {
return "", err
}
downloadDir, err := ioutil.TempDir(currentWorkingDir, DOWNLOAD_PREFIX)
if err != nil {
return "", err
}
defer os.RemoveAll(downloadDir)
downloadLocation := filepath.Join(downloadDir, name)
err = downloadFile(downloadLocation, url)
if err != nil {
return "", err
}
var binary string
if cfg.Compressed {
extractDir, err := ioutil.TempDir(currentWorkingDir, EXTRACT_PREFIX)
if err != nil {
return "", err
}
err = archiver.Unarchive(downloadLocation, extractDir)
if err != nil {
return "", err
}
binaryPath, err := findBinaryPath(extractDir, cfg.Repository)
if err != nil {
return "", err
}
log.Debugf("Found binary path: %s", binaryPath)
return binaryPath, nil
} else {
binary = downloadLocation
}
return binary, err
}
// Combine amd64 and arm64 binary into a macOS universal binary
func (cfg *Configuration) Combine(amd64Path string, arm64Path string, amd64AssetName string) (string, error) {
currentWorkingDir, err := os.Getwd()
if err != nil {
return "", err
}
dir, err := ioutil.TempDir(currentWorkingDir, FAT_PREFIX)
if err != nil {
return "", err
}
fileName := filepath.Base(amd64Path)
target := filepath.Join(dir, fileName)
universalAssetName := generateUniversalAssetName(amd64AssetName, cfg.UniversalIdentifer)
err = MakeFatBinary(amd64Path, arm64Path, target)
if err != nil {
return "", err
}
var universalAssetPath string
if cfg.Compressed {
archiver.Archive([]string{target}, universalAssetName)
universalAssetPath = filepath.Join(currentWorkingDir, universalAssetName)
} else {
renamedPath := filepath.Join(dir, universalAssetName)
err := os.Rename(target, renamedPath)
if err != nil {
return "", err
}
}
return universalAssetPath, nil
}
// Upload the univeral binary to Github as a release asset
func (cfg *Configuration) UploadAsset(release *github.RepositoryRelease, assetPath string) error {
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: cfg.GithubToken,
})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
assetNameToUpload := filepath.Base(assetPath)
uploadOptions := github.UploadOptions{
Name: assetNameToUpload,
}
file, err := os.Open(assetPath)
if err != nil {
return err
}
existingAssets := release.Assets
for _, asset := range existingAssets {
// If the asset to upload is already present in the release, delete the asset so it can be reuploaded
if asset.GetName() == assetNameToUpload {
if cfg.Overwrite {
resp, err := client.Repositories.DeleteReleaseAsset(ctx, cfg.Owner, cfg.Repository, asset.GetID())
if err != nil {
return err
}
log.Info("Deleted pre-existing asset with same name")
log.Debugf("Asset ID: %d", asset.GetID())
log.Debugf("Asset Name: %s", asset.GetName())
log.Debugf("Asset URL: %s", asset.GetURL())
log.Debugf("Response Status: %s", resp.Status)
log.Debugf("Response Status Code: %d", resp.StatusCode)
} else {
return errors.New("found existing asset with same name")
}
}
}
uploadedAsset, resp, err := client.Repositories.UploadReleaseAsset(ctx, cfg.Owner, cfg.Repository, *release.ID, &uploadOptions, file)
if err != nil {
log.Error("Could not upload asset!")
return err
}
defer file.Close()
log.Debug("Successfully uploaded asset")
log.Debugf("Asset ID: %d", uploadedAsset.GetID())
log.Debugf("Asset Name: %s", uploadedAsset.GetName())
log.Debugf("Asset URL: %s", uploadedAsset.GetURL())
log.Debugf("Response Status: %s", resp.Status)
log.Debugf("Response Status Code: %d", resp.StatusCode)
return nil
}