Skip to content

Commit

Permalink
Merge 752e5be into 8fcacd3
Browse files Browse the repository at this point in the history
  • Loading branch information
d4l3k committed Sep 21, 2018
2 parents 8fcacd3 + 752e5be commit 76c2ed2
Show file tree
Hide file tree
Showing 11 changed files with 678 additions and 472 deletions.
474 changes: 474 additions & 0 deletions generate/generate.go

Large diffs are not rendered by default.

490 changes: 35 additions & 455 deletions main.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions playground/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ wasm_exec.js
node_modules/
*.wasm
*.gopry
main.go
stdlib.go
bundles/
playground
6 changes: 6 additions & 0 deletions playground/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM scratch
COPY . /

EXPOSE 8080
CMD ["/playground"]

14 changes: 10 additions & 4 deletions playground/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
build: wasm_exec.js
yarn install
go install -v ..
go-pry -generate="main.go" -i="fmt,log,math" -e='log.Println("Hello world!")'
GOOS=js GOARCH=wasm go build -v -ldflags "-s -w" -o main.wasm main.go
go-pry -generate="stdlib.go" -i="$(shell tr '\n' ',' < packages.txt)" -e='log.Println("Hello world!")'
GOOS=js GOARCH=wasm go build -v -ldflags "-s -w" -o stdlib.wasm stdlib.go
mkdir -p bundles
go-pry -generate="bundles/main.go" -i="fmt,log,math" -e='log.Println("Hello world!")'
GOOS=js GOARCH=wasm go build -v -ldflags "-s -w" -o bundles/main.wasm bundles/main.go
go-pry -generate="bundles/stdlib.go" -i="$(shell tr '\n' ',' < packages.txt)" -e='log.Println("Hello world!")'
GOOS=js GOARCH=wasm go build -v -ldflags "-s -w" -o bundles/stdlib.wasm bundles/stdlib.go
CGO_ENABLED=0 go build -ldflags "-s -w" -o playground ./server

.PHONY: run
run: build
./playground

wasm_exec.js: $(shell go env GOROOT)/misc/wasm/wasm_exec.js
cp $< .
Expand Down
21 changes: 16 additions & 5 deletions playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:400,700|Source+Sans+Pro:400,700" rel="stylesheet">
<link rel="stylesheet" href="node_modules/xterm/dist/xterm.css" />
<link rel="stylesheet" href="node_modules/nprogress/nprogress.css"/>

<style>
body {
color: white;
Expand All @@ -17,6 +19,7 @@
}
#terminal {
display: inline-block;
max-width: 100%;
}
pre, h2 {
font-family: 'Source Code Pro';
Expand Down Expand Up @@ -45,14 +48,19 @@ <h1>go-pry: web</h1>
<h2>hello!<b> </b>👋</h2>
<p>This is a version of <a href="https://github.com/d4l3k/go-pry">go-pry</a>
that runs in your browser using WebAssembly! It's missing a few features
like advanced auto complete since the browser doesn't have filesystem
access.</p>
but works pretty well for the most part.</p>

<p>This sometimes takes quite a while to load in Chrome, but it's almost
instant in Firefox.</p>

<p>I'm pretty excited to try using this to demo popular client
libraries.</p>

<h2>go-pry import bundles</h2>
<ul>
<li><a href="/">just fmt,log,math</a></li>
<li>
<a href="/?file=stdlib.wasm">full standard library</a>
<a href="/?file=bundles/stdlib.wasm">full standard library</a>
(~37MB, will take longer to load)
</li>
</ul>
Expand All @@ -61,12 +69,14 @@ <h2>go-pry import bundles</h2>
<p>Created by <a href="https://fn.lc">Tristan Rice</a>.</p>


<script src="node_modules/xterm/dist/xterm.js"></script><Paste>
<script src="node_modules/nprogress/nprogress.js"></script>
<script>NProgress.start()</script>
<script src="node_modules/xterm/dist/xterm.js"></script>
<script src="wasm_exec.js"></script>
<script>

const file = (new URL(document.location)).searchParams.get('file')
const wasm = fetch(file || "main.wasm")
const wasm = fetch(file || "bundles/main.wasm")

document.fonts.ready.then(() => {
const term = new Terminal({
Expand All @@ -88,6 +98,7 @@ <h2>go-pry import bundles</h2>

const go = new Go();
WebAssembly.instantiateStreaming(wasm, go.importObject).then((result) => {
NProgress.done()
go.run(result.instance);
});
})
Expand Down
9 changes: 4 additions & 5 deletions playground/now.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"name": "go-pry",
"type": "static",
"type": "docker",
"alias": "gopry.rice.sh",
"files": [
"index.html",
"node_modules",
"wasm_exec.js",
"main.wasm",
".main.gopry",
"stdlib.wasm",
".stdlib.gopry"
"bundles",
"playground",
"Dockerfile"
],
"public": true
}
Expand Down
1 change: 1 addition & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"main": "index.js",
"license": "MIT",
"dependencies": {
"nprogress": "^0.2.0",
"xterm": "^3.7.0"
}
}
125 changes: 125 additions & 0 deletions playground/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"strings"

"github.com/d4l3k/go-pry/generate"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)

