forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deno_dir.go
111 lines (98 loc) · 2.6 KB
/
deno_dir.go
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
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
package deno
import (
"crypto/md5"
"encoding/hex"
"flag"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"runtime"
"strings"
)
var flagCacheDir = flag.String("cachedir", "",
"Where to cache compilation artifacts. Default: ~/.deno")
var DenoDir string
var CacheDir string
var SrcDir string
func SourceCodeHash(filename string, sourceCodeBuf []byte) string {
h := md5.New()
h.Write([]byte(filename))
h.Write(sourceCodeBuf)
return hex.EncodeToString(h.Sum(nil))
}
func CacheFileName(filename string, sourceCodeBuf []byte) string {
cacheKey := SourceCodeHash(filename, sourceCodeBuf)
return path.Join(CacheDir, cacheKey+".js")
}
// Fetches a remoteUrl but also caches it to the localFilename.
func FetchRemoteSource(remoteUrl string, localFilename string) ([]byte, error) {
logDebug("FetchRemoteSource %s %s", remoteUrl, localFilename)
assert(strings.HasPrefix(localFilename, SrcDir),
"Expected filename to start with SrcDir: "+localFilename)
var sourceReader io.Reader
file, err := os.Open(localFilename)
if *flagReload || os.IsNotExist(err) {
// Fetch from HTTP.
println("Downloading", remoteUrl)
res, err := http.Get(remoteUrl)
if err != nil {
return nil, err
}
defer res.Body.Close()
err = os.MkdirAll(path.Dir(localFilename), 0700)
if err != nil {
return nil, err
}
// Write to local file. Need to reopen it for writing.
file, err = os.OpenFile(localFilename, os.O_RDWR|os.O_CREATE, 0700)
if err != nil {
return nil, err
}
sourceReader = io.TeeReader(res.Body, file) // Fancy!
} else if err != nil {
return nil, err
} else {
sourceReader = file
}
defer file.Close()
return ioutil.ReadAll(sourceReader)
}
func LoadOutputCodeCache(filename string, sourceCodeBuf []byte) (
outputCode string, err error) {
cacheFn := CacheFileName(filename, sourceCodeBuf)
outputCodeBuf, err := ioutil.ReadFile(cacheFn)
if os.IsNotExist(err) {
// Ignore error if we can't find the cache file.
err = nil
} else if err == nil {
outputCode = string(outputCodeBuf)
}
return outputCode, err
}
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 createDirs() {
if *flagCacheDir == "" {
DenoDir = path.Join(UserHomeDir(), ".deno")
} else {
DenoDir = *flagCacheDir
}
CacheDir = path.Join(DenoDir, "cache")
err := os.MkdirAll(CacheDir, 0700)
check(err)
SrcDir = path.Join(DenoDir, "src")
err = os.MkdirAll(SrcDir, 0700)
check(err)
}