-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internals.jl
160 lines (142 loc) · 5.92 KB
/
internals.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
# Helper functions and macros
module Internals
using ..BaseDirs
using Base.Docs
export @defaccessor, @setxdg, @setxdgs
@static if Sys.isunix()
macro setxdg(envvar::Symbol, default)
quote $(esc(envvar))[] = if haskey(ENV, $("XDG_$envvar")) && !isempty(ENV[$("XDG_$envvar")])
path = ENV[$("XDG_$envvar")]
if endswith(path, first(Base.Filesystem.path_separator))
path[begin:end-1]
else path end
else expanduser($(esc(default))) end
end
end
else
macro setxdg(envvar::Symbol, default)
quote $(esc(envvar))[] = if haskey(ENV, $("XDG_$envvar")) && !isempty(ENV[$("XDG_$envvar")])
path = ENV[$("XDG_$envvar")]
if endswith(path, first(Base.Filesystem.path_separator))
path[begin:end-1]
else path end
else $(esc(default)) end
end
end
end
macro setxdgs(envvar::Symbol, defaults)
quote $(esc(envvar))[] = if haskey(ENV, $("XDG_$envvar")) && !isempty(ENV[$("XDG_$envvar")])
map(split(ENV[$("XDG_$envvar")], ':')) do path
if endswith(path, first(Base.Filesystem.path_separator))
path[begin:end-1]
else path end
end
else $(esc(defaults)) end
end
end
const NEW_BASEDIR_MODE = 0o700
ensurebasedir(path::String) =
isdir(path) || mkpath(path, mode=NEW_BASEDIR_MODE)
const DIRECTORY_SUFFIX_FLAG = '/'
"""
ensurepath(path::String)
Ensure that `path` exists. Should `path` end with `$DIRECTORY_SUFFIX_FLAG` it is
interpreted as a directory. The directory-suffix `$DIRECTORY_SUFFIX_FLAG` is
used on all filesystems for consistency of the API, regardless of the native
path seperator of the host filesystem.
"""
function ensurepath(path::String)
if !ispath(path)
if endswith(path, DIRECTORY_SUFFIX_FLAG)
mkpath(path[begin:prevind(path, end)])
else
mkpath(dirname(path))
touch(path)
end
end
end
"""
ensureexecutable(path::String)
Make `path` executable by everybody who can read it. Returns `path`.
"""
function ensureexecutable(path::String)
if isfile(path)
basemode = filemode(path)
uread = basemode & 0o400 > 0
gread = basemode & 0o040 > 0
oread = basemode & 0o004 > 0
xmask = 0o100 * uread + 0o010 * gread + 0o001 * oread
chmod(path, basemode | xmask)
end
path
end
function resolvedirpath(basedir::String, pathcomponents::Union{Tuple, AbstractVector}; create::Bool=false)
create && ensurebasedir(basedir)
if isempty(pathcomponents)
basedir
else
fullpath = joinpath(basedir, pathcomponents...)
create && ensurepath(fullpath)
fullpath
end
end
function resolvedirpaths(basedirs::Vector{String}, pathcomponents::Union{Tuple, AbstractVector}; create::Bool=false, existent::Bool=false)
allpaths = resolvedirpath.(basedirs, Ref(pathcomponents); create)
if existent
filter(ispath, allpaths)
else
allpaths
end
end
macro defaccessor(fnname::Symbol, var::Union{Symbol, Expr})
dirvar = if var isa Symbol
Expr(:ref, Expr(:., :BaseDirs, QuoteNode(var)))
else esc(var) end
vecfns = (:vec, :vcat, :filter, :map, :push!, :pushfirst!) # a few that come to mind
resolver = if (var isa Symbol && getfield(BaseDirs, var) isa Ref{Vector{String}}) ||
(var isa Expr && (var.head == :vect ||
(var.head == :call && var.args[1] in vecfns)))
:resolvedirpaths
else
:resolvedirpath
end
quote
$(esc(fnname))(pathcomponents...; kwargs...) =
$resolver($dirvar, pathcomponents; kwargs...)
$(esc(fnname))(project::BaseDirs.Project, pathcomponents...; kwargs...) =
$(esc(fnname))(BaseDirs.projectpath(project, $dirvar), pathcomponents...; kwargs...)
end
end
function acessordoc(finfo::Union{Symbol, Tuple{String, Symbol}},
var::Union{Nothing, Symbol, Vector{Symbol}}=nothing;
plural::Bool=if isnothing(var) false
elseif var isa Vector true
else getfield(BaseDirs, var) isa Ref{Vector{String}} end,
name::String=String(if fname isa Symbol fname else last(fname) end))
fprefix, fname = if finfo isa Symbol; ("", finfo) else finfo end
rettype = ifelse(plural, "Vector{String}", "String")
dirprefix, dirterm = ifelse(plural, ("all", "directories"), ("the", "directory"))
existentkwarg = ifelse(plural, " - `existent::Bool` (default `false`), filter out paths that do not exist.", "")
vardoc = if var isa Vector && (dvars = filter(v -> haskey(Docs.meta(BaseDirs), Docs.Binding(BaseDirs, v)), var)) |> !isempty
"\nThe returned path is based on the variables $(join(map(v -> "`BaseDirs.$v`", dvars), ", ", ", and ")), which see.\n"
elseif !isnothing(var) && haskey(Docs.meta(BaseDirs), Docs.Binding(BaseDirs, var))
"\nThe returned path is based on the variable `BaseDirs.$var`, which see.\n"
else "" end
kwargs = ifelse(plural, "; create, existent", "; create")
"""
$fprefix$fname($kwargs) -> $rettype # $dirprefix $dirterm
$fprefix$fname(parts...$kwargs) # $dirprefix $dirterm joined with parts
$fprefix$fname(proj::Project$kwargs) # $dirprefix project-specific $dirterm
$fprefix$fname(proj::Project, parts...$kwargs) # $dirprefix project-specific $dirterm joined with parts
Locate $dirprefix $name $dirterm. Optionally, a project and/or path components
can be provided as arguments, in which case they are joined with the $name
$dirterm as appropriate.
$vardoc
## Keyword arguments
- `create::Bool` (default `false`), whether the path should be created if it
does not exist. Paths ending in `$DIRECTORY_SUFFIX_FLAG` are interpreted as
directories, and all other paths are considered files. This takes care to
create the base directories with the appropriate permissions ($(string(NEW_BASEDIR_MODE, base=8))).
$existentkwarg"""
end
end