-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.jl
167 lines (146 loc) · 5.29 KB
/
main.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
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
@require "github.com/jkroso/parse-json" parse => parseJSON
@require "github.com/coiljl/URI" encode encode_component
@require "github.com/JuliaWeb/Requests.jl" => Requests
# Some packages don't declare all their dependencies
const known_fuckups = Dict(
"pg-cursor" => Dict("pg" => "latest"),
"pg-then" => Dict("pg" => "latest"))
function install(dir::AbstractString, progress, spec_cache=Dict(), uri_cache=Dict(); development=false)
haskey(uri_cache, dir) && return nothing
uri_cache[dir] = dir
progress.pending += 1
json = open(parseJSON, joinpath(dir, "package.json"))
dependencies = merge(get(json, "dependencies", Dict()),
get(json, "peerDependencies", Dict()))
development && merge!(dependencies, get(json, "devDependencies", Dict()))
haskey(known_fuckups, json["name"]) && merge!(dependencies, known_fuckups[json["name"]])
@sync for spec in dependencies
@async begin
uri = need(resolve_spec(spec, dir, spec_cache))
path = need(isabspath(uri) ? uri : download(uri, uri_cache))
install(path, progress, spec_cache, uri_cache)
linkpackage(dir, path)
end
end
# unofficially its possible for modules to depend on themselves.
# Some numskulls take advantage of this (babel-runtime) so we need
# to replicate that feature
link = joinpath(dir, "node_modules", json["name"])
mkpath(dirname(link))
islink(link) || symlink(dir, link)
run_install_script(json, dir)
progress.pending -= 1
end
function resolve_spec(spec, dir, cache)
haskey(cache, spec) && return cache[spec]
cache[spec] = @schedule(ismatch(r"^file:", spec[2])
? joinpath(dir, spec[2][6:end])
: toURL(spec...))
end
need(value) = value
need(t::Task) = wait(t)
# spec at docs.npmjs.com/misc/scripts
# TODO: support config
function run_install_script(json, dir)
scripts = get(json, "scripts", nothing)
isa(scripts, Associative) || return nothing
hook = if haskey(scripts, "install") scripts["install"]
elseif haskey(scripts, "postinstall") scripts["postinstall"]
elseif ispath(joinpath(dir, "binding.gyp")) "node-gyp rebuild"
else "" end
isempty(hook) && return nothing
PATH = ENV["PATH"] * ":" * joinpath(dir, "node_modules", ".bin")
env = Dict("PATH" => PATH,
"npm_package_name" => json["name"],
"npm_package_version" => json["version"],
"npm_lifecycle_event" => "install")
cmd = setenv(@eval(@cmd $hook), merge(ENV, env))
cd(()-> run(pipeline(cmd, stderr=DevNull, stdout=DevNull)), dir)
end
function linkpackage(from, to)
json = open(parseJSON, joinpath(to, "package.json"))
path = joinpath(from, "node_modules", json["name"])
mkpath(dirname(path))
islink(path) && rm(path)
ispath(path) || symlink(to, path)
# add its binaries to $from/node_modules/.bin
for (name, script) in getbin(json, to)
name = joinpath(from, "node_modules", ".bin", name)
islink(name) && continue
mkpath(dirname(name))
symlink(joinpath(to, script), name)
end
end
# spec at docs.npmjs.com/files/package.json#bin
function getbin(json, dir)
if haskey(json, "bin")
(isa(json["bin"], AbstractString)
? Dict(json["name"] => json["bin"])
: json["bin"])
else
directories = get(json, "directories", Dict())
name = get(directories, "bin", nothing)
name == nothing && return Dict()
foldl(Dict(), readdir(joinpath(dir, name))) do dict,filename
push!(dict, filename => joinpath(dir, name, filename))
end
end
end
function download(url, cache)
haskey(cache, url) && return cache[url]
cache[url] = @schedule begin
path = joinpath(homedir(), ".packin", replace(url, r"^.*://", ""))
if !ispath(path)
if ismatch(r"^git(?:\+ssh|https?)?://", url)
m = match(r"#([^/]+)$", url)
branch = "master"
if m != nothing
branch = m[1]
url = url[1:m.offset - 1]
end
run(pipeline(`git clone $url $path --depth 1 --branch $branch`, stderr=DevNull))
else
data = GET(url)
mkpath(path)
stdin, proc = open(`tar --strip-components 1 -xmpf - -C $path`, "w")
write(stdin, data)
close(stdin)
wait(proc)
end
end
path
end
end
const gh_shorthand = r"^([^/]+/[^#/]+)(?:#(.+))?$"
const npm_scoped = r"^@([^/]+)/(.+)$"
const full_url = r"^\w+://"
function toURL(name, spec)
if spec == "latest" || spec == "" spec = "*" end
if ismatch(full_url, spec)
return spec
end
if ismatch(gh_shorthand, spec)
name,tag = match(gh_shorthand, spec).captures
if tag == nothing; tag = latest_gh_commit(name) end
return "https://codeload.github.com/$name/legacy.tar.gz/$tag"
end
if ismatch(npm_scoped, name)
name = replace(name, r"/", "%2f")
spec = encode_component(spec)
end
parseJSON(GET("http://registry.npmjs.com/$name/$spec"))["dist"]["tarball"]
end
const headers = Dict(
"Authorization" => "Basic $(base64encode(ENV["GITHUB_USERNAME"] * ":" * ENV["GITHUB_PASSWORD"]))")
function latest_gh_commit(name)
head = GET("https://api.github.com/repos/$name/git/refs/heads/master", meta=headers)
parseJSON(head)["object"]["sha"]
end
function GET(url; meta=Dict())
response = Requests.get(encode(url); headers=meta)
if response.status >= 400
error("status $(response.status) for $(encode(url))")
else
response.data
end
end