-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsx.risor
121 lines (100 loc) · 2.75 KB
/
rsx.risor
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
import os
import regexp
import exec
func shell(command) {
exec("bash", ["-c", command])
}
func log(msg) {
print(msg)
}
func debug(msg) {
if os.getenv("RSX_DEBUG") == "1" {
print(msg)
}
}
func env(key, def) {
v := os.getenv(key)
if v == "" {
v = def
}
return v
}
func is_file(target) {
try(func() { f := os.stat(target); !f.is_dir }, false)
}
func is_dir(target) {
try(func() { f := os.stat(target); f.is_dir }, false)
}
func lines(content) {
if is_file(content) {
return string(os.read_file(content)).split("\n")
}
return content.split("\n")
}
// Replace an occurrence of a regular expression in a file or string.
func replace_in(target, rgxp, repl) {
l := []
r := regexp.compile(rgxp)
lines(target).each(func(line){
if r.match(line) {
l.append(r.replace_all(line, repl))
} else {
l.append(line)
}
})
os.write_file(target, strings.join(l, "\n"))
}
func grep(target, regex) {
l := []
lines(target).each(func(line){
if regexp.match(regex, line) {
l.append(line)
}
})
return l
}
// Load a module from a github repository
//
// opts: Options available:
// - force: force module download if present (false, bool)
// - branch: repository branch to use (main, string)
// - install_path: path to install the module ($HOME/.local/share/risor/modules, string)
__module_dir := filepath.join(getenv("HOME"), ".local/share/risor/modules")
func load(path, opts) {
install_path := opts.get("install_path", __module_dir)
os.mkdir_all(install_path)
debug("install path: "+install_path)
branch := opts.get("branch", "main")
debug("branch: "+branch)
force := opts.get("force", false)
if !regexp.match(`gh:(\w|-|_)+\/(\w|-|_)+\/.*`, path) {
error("invalid path")
}
nwo := strings.split(path, "/")[0:2]
nwo = strings.join(nwo, "/").replace_all("gh:", "")
debug("nwo: "+nwo)
lib_path := strings.split(path, "/")[2:]
lib_path = strings.join(lib_path, "/")
lib_path = strings.trim_suffix(lib_path, ".risor")
lib_path = lib_path + ".risor"
debug("library path: "+lib_path)
dest := filepath.join(install_path, filepath.base(path))
dest = strings.trim_suffix(dest, ".risor")
dest = dest + ".risor"
if is_file(dest) && !force {
debug(sprintf("%s already exists, ignoring", filepath.base(dest)))
return false
}
if is_file(dest) && force {
debug(sprintf("library %s, force requested", filepath.base(dest)))
}
resp := fetch('https://raw.githubusercontent.com/{nwo}/refs/heads/{branch}/{lib_path}')
if resp.status_code != 200 {
error('failed to fetch the library from {path}, status code {resp.status_code}')
}
f := os.create(dest)
defer f.close()
f.write(resp.text())
debug(sprintf("library %s installed", filepath.base(dest)))
return true
}