diff --git a/README.md b/README.md index e42b541..71d4e6d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/FilePathsBase.jl b/src/FilePathsBase.jl index ea46ba4..37a2005 100644 --- a/src/FilePathsBase.jl +++ b/src/FilePathsBase.jl @@ -46,6 +46,8 @@ export # Macros @p_str, + @__PATH__, + @__FILEPATH__, # Constants READ, diff --git a/src/path.jl b/src/path.jl index 534f6d4..290ce67 100644 --- a/src/path.jl +++ b/src/path.jl @@ -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 . +""" +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 . +""" +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 =============================================== diff --git a/test/path.jl b/test/path.jl index d9e1996..8171e0a 100644 --- a/test/path.jl +++ b/test/path.jl @@ -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