-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
202 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
name = "MethodURL" | ||
uuid = "461c4225-bb7a-4706-8416-467e5545dbd6" | ||
authors = ["Adrian Hill <gh@adrianhill.de>"] | ||
version = "1.0.0-DEV" | ||
authors = ["Adrian Hill <gh@adrianhill.de>", "Kristoffer Carlsson"] | ||
version = "0.1.0-DEV" | ||
|
||
[deps] | ||
RegistryInstances = "2792f1a3-b283-48e8-9a74-f99dce5104f3" | ||
|
||
[compat] | ||
RegistryInstances = "0.1" | ||
julia = "1.10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,104 @@ | ||
# Based on code by Kristoffer Carlsson: | ||
# /~https://github.com/JuliaLang/julia/issues/47709#issuecomment-2388629772 | ||
|
||
module MethodURL | ||
|
||
# Write your package code here. | ||
using Base: PkgId, UUID, Sys, inbase | ||
using RegistryInstances: reachable_registries, registry_info | ||
|
||
export url | ||
|
||
function repo_and_path_to_url(repo, version, path, line) | ||
repo = chopsuffix(repo, ".git") | ||
# TODO: Handle more git forges | ||
if startswith(repo, "https://github.com") | ||
# /~https://github.com/owner/Package.jl/blob/v0.1.0/src/foo.jl#L42 | ||
return join([repo, "blob", "v" * version, path * "#L$line"], "/") | ||
elseif startswith(repo, "https://gitlab.com") | ||
# https://gitlab.com/owner/Package.jl/-/blob/v0.1.0/src/foo.jl#L42 | ||
return join([repo, "-", "blob", "v" * version, path * "#L$line"], "/") | ||
elseif startswith(repo, "https://git.sr.ht") | ||
# https://git.sr.ht/~owner/Package.jl/tree/v0.1.0/item/src/foo.jl#L42 | ||
return join([repo, "tree", "v" * version, "item", path * "#L$line"], "/") | ||
else | ||
error("Failed to construct URL for repository $repo.") | ||
end | ||
end | ||
|
||
# Find repository in reachable registries by looking up UUID | ||
function repos_package(uuid::UUID) | ||
repos = String[] | ||
for reg in reachable_registries() | ||
entry = get(reg, uuid, nothing) | ||
if entry !== nothing | ||
info = registry_info(entry) | ||
push!(repos, info.repo) | ||
end | ||
end | ||
return repos | ||
end | ||
|
||
# Return errors instead of `nothing` | ||
function _uuid(M::Module) | ||
uuid = PkgId(M).uuid | ||
isnothing(uuid) && error("Failed to find UUID of package $M.") | ||
return uuid | ||
end | ||
|
||
# Return errors instead of `nothing` | ||
function _pkgdir(M::Module) | ||
dir = pkgdir(M) | ||
isnothing(dir) && error("Failed to find directory of package $M.") | ||
return dir | ||
end | ||
|
||
# TODO: is this heuristic sufficient? | ||
instdlib(pkgdir) = contains(pkgdir, Sys.STDLIB) | ||
|
||
# TODO: If package is devved use local path | ||
# TODO: If package is added by URL, use that | ||
# TODO: Support monorepos | ||
function url(m::Method) | ||
M = parentmodule(m) | ||
file = String(m.file) | ||
line = m.line | ||
|
||
urls = String[] | ||
if inbase(M) | ||
# adapted from /~https://github.com/JuliaLang/julia/blob/8f5b7ca12ad48c6d740e058312fc8cf2bbe67848/base/methodshow.jl#L382-L388 | ||
commit = Base.GIT_VERSION_INFO.commit | ||
if isempty(commit) | ||
url = "/~https://github.com/JuliaLang/julia/tree/v$VERSION/base/$file#L$line" | ||
else | ||
url = "/~https://github.com/JuliaLang/julia/tree/$commit/base/$file#L$line" | ||
end | ||
push!(urls, url) | ||
else | ||
uuid = _uuid(M) | ||
repos = repos_package(uuid) | ||
pkgdir = _pkgdir(M) | ||
|
||
if isempty(repos) && instdlib(pkgdir) # stdlib package | ||
package, file = match(r"/stdlib/v(?:.*?)/(.*?)/src/(.*)", file).captures | ||
url = "/~https://github.com/JuliaLang/julia/blob/v$VERSION/stdlib/$package/src/$file#L$line" | ||
push!(urls, url) | ||
else # external package | ||
pkg_splitpath = splitpath(pkgdir) | ||
file_splitpath = splitpath(file) | ||
while !isempty(pkg_splitpath) && first(pkg_splitpath) == first(file_splitpath) | ||
popfirst!(pkg_splitpath) | ||
popfirst!(file_splitpath) | ||
end | ||
local_dir = join(file_splitpath, "/") | ||
|
||
v = string(pkgversion(M)) | ||
for repo in repos | ||
url = repo_and_path_to_url(repo, v, local_dir, line) | ||
push!(urls, url) | ||
end | ||
end | ||
end | ||
return urls | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
[deps] | ||
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" | ||
Arxiv = "95bf46a4-16f0-449f-8b01-023b953c38f0" | ||
ExplicitImports = "7d51a73a-1435-4ff3-83d9-f097790105c7" | ||
GPMaxlik = "988d40dc-a58a-4803-bd2c-6d7438fe27fe" | ||
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3" | ||
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" | ||
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" | ||
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" | ||
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters