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

Use cached content in @ include only as a fallback #92

Merged
merged 1 commit into from
Sep 25, 2020
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
19 changes: 14 additions & 5 deletions src/Requires.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ module Requires
using UUIDs

"""
@include("file.jl")
@include("somefile.jl")

Behaves like `include`, but loads the target file contents at load-time before
evaluating its contents at runtime. This is useful when the target file may not
be available at runtime (for example, because of compiling a sysimg).
Behaves like `include`, but caches the target file content at macro expansion
time, and uses this as a fallback when the file doesn't exist at runtime. This
is useful when compiling a sysimg. The argument `"somefile.jl"` must be a
string literal, not an expression.

`@require` blocks insert this automatically when you use `include`.
"""
macro include(file)
file = joinpath(dirname(String(__source__.file)), file)
s = String(read(file))
:(include_string($__module__, $s, $file))
quote
file = $file
mod = $__module__
if isfile(file)
Base.include(mod, file)
else
include_string(mod, $s, file)
end
end
end

include("init.jl")
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ end

@testset "Requires" begin
mktempdir() do pkgsdir
local rm_CachedIncludeTest_submod_file
cd(pkgsdir) do
npcdir = joinpath("FooNPC", "src")
mkpath(npcdir)
Expand All @@ -65,6 +66,14 @@ end
cd(npcdir) do
writepkg("FooSubPC", true, true)
end
npcdir = joinpath("CachedIncludeTest", "src")
mkpath(npcdir)
cd(npcdir) do
writepkg("CachedIncludeTest", true, true)
submod_file = abspath("CachedIncludeTest_submod.jl")
@test isfile(submod_file)
rm_CachedIncludeTest_submod_file = ()->rm(submod_file)
end
end
push!(LOAD_PATH, pkgsdir)

Expand All @@ -76,6 +85,12 @@ end
@test !(:SubModule in names(FooSubNPC))
@eval using FooSubPC
@test !(:SubModule in names(FooSubPC))
@eval using CachedIncludeTest
# Test that the content of the file which defines
# CachedIncludeTest.SubModule is cached by `@require` so it can be used
# even when the file itself is removed.
rm_CachedIncludeTest_submod_file()
@test !(:SubModule in names(CachedIncludeTest))

@eval using Colors

Expand All @@ -85,6 +100,7 @@ end
@test FooSubNPC.SubModule.flag
@test :SubModule in names(FooSubPC)
@test FooSubPC.SubModule.flag
@test :SubModule in names(CachedIncludeTest)

cd(pkgsdir) do
npcdir = joinpath("FooAfterNPC", "src")
Expand Down