Skip to content

Commit c3550e9

Browse files
committed
cmd/present: use embed, drop native client
Now that x/tools assumes a new enough Go release, use //go:embed to avoid having to assume the cmd/present source code is available when running the binary. Leave App Engine deployment alone for now. Also delete Native Client code since Go does not support Native Client anymore. Change-Id: Ib4da006ca6291d2f9d078353f52cb5a4dc62b19d Reviewed-on: https://go-review.googlesource.com/c/tools/+/468355 Reviewed-by: Tim King <[email protected]> Run-TryBot: Russ Cox <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 6baf6e8 commit c3550e9

File tree

3 files changed

+24
-40
lines changed

3 files changed

+24
-40
lines changed

cmd/present/dir.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package main
77
import (
88
"html/template"
99
"io"
10+
"io/fs"
1011
"log"
1112
"net"
1213
"net/http"
@@ -65,29 +66,29 @@ var (
6566
contentTemplate map[string]*template.Template
6667
)
6768

68-
func initTemplates(base string) error {
69+
func initTemplates(fsys fs.FS) error {
6970
// Locate the template file.
70-
actionTmpl := filepath.Join(base, "templates/action.tmpl")
71+
actionTmpl := "templates/action.tmpl"
7172

7273
contentTemplate = make(map[string]*template.Template)
7374

7475
for ext, contentTmpl := range map[string]string{
7576
".slide": "slides.tmpl",
7677
".article": "article.tmpl",
7778
} {
78-
contentTmpl = filepath.Join(base, "templates", contentTmpl)
79+
contentTmpl = "templates/" + contentTmpl
7980

8081
// Read and parse the input.
8182
tmpl := present.Template()
8283
tmpl = tmpl.Funcs(template.FuncMap{"playable": playable})
83-
if _, err := tmpl.ParseFiles(actionTmpl, contentTmpl); err != nil {
84+
if _, err := tmpl.ParseFS(fsys, actionTmpl, contentTmpl); err != nil {
8485
return err
8586
}
8687
contentTemplate[ext] = tmpl
8788
}
8889

8990
var err error
90-
dirListTemplate, err = template.ParseFiles(filepath.Join(base, "templates/dir.tmpl"))
91+
dirListTemplate, err = template.ParseFS(fsys, "templates/dir.tmpl")
9192
return err
9293
}
9394

cmd/present/main.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
package main
66

77
import (
8+
"embed"
89
"flag"
910
"fmt"
10-
"go/build"
11+
"io/fs"
1112
"log"
1213
"net"
1314
"net/http"
@@ -18,17 +19,17 @@ import (
1819
"golang.org/x/tools/present"
1920
)
2021

21-
const basePkg = "golang.org/x/tools/cmd/present"
22-
2322
var (
2423
httpAddr = flag.String("http", "127.0.0.1:3999", "HTTP service address (e.g., '127.0.0.1:3999')")
2524
originHost = flag.String("orighost", "", "host component of web origin URL (e.g., 'localhost')")
2625
basePath = flag.String("base", "", "base path for slide template and static resources")
2726
contentPath = flag.String("content", ".", "base path for presentation content")
2827
usePlayground = flag.Bool("use_playground", false, "run code snippets using play.golang.org; if false, run them locally and deliver results by WebSocket transport")
29-
nativeClient = flag.Bool("nacl", false, "use Native Client environment playground (prevents non-Go code execution) when using local WebSocket transport")
3028
)
3129

30+
//go:embed static templates
31+
var embedFS embed.FS
32+
3233
func main() {
3334
flag.BoolVar(&present.PlayEnabled, "play", true, "enable playground (permit execution of arbitrary user code)")
3435
flag.BoolVar(&present.NotesEnabled, "notes", false, "enable presenter notes (press 'N' from the browser to display them)")
@@ -50,16 +51,11 @@ func main() {
5051
*contentPath = "./content/"
5152
}
5253

53-
if *basePath == "" {
54-
p, err := build.Default.Import(basePkg, "", build.FindOnly)
55-
if err != nil {
56-
fmt.Fprintf(os.Stderr, "Couldn't find gopresent files: %v\n", err)
57-
fmt.Fprintf(os.Stderr, basePathMessage, basePkg)
58-
os.Exit(1)
59-
}
60-
*basePath = p.Dir
54+
var fsys fs.FS = embedFS
55+
if *basePath != "" {
56+
fsys = os.DirFS(*basePath)
6157
}
62-
err := initTemplates(*basePath)
58+
err := initTemplates(fsys)
6359
if err != nil {
6460
log.Fatalf("Failed to parse templates: %v", err)
6561
}
@@ -98,11 +94,11 @@ func main() {
9894
}
9995
}
10096

101-
initPlayground(*basePath, origin)
102-
http.Handle("/static/", http.FileServer(http.Dir(*basePath)))
97+
initPlayground(fsys, origin)
98+
http.Handle("/static/", http.FileServer(http.FS(fsys)))
10399

104100
if !ln.Addr().(*net.TCPAddr).IP.IsLoopback() &&
105-
present.PlayEnabled && !*nativeClient && !*usePlayground {
101+
present.PlayEnabled && !*usePlayground {
106102
log.Print(localhostWarning)
107103
}
108104

cmd/present/play.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ package main
77
import (
88
"bytes"
99
"fmt"
10-
"io/ioutil"
10+
"io/fs"
1111
"net/http"
1212
"net/url"
13-
"path/filepath"
14-
"runtime"
1513
"time"
1614

1715
"golang.org/x/tools/godoc/static"
@@ -31,15 +29,15 @@ var scripts = []string{"jquery.js", "jquery-ui.js", "playground.js", "play.js"}
3129
// playScript registers an HTTP handler at /play.js that serves all the
3230
// scripts specified by the variable above, and appends a line that
3331
// initializes the playground with the specified transport.
34-
func playScript(root, transport string) {
32+
func playScript(fsys fs.FS, transport string) {
3533
modTime := time.Now()
3634
var buf bytes.Buffer
3735
for _, p := range scripts {
3836
if s, ok := static.Files[p]; ok {
3937
buf.WriteString(s)
4038
continue
4139
}
42-
b, err := ioutil.ReadFile(filepath.Join(root, "static", p))
40+
b, err := fs.ReadFile(fsys, "static/"+p)
4341
if err != nil {
4442
panic(err)
4543
}
@@ -53,27 +51,16 @@ func playScript(root, transport string) {
5351
})
5452
}
5553

56-
func initPlayground(basepath string, origin *url.URL) {
54+
func initPlayground(fsys fs.FS, origin *url.URL) {
5755
if !present.PlayEnabled {
5856
return
5957
}
6058
if *usePlayground {
61-
playScript(basepath, "HTTPTransport")
59+
playScript(fsys, "HTTPTransport")
6260
return
6361
}
6462

65-
if *nativeClient {
66-
// When specifying nativeClient, non-Go code cannot be executed
67-
// because the NaCl setup doesn't support doing so.
68-
socket.RunScripts = false
69-
socket.Environ = func() []string {
70-
if runtime.GOARCH == "amd64" {
71-
return environ("GOOS=nacl", "GOARCH=amd64p32")
72-
}
73-
return environ("GOOS=nacl")
74-
}
75-
}
76-
playScript(basepath, "SocketTransport")
63+
playScript(fsys, "SocketTransport")
7764
http.Handle("/socket", socket.NewHandler(origin))
7865
}
7966

0 commit comments

Comments
 (0)