-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
87 lines (73 loc) · 2.16 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"go.encore.dev/go/builder"
)
func main() {
goos := flag.String("goos", "", "GOOS")
goarch := flag.String("goarch", "", "GOARCH")
dst := flag.String("dst", "dist", "build destination")
readVersion := flag.Bool("read-built-version", false, "If set we'll simply parse go/VERSION.cache and return the Go verison")
flag.Parse()
if *readVersion {
readBuiltVersion()
return
}
if *goos == "" || *goarch == "" || *dst == "" {
log.Fatalf("missing -dst %q, -goos %q, or -goarch %q", *dst, *goos, *goarch)
}
root, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
if err := builder.BuildEncoreGo(*goos, *goarch, root, *dst); err != nil {
log.Fatalln(err)
}
}
func readBuiltVersion() {
str := ""
if isfile("go/VERSION") {
// If we're building from a release branch, we use this as the base
str = readfile("go/VERSION")
// Then we repeat the replace we do within the src/cmd/dist/build.go
str = strings.Replace(str, "go1.", "encore-go1.", 1)
} else {
// Otherwise we read the cache file which would be created by the build process
// if there was no VERSION file present
str = readfile("go/VERSION.cache")
}
// With our patches there must always be an `encore-go1.xx` version in this string
// (there may be other bits, like "devel" or "beta" which we don't care about)
re, err := regexp.Compile("(encore-go[^ ]+)")
if err != nil {
log.Fatalf("Unable to compile regex: %+v", err)
}
version := re.FindString(str)
if version == "" {
log.Fatalf("Unable to extract version, read: %s", str)
}
// In Go 1.21 the time was added as the second line of the VERSION file
// so we only want the first line
version, _, _ = strings.Cut(version, "\n")
version = strings.TrimSpace(version)
fmt.Println(version)
}
// isfile reports whether p names an existing file.
func isfile(p string) bool {
fi, err := os.Stat(p)
return err == nil && fi.Mode().IsRegular()
}
// readfile returns the content of the named file.
func readfile(file string) string {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf("%v", err)
}
return strings.TrimRight(string(data), " \t\r\n")
}