var bind = flag.String("bind", ":8080", "address to bind to")

func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}

func pkgHash(pkgs []string) string {
hash := sha256.Sum256([]byte(strings.Join(pkgs, ",")))
return hex.EncodeToString(hash[:])
}

func normalizePackages(packages string) []string {
var pkgs []string
for _, pkg := range strings.Split(strings.ToLower(packages), ",") {
pkg = strings.TrimSpace(pkg)
if len(pkg) == 0 {
continue
}
pkgs = append(pkgs, pkg)
}
sort.Strings(pkgs)
return pkgs
}

func generateBundle(w http.ResponseWriter, r *http.Request, packages string) (retErr error) {
pkgs := normalizePackages(packages)
hash := pkgHash(pkgs)
path := filepath.Join("bundles", hash+".wasm")
goPath := filepath.Join("bundles", hash+".go")
_, err := os.Stat(path)
if err == nil {
http.ServeFile(w, r, path)
return nil
} else if !os.IsNotExist(err) {
return err
}

dir, err := ioutil.TempDir("", "pry-playground-gopath")
if err != nil {
return err
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
if retErr == nil {
retErr = err
}
}
}()

env := []string{
fmt.Sprintf("GOPATH=%s", dir),
}

g := generate.NewGenerator(false)
g.Build.GOPATH = dir
g.Build.CgoEnabled = false

for _, pkg := range append([]string{"github.com/d4l3k/go-pry/pry"}, pkgs...) {
if err := g.ExecuteGoCmd(r.Context(), []string{
"get",
pkg,
}, env); err != nil {
return errors.Wrapf(err, "error go get %q", pkg)
}
}

if err := g.GenerateFile(pkgs, "", goPath); err != nil {
return errors.Wrap(err, "GenerateFile")
}

if err := g.ExecuteGoCmd(r.Context(), []string{
"build",
"-ldflags",
"-s -w",
"-o",
path,
goPath,
}, append([]string{
"GOOS=js",
"GOARCH=wasm",
}, env...)); err != nil {
return errors.Wrapf(err, "go build")
}

http.ServeFile(w, r, path)
return nil
}

func run() error {
log.SetFlags(log.Flags() | log.Lshortfile)

router := mux.NewRouter()
router.PathPrefix("/wasm/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pkgs := strings.Join(strings.Split(r.URL.Path, "/")[2:], "/")
if err := generateBundle(w, r, pkgs); err != nil {
http.Error(w, fmt.Sprintf("%+v", err), http.StatusInternalServerError)
}
})
router.NotFoundHandler = http.FileServer(http.Dir("."))

log.Printf("Listening %s...", *bind)
return http.ListenAndServe(*bind, router)
}
4 changes: 4 additions & 0 deletions playground/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# yarn lockfile v1


nprogress@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/nprogress/-/nprogress-0.2.0.tgz#cb8f34c53213d895723fcbab907e9422adbcafb1"

xterm@^3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/xterm/-/xterm-3.7.0.tgz#b3eb1335dc04b92abe361711731d3b661e13db17"
2 changes: 1 addition & 1 deletion pry/io_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func readFile(path string) ([]byte, error) {
path = filepath.Base(path)
path = filepath.Join("bundles", filepath.Base(path))

r, w := io.Pipe()
var respCB js.Callback
Expand Down

0 comments on commit 76c2ed2

Please sign in to comment.