Skip to content

Commit ad471f5

Browse files
authored
Merge pull request #49 from disberd/small_fixes
2 parents 3cac4f3 + 074a7fb commit ad471f5

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

docs/src/frompackage/basic_use.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
@frompackage target import_block
55
```
66

7-
The `@frompackage` macro takes a local Package (derived from the `target` path), loads it as
8-
a submodule of the current Pluto workspace and then process the various
9-
import/using statements inside `import_block` to extract varables/functions from
10-
the local Package into the notebook workspace.
7+
The `@frompackage` macro takes a local Package (derived from the `target` path),
8+
loads it as a submodule of the current Pluto workspace and then process the
9+
various import/using statements inside `import_block` to extract
10+
varables/functions from the local Package into the notebook workspace.
1111

1212
!!! note
1313
`@fromparent` is simply a convenience synthax that uses the calling notebook file as `target`. More details on how the target path is processed are given below.\
@@ -26,9 +26,10 @@ For this reason the *reload* of local code is only triggered manually within `@f
2626

2727
## `target` path
2828

29-
The first argument to `@frompackage` (`target`) has to be a String containing the path (either
30-
absolute or relative to the file calling the macro) that points to a local
31-
Package (the path can be to any file or subfolder within the Package folder).
29+
The first argument to `@frompackage` (`target`) has to be an AbstractString (or
30+
a `@raw_str`) containing the path (either absolute or relative to the file
31+
calling the macro) that points to a local Package (the path can be to any file
32+
or subfolder within the Package folder).
3233

3334
The main module of the package identified by the `target` path will be used as the module to process and load within the calling notebook
3435

src/frompackage/helpers.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,15 @@ end
261261
issamepath(path1::Symbol, path2::Symbol) = issamepath(String(path1), String(path2))
262262

263263
# Create a Base.PkgId from a PkgInfo
264-
to_pkgid(p::PkgInfo) = Base.PkgId(p.uuid, p.name)
264+
to_pkgid(p::PkgInfo) = Base.PkgId(p.uuid, p.name)
265+
266+
# This will extract the string from a raw_str macro, and will throw an error otherwise
267+
function extract_raw_str(ex::Expr)
268+
valid = Meta.isexpr(ex, :macrocall) && ex.args[1] === Symbol("@raw_str")
269+
if valid
270+
return ex.args[end], true
271+
else
272+
return "", false
273+
end
274+
end
275+
extract_raw_str(s::AbstractString) = String(s), true

src/frompackage/macro.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ function frompackage(ex, target_file, caller, caller_module; macroname)
8181
end
8282
# Now we add the call to maybe load the package extensions
8383
push!(args, :($load_package_extensions($dict, @__MODULE__)))
84-
# Check if we are inside a direct macroexpand code, and clean the LOAD_PATH if we do as we won't be executing the retured expression
85-
is_macroexpand(stacktrace(), cell_id) && clean_loadpath(proj_file)
8684
# We wrap the import expressions inside a try-catch, as those also correctly work from there.
8785
# This also allow us to be able to catch the error in case something happens during loading and be able to gracefully clean the work space
8886
text = "Reload $macroname"
@@ -134,10 +132,11 @@ end
134132
"""
135133
@frompackage target import_block
136134
137-
This macro takes a local Package (derived from the `target` path), loads it as
138-
a submodule of the current Pluto workspace and then process the various
139-
import/using statements inside `import_block` to extract varables/functions from
140-
the local Package into the notebook workspace.
135+
This macro takes a local Package (derived from the `target` path, which can be
136+
an `AbstractString` or a `@raw_str`), loads it as a submodule of the current
137+
Pluto workspace and then process the various import/using statements inside
138+
`import_block` to extract varables/functions from the local Package into the
139+
notebook workspace.
141140
142141
Its main use is allowing to load a local package under development within a
143142
running Pluto notebook in order to facilitate prototyping and testing.
@@ -162,9 +161,11 @@ See the package [documentation](https://disberd.github.io/PlutoDevMacros.jl/dev/
162161
163162
See also: [`@fromparent`](@ref)
164163
"""
165-
macro frompackage(target::String, ex)
164+
macro frompackage(target::Union{AbstractString, Expr}, ex)
165+
target_file, valid = extract_raw_str(target)
166+
@assert valid "Only `AbstractStrings` or `Exprs` of type `raw\"...\"` are allowed as `target` in the `@frompackage` macro."
166167
calling_file = String(__source__.file)
167-
out = _combined(ex, target, calling_file, __module__; macroname = "@frompackage")
168+
out = _combined(ex, target_file, calling_file, __module__; macroname = "@frompackage")
168169
esc(out)
169170
end
170171

test/frompackage/basics.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import PlutoDevMacros.FromPackage: process_outside_pluto!, load_module_in_caller, modname_path, fromparent_module, parseinput, get_package_data, @fromparent, _combined, process_skiplines!, get_temp_module, LineNumberRange, parse_skipline, extract_module_expression, _inrange, filterednames, reconstruct_import_expr, extract_import_args
1+
import PlutoDevMacros.FromPackage: process_outside_pluto!, load_module_in_caller, modname_path, fromparent_module, parseinput, get_package_data, @fromparent, _combined, process_skiplines!, get_temp_module, LineNumberRange, parse_skipline, extract_module_expression, _inrange, filterednames, reconstruct_import_expr, extract_import_args, extract_raw_str, @frompackage
22
import Pkg
33

44
using Test
@@ -49,6 +49,22 @@ finally
4949
Pkg.activate(current_project)
5050
end
5151

52+
# Test the macroexpand part
53+
@test nothing === Core.eval(@__MODULE__, :(@macroexpand @frompackage $(inpackage_target) import *))
54+
@test_throws "No project was found" Core.eval(@__MODULE__, :(@macroexpand @frompackage "/asd/lol" import TOML))
55+
@testset "raw_str" begin
56+
str, valid = extract_raw_str("asd")
57+
@test valid
58+
@test str == "asd"
59+
str, valid = extract_raw_str(:(raw"asd\lol"))
60+
@test valid
61+
@test str == "asd\\lol"
62+
@test_throws "Only `AbstractStrings`" Core.eval(Main, :(@frompackage 3+2 import *))
63+
cd(dirname(inpackage_target)) do
64+
@test Core.eval(@__MODULE__, :(@frompackage raw".." import *)) === nothing
65+
end
66+
end
67+
5268
@testset "Outside Pluto" begin
5369
dict = get_package_data(inpackage_target)
5470
valid(ex) = nothing !== process_outside_pluto!(deepcopy(ex), dict)

0 commit comments

Comments
 (0)