forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.jl
56 lines (49 loc) · 1.56 KB
/
dir.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
module Dir
import ..Pkg: DEFAULT_META, META_BRANCH
import ..Git
const DIR_NAME = ".julia"
_pkgroot() = abspath(get(ENV,"JULIA_PKGDIR",joinpath(homedir(),DIR_NAME)))
isversioned(p::AbstractString) = ((x,y) = (VERSION.major, VERSION.minor); basename(p) == "v$x.$y")
function path()
b = _pkgroot()
x, y = VERSION.major, VERSION.minor
d = joinpath(b,"v$x.$y")
if isdir(d) || !isdir(b) || !isdir(joinpath(b, "METADATA"))
return d
end
return b
end
path(pkg::AbstractString...) = normpath(path(),pkg...)
function cd(f::Function, args...; kws...)
dir = path()
if !isdir(dir)
!haskey(ENV,"JULIA_PKGDIR") ? init() :
error("package directory $dir doesn't exist; run Pkg.init() to create it.")
end
Base.cd(()->f(args...; kws...), dir)
end
function init(meta::AbstractString=DEFAULT_META, branch::AbstractString=META_BRANCH)
if Git.version() < v"1.7.3"
warn("Pkg only works with git versions greater than v1.7.3")
end
dir = path()
info("Initializing package repository $dir")
if isdir(joinpath(dir,"METADATA"))
info("Package directory $dir is already initialized.")
Git.set_remote_url(meta, dir=joinpath(dir,"METADATA"))
return
end
try
mkpath(dir)
Base.cd(dir) do
info("Cloning METADATA from $meta")
run(`git clone -q -b $branch $meta METADATA`)
Git.set_remote_url(meta, dir="METADATA")
run(`touch REQUIRE`)
end
catch e
rm(dir, recursive=true)
rethrow(e)
end
end
end # module