Skip to content

Commit

Permalink
Merge pull request #1 from davidanthoff/various-macros
Browse files Browse the repository at this point in the history
Add @__DIR_P__, @__FILE_P__ and @Local macro
  • Loading branch information
rofinn authored May 17, 2018
2 parents 3dee7b5 + 7fd4a07 commit 85e9d70
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ chmod | chmod (recursive unix-only)
chown (unix only) | chown (unix only)
N/A | read
N/A | write
@__DIR__ | @__PATH__
@__FILE__ | @__FILEPATH__

## TODO:
* cross platform chmod and chown
Expand Down
2 changes: 2 additions & 0 deletions src/FilePathsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export

# Macros
@p_str,
@__PATH__,
@__FILEPATH__,

# Constants
READ,
Expand Down
32 changes: 32 additions & 0 deletions src/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ home() = Path(homedir())

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

"""
@__PATH__ -> AbstractPath
@__PATH__ expands to a path with the directory part of the absolute path
of the file containing the macro. Returns an empty Path if run from a REPL or
if evaluated by julia -e <expr>.
"""
macro __PATH__()
:(Path(@__DIR__()===nothing ? Path() : @__DIR__))
end

"""
@__FILEPATH__ -> AbstractPath
@__FILEPATH__ expands to a path with the absolute file path of the file
containing the macro. Returns an empty Path if run from a REPL or if
evaluated by julia -e <expr>.
"""
macro __FILEPATH__()
:(Path(@__FILE__()===nothing ? Path() : @__FILE__))
end

"""
@LOCAL(filespec)
Construct an absolute path to `filespec` relative to the source file
containing the macro call.
"""
macro LOCAL(filespec)
:(join(@__PATH__, Path($(esc(filespec)))))
end

#=
Path Modifiers
===============================================
Expand Down
3 changes: 3 additions & 0 deletions test/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ cd(abs(parent(Path(@__FILE__)))) do
@test p4.drive == ""
@test p4.root == ""

@test @__PATH__() == Path(@__DIR__)
@test @__FILEPATH__() == Path(@__FILE__)
@test FilePathsBase.@LOCAL("foo.txt") == join(@__PATH__, "foo.txt")
end
end

Expand Down

0 comments on commit 85e9d70

Please sign in to comment.