Skip to content

Instantly share code, notes, and snippets.

@ExpandingMan
Created June 2, 2018 19:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ExpandingMan/41d1b3a07987a5cccc1cfab44e3322ab to your computer and use it in GitHub Desktop.
Save ExpandingMan/41d1b3a07987a5cccc1cfab44e3322ab to your computer and use it in GitHub Desktop.
a quick and dirty tool for generating `Project.toml`
module GenProject
using Pkg
export genproject
requirefile(dir::AbstractString) = joinpath(dir, "REQUIRE")
function openrequire(dir::AbstractString)
fname = requirefile(dir)
isfile(fname) || throw(ErrorException("Directory $dir lacks a REQUIRE file"))
open(fname)
end
function deplist(dir::AbstractString; use_latest::Bool=true)
f = openrequire(dir)
pkgs = Any[]
for line ∈ eachline(f)
pkg = split(line)
first(pkg) == "julia" && continue
if length(pkg) > 1 && !use_latest
push!(pkgs, Pkg.PackageSpec(first(pkg), VersionNumber(last(pkg))))
else
push!(pkgs, Pkg.PackageSpec(first(pkg)))
end
end
pkgs
end
adddeps(dps::AbstractVector) = foreach(Pkg.add, dps)
adddeps(dir::AbstractString; use_latest::Bool=true) = adddeps(deplist(dir, use_latest=use_latest))
function genproject(dir::AbstractString; use_latest::Bool=true)
Pkg.init(Pkg.Context(), dir)
adddeps(dir, use_latest=use_latest)
end
genproject(;use_latest::Bool=true) = genproject(pwd(), use_latest=use_latest)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment