Skip to content

Commit

Permalink
stop using Pkg.Types.Project (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristoffer Carlsson committed Dec 20, 2022
1 parent 9c419c5 commit cda894b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 170 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RegistryTools"
uuid = "d1eb7eb1-105f-429d-abf5-b0f65cb9e2c4"
authors = ["Stefan Karpinski <[email protected]>"]
version = "1.9.1"
version = "2.0.0"

[deps]
AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
[![codecov](https://codecov.io/gh/JuliaRegistries/RegistryTools.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaRegistries/RegistryTools.jl)

Functionality for modifying Julia registry files.

#### Updating to 2.0

RegistryTools.jl changed the API in 2.0 from taking a `Pkg.Types.Project` to just taking a path to a project file instead.
8 changes: 0 additions & 8 deletions src/RegistryTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ using Pkg: Pkg, TOML, GitTools
using UUIDs

const DEFAULT_REGISTRY_URL = "https://github.com/JuliaRegistries/General"
const PKG_HAS_WEAK = hasfield(Pkg.Types.Project, :_deps_weak)

function __init__()
if !PKG_HAS_WEAK
@warn "Running Registrator on a Julia version that does not support weak dependencies. " *
"Weak dependencies will not be registered."
end
end

include("Compress.jl")
include("builtin_pkgs.jl")
Expand Down
158 changes: 31 additions & 127 deletions src/register.jl
Original file line number Diff line number Diff line change
@@ -1,59 +1,3 @@
"""
Given a remote repo URL and a git tree spec, get a `Project` object
for the project file in that tree and a hash string for the tree.
"""
# function get_project(remote_url::AbstractString, tree_spec::AbstractString)
# # TODO?: use raw file downloads for GitHub/GitLab
# mktempdir(mkpath("packages")) do tmp
# # bare clone the package repo
# @debug("bare clone the package repo")
# repo = LibGit2.clone(remote_url, joinpath(tmp, "repo"), isbare=true)
# tree = try
# LibGit2.GitObject(repo, tree_spec)
# catch err
# err isa LibGit2.GitError && err.code == LibGit2.Error.ENOTFOUND || rethrow(err)
# error("$remote_url: git object $(repr(tree_spec)) could not be found")
# end
# tree isa LibGit2.GitTree || (tree = LibGit2.peel(LibGit2.GitTree, tree))
#
# # check out the requested tree
# @debug("check out the requested tree")
# tree_path = abspath(tmp, "tree")
# GC.@preserve tree_path begin
# opts = LibGit2.CheckoutOptions(
# checkout_strategy = LibGit2.Consts.CHECKOUT_FORCE,
# target_directory = Base.unsafe_convert(Cstring, tree_path)
# )
# LibGit2.checkout_tree(repo, tree, options=opts)
# end
#
# # look for a project file in the tree
# @debug("look for a project file in the tree")
# project_file = Pkg.Types.projectfile_path(tree_path)
# project_file !== nothing && isfile(project_file) ||
# error("$remote_url: git tree $(repr(tree_spec)) has no project file")
#
# # parse the project file
# @debug("parse the project file")
# project = Pkg.Types.read_project(project_file)
# project.name === nothing &&
# error("$remote_url $(repr(tree_spec)): package has no name")
# project.uuid === nothing &&
# error("$remote_url $(repr(tree_spec)): package has no UUID")
# project.version === nothing &&
# error("$remote_url $(repr(tree_spec)): package has no version")
#
# return project, string(LibGit2.GitHash(tree))
# end
# end

function getdeps(pkg)
if PKG_HAS_WEAK
return merge(pkg.deps, pkg._deps_weak)
end
return pkg.deps
end

# These can compromise the integrity of the registry and cannot be
# opted out of.
const mandatory_errors = [:version_exists,
Expand Down Expand Up @@ -105,7 +49,7 @@ struct RegBranch

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

function RegBranch(pkg::Pkg.Types.Project, branch::AbstractString)
function RegBranch(pkg::Project, branch::AbstractString)
new(pkg.name, pkg.version, branch, Dict{String,Any}())
end
end
Expand Down Expand Up @@ -282,7 +226,7 @@ function versionrange(lo::VersionBound, hi::VersionBound)
return VersionRange(lo, hi)
end

function find_package_in_registry(pkg::Pkg.Types.Project,
function find_package_in_registry(pkg::Project,
registry_file::AbstractString,
registry_path::AbstractString,
registry_data::RegistryData,
Expand Down Expand Up @@ -346,7 +290,7 @@ function check_package!(package_repo::AbstractString,
return package_repo
end

function update_package_file(pkg::Pkg.Types.Project,
function update_package_file(pkg::Project,
package_repo::AbstractString,
subdir::AbstractString,
package_path::AbstractString)
Expand All @@ -371,15 +315,15 @@ function get_versions_file(package_path::AbstractString)
return filename, data
end

function check_versions!(pkg::Pkg.Types.Project,
function check_versions!(pkg::Project,
versions_data::Dict{String, Any},
status::ReturnStatus)
versions = sort!([VersionNumber(v) for v in keys(versions_data)])
check_version!(pkg.version, versions, status)
return versions
end

function update_versions_file(pkg::Pkg.Types.Project,
function update_versions_file(pkg::Project,
versions_file::AbstractString,
versions_data::Dict{String, Any},
tree_hash::AbstractString)
Expand Down Expand Up @@ -408,12 +352,10 @@ function update_versions_file(pkg::Pkg.Types.Project,
end
end

function check_deps!(pkg::Pkg.Types.Project,
function check_deps!(pkg::Project,
regdata::Vector{RegistryData},
status::ReturnStatus)
depses = [getdeps(pkg)]
PKG_HAS_WEAK && push!(depses, pkg.weakdeps)
for deps in depses
for deps in [pkg.deps, pkg.weakdeps]
if pkg.name in keys(deps)
err = :package_self_dep
@debug(err)
Expand All @@ -433,12 +375,10 @@ end
# Versions.toml after update. This is handled with the `old_versions'
# argument and the assumption that Versions.toml has been updated with
# the new version before calling this function.
function update_deps_file(pkg::Pkg.Types.Project,
function update_deps_file(pkg::Project,
package_path::AbstractString,
old_versions::Vector{VersionNumber})
file_depses = [("Deps.toml", getdeps(pkg))]
PKG_HAS_WEAK && push!(file_depses, ("WeakDeps.toml", pkg.weakdeps))
for (file, deps) in file_depses
for (file, deps) in [("Deps.toml", pkg.deps), ("WeakDeps.toml", pkg.weakdeps)]
deps_file = joinpath(package_path, file)
if isfile(deps_file)
deps_data = Compress.load(deps_file, old_versions)
Expand All @@ -453,17 +393,12 @@ function update_deps_file(pkg::Pkg.Types.Project,
end
end

function check_compat!(pkg::Pkg.Types.Project,
function check_compat!(pkg::Project,
regdata::Vector{RegistryData},
regpaths::Vector{String},
status::ReturnStatus)
if haskey(pkg.compat, "julia")
if Base.VERSION >= v"1.7-"
ver = pkg.compat["julia"].val
else
ver = Pkg.Types.semver_spec(pkg.compat["julia"])
end
if any(map(x -> !isempty(intersect(Pkg.Types.VersionRange("0-0.6"), x)), ver.ranges))
ver = Pkg.Types.semver_spec(pkg.compat["julia"])
if any(map(x -> !isempty(intersect(VersionRange("0-0.6"), x)), ver.ranges))
err = :julia_before_07_in_compat
@debug(err)
add!(status, err)
Expand All @@ -476,9 +411,9 @@ function check_compat!(pkg::Pkg.Types.Project,
# entries not mentioned in deps, nor in extras.
invalid_compats = []
for name in keys(pkg.compat)
indeps = haskey(getdeps(pkg), name)
inextras = haskey(pkg.extras, name)
inweaks = PKG_HAS_WEAK ? haskey(pkg.weakdeps, name) : false
indeps = haskey(pkg.deps, name)
inextras = haskey(pkg.extras, name)
inweaks = haskey(pkg.weakdeps, name)
if !(indeps || inextras || inweaks || name == "julia")
push!(invalid_compats, name)
end
Expand All @@ -500,11 +435,11 @@ function check_compat!(pkg::Pkg.Types.Project,
# anyway.
for name in keys(pkg.compat)
if name != "julia"
indeps = haskey(getdeps(pkg), name)
indeps = haskey(pkg.deps, name)
inextras = haskey(pkg.extras, name)

if indeps
uuidofdep = string(getdeps(pkg)[name])
uuidofdep = string(pkg.deps[name])
findpackageerror!(name, uuidofdep, regdata, status)
elseif inextras
uuidofdep = string(pkg.extras[name])
Expand All @@ -520,14 +455,12 @@ end

# See the comments for `update_deps_file` for the rationale for the
# `old_versions` argument.
function update_compat_file(pkg::Pkg.Types.Project,
function update_compat_file(pkg::Project,
package_path::AbstractString,
old_versions::Vector{VersionNumber})
@debug("update package data: compat file")

file_depses = [("Compat.toml", getdeps(pkg))]
PKG_HAS_WEAK && push!(file_depses, ("WeakCompat.toml", pkg.weakdeps))
for (file, deps) in file_depses
for (file, deps) in [("Compat.toml", pkg.deps), ("WeakCompat.toml", pkg.weakdeps)]
compat_file = joinpath(package_path, file)
if isfile(compat_file)
compat_data = Compress.load(compat_file, old_versions)
Expand All @@ -546,11 +479,8 @@ function update_compat_file(pkg::Pkg.Types.Project,
continue
end

if Base.VERSION >= v"1.7-"
spec = version.val
else
spec = Pkg.Types.semver_spec(version)
end
spec = Pkg.Types.semver_spec(version)

# The call to `map(versionrange, )` can be removed
# once Pkg is updated to a version including
# https://github.com/JuliaLang/Pkg.jl/pull/1181
Expand All @@ -568,6 +498,7 @@ function update_compat_file(pkg::Pkg.Types.Project,
end

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

function check_and_update_registry_files(pkg, package_repo, tree_hash,
function check_and_update_registry_files(pkg::Project, package_repo, tree_hash,
registry_path, registry_deps_paths,
status; subdir = "")
# find package in registry
Expand Down Expand Up @@ -617,8 +548,7 @@ function check_and_update_registry_files(pkg, package_repo, tree_hash,

# update package data: compat file
@debug("check compat section")
regpaths = [registry_path; registry_deps_paths]
check_compat!(pkg, regdata, regpaths, status)
check_compat!(pkg, regdata, status)
haserror(status) && return
update_compat_file(pkg, package_path, old_versions)
end
Expand All @@ -633,7 +563,7 @@ errors or warnings that occurred.
# Arguments
* `package_repo::AbstractString`: The git repository URL for the package to be registered. If empty, keep the stored repository URL.
* `pkg::Pkg.Types.Project`: the parsed (Julia)Project.toml file for the package to be registered
* `pkg::String`: the path of the project file for the package to be registered
* `tree_hash::AbstractString`: the tree hash (not commit hash) of the package revision to be registered
# Keyword Arguments
Expand All @@ -647,7 +577,7 @@ errors or warnings that occurred.
* `gitconfig::Dict=Dict()`: dictionary of configuration options for the `git` command
"""
function register(
package_repo::AbstractString, pkg::Pkg.Types.Project, tree_hash::AbstractString;
package_repo::AbstractString, pkg::Union{String, Project}, tree_hash::AbstractString;
registry::AbstractString = DEFAULT_REGISTRY_URL,
registry_fork::AbstractString = registry,
registry_deps::Vector{<:AbstractString} = AbstractString[],
Expand All @@ -659,6 +589,10 @@ function register(
cache::RegistryCache=REGISTRY_CACHE,
gitconfig::Dict = Dict()
)
if pkg isa String
pkg = Project(pkg)
end

# get info from package registry
@debug("get info from package registry")
if !isempty(package_repo)
Expand Down Expand Up @@ -736,44 +670,14 @@ function register(
return set_metadata!(regbr, status)
end

struct RegisterParams
package_repo::String
pkg::Pkg.Types.Project
tree_sha::String
registry::String
registry_fork::String
registry_deps::Vector{<:String}
subdir::String
push::Bool
gitconfig::Dict

function RegisterParams(package_repo::AbstractString,
pkg::Pkg.Types.Project,
tree_sha::AbstractString;
registry::AbstractString=DEFAULT_REGISTRY_URL,
registry_fork::AbstractString=registry,
registry_deps::Vector{<:AbstractString}=[],
subdir::AbstractString="",
push::Bool=false,
gitconfig::Dict=Dict(),)
new(package_repo, pkg, tree_sha, registry, registry_fork,
registry_deps, subdir, push, gitconfig,)
end
end

register(regp::RegisterParams) = register(regp.package_repo, regp.pkg, regp.tree_sha;
registry=regp.registry, registry_fork=regp.registry_fork,
registry_deps=regp.registry_deps,
subdir=regp.subdir, push=regp.push, gitconfig=regp.gitconfig,)

"""
find_registered_version(pkg, registry_path)
If the package and version specified by `pkg` exists in the registry
at `registry_path`, return its tree hash. Otherwise return the empty
string.
"""
function find_registered_version(pkg::Pkg.Types.Project,
function find_registered_version(pkg::Project,
registry_path::AbstractString)
registry_file = joinpath(registry_path, "Registry.toml")
registry_data = parse_registry(registry_file)
Expand Down
28 changes: 26 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ function get_registry_default_branch(git::Cmd)
strip(split(lines[idx], ":")[2])
end

struct Project
name::Union{Nothing, String}
uuid::Union{Nothing, UUID}
version::Union{Nothing, VersionNumber}
deps::Dict{String, String}
weakdeps::Dict{String, String}
compat::Dict{String, String}
extras::Dict{String, String}
end

Project(project_file::String) = Project(isfile(project_file) ? TOML.parsefile(project_file) : Dict())
function Project(d::Dict)
name = get(d, "name", nothing)
uuid = get(d, "uuid", nothing)
uuid !== nothing && (uuid = UUID(uuid))
version = get(d, "version", nothing)
version !== nothing && (version = VersionNumber(version))
deps = get(Dict, d, "deps")
weakdeps = get(Dict, d, "weakdeps")
compat = get(Dict, d, "compat")
extras = get(Dict, d, "extras")
Project(name, uuid, version, deps, weakdeps, compat, extras)
end

"""
RegEdit.get_registry(registry_url)
Expand Down Expand Up @@ -160,7 +184,7 @@ end

# Not using `joinpath` here since we don't want backslashes in
# Registry.toml when running on Windows.
function package_relpath(pkg::Pkg.Types.Project)
function package_relpath(pkg::Project)
return package_relpath(pkg.name)
end
function package_relpath(pkg_name::AbstractString)
Expand All @@ -169,7 +193,7 @@ function package_relpath(pkg_name::AbstractString)
return join(path_components, path_separator)
end

function Base.push!(reg::RegistryData, pkg::Pkg.Types.Project)
function Base.push!(reg::RegistryData, pkg::Project)
reg.packages[string(pkg.uuid)] = Dict(
"name" => pkg.name, "path" => package_relpath(pkg)
)
Expand Down
Loading

2 comments on commit cda894b

@KristofferC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/74416

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.0.0 -m "<description of version>" cda894b9aba905af75d256169648f60821b5f334
git push origin v2.0.0

Please sign in to comment.