Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @__DIR_P__, @__FILE_P__ and @LOCAL macro #1

Merged
merged 5 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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