Skip to content

Commit 8d00f9f

Browse files
committed
Implement pkgfiles
1 parent eede4fc commit 8d00f9f

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

src/CodeTracking.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module CodeTracking
22

3+
using Base: PkgId
34
using Core: LineInfoNode
45

56
export whereis, definition, pkgfiles
@@ -69,12 +70,19 @@ definition(method::Method, ::Type{Expr}) = get(method_definitions, method.sig, n
6970
definition(method::Method) = definition(method, Expr)
7071

7172
"""
72-
files = pkgfiles(mod::Module)
73+
info = pkgfiles(id::PkgId)
7374
74-
Return a list of the files that were loaded to define `mod`.
75+
Return a [`PkgFiles`](@ref) structure with information about the files that define package `id`.
76+
Returns `nothing` if `id` has not been loaded.
7577
"""
76-
function pkgfiles(mod::Module)
77-
error("not implemented")
78-
end
78+
pkgfiles(id::PkgId) = get(_pkgfiles, id, nothing)
79+
80+
"""
81+
info = pkgfiles(mod::Module)
82+
83+
Return a [`PkgFiles`](@ref) structure with information about the files that were loaded to
84+
define the package that defined `mod`.
85+
"""
86+
pkgfiles(mod::Module) = pkgfiles(PkgId(mod))
7987

8088
end # module

src/data.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
# The variables here get populated by Revise.jl.
22

3+
"""
4+
PkgFiles encodes information about the current location of a package.
5+
Fields:
6+
- `id`: the `PkgId` of the package
7+
- `basedir`: the current base directory of the package
8+
- `files`: a list of files (relative path to `basedir`) that define the package.
9+
10+
Note that `basedir` may be subsequently updated by Pkg operations such as `add` and `dev`.
11+
"""
12+
mutable struct PkgFiles
13+
id::PkgId
14+
basedir::String
15+
files::Vector{String}
16+
end
17+
18+
PkgFiles(id::PkgId, path::AbstractString) = PkgFiles(id, path, String[])
19+
PkgFiles(id::PkgId, ::Nothing) = PkgFiles(id, "")
20+
PkgFiles(id::PkgId) = PkgFiles(id, normpath(basepath(id)))
21+
PkgFiles(id::PkgId, files::AbstractVector{<:AbstractString}) =
22+
PkgFiles(id, normpath(basepath(id)), files)
23+
24+
# Abstraction interface
25+
Base.PkgId(info::PkgFiles) = info.id
26+
srcfiles(info::PkgFiles) = info.files
27+
basedir(info::PkgFiles) = info.basedir
28+
329
const method_locations = IdDict{Type,LineInfoNode}()
430

531
const method_definitions = IdDict{Type,Expr}()
32+
33+
const _pkgfiles = Dict{PkgId,PkgFiles}()

src/utils.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ function isfuncexpr(ex)
1414
end
1515

1616
fileline(lin::LineInfoNode) = String(lin.file), lin.line
17+
18+
function basepath(id::PkgId)
19+
id.name ("Main", "Base", "Core") && return ""
20+
loc = Base.locate_package(id)
21+
loc === nothing && return ""
22+
return dirname(dirname(loc))
23+
end

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ include("script.jl")
1919
@test src == """
2020
f2(x, y) = x + y
2121
"""
22+
23+
info = CodeTracking.PkgFiles(Base.PkgId(CodeTracking))
24+
@test Base.PkgId(info) === info.id
25+
@test CodeTracking.basedir(info) == dirname(@__DIR__)
2226
end

0 commit comments

Comments
 (0)