Skip to content

Commit 85e9d70

Browse files
authored
Merge pull request #1 from davidanthoff/various-macros
Add @__DIR_P__, @__FILE_P__ and @Local macro
2 parents 3dee7b5 + 7fd4a07 commit 85e9d70

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ chmod | chmod (recursive unix-only)
138138
chown (unix only) | chown (unix only)
139139
N/A | read
140140
N/A | write
141+
@__DIR__ | @__PATH__
142+
@__FILE__ | @__FILEPATH__
141143

142144
## TODO:
143145
* cross platform chmod and chown

src/FilePathsBase.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export
4646

4747
# Macros
4848
@p_str,
49+
@__PATH__,
50+
@__FILEPATH__,
4951

5052
# Constants
5153
READ,

src/path.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,38 @@ home() = Path(homedir())
2727

2828
anchor(path::AbstractPath) = drive(path) * root(path)
2929

30+
"""
31+
@__PATH__ -> AbstractPath
32+
33+
@__PATH__ expands to a path with the directory part of the absolute path
34+
of the file containing the macro. Returns an empty Path if run from a REPL or
35+
if evaluated by julia -e <expr>.
36+
"""
37+
macro __PATH__()
38+
:(Path(@__DIR__()===nothing ? Path() : @__DIR__))
39+
end
40+
41+
"""
42+
@__FILEPATH__ -> AbstractPath
43+
44+
@__FILEPATH__ expands to a path with the absolute file path of the file
45+
containing the macro. Returns an empty Path if run from a REPL or if
46+
evaluated by julia -e <expr>.
47+
"""
48+
macro __FILEPATH__()
49+
:(Path(@__FILE__()===nothing ? Path() : @__FILE__))
50+
end
51+
52+
"""
53+
@LOCAL(filespec)
54+
55+
Construct an absolute path to `filespec` relative to the source file
56+
containing the macro call.
57+
"""
58+
macro LOCAL(filespec)
59+
:(join(@__PATH__, Path($(esc(filespec)))))
60+
end
61+
3062
#=
3163
Path Modifiers
3264
===============================================

test/path.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ cd(abs(parent(Path(@__FILE__)))) do
8181
@test p4.drive == ""
8282
@test p4.root == ""
8383

84+
@test @__PATH__() == Path(@__DIR__)
85+
@test @__FILEPATH__() == Path(@__FILE__)
86+
@test FilePathsBase.@LOCAL("foo.txt") == join(@__PATH__, "foo.txt")
8487
end
8588
end
8689

0 commit comments

Comments
 (0)