This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
safepkg.jl
78 lines (63 loc) · 1.57 KB
/
safepkg.jl
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
module SafePkg
include("packages.jl")
using Pkg
@enum Mode stable master
global mode = stable
function set_mode(m)
global mode
mode = m
end
localpath(p) = "$(ENV["WORKSPACE"])/$(p).jl"
Master(name) = get(locations, name, PackageSpec(path=localpath(name)))
Stable(name) = get(locations, name, PackageSpec(name=name))
GetPackageSpec(name) = mode == stable ? Stable(name) : Master(name)
global pkglog = ".pkgerrors"
function LogPkgErr(err, name = nothing)
try
msg = replace(replace(replace(err.msg,
r"├|└" => "+"), r"─" => "-"), r"│" => "|")
open(pkglog, "a") do fp
if name !== nothing
write(fp, string("=== failed to add package ", name, "\n"))
end
write(fp, string(msg, "\n"))
end
catch
# ignore IO errors
end
end
function Safe(action; onerror = nothing)
try
action()
catch err
if onerror !== nothing
onerror(err)
end
for (exception, backtrace) in Base.catch_stack()
showerror(stdout, exception, backtrace)
println()
end
end
end
function add_smart(name)
Safe(()->Pkg.add(GetPackageSpec(name));
onerror = err -> LogPkgErr(err, name))
end
function add(pkg)
if startswith(pkg, "https:")
pkg = PackageSpec(url=pkg)
elseif startswith(pkg, "/")
pkg = PackageSpec(path=pkg)
end
Safe(()->Pkg.add(pkg))
end
function build(name)
Safe(()->Pkg.build(name))
end
function precompile()
Safe(()->Pkg.precompile(); onerror = err ->LogPkgErr(err))
end
function update()
Safe(()->Pkg.update())
end
end