Skip to content

Commit 3f2c9b7

Browse files
committed
Add new versions of use and extend, that add variables to public/develop lists
1 parent bf0874f commit 3f2c9b7

File tree

3 files changed

+70
-46
lines changed

3 files changed

+70
-46
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ git:
2727

2828
## uncomment the following lines to override the default test script
2929
script:
30-
- julia -e 'if VERSION < v"0.7.0-DEV.5183" ; Pkg.clone(pwd()) ; else ; Pkg.up() ; end ; Pkg.test("ModuleInterfaceTools"; coverage=true)'
30+
- julia -e 'if VERSION < v"0.7.0-DEV.5183" ; Pkg.clone(pwd()) ; else ; using Pkg; Pkg.add(pwd()) ; end ; Pkg.test("ModuleInterfaceTools"; coverage=true)'
3131
after_success:
3232
# push coverage results to Coveralls
3333
- julia -e 'cd(Pkg.dir("ModuleInterfaceTools")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'

src/ModuleInterfaceTools.jl

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const BIG_ENDIAN = (ENDIAN_BOM == 0x01020304)
2020
export Pkg
2121
end
2222

23+
_stdout() = @static V6_COMPAT ? STDOUT : stdout
24+
_stderr() = @static V6_COMPAT ? STDERR : stderr
25+
2326
Base.parse(::Type{Expr}, args...; kwargs...) =
2427
Meta.parse(args...; kwargs...)
2528

