Skip to content

Commit

Permalink
Add @__DIR__ and document use in pkgs. Fix #12120
Browse files Browse the repository at this point in the history
Add test.

Add docs.

Move docstrings for @__FILE__ and @__DIR__ inline.

Missing paren.

Rephrase example.
  • Loading branch information
helgee committed Sep 7, 2016
1 parent 8761c48 commit edcd65d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
9 changes: 0 additions & 9 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2364,15 +2364,6 @@ in the requested `mime` format and simply returns `x`.
"""
reprmime

"""
@__FILE__ -> AbstractString
`@__FILE__` expands to a string with the absolute file path of the file containing the
macro. Returns `nothing` if run from a REPL or an empty string if evaluated by
`julia -e <expr>`. Alternatively see [`PROGRAM_FILE`](:data:`PROGRAM_FILE`).
"""
:@__FILE__

"""
!(x)
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@ export
# Macros
# parser internal
@__FILE__,
@__DIR__,
@int128_str,
@uint128_str,
@big_str,
Expand Down
16 changes: 16 additions & 0 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,24 @@ function source_dir()
p === nothing ? p : dirname(p)
end

"""
@__FILE__ -> AbstractString
`@__FILE__` expands to a string with the absolute file path of the file containing the
macro. Returns `nothing` if run from a REPL or an empty string if evaluated by
`julia -e <expr>`. Alternatively see [`PROGRAM_FILE`](:data:`PROGRAM_FILE`).
"""
macro __FILE__() source_path() end

"""
@__DIR__ -> AbstractString
`@__DIR__` expands to a string with the directory part of the absolute path of the file
containing the macro. Returns `nothing` if run from a REPL or an empty string if
evaluated by `julia -e <expr>`.
"""
macro __DIR__() source_dir() end

"""
include(path::AbstractString)
Expand Down
10 changes: 10 additions & 0 deletions doc/manual/packages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,16 @@ testing service, and an ``appveyor.yml`` file for using `AppVeyor <https://www.a
the Travis and AppVeyor websites for your package repository, but once you've done that, it will already have working tests.
Of course, all the default testing does is verify that ``using FooBar`` in Julia works.

Loading Static Non-Julia Files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If your package code needs to load static files which are not Julia code, e.g. an external library or data files, and are
located within the package directory, use the ``@__DIR__`` macro to determine the directory of the current source file.
For example if ``FooBar/src/FooBar.jl`` needs to load ``FooBar/data/foo.csv``, use the following code::

datapath = joinpath(@__DIR__, "..", "data")
foo = readcsv(joinpath(datapath, "foo.csv"))

Making Your Package Available
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
6 changes: 6 additions & 0 deletions doc/stdlib/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@
``@__FILE__`` expands to a string with the absolute file path of the file containing the macro. Returns ``nothing`` if run from a REPL or an empty string if evaluated by ``julia -e <expr>``\ . Alternatively see :data:`PROGRAM_FILE`\ .

.. function:: @__DIR__ -> AbstractString

.. Docstring generated from Julia source
``@__DIR__`` expands to a string with the directory part of the absolute path of the file containing the macro. Returns ``nothing`` if run from a REPL or an empty string if evaluated by ``julia -e <expr>``\ .

.. function:: @__LINE__ -> Int

.. Docstring generated from Julia source
Expand Down
3 changes: 3 additions & 0 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ thefname = "the fname!//\\&\1*"
@test basename(@__FILE__) == "loading.jl"
@test isabspath(@__FILE__)

@test isdir(@__DIR__)
@test @__DIR__() == dirname(@__FILE__)

# Issue #5789 and PR #13542:
let true_filename = "cAsEtEsT.jl", lowered_filename="casetest.jl"
touch(true_filename)
Expand Down

0 comments on commit edcd65d

Please sign in to comment.