summaryrefslogtreecommitdiff
path: root/os.go
blob: 9a677ed6ec1a144e5b7526e41d668cd3f9c8b428 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main

import (
	"github.com/golang/protobuf/proto"
	"io/ioutil"
	"net/url"
	"os"
	"path"
	"strings"
)

const assetPrefix string = "/$asset$/"

func InitOS() {
	Sub("os", func(buf []byte) []byte {
		msg := &Msg{}
		check(proto.Unmarshal(buf, msg))
		switch msg.Payload.(type) {
		case *Msg_Exit:
			payload := msg.GetExit()
			os.Exit(int(payload.Code))
		case *Msg_SourceCodeFetch:
			payload := msg.GetSourceCodeFetch()
			return HandleSourceCodeFetch(payload.ModuleSpecifier,
				payload.ContainingFile)
		case *Msg_SourceCodeCache:
			payload := msg.GetSourceCodeCache()
			return HandleSourceCodeCache(payload.Filename, payload.SourceCode,
				payload.OutputCode)
		default:
			panic("[os] Unexpected message " + string(buf))
		}
		return nil
	})
}

func ResolveModule(moduleSpecifier string, containingFile string) (
	moduleName string, filename string, err error) {

	logDebug("ResolveModule %s %s", moduleSpecifier, containingFile)

	moduleUrl, err := url.Parse(moduleSpecifier)
	if err != nil {
		return
	}
	baseUrl, err := url.Parse(containingFile)
	if err != nil {
		return
	}
	resolved := baseUrl.ResolveReference(moduleUrl)
	moduleName = resolved.String()
	if moduleUrl.IsAbs() {
		filename = path.Join(SrcDir, resolved.Host, resolved.Path)
	} else {
		filename = resolved.Path
	}
	return
}

func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out []byte) {
	assert(moduleSpecifier != "", "moduleSpecifier shouldn't be empty")
	res := &Msg{}
	var sourceCodeBuf []byte
	var err error

	defer func() {
		if err != nil {
			res.Error = err.Error()
		}
		out, err = proto.Marshal(res)
		check(err)
	}()

	moduleName, filename, err := ResolveModule(moduleSpecifier, containingFile)
	if err != nil {
		return
	}

	//println("HandleSourceCodeFetch", "moduleSpecifier", moduleSpecifier,
	//		"containingFile", containingFile, "filename", filename)

	if isRemote(moduleName) {
		sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)
	} else if strings.HasPrefix(moduleName, assetPrefix) {
		f := strings.TrimPrefix(moduleName, assetPrefix)
		sourceCodeBuf, err = Asset("dist/" + f)
	} else {
		assert(moduleName == filename,
			"if a module isn't remote, it should have the same filename")
		sourceCodeBuf, err = ioutil.ReadFile(moduleName)
	}
	if err != nil {
		return
	}

	outputCode, err := LoadOutputCodeCache(filename, sourceCodeBuf)
	if err != nil {
		return
	}

	var sourceCode = string(sourceCodeBuf)
	var command = Msg_SOURCE_CODE_FETCH_RES
	res = &Msg{
		Command: command,
		SourceCodeFetchResModuleName: moduleName,
		SourceCodeFetchResFilename:   filename,
		SourceCodeFetchResSourceCode: sourceCode,
		SourceCodeFetchResOutputCode: outputCode,
	}
	return
}

func HandleSourceCodeCache(filename string, sourceCode string,
	outputCode string) []byte {

	fn := CacheFileName(filename, []byte(sourceCode))
	outputCodeBuf := []byte(outputCode)
	err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
	res := &Msg{}
	if err != nil {
		res.Error = err.Error()
	}
	out, err := proto.Marshal(res)
	check(err)
	return out
}