2018-05-13 23:32:01 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-05-14 00:31:48 -04:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-05-13 23:32:01 -04:00
|
|
|
"github.com/ry/v8worker2"
|
2018-05-14 02:50:55 -04:00
|
|
|
"io/ioutil"
|
2018-05-14 00:31:48 -04:00
|
|
|
"os"
|
2018-05-15 04:39:03 -04:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2018-05-13 23:32:01 -04:00
|
|
|
)
|
|
|
|
|
2018-05-15 04:39:03 -04:00
|
|
|
func HandleCompileOutput(source string, filename string) []byte {
|
|
|
|
// println("compile output from golang", filename)
|
|
|
|
// Remove any ".." elements. This prevents hacking by trying to move up.
|
|
|
|
filename, err := filepath.Rel("/", filename)
|
|
|
|
check(err)
|
|
|
|
if strings.Contains(filename, "..") {
|
|
|
|
panic("Assertion error.")
|
|
|
|
}
|
|
|
|
filename = path.Join(CompileDir, filename)
|
|
|
|
err = os.MkdirAll(path.Dir(filename), 0700)
|
|
|
|
check(err)
|
|
|
|
err = ioutil.WriteFile(filename, []byte(source), 0600)
|
|
|
|
check(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-14 02:50:55 -04:00
|
|
|
func ReadFileSync(filename string) []byte {
|
|
|
|
buf, err := ioutil.ReadFile(filename)
|
|
|
|
msg := &Msg{Kind: Msg_DATA_RESPONSE}
|
|
|
|
if err != nil {
|
|
|
|
msg.Error = err.Error()
|
|
|
|
} else {
|
|
|
|
msg.Data = buf
|
|
|
|
}
|
|
|
|
out, err := proto.Marshal(msg)
|
2018-05-15 04:39:03 -04:00
|
|
|
check(err)
|
2018-05-14 02:50:55 -04:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-05-15 04:39:03 -04:00
|
|
|
func UserHomeDir() string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
|
|
|
if home == "" {
|
|
|
|
home = os.Getenv("USERPROFILE")
|
|
|
|
}
|
|
|
|
return home
|
|
|
|
}
|
|
|
|
return os.Getenv("HOME")
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadAsset(w *v8worker2.Worker, path string) {
|
|
|
|
data, err := Asset(path)
|
|
|
|
check(err)
|
|
|
|
err = w.Load(path, string(data))
|
|
|
|
check(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var DenoDir string
|
|
|
|
var CompileDir string
|
|
|
|
var SrcDir string
|
|
|
|
|
|
|
|
func createDirs() {
|
|
|
|
DenoDir = path.Join(UserHomeDir(), ".deno")
|
|
|
|
CompileDir = path.Join(DenoDir, "compile")
|
|
|
|
err := os.MkdirAll(CompileDir, 0700)
|
|
|
|
check(err)
|
|
|
|
SrcDir = path.Join(DenoDir, "src")
|
|
|
|
err = os.MkdirAll(SrcDir, 0700)
|
|
|
|
check(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func check(e error) {
|
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 02:50:55 -04:00
|
|
|
func recv(buf []byte) []byte {
|
|
|
|
msg := &Msg{}
|
|
|
|
err := proto.Unmarshal(buf, msg)
|
2018-05-15 04:39:03 -04:00
|
|
|
check(err)
|
2018-05-14 02:50:55 -04:00
|
|
|
switch msg.Kind {
|
|
|
|
case Msg_READ_FILE_SYNC:
|
|
|
|
return ReadFileSync(msg.Path)
|
2018-05-14 17:27:34 -04:00
|
|
|
case Msg_EXIT:
|
|
|
|
os.Exit(int(msg.Code))
|
2018-05-15 04:39:03 -04:00
|
|
|
case Msg_COMPILE_OUTPUT:
|
|
|
|
payload := msg.GetCompileOutput()
|
|
|
|
return HandleCompileOutput(payload.Source, payload.Filename)
|
2018-05-14 02:50:55 -04:00
|
|
|
default:
|
|
|
|
panic("Unexpected message")
|
|
|
|
}
|
2018-05-14 17:27:34 -04:00
|
|
|
|
|
|
|
return nil
|
2018-05-13 23:32:01 -04:00
|
|
|
}
|
|
|
|
|
2018-05-14 00:31:48 -04:00
|
|
|
func main() {
|
2018-05-14 20:57:49 -04:00
|
|
|
args := v8worker2.SetFlags(os.Args)
|
2018-05-15 04:39:03 -04:00
|
|
|
createDirs()
|
2018-05-13 23:32:01 -04:00
|
|
|
worker := v8worker2.New(recv)
|
2018-05-14 00:31:48 -04:00
|
|
|
loadAsset(worker, "dist/main.js")
|
2018-05-14 13:02:47 -04:00
|
|
|
cwd, err := os.Getwd()
|
2018-05-15 04:39:03 -04:00
|
|
|
check(err)
|
|
|
|
|
2018-05-14 13:02:47 -04:00
|
|
|
out, err := proto.Marshal(&Msg{
|
|
|
|
Kind: Msg_START,
|
2018-05-15 04:39:03 -04:00
|
|
|
Payload: &Msg_Start{
|
|
|
|
Start: &StartMsg{
|
|
|
|
Cwd: cwd,
|
|
|
|
Argv: args,
|
|
|
|
},
|
|
|
|
},
|
2018-05-14 13:02:47 -04:00
|
|
|
})
|
2018-05-15 04:39:03 -04:00
|
|
|
check(err)
|
2018-05-14 00:31:48 -04:00
|
|
|
err = worker.SendBytes(out)
|
|
|
|
if err != nil {
|
2018-05-14 03:12:36 -04:00
|
|
|
os.Stderr.WriteString(err.Error())
|
|
|
|
os.Exit(1)
|
2018-05-14 00:31:48 -04:00
|
|
|
}
|
2018-05-13 23:32:01 -04:00
|
|
|
}
|