Repository navigation
Expand file tree
/
Copy pathtinygo.go
More file actions
66 lines (53 loc) · 1.71 KB
/
Copy pathtinygo.go
File metadata and controls
66 lines (53 loc) · 1.71 KB
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
//go:build !wasm
package goflare
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"webtyp.com/tinygo"
)
const (
// TinyGoVersion is the TinyGo version this goflare binary will install.
TinyGoVersion = tinygo.DefaultVersion
// TinyGoBinDirPrefix marks the stdout line carrying the directory.
TinyGoBinDirPrefix = "TINYGO_BINDIR="
// TinyGoVersionPrefix marks the stdout line carrying the version.
TinyGoVersionPrefix = "TINYGO_VERSION="
)
// TinyGo is not used by the edge (JavaScript only). The site build needs it for the Web Workers
// that declare //go:build wasm && tinygo, so CI installs it through `goflare tinygo`.
// TinyGoBinDir installs TinyGo when missing and returns the directory holding
// the binary, along with the version it reports.
func TinyGoBinDir() (dir, version string, err error) {
installedPath, err := tinygo.EnsureInstalled()
if err != nil {
return "", "", fmt.Errorf("tinygo setup: %w", err)
}
binDir := filepath.Dir(installedPath)
cmd := exec.Command(installedPath, "version")
out, err := cmd.Output()
if err != nil {
return "", "", fmt.Errorf("tinygo version check: %w", err)
}
return binDir, string(out), nil
}
// EnsureTinyGo installs TinyGo if absent and guarantees its bin dir is in PATH
// before any compilation attempt. Safe to call multiple times (idempotent).
func EnsureTinyGo(out io.Writer) error {
binDir, _, err := TinyGoBinDir()
if err != nil {
return err
}
if _, lookErr := exec.LookPath("tinygo"); lookErr != nil {
current := os.Getenv("PATH")
if current != "" {
os.Setenv("PATH", current+string(os.PathListSeparator)+binDir)
} else {
os.Setenv("PATH", binDir)
}
fmt.Fprintln(out, "TinyGo ready:", filepath.Join(binDir, "tinygo"))
}
return nil
}