|
80 | 80 | end
|
81 | 81 | end
|
82 | 82 |
|
83 |
| -function load_hook(prefix::String, name::String, ::Void) |
84 |
| - name_jl = "$name.jl" |
85 |
| - path = joinpath(prefix, name_jl) |
86 |
| - isfile_casesensitive(path) && return abspath(path) |
87 |
| - path = joinpath(prefix, name_jl, "src", name_jl) |
88 |
| - isfile_casesensitive(path) && return abspath(path) |
89 |
| - path = joinpath(prefix, name, "src", name_jl) |
90 |
| - isfile_casesensitive(path) && return abspath(path) |
91 |
| - return nothing |
| 83 | +macro return_if_file(path) |
| 84 | + quote |
| 85 | + path = $(esc(path)) |
| 86 | + isfile_casesensitive(path) && return path |
| 87 | + end |
92 | 88 | end
|
93 |
| -load_hook(prefix::String, name::String, path::String) = path |
94 |
| -load_hook(prefix, name::String, ::Any) = |
95 |
| - throw(ArgumentError("unrecognized custom loader in LOAD_PATH: $prefix")) |
96 | 89 |
|
97 |
| -_str(x::AbstractString) = String(x) |
98 |
| -_str(x) = x |
99 |
| - |
100 |
| -# `wd` is a working directory to search. defaults to current working directory. |
101 |
| -# if `wd === nothing`, no extra path is searched. |
102 |
| -function find_in_path(name::String, wd::Union{Void,String}) |
103 |
| - isabspath(name) && return name |
104 |
| - base = name |
105 |
| - if endswith(name,".jl") |
106 |
| - base = name[1:prevind(name, end-2)] |
107 |
| - else |
108 |
| - name = string(base,".jl") |
| 90 | +function find_package(name::String) |
| 91 | + endswith(name, ".jl") && (name = chop(name, 0, 3)) |
| 92 | + for dir in [Pkg.dir(); LOAD_PATH] |
| 93 | + dir = abspath(dir) |
| 94 | + @return_if_file joinpath(dir, "$name.jl") |
| 95 | + @return_if_file joinpath(dir, "$name.jl", "src", "$name.jl") |
| 96 | + @return_if_file joinpath(dir, name, "src", "$name.jl") |
109 | 97 | end
|
110 |
| - if wd !== nothing |
111 |
| - isfile_casesensitive(joinpath(wd,name)) && return joinpath(wd,name) |
112 |
| - end |
113 |
| - path = nothing |
114 |
| - path = _str(load_hook(_str(Pkg.dir()), base, path)) |
115 |
| - for dir in LOAD_PATH |
116 |
| - path = _str(load_hook(_str(dir), base, path)) |
117 |
| - end |
118 |
| - return path |
| 98 | + return nothing |
119 | 99 | end
|
120 |
| -find_in_path(name::AbstractString, wd::AbstractString = pwd()) = |
121 |
| - find_in_path(String(name), String(wd)) |
122 | 100 |
|
123 |
| -function find_source_file(file::String) |
124 |
| - (isabspath(file) || isfile(file)) && return file |
125 |
| - file2 = find_in_path(file) |
126 |
| - file2 !== nothing && return file2 |
127 |
| - file2 = joinpath(JULIA_HOME, DATAROOTDIR, "julia", "base", file) |
128 |
| - return isfile(file2) ? file2 : nothing |
| 101 | +function find_source_file(path::String) |
| 102 | + (isabspath(path) || isfile(path)) && return path |
| 103 | + base_path = joinpath(JULIA_HOME, DATAROOTDIR, "julia", "base", path) |
| 104 | + return isfile(base_path) ? base_path : nothing |
129 | 105 | end
|
130 | 106 |
|
131 | 107 | function find_all_in_cache_path(mod::Symbol)
|
@@ -416,7 +392,7 @@ function _require(mod::Symbol)
|
416 | 392 | toplevel_load[] = false
|
417 | 393 | # perform the search operation to select the module file require intends to load
|
418 | 394 | name = string(mod)
|
419 |
| - path = find_in_path(name, nothing) |
| 395 | + path = find_package(name) |
420 | 396 | if path === nothing
|
421 | 397 | throw(ArgumentError("Module $name not found in current path.\nRun `Pkg.add(\"$name\")` to install the $name package."))
|
422 | 398 | end
|
@@ -628,7 +604,7 @@ for important notes.
|
628 | 604 | """
|
629 | 605 | function compilecache(name::String)
|
630 | 606 | # decide where to get the source file from
|
631 |
| - path = find_in_path(name, nothing) |
| 607 | + path = find_package(name) |
632 | 608 | path === nothing && throw(ArgumentError("$name not found in path"))
|
633 | 609 | path = String(path)
|
634 | 610 | # decide where to put the resulting cache file
|
@@ -778,7 +754,7 @@ function stale_cachefile(modpath::String, cachefile::String)
|
778 | 754 | continue
|
779 | 755 | end
|
780 | 756 | name = string(mod)
|
781 |
| - path = find_in_path(name, nothing) |
| 757 | + path = find_package(name) |
782 | 758 | if path === nothing
|
783 | 759 | return true # Won't be able to fullfill dependency
|
784 | 760 | end
|
|
0 commit comments