Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Salceanu committed Aug 8, 2024
2 parents 27d38ea + e25d049 commit 509886d
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/Loader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,17 @@ end
"""
@using(package_path)
macro to simplify loading of modules that are not located in the LOAD_PATH
`package_path` can be
- a path to a directory containing a module file of the same name
e.g 'models/MyApp' to load 'models/MyApp/MyApp.jl'
- a path to a module (without extension '.jl')
e.g. 'models/MyApp' to load models/MyApp.jl'
- a path to a package directory containing a 'src' directory and module file therein
e.g. 'models/MyApp' to load 'models/MyApp/src/MyApp.jl'
Macro to simplify loading of modules taking advantage of precompilation.
When called from Main it temporarilty adds the path to LOAD_PATH and loads the module via `using`
When called from a different module it includes the module file and uses `using .MyModule`
`package_path` can be used in several ways
- as a path to a directory containing a module file of the same name
e.g '@using models/MyApp' to load 'models/MyApp/MyApp.jl'
- as a path to a module (without extension '.jl')
e.g. '@using models/MyApp' to load models/MyApp.jl'
- as a path to a package directory containing a 'src' directory and module file therein
e.g. '@using models/MyApp' to load 'models/MyApp/src/MyApp.jl'
### Examples
Expand All @@ -433,6 +435,8 @@ Caveat: Due to precompilation it is not possible to supply variables to the macr
Calls need to supply explicit paths.
"""
macro _using(package)
# determine whether @using is called from Main or a different module
is_submodule = __module__ != Base.Main
package = expr_to_path(package)
fp = _findpackage(package)
if fp === nothing
Expand All @@ -442,7 +446,17 @@ macro _using(package)
path, package_name = fp
package_symbol = Symbol(package_name)

quote
if is_submodule
# if called from submodule add module via `include()` and, `using .MyModule`
quote
include(joinpath($path, "$($package_name).jl"))
using .$(package_symbol)
end |> esc
else
# if called from Main add module via setting LOAD_PATH and `using MyModule`
quote
println("hi $($package_name)")

let pp = split($path, ';')
for p in reverse(pp)
pushfirst!(LOAD_PATH, p)
Expand All @@ -451,14 +465,16 @@ macro _using(package)
@debug "using $($package_name) (from '$($path)')"
try
using $package_symbol
catch
catch e
error(e)
finally
for _ in pp
popfirst!(LOAD_PATH)
end
end
end
nothing
end
end
end

Expand Down

2 comments on commit 509886d

@essenciary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/112664

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v5.30.4 -m "<description of version>" 509886d61a75d74bcbd45db1f77230abd4cd11a8
git push origin v5.30.4

Please sign in to comment.