Skip to content

Instantly share code, notes, and snippets.

@jw3126 jw3126/release.jl
Created Apr 28, 2019

Embed
What would you like to do?
Tag and release package to local registry.
using LibGit2
using ArgCheck
using Pkg: TOML
import Pkg
import Registrator
struct Release
package::Module
registry::String
check_master::Bool
run_tests::Bool
end
function Release(pkg; registry="ToyRegistry",
check_master=true,
run_tests=true,
)
Release(pkg, registry, check_master, run_tests)
end
function devpath(m::Module)
path = joinpath(dirname(pathof(m)), "..")
normpath(path)
end
function package_repo(r::Release)
path = devpath(r.package)
GitRepo(path)
end
function registry_path(r::Release)
joinpath(DEPOT_PATH[1], "registries", r.registry)
end
function registry_repo(r::Release)
path = registry_path(r)
GitRepo(path)
end
function pre_register(r::Release)
@info "Preparing registration."
@info "Updating registry"
Pkg.Registry.update(r.registry)
@info "Checking registry repo tidy."
reg_repo = registry_repo(r)
@check !LibGit2.isdirty(reg_repo)
if r.check_master
@check LibGit2.branch(reg_repo) == "master"
end
@info "Checking package repo tidy."
pkg_repo = package_repo(r)
@check !LibGit2.isdirty(pkg_repo)
if r.check_master
@check LibGit2.branch(pkg_repo) == "master"
end
vtag = "v" * get_project_toml_tag(r.package)
@info "Checking proposed git tag $vtag does not yet exist."
vtags = LibGit2.tag_list(pkg_repo)
if vtag in vtags
error("Git tag $vtag exists already.")
end
if r.run_tests
@info "Running package test suite."
Pkg.API.test(string(nameof(r.package)))
else
@info "Skipping package test suite."
end
@info "Pushing package before registration"
LibGit2.push(pkg_repo, remote="origin", refspecs=["refs/heads/master"])
end
function get_project_toml_tag(package::Module)
tomlpath = joinpath(devpath(package), "Project.toml")
@check ispath(tomlpath)
d = TOML.parsefile(tomlpath)
if !haskey(d, "version")
error("Cannot find version in $tomlpath")
end
d["version"]
end
function release(r::Release)
pre_register(r)
@info "Commiting release to registry"
Registrator.register(r.package, registry_path(r))
post_register(r)
@info "Release finished successful!"
r
end
function post_register(r::Release)
repo = package_repo(r)
vtag = get_project_toml_tag(r.package)
@info "Adding git tag $vtag to $(r.package)"
LibGit2.tag_create(repo, vtag, "master")
@info "Pushing package"
LibGit2.push(repo, remote="origin", refspecs=["refs/tags/$vtag", "refs/heads/master"])
@info "Pushing registry"
repo = registry_repo(r)
LibGit2.push(repo, remote="origin", refspecs=["refs/heads/master"])
end
using ToyPkg
r = Release(ToyPkg)
release(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.