From 040d7518bd2eace8dd8f64beb42e37143feda238 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Wed, 16 May 2018 15:37:05 -0700 Subject: [PATCH 1/5] Add @__DIR_P__, @__FILE_P__ and @LOCAL macro --- README.md | 3 +++ src/FilePathsBase.jl | 3 +++ src/path.jl | 32 ++++++++++++++++++++++++++++++++ test/path.jl | 3 +++ 4 files changed, 41 insertions(+) diff --git a/README.md b/README.md index e42b541..b49ff05 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,9 @@ chmod | chmod (recursive unix-only) chown (unix only) | chown (unix only) N/A | read N/A | write +@__DIR__ | @__DIR_P__ +@__FILE__ | @__FILE_P__ +N/A | @LOCAL ## TODO: * cross platform chmod and chown diff --git a/src/FilePathsBase.jl b/src/FilePathsBase.jl index ea46ba4..e7ed516 100644 --- a/src/FilePathsBase.jl +++ b/src/FilePathsBase.jl @@ -46,6 +46,9 @@ export # Macros @p_str, + @__DIR_P__, + @__FILE_P__, + @LOCAL, # Constants READ, diff --git a/src/path.jl b/src/path.jl index 534f6d4..e972994 100644 --- a/src/path.jl +++ b/src/path.jl @@ -27,6 +27,38 @@ home() = Path(homedir()) anchor(path::AbstractPath) = drive(path) * root(path) +""" + @__DIR_P__ -> AbstractPath + +@__DIR_P__ 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 __DIR_P__() + :(Path(@__DIR__()===nothing?Path():@__DIR__)) +end + +""" + @__FILE_P__ -> AbstractPath + +@__FILE_P__ 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 __FILE_P__() + :(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(@__DIR_P__, Path($(esc(filespec))))) +end + #= Path Modifiers =============================================== diff --git a/test/path.jl b/test/path.jl index d9e1996..1ca4b49 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 @__DIR_P__() == Path(@__DIR__) + @test @__FILE_P__() == Path(@__FILE__) + @test @LOCAL("foo.txt") == join(@__DIR_P__, "foo.txt") end end From 3ce612eef170f7ee035c3d0fe3f2b5ec427a6cab Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Wed, 16 May 2018 16:06:08 -0700 Subject: [PATCH 2/5] Use @__PATH__ and @__FILEPATH__ names --- README.md | 4 ++-- src/FilePathsBase.jl | 4 ++-- src/path.jl | 14 +++++++------- test/path.jl | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index b49ff05..ad76ae1 100644 --- a/README.md +++ b/README.md @@ -138,8 +138,8 @@ chmod | chmod (recursive unix-only) chown (unix only) | chown (unix only) N/A | read N/A | write -@__DIR__ | @__DIR_P__ -@__FILE__ | @__FILE_P__ +@__DIR__ | @__PATH__ +@__FILE__ | @__FILEPATH__ N/A | @LOCAL ## TODO: diff --git a/src/FilePathsBase.jl b/src/FilePathsBase.jl index e7ed516..1d4208c 100644 --- a/src/FilePathsBase.jl +++ b/src/FilePathsBase.jl @@ -46,8 +46,8 @@ export # Macros @p_str, - @__DIR_P__, - @__FILE_P__, + @__PATH__, + @__FILEPATH__, @LOCAL, # Constants diff --git a/src/path.jl b/src/path.jl index e972994..50defb9 100644 --- a/src/path.jl +++ b/src/path.jl @@ -28,24 +28,24 @@ home() = Path(homedir()) anchor(path::AbstractPath) = drive(path) * root(path) """ - @__DIR_P__ -> AbstractPath + @__PATH__ -> AbstractPath -@__DIR_P__ expands to a path with the directory part of the absolute path +@__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 __DIR_P__() +macro __PATH__() :(Path(@__DIR__()===nothing?Path():@__DIR__)) end """ - @__FILE_P__ -> AbstractPath + @__FILEPATH__ -> AbstractPath -@__FILE_P__ expands to a path with the absolute file path of the file +@__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 __FILE_P__() +macro __FILEPATH__() :(Path(@__FILE__()===nothing?Path():@__FILE__)) end @@ -56,7 +56,7 @@ Construct an absolute path to `filespec` relative to the source file containing the macro call. """ macro LOCAL(filespec) - :(join(@__DIR_P__, Path($(esc(filespec))))) + :(join(@__PATH__, Path($(esc(filespec))))) end #= diff --git a/test/path.jl b/test/path.jl index 1ca4b49..1c38cf0 100644 --- a/test/path.jl +++ b/test/path.jl @@ -81,9 +81,9 @@ cd(abs(parent(Path(@__FILE__)))) do @test p4.drive == "" @test p4.root == "" - @test @__DIR_P__() == Path(@__DIR__) - @test @__FILE_P__() == Path(@__FILE__) - @test @LOCAL("foo.txt") == join(@__DIR_P__, "foo.txt") + @test @__PATH__() == Path(@__DIR__) + @test @__FILEPATH__() == Path(@__FILE__) + @test @LOCAL("foo.txt") == join(@__PATH__, "foo.txt") end end From 8cf91d4dce7a233ecc6804e2e72100c2b65d6fe6 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Wed, 16 May 2018 17:16:25 -0700 Subject: [PATCH 3/5] Fix julia 0.7 spacing issue --- src/path.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/path.jl b/src/path.jl index 50defb9..290ce67 100644 --- a/src/path.jl +++ b/src/path.jl @@ -35,7 +35,7 @@ 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__)) + :(Path(@__DIR__()===nothing ? Path() : @__DIR__)) end """ @@ -46,7 +46,7 @@ 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__)) + :(Path(@__FILE__()===nothing ? Path() : @__FILE__)) end """ From 70d49540ee45733c92d43c7d84e31907deb6b034 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Wed, 16 May 2018 20:19:08 -0700 Subject: [PATCH 4/5] Unexport @LOCAL --- src/FilePathsBase.jl | 1 - test/path.jl | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/FilePathsBase.jl b/src/FilePathsBase.jl index 1d4208c..37a2005 100644 --- a/src/FilePathsBase.jl +++ b/src/FilePathsBase.jl @@ -48,7 +48,6 @@ export @p_str, @__PATH__, @__FILEPATH__, - @LOCAL, # Constants READ, diff --git a/test/path.jl b/test/path.jl index 1c38cf0..8171e0a 100644 --- a/test/path.jl +++ b/test/path.jl @@ -83,7 +83,7 @@ cd(abs(parent(Path(@__FILE__)))) do @test @__PATH__() == Path(@__DIR__) @test @__FILEPATH__() == Path(@__FILE__) - @test @LOCAL("foo.txt") == join(@__PATH__, "foo.txt") + @test FilePathsBase.@LOCAL("foo.txt") == join(@__PATH__, "foo.txt") end end From 7fd4a078269738f2e0f80859a0aa0ae015f31cc6 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Wed, 16 May 2018 20:20:47 -0700 Subject: [PATCH 5/5] Remove mention of @LOCAL from README --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index ad76ae1..71d4e6d 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,6 @@ N/A | read N/A | write @__DIR__ | @__PATH__ @__FILE__ | @__FILEPATH__ -N/A | @LOCAL ## TODO: * cross platform chmod and chown