Skip to content

Commit cda894b

Browse files
author
Kristoffer Carlsson
authored
stop using Pkg.Types.Project (#78)
1 parent 9c419c5 commit cda894b

File tree

7 files changed

+80
-170
lines changed

7 files changed

+80
-170
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RegistryTools"
22
uuid = "d1eb7eb1-105f-429d-abf5-b0f65cb9e2c4"
33
authors = ["Stefan Karpinski <stefan@karpinski.org>"]
4-
version = "1.9.1"
4+
version = "2.0.0"
55

66
[deps]
77
AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f"

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
[![codecov](https://codecov.io/gh/JuliaRegistries/RegistryTools.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaRegistries/RegistryTools.jl)
66

77
Functionality for modifying Julia registry files.
8+
9+
#### Updating to 2.0
10+
11+
RegistryTools.jl changed the API in 2.0 from taking a `Pkg.Types.Project` to just taking a path to a project file instead.

src/RegistryTools.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ using Pkg: Pkg, TOML, GitTools
1111
using UUIDs
1212

1313
const DEFAULT_REGISTRY_URL = "https://github.com/JuliaRegistries/General"
14-
const PKG_HAS_WEAK = hasfield(Pkg.Types.Project, :_deps_weak)
15-
16-
function __init__()
17-
if !PKG_HAS_WEAK
18-
@warn "Running Registrator on a Julia version that does not support weak dependencies. " *
19-
"Weak dependencies will not be registered."
20-
end
21-
end
2214

2315
include("Compress.jl")
2416
include("builtin_pkgs.jl")

src/register.jl

Lines changed: 31 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,3 @@
1-
"""
2-
Given a remote repo URL and a git tree spec, get a `Project` object
3-
for the project file in that tree and a hash string for the tree.
4-
"""
5-
# function get_project(remote_url::AbstractString, tree_spec::AbstractString)
6-
# # TODO?: use raw file downloads for GitHub/GitLab
7-
# mktempdir(mkpath("packages")) do tmp
8-
# # bare clone the package repo
9-
# @debug("bare clone the package repo")
10-
# repo = LibGit2.clone(remote_url, joinpath(tmp, "repo"), isbare=true)
11-
# tree = try
12-
# LibGit2.GitObject(repo, tree_spec)
13-
# catch err
14-
# err isa LibGit2.GitError && err.code == LibGit2.Error.ENOTFOUND || rethrow(err)
15-
# error("$remote_url: git object $(repr(tree_spec)) could not be found")
16-
# end
17-
# tree isa LibGit2.GitTree || (tree = LibGit2.peel(LibGit2.GitTree, tree))
18-
#
19-
# # check out the requested tree
20-
# @debug("check out the requested tree")
21-
# tree_path = abspath(tmp, "tree")
22-
# GC.@preserve tree_path begin
23-
# opts = LibGit2.CheckoutOptions(
24-
# checkout_strategy = LibGit2.Consts.CHECKOUT_FORCE,
25-
# target_directory = Base.unsafe_convert(Cstring, tree_path)
26-
# )
27-
# LibGit2.checkout_tree(repo, tree, options=opts)
28-
# end
29-
#
30-
# # look for a project file in the tree
31-
# @debug("look for a project file in the tree")
32-
# project_file = Pkg.Types.projectfile_path(tree_path)
33-
# project_file !== nothing && isfile(project_file) ||
34-
# error("$remote_url: git tree $(repr(tree_spec)) has no project file")
35-
#
36-
# # parse the project file
37-
# @debug("parse the project file")
38-
# project = Pkg.Types.read_project(project_file)
39-
# project.name === nothing &&
40-
# error("$remote_url $(repr(tree_spec)): package has no name")
41-
# project.uuid === nothing &&
42-
# error("$remote_url $(repr(tree_spec)): package has no UUID")
43-
# project.version === nothing &&
44-
# error("$remote_url $(repr(tree_spec)): package has no version")
45-
#
46-
# return project, string(LibGit2.GitHash(tree))
47-
# end
48-
# end
49-
50-
function getdeps(pkg)
51-
if PKG_HAS_WEAK
52-
return merge(pkg.deps, pkg._deps_weak)
53-
end
54-
return pkg.deps
55-
end
56-
571
# These can compromise the integrity of the registry and cannot be
582
# opted out of.
593
const mandatory_errors = [:version_exists,
@@ -105,7 +49,7 @@ struct RegBranch
10549

10650
metadata::Dict{String, Any} # "error", "warning", kind etc.
10751

108-
function RegBranch(pkg::Pkg.Types.Project, branch::AbstractString)
52+
function RegBranch(pkg::Project, branch::AbstractString)
10953
new(pkg.name, pkg.version, branch, Dict{String,Any}())
11054
end
11155
end
@@ -282,7 +226,7 @@ function versionrange(lo::VersionBound, hi::VersionBound)
282226
return VersionRange(lo, hi)
283227
end
284228

285-
function find_package_in_registry(pkg::Pkg.Types.Project,
229+
function find_package_in_registry(pkg::Project,
286230
registry_file::AbstractString,
287231
registry_path::AbstractString,
288232
registry_data::RegistryData,
@@ -346,7 +290,7 @@ function check_package!(package_repo::AbstractString,
346290
return package_repo
347291
end
348292

349-
function update_package_file(pkg::Pkg.Types.Project,
293+
function update_package_file(pkg::Project,
350294
package_repo::AbstractString,
351295
subdir::AbstractString,
352296
package_path::AbstractString)
@@ -371,15 +315,15 @@ function get_versions_file(package_path::AbstractString)
371315
return filename, data
372316
end
373317

374-
function check_versions!(pkg::Pkg.Types.Project,
318+
function check_versions!(pkg::Project,
375319
versions_data::Dict{String, Any},
376320
status::ReturnStatus)
377321
versions = sort!([VersionNumber(v) for v in keys(versions_data)])
378322
check_version!(pkg.version, versions, status)
379323
return versions
380324
end
381325

382-
function update_versions_file(pkg::Pkg.Types.Project,
326+
function update_versions_file(pkg::Project,
383327
versions_file::AbstractString,
384328
versions_data::Dict{String, Any},
385329
tree_hash::AbstractString)
@@ -408,12 +352,10 @@ function update_versions_file(pkg::Pkg.Types.Project,
408352
end
409353
end
410354

411-
function check_deps!(pkg::Pkg.Types.Project,
355+
function check_deps!(pkg::Project,
412356
regdata::Vector{RegistryData},
413357
status::ReturnStatus)
414-
depses = [getdeps(pkg)]
415-
PKG_HAS_WEAK && push!(depses, pkg.weakdeps)
416-
for deps in depses
358+
for deps in [pkg.deps, pkg.weakdeps]
417359
if pkg.name in keys(deps)
418360
err = :package_self_dep
419361
@debug(err)
@@ -433,12 +375,10 @@ end
433375
# Versions.toml after update. This is handled with the `old_versions'
434376
# argument and the assumption that Versions.toml has been updated with
435377
# the new version before calling this function.
436-
function update_deps_file(pkg::Pkg.Types.Project,
378+
function update_deps_file(pkg::Project,
437379
package_path::AbstractString,
438380
old_versions::Vector{VersionNumber})
439-
file_depses = [("Deps.toml", getdeps(pkg))]
440-
PKG_HAS_WEAK && push!(file_depses, ("WeakDeps.toml", pkg.weakdeps))
441-
for (file, deps) in file_depses
381+
for (file, deps) in [("Deps.toml", pkg.deps), ("WeakDeps.toml", pkg.weakdeps)]
442382
deps_file = joinpath(package_path, file)
443383
if isfile(deps_file)
444384
deps_data = Compress.load(deps_file, old_versions)
@@ -453,17 +393,12 @@ function update_deps_file(pkg::Pkg.Types.Project,
453393
end
454394
end
455395

456-
function check_compat!(pkg::Pkg.Types.Project,
396+
function check_compat!(pkg::Project,
457397
regdata::Vector{RegistryData},
458-
regpaths::Vector{String},
459398
status::ReturnStatus)
460399
if haskey(pkg.compat, "julia")
461-
if Base.VERSION >= v"1.7-"
462-
ver = pkg.compat["julia"].val
463-
else
464-
ver = Pkg.Types.semver_spec(pkg.compat["julia"])
465-
end
466-
if any(map(x -> !isempty(intersect(Pkg.Types.VersionRange("0-0.6"), x)), ver.ranges))
400+
ver = Pkg.Types.semver_spec(pkg.compat["julia"])
401+
if any(map(x -> !isempty(intersect(VersionRange("0-0.6"), x)), ver.ranges))
467402
err = :julia_before_07_in_compat
468403
@debug(err)
469404
add!(status, err)
@@ -476,9 +411,9 @@ function check_compat!(pkg::Pkg.Types.Project,
476411
# entries not mentioned in deps, nor in extras.
477412
invalid_compats = []
478413
for name in keys(pkg.compat)
479-
indeps = haskey(getdeps(pkg), name)
480-
inextras = haskey(pkg.extras, name)
481-
inweaks = PKG_HAS_WEAK ? haskey(pkg.weakdeps, name) : false
414+
indeps = haskey(pkg.deps, name)
415+
inextras = haskey(pkg.extras, name)
416+
inweaks = haskey(pkg.weakdeps, name)
482417
if !(indeps || inextras || inweaks || name == "julia")
483418
push!(invalid_compats, name)
484419
end
@@ -500,11 +435,11 @@ function check_compat!(pkg::Pkg.Types.Project,
500435
# anyway.
501436
for name in keys(pkg.compat)
502437
if name != "julia"
503-
indeps = haskey(getdeps(pkg), name)
438+
indeps = haskey(pkg.deps, name)
504439
inextras = haskey(pkg.extras, name)
505440

506441
if indeps
507-
uuidofdep = string(getdeps(pkg)[name])
442+
uuidofdep = string(pkg.deps[name])
508443
findpackageerror!(name, uuidofdep, regdata, status)
509444
elseif inextras
510445
uuidofdep = string(pkg.extras[name])
@@ -520,14 +455,12 @@ end
520455

521456
# See the comments for `update_deps_file` for the rationale for the
522457
# `old_versions` argument.
523-
function update_compat_file(pkg::Pkg.Types.Project,
458+
function update_compat_file(pkg::Project,
524459
package_path::AbstractString,
525460
old_versions::Vector{VersionNumber})
526461
@debug("update package data: compat file")
527462

528-
file_depses = [("Compat.toml", getdeps(pkg))]
529-
PKG_HAS_WEAK && push!(file_depses, ("WeakCompat.toml", pkg.weakdeps))
530-
for (file, deps) in file_depses
463+
for (file, deps) in [("Compat.toml", pkg.deps), ("WeakCompat.toml", pkg.weakdeps)]
531464
compat_file = joinpath(package_path, file)
532465
if isfile(compat_file)
533466
compat_data = Compress.load(compat_file, old_versions)
@@ -546,11 +479,8 @@ function update_compat_file(pkg::Pkg.Types.Project,
546479
continue
547480
end
548481

549-
if Base.VERSION >= v"1.7-"
550-
spec = version.val
551-
else
552-
spec = Pkg.Types.semver_spec(version)
553-
end
482+
spec = Pkg.Types.semver_spec(version)
483+
554484
# The call to `map(versionrange, )` can be removed
555485
# once Pkg is updated to a version including
556486
# https://github.com/JuliaLang/Pkg.jl/pull/1181
@@ -568,6 +498,7 @@ function update_compat_file(pkg::Pkg.Types.Project,
568498
end
569499

570500
function get_registrator_tree_sha()
501+
# TODO: Rewrite this to not use as much Pkg internals
571502
# If Registrator is in the manifest, return its tree-sha.
572503
# Otherwise return the tree-sha for RegistryTools.
573504
manifest = Pkg.Types.Context().env.manifest
@@ -581,7 +512,7 @@ function get_registrator_tree_sha()
581512
return "unknown"
582513
end
583514

584-
function check_and_update_registry_files(pkg, package_repo, tree_hash,
515+
function check_and_update_registry_files(pkg::Project, package_repo, tree_hash,
585516
registry_path, registry_deps_paths,
586517
status; subdir = "")
587518
# find package in registry
@@ -617,8 +548,7 @@ function check_and_update_registry_files(pkg, package_repo, tree_hash,
617548

618549
# update package data: compat file
619550
@debug("check compat section")
620-
regpaths = [registry_path; registry_deps_paths]
621-
check_compat!(pkg, regdata, regpaths, status)
551+
check_compat!(pkg, regdata, status)
622552
haserror(status) && return
623553
update_compat_file(pkg, package_path, old_versions)
624554
end
@@ -633,7 +563,7 @@ errors or warnings that occurred.
633563
# Arguments
634564
635565
* `package_repo::AbstractString`: The git repository URL for the package to be registered. If empty, keep the stored repository URL.
636-
* `pkg::Pkg.Types.Project`: the parsed (Julia)Project.toml file for the package to be registered
566+
* `pkg::String`: the path of the project file for the package to be registered
637567
* `tree_hash::AbstractString`: the tree hash (not commit hash) of the package revision to be registered
638568
639569
# Keyword Arguments
@@ -647,7 +577,7 @@ errors or warnings that occurred.
647577
* `gitconfig::Dict=Dict()`: dictionary of configuration options for the `git` command
648578
"""
649579
function register(
650-
package_repo::AbstractString, pkg::Pkg.Types.Project, tree_hash::AbstractString;
580+
package_repo::AbstractString, pkg::Union{String, Project}, tree_hash::AbstractString;
651581
registry::AbstractString = DEFAULT_REGISTRY_URL,
652582
registry_fork::AbstractString = registry,
653583
registry_deps::Vector{<:AbstractString} = AbstractString[],
@@ -659,6 +589,10 @@ function register(
659589
cache::RegistryCache=REGISTRY_CACHE,
660590
gitconfig::Dict = Dict()
661591
)
592+
if pkg isa String
593+
pkg = Project(pkg)
594+
end
595+
662596
# get info from package registry
663597
@debug("get info from package registry")
664598
if !isempty(package_repo)
@@ -736,44 +670,14 @@ function register(
736670
return set_metadata!(regbr, status)
737671
end
738672

739-
struct RegisterParams
740-
package_repo::String
741-
pkg::Pkg.Types.Project
742-
tree_sha::String
743-
registry::String
744-
registry_fork::String
745-
registry_deps::Vector{<:String}
746-
subdir::String
747-
push::Bool
748-
gitconfig::Dict
749-
750-
function RegisterParams(package_repo::AbstractString,
751-
pkg::Pkg.Types.Project,
752-
tree_sha::AbstractString;
753-
registry::AbstractString=DEFAULT_REGISTRY_URL,
754-
registry_fork::AbstractString=registry,
755-
registry_deps::Vector{<:AbstractString}=[],
756-
subdir::AbstractString="",
757-
push::Bool=false,
758-
gitconfig::Dict=Dict(),)
759-
new(package_repo, pkg, tree_sha, registry, registry_fork,
760-
registry_deps, subdir, push, gitconfig,)
761-
end
762-
end
763-
764-
register(regp::RegisterParams) = register(regp.package_repo, regp.pkg, regp.tree_sha;
765-
registry=regp.registry, registry_fork=regp.registry_fork,
766-
registry_deps=regp.registry_deps,
767-
subdir=regp.subdir, push=regp.push, gitconfig=regp.gitconfig,)
768-
769673
"""
770674
find_registered_version(pkg, registry_path)
771675
772676
If the package and version specified by `pkg` exists in the registry
773677
at `registry_path`, return its tree hash. Otherwise return the empty
774678
string.
775679
"""
776-
function find_registered_version(pkg::Pkg.Types.Project,
680+
function find_registered_version(pkg::Project,
777681
registry_path::AbstractString)
778682
registry_file = joinpath(registry_path, "Registry.toml")
779683
registry_data = parse_registry(registry_file)

src/types.jl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ function get_registry_default_branch(git::Cmd)
2828
strip(split(lines[idx], ":")[2])
2929
end
3030

31+
struct Project
32+
name::Union{Nothing, String}
33+
uuid::Union{Nothing, UUID}
34+
version::Union{Nothing, VersionNumber}
35+
deps::Dict{String, String}
36+
weakdeps::Dict{String, String}
37+
compat::Dict{String, String}
38+
extras::Dict{String, String}
39+
end
40+
41+
Project(project_file::String) = Project(isfile(project_file) ? TOML.parsefile(project_file) : Dict())
42+
function Project(d::Dict)
43+
name = get(d, "name", nothing)
44+
uuid = get(d, "uuid", nothing)
45+
uuid !== nothing && (uuid = UUID(uuid))
46+
version = get(d, "version", nothing)
47+
version !== nothing && (version = VersionNumber(version))
48+
deps = get(Dict, d, "deps")
49+
weakdeps = get(Dict, d, "weakdeps")
50+
compat = get(Dict, d, "compat")
51+
extras = get(Dict, d, "extras")
52+
Project(name, uuid, version, deps, weakdeps, compat, extras)
53+
end
54+
3155
"""
3256
RegEdit.get_registry(registry_url)
3357
@@ -160,7 +184,7 @@ end
160184

161185
# Not using `joinpath` here since we don't want backslashes in
162186
# Registry.toml when running on Windows.
163-
function package_relpath(pkg::Pkg.Types.Project)
187+
function package_relpath(pkg::Project)
164188
return package_relpath(pkg.name)
165189
end
166190
function package_relpath(pkg_name::AbstractString)
@@ -169,7 +193,7 @@ function package_relpath(pkg_name::AbstractString)
169193
return join(path_components, path_separator)
170194
end
171195

172-
function Base.push!(reg::RegistryData, pkg::Pkg.Types.Project)
196+
function Base.push!(reg::RegistryData, pkg::Project)
173197
reg.packages[string(pkg.uuid)] = Dict(
174198
"name" => pkg.name, "path" => package_relpath(pkg)
175199
)

0 commit comments

Comments
 (0)