-
Notifications
You must be signed in to change notification settings - Fork 11
/
gracepm.grace
206 lines (193 loc) · 5.14 KB
/
gracepm.grace
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import "io" as io
import "sys" as sys
import "curl" as curl
def moduleLocations = [
sys.environ.at "HOME" ++ "/.local/lib/grace/modules/",
"/usr/local/lib/grace/modules/",
"/usr/lib/grace/modules/",
"./",
"../minigrace/"
]
method findImports(filename) {
def fp = io.open(filename, "r")
def imports = []
while {!fp.eof} do {
def line = fp.getline
if (line.startsWith "import \"") then {
def start = 9
var end := 10
while {line.at(end) != "\""} do {
end := end + 1
}
imports.push(line.substringFrom(start)to(end - 1))
} else {
if (line.startsWith "#" || line.startsWith "//"
|| line.startsWith "type " || line.startsWith " "
|| line.startsWith "\}")
then {
} else {
if (line == "") then {
} else {
return imports
}
}
}
}
return imports
}
method mkdirp(filepath) {
io.system("mkdir -p \"{filepath}\"")
}
method dirname(filepath) {
var i := filepath.size
while {i > 0} do {
if (filepath.at(i) == "/") then {
return filepath.substringFrom(1)to(i - 1)
}
i := i - 1
}
return filepath
}
method fetchModule(path, dest) {
print "Retrieving module from https://{path}.grace..."
def req = curl.easy
req.url := "https://{path}.grace"
var source := ""
req.onReceive { data ->
source := source ++ data.decode "utf-8"
}
req.followLocation := true
req.perform
if (req.responseCode == 200) then {
mkdirp(dirname("{dest}{path}"))
def fp = io.open("{dest}{path}.grace", "w")
fp.write(source)
fp.close
} else {
io.error.write "Unable to retrieve {path}: response code {req.responseCode}"
}
}
method fetchResource(path, dest) {
print "Retrieving resource from https://{path}..."
def req = curl.easy
req.url := "https://{path}"
var source := false
req.onReceive { data ->
if (false == source) then {
source := data
} else {
source := source ++ data
}
}
req.followLocation := true
req.perform
if (req.responseCode == 200) then {
mkdirp(dirname("{dest}{path}"))
def fp = io.open("{dest}{path}", "w")
fp.writeBinary(source)
fp.close
} else {
io.error.write "Unable to retrieve {path}: response code {req.responseCode}"
}
}
method isResourcePath(path) {
var i := path.size
while {i > 0} do {
if (path.at(i) == ".") then {
return true
}
if (path.at(i) == "/") then {
return false
}
i := i - 1
}
return false
}
method findLocalPath(path) {
def isRP = isResourcePath(path)
for (moduleLocations) do {loc->
if (isRP) then {
if (io.exists(loc ++ path)) then {
return loc ++ path
}
} else {
if (io.exists("{loc}{path}.grace")) then {
return "{loc}{path}.grace"
}
if (io.exists("{loc}{path}.gso")) then {
return "{loc}{path}.gso"
}
}
}
return ""
}
method satisfyImport(path, force) {
if (path == "io") then {
return true
}
if (path == "sys") then {
return true
}
def lp = findLocalPath(path)
if (!force) then {
if (lp != "") then {
return true
}
} else {
if (lp.substringFrom(lp.size - 3)to(lp.size) == ".gso") then {
return true
}
}
if (isResourcePath(path)) then {
fetchResource(path, moduleLocations.at 1)
} else {
fetchModule(path, moduleLocations.at 1)
}
}
method satisfyDependencies(filename, force) {
if (filename.substringFrom(filename.size - 3)to(filename.size) == ".gso")
then {
return true
}
def imports = findImports(filename)
for (imports) do { im ->
satisfyImport(im, force)
if (!isResourcePath(im)) then {
satisfyDependencies(findLocalPath(im), force)
}
}
}
method installCommand(path) {
satisfyImport(path, false)
if (isResourcePath(path)) then {
return true
}
def lp = findLocalPath(path)
if (lp != "") then {
satisfyDependencies(lp, false)
}
}
method updateCommand(path) {
satisfyImport(path, true)
if (isResourcePath(path)) then {
return true
}
def lp = findLocalPath(path)
if (lp != "") then {
satisfyDependencies(lp, true)
}
}
if (sys.argv.size > 1) then {
def command = sys.argv.at(2)
match(command)
case { "satisfy" -> satisfyDependencies(sys.argv.at(3), false) }
case { "install" -> installCommand(sys.argv.at(3)) }
case { "update" -> updateCommand(sys.argv.at(3)) }
case { "fetch" -> satisfyImport(sys.argv.at(3), true) }
case { _ ->
io.error.write "No such command '{command}'."
sys.exit(1)
}
} else {
print "Usage: gracepm [satisfy FILE | install PATH | update PATH]"
}