Skip to content

Commit 1f71c39

Browse files
authored
Merge pull request #46 from MikeInnes/teh/pc
RFC: test, fix, and redesign Requires
2 parents b550be7 + afcc30f commit 1f71c39

File tree

6 files changed

+86
-55
lines changed

6 files changed

+86
-55
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
### Note: changes in v0.7
2+
3+
Requires now needs a UUID, and must be called from within your packages `__init__` function. For example:
4+
5+
```julia
6+
function __init__()
7+
@require JSON="682c06a0-de6a-54ab-a142-c8b1cf79cde6" do_stuff()
8+
end
9+
```
10+
111
# Requires.jl
212

313
[![Build Status](https://travis-ci.org/MikeInnes/Requires.jl.svg?branch=master)](https://travis-ci.org/MikeInnes/Requires.jl)

src/Requires.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ __precompile__()
33
module Requires
44

55
include("init.jl")
6-
include("getthing.jl")
76
include("require.jl")
87

8+
function __init__()
9+
push!(package_callbacks, loadpkg)
10+
end
11+
912
end # module

src/getthing.jl

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/init.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,3 @@ end
2222
macro init(args...)
2323
initm(args...)
2424
end
25-
26-
"Prevent init fns being called multiple times during precompilation."
27-
macro guard(ex)
28-
:(!isprecompiling() && $(esc(ex)))
29-
end

src/require.jl

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ export @require
55

66
isprecompiling() = ccall(:jl_generating_output, Cint, ()) == 1
77

8-
@init begin
9-
push!(package_callbacks, loadpkg)
10-
end
11-
128
loaded(pkg) = haskey(Base.loaded_modules, pkg)
139

1410
const _callbacks = Dict{PkgId, Vector{Function}}()
@@ -52,33 +48,28 @@ function parsepkg(ex)
5248
isexpr(ex, :(=)) || @goto fail
5349
mod, id = ex.args
5450
(mod isa Symbol && id isa String) || @goto fail
55-
return Base.PkgId(Base.UUID(id), String(mod))
51+
return id, String(mod)
5652
@label fail
5753
error("Requires syntax is: `@require Pkg=\"uuid\"`")
5854
end
5955

6056
macro require(pkg, expr)
6157
pkg isa Symbol &&
6258
return Expr(:macrocall, Symbol("@warn"), __source__,
63-
"Requires now needs a UUID: `@require $pkg=\"uuid\"`")
64-
pkg = parsepkg(pkg)
65-
ex = quote
66-
listenpkg($pkg) do
67-
withpath(@__FILE__) do
68-
err($__module__, $(pkg.name)) do
69-
$(esc(:(eval($(Expr(:quote, Expr(:block,
70-
:(const $(Symbol(pkg.name)) = Base.require($pkg)),
71-
expr)))))))
59+
"Requires now needs a UUID; please see the readme for changes in 0.7.")
60+
id, modname = parsepkg(pkg)
61+
pkg = Base.PkgId(Base.UUID(id), modname)
62+
quote
63+
if !isprecompiling()
64+
listenpkg(Base.PkgId(Base.UUID($id), $modname)) do
65+
withpath($(string(__source__.file))) do
66+
err($__module__, $(pkg.name)) do
67+
$(esc(:(eval($(Expr(:quote, Expr(:block,
68+
:(const $(Symbol(pkg.name)) = Base.require($pkg)),
69+
expr)))))))
70+
end
7271
end
7372
end
7473
end
7574
end
76-
quote
77-
if isprecompiling()
78-
@init @guard $(ex)
79-
else
80-
$(ex)
81-
end
82-
nothing
83-
end
8475
end

test/runtests.jl

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,67 @@
1-
module Foo
1+
using Test
22

3-
using Requires, Test
3+
function writepkg(name, precomp::Bool)
4+
open("$name.jl", "w") do io
5+
println(io, """
6+
__precompile__($precomp)
47
5-
beforeflag = false
6-
afterflag = false
8+
module $name
79
8-
@require JSON="682c06a0-de6a-54ab-a142-c8b1cf79cde6" global beforeflag = true
10+
using Requires
911
10-
@test !beforeflag
11-
using JSON
12-
@test beforeflag
12+
flag = false
1313
14-
@require JSON="682c06a0-de6a-54ab-a142-c8b1cf79cde6" global afterflag = true
14+
function __init__()
15+
@require JSON="682c06a0-de6a-54ab-a142-c8b1cf79cde6" global flag = true
16+
end
17+
18+
end
19+
""")
20+
end
21+
end
22+
23+
@testset "Requires" begin
24+
mktempdir() do pkgsdir
25+
cd(pkgsdir) do
26+
npcdir = joinpath("FooNPC", "src")
27+
mkpath(npcdir)
28+
cd(npcdir) do
29+
writepkg("FooNPC", false)
30+
end
31+
npcdir = joinpath("FooPC", "src")
32+
mkpath(npcdir)
33+
cd(npcdir) do
34+
writepkg("FooPC", true)
35+
end
36+
end
37+
push!(LOAD_PATH, pkgsdir)
38+
39+
@eval using FooNPC
40+
@test !FooNPC.flag
41+
@eval using FooPC
42+
@test !FooPC.flag
43+
44+
@eval using JSON
45+
46+
@test FooNPC.flag
47+
@test FooPC.flag
1548

16-
@test afterflag
49+
cd(pkgsdir) do
50+
npcdir = joinpath("FooAfterNPC", "src")
51+
mkpath(npcdir)
52+
cd(npcdir) do
53+
writepkg("FooAfterNPC", false)
54+
end
55+
pcidr = joinpath("FooAfterPC", "src")
56+
mkpath(pcidr)
57+
cd(pcidr) do
58+
writepkg("FooAfterPC", true)
59+
end
60+
end
1761

62+
@eval using FooAfterNPC
63+
@eval using FooAfterPC
64+
@test FooAfterNPC.flag
65+
@test FooAfterPC.flag
66+
end
1867
end

0 commit comments

Comments
 (0)