From dd3c5ce90cb9251bad699e7a9c06383701571176 Mon Sep 17 00:00:00 2001 From: Florian Atteneder Date: Tue, 1 Aug 2023 22:17:49 +0200 Subject: [PATCH] refactor depot path helpers --- base/initdefs.jl | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/base/initdefs.jl b/base/initdefs.jl index a3e0d48aa46afa..9f75ae557e4eaa 100644 --- a/base/initdefs.jl +++ b/base/initdefs.jl @@ -115,23 +115,24 @@ function init_depot_path() nothing end -# replace leading dirname with `@depot, @stdlib` if `path` is located within any of -# DEPOT_PATH or Sys.STDLIB +# replace leading dirname with `@depot, @stdlib` if `path` is located within any of DEPOT_PATH or Sys.STDLIB # return normalized path otherwise function replace_depot_path(_path::AbstractString) path = normpath(_path) if startswith(path, Sys.STDLIB) - length(path) == 1+length(Sys.STDLIB) && return joinpath("@stdlib", "") - return joinpath("@stdlib", path[2+length(Sys.STDLIB):end]) + subpath = path[nextind(path,length(Sys.STDLIB)):end] + if isabspath(subpath) + subpath = subpath[nextind(subpath,1):end] + end + return joinpath("@stdlib", subpath) end for depot in DEPOT_PATH - if startswith(path, depot) # joinpath(depot, "compiled")) - subpath = path[1+length(depot):end] - if startswith(subpath, "/") - subpath = subpath[2:end] + if startswith(path, joinpath(depot, "compiled")) || startswith(path, joinpath(depot, "packages")) + subpath = path[nextind(path,length(depot)):end] + if isabspath(subpath) + subpath = subpath[nextind(subpath,1):end] end return joinpath("@depot", subpath) - # return joinpath("@depot", path[1+length(depot):end]) end end return path @@ -143,21 +144,16 @@ end function resolve_depot_path(_path::AbstractString) path = normpath(_path) if startswith(path, "@stdlib") - length(path) == 1+length("@stdlib") && return joinpath(Sys.STDLIB, "") - fullpath = joinpath(Sys.STDLIB, path[2+length("@stdlib"):end]) + fullpath = joinpath(Sys.STDLIB, path[nextind(path,length("@stdlib")+1):end]) ispath(fullpath) && return fullpath - throw(ErrorException("Failed to resolve `$path` to a stdlib path in `$(Sys.STDLIB)`.")) - elseif startswith(path, joinpath("@depot")) #, "compiled")) - dir = path[2+length("@depot"):end] + throw(ErrorException("Failed to resolve `$path` ($fullpath) to a stdlib path in `$(Sys.STDLIB)`.")) + elseif startswith(path, joinpath("@depot")) + dir = path[nextind(path,length("@depot")+1):end] for depot in DEPOT_PATH fullpath = joinpath(depot, dir) ispath(fullpath) && return fullpath end - # TODO Why IOBuffer here? - io = IOBuffer() - print(io, "Failed to resolve `$path` to a valid path for any depot in `DEPOT_PATH = ", - DEPOT_PATH, "`.") - throw(ErrorException(String(take!(io)))) + throw(ErrorException("Failed to resolve `$path` to a valid path for any depot in `DEPOT_PATH`")) end return path end