@@ -80,7 +83,8 @@ function m_eval(mod, expr)
8083
Core.eval(mod, expr)
8184
catch ex
8285
println("m_eval($mod, $expr)");
83-
rethrow(ex)
86+
println(sprint(showerror, ex, catch_backtrace()))
87+
#rethrow(ex)
8488
end
8589
end
8690
m_eval(expr) = m_eval(cur_mod(), expr)
@@ -111,7 +115,7 @@ m_eval(expr) = m_eval(cur_mod(), expr)
111115
112116
"""
113117
macro api(cmd::Symbol)
114-
mod = @static V6_COMPAT ? current_module() : __module__
118+
mod = @static V6_COMPAT ? current_module() : @__MODULE__
115119
cmd == :list ? _api_list(mod) :
116120
cmd == :freeze ? _api_freeze(mod) :
117121
error("@api unrecognized command: $cmd")
@@ -167,15 +171,20 @@ function push_args!(symbols, lst, grp)
167171
end
168172
end
169173

174+
"""Initialize the temp api variable for this module"""
175+
_init_api(curmod) =
176+
isdefined(curmod, :__tmp_api__) ||
177+
m_eval(curmod, :( export @api, ModuleInterfaceTools ;
178+
global __tmp_api__ = ModuleInterfaceTools.TMP_API($curmod)))
179+
170180
"""Add symbols"""
171181
function _add_symbols(curmod, grp, exprs)
172182
if debug[]
173183
print("_add_symbols($curmod, $grp, $exprs)")
174184
isdefined(curmod, :__tmp_api__) && print(" => ", m_eval(curmod, :__tmp_api__))
175185
println()
176186
end
177-
ex = :( export @api, ModuleInterfaceTools ; global __tmp_api__ = ModuleInterfaceTools.TMP_API($curmod) )
178-
isdefined(curmod, :__tmp_api__) || m_eval(curmod, ex)
187+
_init_api(curmod)
179188
if grp == :base!
180189
for ex in exprs
181190
if isa(ex, Expr) && ex.head == :tuple
@@ -212,18 +221,21 @@ function _add_symbols(curmod, grp, exprs)
212221
nothing
213222
end
214223

224+
has_api(mod) = isdefined(mod, :__api__)
225+
get_api(mod) = m_eval(mod, :__api__)
226+
215227
function _api_extend(curmod, modules, cpy::Bool)
216228
for nam in modules
217229
mod = m_eval(curmod, nam)
218-
if isdefined(mod, :__api__)
219-
api = m_eval(mod, :__api__)
220-
_do_list(curmod, cpy, :import, :Base, :base, api)
221-
_do_list(curmod, cpy, :import, nam, :public!, api)
222-
_do_list(curmod, cpy, :import, nam, :develop!, api)
223-
_do_list(curmod, cpy, :using, nam, :public, api)
224-
_do_list(curmod, cpy, :using, nam, :develop, api)
230+
if has_api(mod)
231+
api = get_api(mod)
232+
_do_list(curmod, cpy, :import, Base, :Base, :base, api)
233+
_do_list(curmod, cpy, :import, mod, nam, :public!, api)
234+
_do_list(curmod, cpy, :import, mod, nam, :develop!, api)
235+
_do_list(curmod, cpy, :using, mod, nam, :public, api)
236+
_do_list(curmod, cpy, :using, mod, nam, :develop, api)
225237
else
226-
_do_list(curmod, cpy, :import, nam, :public!, names(mod))
238+
_do_list(curmod, cpy, :import, mod, nam, :public!, names(mod))
227239
end
228240
end
229241
nothing
@@ -232,12 +244,12 @@ end
232244
function _api_use(curmod, modules, cpy::Bool)
233245
for nam in modules
234246
mod = m_eval(curmod, nam)
235-
if isdefined(mod, :__api__)
236-
api = m_eval(mod, :__api__)
237-
_do_list(curmod, cpy, :using, nam, :public, api)
238-
_do_list(curmod, cpy, :using, nam, :public!, api)
247+
if has_api(mod)
248+
api = get_api(mod)
249+
_do_list(curmod, cpy, :using, mod, nam, :public, api)
250+
_do_list(curmod, cpy, :using, mod, nam, :public!, api)
239251
else
240-
_do_list(curmod, cpy, :using, nam, :public!, names(mod))
252+
_do_list(curmod, cpy, :using, mod, nam, :public!, names(mod))
241253
end
242254
end
243255
nothing
@@ -246,8 +258,9 @@ end
246258
function _api_export(curmod, modules)
247259
for nam in modules
248260
mod = m_eval(curmod, nam)
249-
if isdefined(mod, :__api__)
250-
api = m_eval(mod, :__api__)
261+
api = get_api(mod)
262+
if has_api(mod)
263+
api = get_api(mod)
251264
m_eval(curmod, Expr( :export, getfield(api, :modules)...))
252265
m_eval(curmod, Expr( :export, getfield(api, :public)...))
253266
m_eval(curmod, Expr( :export, getfield(api, :public!)...))
@@ -300,48 +313,60 @@ function _api(curmod::Module, cmd::Symbol, exprs)
300313

301314
for nam in modules
302315
mod = m_eval(curmod, nam)
303-
for sym in getfield(m_eval(mod, :__api__), :modules)
304-
m_eval(curmod, :(using $nam.$sym))
316+
if has_api(mod)
317+
for sym in getfield(get_api(mod), :modules)
318+
if isdefined(mod, sym)
319+
m_eval(curmod, :(using $nam.$sym))
320+
else
321+
println(_stderr(), "Warning: Exported symbol $sym is not defined in $nam")
322+
end
323+
end
305324
end
306325
end
307326

308327
# Be nice and set up standard Test
309328
cmd == :test && m_eval(curmod, V6_COMPAT ? :(using Base.Test) : :(using Test))
310329

330+
cpy = (cmd == :use!) || (cmd == :extend!)
331+
332+
cpy && _init_api(curmod)
311333
((cmd == :use || cmd == :use!)
312-
? _api_use(curmod, modules, cmd == :use!)
313-
: _api_extend(curmod, modules, cmd == :extend!))
334+
? _api_use(curmod, modules, cpy)
335+
: _api_extend(curmod, modules, cpy))
314336
end
315337

316338
@static V6_COMPAT || (_dot_name(nam) = Expr(:., nam))
317339

318-
_do_list(curmod, cpy, cmd, mod, grp, api::API) =
319-
_do_list(curmod, cpy, cmd, mod, grp, getfield(api, grp))
340+
_do_list(curmod, cpy, cmd, mod, nam, grp, api::API) =
341+
_do_list(curmod, cpy, cmd, mod, nam, grp, getfield(api, grp))
320342

321-
function _do_list(curmod, cpy, cmd, mod, grp, lst)
322-
isempty(lst) && return
323-
@static if V6_COMPAT
324-
for nam in lst
325-
exp = Expr(cmd, mod, nam)
326-
debug[] && println("V6: $cmd, $mod, $mod, $exp")
327-
m_eval(curmod, exp)
328-
end
329-
else
330-
exp = Expr(cmd, Expr(:(:), _dot_name(mod), _dot_name.(lst)...))
331-
debug[] && println("V7: $cmd, $mod, $mod, $exp")
332-
try
333-
m_eval(curmod, exp)
334-
catch ex
335-
println("ModuleInterfaceTools: Error evaluating $exp")
336-
dump(exp)
337-
println(sprint(showerror, ex, catch_backtrace()))
343+
function _do_list(curmod, cpy, cmd, mod, nam, grp, lst)
344+
for sym in lst
345+
if isdefined(mod, sym)
346+
m_eval(curmod, Expr(cmd, nam, sym))
347+
cpy && m_eval(curmod, :( push!(__tmp_api__.$grp, $(QuoteNode(sym)) )))
348+
else
349+
println(_stderr(), "Warning: Exported symbol $sym is not defined in $nam")
338350
end
339351
end
352+
end
353+
354+
#=
355+
function _do_list(curmod, cpy, cmd, mod, nam, grp, lst)
356+
exp = Expr(cmd, Expr(:(:), _dot_name(nam), _dot_name.(lst)...))
357+
try
358+
m_eval(curmod, exp)
359+
catch ex
360+
println("ModuleInterfaceTools: Error evaluating $exp")
361+
dump(exp)
362+
println(sprint(showerror, ex, catch_backtrace()))
363+
end
340364
cpy && for sym in lst; m_eval(curmod, :( push!(__tmp_api__.$grp, $(QuoteNode(sym)) ))); end
341365
end
366+
=#
342367

343368
macro api(cmd::Symbol, exprs...)
344-
@static V6_COMPAT ? _api(current_module(), cmd, exprs) : _api(__module__, cmd, exprs)
369+
_api((@static V6_COMPAT ? current_module() : @__MODULE__), cmd, exprs)
345370
end
346371

347372
end # module ModuleInterfaceTools

test/runtests.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ using ModuleInterfaceTools
55

66
@static V6_COMPAT ? (using Base.Test) : (using Test)
77

8-
# Pick up APITest from the test directory
9-
push!(LOAD_PATH, @__DIR__)
8+
push!(LOAD_PATH, @static V6_COMPAT ? joinpath(Pkg.dir(), "test") : @__DIR__)
109

1110
@api extend APITest
1211

0 commit comments

Comments
 (0)