Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Commit

Permalink
Support editing sail Dockerfile inside code-server
Browse files Browse the repository at this point in the history
Closes #98

Only thing left to do is switch to using a output pane instead of
a terminal and blocking the save on the dockerfile while the reload
is in progress.
  • Loading branch information
nhooyr committed May 2, 2019
1 parent 899005c commit a822554
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
4 changes: 1 addition & 3 deletions proxycmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ func (c *proxycmd) proxy(cntName string) (addr string, err error) {
go func() {
m := http.NewServeMux()
m.HandleFunc("/sail.js", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
console.log("injected")
`))
w.Write([]byte(sailJS))
})
m.HandleFunc("/sail/api/v1/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok\n"))
Expand Down
43 changes: 43 additions & 0 deletions sail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
let reloadInProgress = false
let tty
window.addEventListener("ide-ready", (ev) => {
window.ide.workbench.onFileSaved((ev) => {
if (!ev.endsWith(".sail/Dockerfile")) {
return
}

if (reloadInProgress) {
alert("reload is still in progress")
return
}
reloadInProgress = true

const srv = window.ide.workbench.terminalService

if (tty == null) {
tty = srv.createTerminal({
name: "sail",
isRendererOnly: true,
}, false)
} else {
tty.clear()
}
let oldTTY = srv.getActiveInstance()
srv.setActiveInstance(tty)

const ws = new WebSocket("ws://" + location.host + "/sail/api/v1/reload")
ws.onmessage = (ev) => {
const msg = JSON.parse(ev.data)
const out = atob(msg.v).replace(/\n/g, "\n\r")
tty.write(out)
}
ws.onclose = (ev) => {
if (ev.code === 1000) {
srv.setActiveInstance(oldTTY)
} else {
alert("reload failed; please see logs in sail terminal")
}
reloadInProgress = false
}
})
})
4 changes: 4 additions & 0 deletions sail.js.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

//go:generate go run sail.js_gen.go
const sailJS = "let reloadInProgress = false\nlet tty\nwindow.addEventListener(\"ide-ready\", (ev) => {\n window.ide.workbench.onFileSaved((ev) => {\n if (!ev.endsWith(\".sail/Dockerfile\")) {\n return\n }\n\n if (reloadInProgress) {\n alert(\"reload is still in progress\")\n return\n }\n reloadInProgress = true\n\n const srv = window.ide.workbench.terminalService\n\n if (tty == null) {\n tty = srv.createTerminal({\n name: \"sail\",\n isRendererOnly: true,\n }, false)\n } else {\n tty.clear()\n }\n let oldTTY = srv.getActiveInstance()\n srv.setActiveInstance(tty)\n\n const ws = new WebSocket(\"ws://\" + location.host + \"/sail/api/v1/reload\")\n ws.onmessage = (ev) => {\n const msg = JSON.parse(ev.data)\n const out = atob(msg.v).replace(/\\n/g, \"\\n\\r\")\n tty.write(out)\n }\n ws.onclose = (ev) => {\n if (ev.code == 1000) {\n srv.setActiveInstance(oldTTY)\n alert(\"success\")\n } else {\n alert(\"reload failed; please see logs in sail terminal\")\n }\n reloadInProgress = false\n }\n })\n})\n"
31 changes: 31 additions & 0 deletions sail.js_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// +build ignore

package main

import (
"fmt"
"io/ioutil"
"log"
)

func main() {
sailJS, err := ioutil.ReadFile("sail.js")
if err != nil {
log.Fatalf("failed to read sail.js: %v", err)
}

err = ioutil.WriteFile("sail.js.go", genFile(sailJS), 0644)
if err != nil {
log.Fatalf("failed to write sail.js: %v", err)
}
}

func genFile(sailJS []byte) []byte {
tmpl := `package main
//go:generate go run sail.js_gen.go
const sailJS = %q
`

return []byte(fmt.Sprintf(tmpl, sailJS))
}

0 comments on commit a822554

Please sign in to comment.