Skip to content

Commit 8b78006

Browse files
committed
Rewrite GopherJS version upgrade procedure and precompilation script.
The standard library precompilation is now rewritten in Go and uses build session API. This has several advantages over the old approach: - We no longer depend on path conventions for the library install path that the gopherjs tool uses. - Precompiled libraries will be generated with exactly the same version of GopherJS that the playground imports. - Less bash 🎉
1 parent af6421d commit 8b78006

File tree

6 files changed

+161
-61
lines changed

6 files changed

+161
-61
lines changed

playground/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ gopherjs serve
1313
```
1414

1515
Then open <http://localhost:8080/github.com/gopherjs/gopherjs.github.io/playground>.
16+
17+
## Upgrading GopherJS release
18+
19+
```shell
20+
VERSION="$(go list -m -versions -f "{{ range .Versions }}{{ println . }}{{ end }}" github.com/gopherjs/gopherjs | tail -n 1)"
21+
echo "$VERSION"
22+
go get -v "github.com/gopherjs/gopherjs@$VERSION"
23+
go mod tidy
24+
```

playground/gen.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
//go:generate ./update.sh
1+
//go:generate go run ./internal/cmd/precompile
2+
//go:generate go install github.com/gopherjs/gopherjs
3+
//go:generate gopherjs build -m .
24

35
package main

playground/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ go 1.17
55
require (
66
github.com/gopherjs/gopherjs v1.18.0-beta1
77
github.com/neelance/go-angularjs v0.0.0-20170205214111-8c6312cca6e2
8+
github.com/sirupsen/logrus v1.8.1
89
golang.org/x/tools v0.1.12
910
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2
1011
honnef.co/go/js/xhr v0.0.0-20150307031022-00e3346113ae
1112
)
1213

1314
require (
15+
github.com/fsnotify/fsnotify v1.5.1 // indirect
1416
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86 // indirect
17+
github.com/neelance/sourcemap v0.0.0-20200213170602-2833bce08e4c // indirect
18+
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
19+
github.com/stretchr/testify v1.7.0 // indirect
20+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
21+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
1522
honnef.co/go/js/util v0.0.0-20150216223935-96b8dd9d1621 // indirect
1623
)

playground/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
5757
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
5858
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
5959
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
60+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6061
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6162
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
6263
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -67,6 +68,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.m
6768
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
6869
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
6970
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
71+
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
7072
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
7173
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
7274
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
@@ -193,11 +195,13 @@ github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86 h1:D6paGObi5Wu
193195
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo=
194196
github.com/neelance/go-angularjs v0.0.0-20170205214111-8c6312cca6e2 h1:BjjdRhf8xbuU4aYCpJUyNxZqSc4Pfdy8PFUKHEz+eaY=
195197
github.com/neelance/go-angularjs v0.0.0-20170205214111-8c6312cca6e2/go.mod h1:LverhO5Aaa+/jkOEMBe5N1yN51wJyTJSSxbtoN7l60I=
198+
github.com/neelance/sourcemap v0.0.0-20200213170602-2833bce08e4c h1:bY6ktFuJkt+ZXkX0RChQch2FtHpWQLVS8Qo1YasiIVk=
196199
github.com/neelance/sourcemap v0.0.0-20200213170602-2833bce08e4c/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM=
197200
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
198201
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
199202
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
200203
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
204+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
201205
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
202206
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
203207
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
@@ -206,10 +210,13 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
206210
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
207211
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
208212
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
213+
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636 h1:aSISeOcal5irEhJd1M+IrApc0PdcN7e7Aj4yuEnOrfQ=
209214
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
215+
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk=
210216
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
211217
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
212218
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
219+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
213220
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
214221
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
215222
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
@@ -225,6 +232,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
225232
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
226233
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
227234
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
235+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
228236
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
229237
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
230238
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -292,6 +300,7 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
292300
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
293301
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
294302
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
303+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
295304
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
296305
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
297306
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -592,6 +601,7 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
592601
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
593602
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
594603
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
604+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
595605
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
596606
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 h1:oomkgU6VaQDsV6qZby2uz1Lap0eXmku8+2em3A/l700=
597607
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2/go.mod h1:sUMDUKNB2ZcVjt92UnLy3cdGs+wDAcrPdV3JP6sVgA4=
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Program precompile updates pre-built standard library packages for the
2+
// playground.
3+
//
4+
// This script performs the following sequence of steps:
5+
//
6+
// - Enumerate all standard packages that should be available in the playground.
7+
// - Precompile them, including transitive dependencies.
8+
// - Delete all old precompiled archive.
9+
// - Write all new precompiled archive in their place.
10+
//
11+
// This will use the same GopherJS version as specified in the playground gm.mod
12+
// to ensure consistency. The script uses GopherJS compiler API directly, so
13+
// it doesn't require the GopherJS tool to be installed.
14+
package main
15+
16+
import (
17+
"flag"
18+
"fmt"
19+
gobuild "go/build"
20+
"os"
21+
"path/filepath"
22+
"strings"
23+
24+
"github.com/gopherjs/gopherjs/build"
25+
"github.com/gopherjs/gopherjs/compiler"
26+
log "github.com/sirupsen/logrus"
27+
)
28+
29+
type logLevelFlag struct{ log.Level }
30+
31+
func (l *logLevelFlag) Set(raw string) error { return l.UnmarshalText([]byte(raw)) }
32+
33+
var (
34+
logLevel logLevelFlag = logLevelFlag{Level: log.ErrorLevel}
35+
)
36+
37+
func init() {
38+
flag.Var(&logLevel, "log_level", "Default logging level.")
39+
}
40+
41+
func run() error {
42+
s, err := build.NewSession(&build.Options{
43+
Verbose: true,
44+
Minify: true,
45+
NoCache: true,
46+
})
47+
if err != nil {
48+
return fmt.Errorf("failed to create a build session: %w", err)
49+
}
50+
51+
packages, err := s.XContext().Match([]string{"std"})
52+
if err != nil {
53+
return fmt.Errorf("failed to enumerate standard library packages")
54+
}
55+
packages = importable(packages)
56+
packages = append(packages, "github.com/gopherjs/gopherjs/js", "github.com/gopherjs/gopherjs/nosync")
57+
58+
for _, pkg := range packages {
59+
_, err := s.BuildImportPath(pkg)
60+
if err != nil {
61+
return fmt.Errorf("failed to precompile package %q: %w", pkg, err)
62+
}
63+
}
64+
65+
target, err := targetDir(s)
66+
if err := os.RemoveAll(target); err != nil {
67+
return fmt.Errorf("failed to clean out old precompiled archives: %w", err)
68+
}
69+
70+
for _, archive := range s.UpToDateArchives {
71+
if err := writeArchive(target, archive); err != nil {
72+
return fmt.Errorf("failed to write package %q archive: %w", archive.ImportPath, err)
73+
}
74+
}
75+
76+
return nil
77+
}
78+
79+
func writeArchive(target string, archive *compiler.Archive) error {
80+
path := filepath.Join(target, filepath.FromSlash(archive.ImportPath)+".a.js")
81+
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
82+
return fmt.Errorf("failed to create precompiled package directory %q: %w", filepath.Dir(path), err)
83+
}
84+
f, err := os.Create(path)
85+
if err != nil {
86+
return fmt.Errorf("failed to create precompiled archive %q: %w", path, err)
87+
}
88+
defer f.Close()
89+
90+
return compiler.WriteArchive(archive, f)
91+
}
92+
93+
// targetDir returns path to the directory where precompiled packages must be
94+
// stored.
95+
func targetDir(s *build.Session) (string, error) {
96+
pkg, err := s.XContext().Import("github.com/gopherjs/gopherjs.github.io/playground", "", gobuild.FindOnly)
97+
if err != nil {
98+
return "", fmt.Errorf("failed to find playground package directory: %w", err)
99+
}
100+
target := filepath.Join(pkg.Dir, "pkg")
101+
if _, err := os.Stat(target); os.IsNotExist(err) {
102+
return "", fmt.Errorf("target directory %q not found", target)
103+
}
104+
return target, nil
105+
}
106+
107+
// importable excludes packages that are incompatible with GopherJS or can't be
108+
// directly imported by the user code. The remaining packages will be used as a
109+
// starting points for precompilation.
110+
func importable(all []string) []string {
111+
result := []string{}
112+
for _, pkg := range all {
113+
switch {
114+
case strings.HasPrefix(pkg, "vendor/"),
115+
strings.Contains(pkg, "internal"),
116+
strings.Contains(pkg, "pprof"),
117+
strings.Contains(pkg, "plugin"):
118+
continue
119+
default:
120+
result = append(result, pkg)
121+
}
122+
}
123+
return result
124+
}
125+
126+
func main() {
127+
flag.Parse()
128+
log.SetLevel(logLevel.Level)
129+
if err := run(); err != nil {
130+
log.Fatalf("Precompilation failed: %v", err)
131+
}
132+
}

playground/update.sh

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)