Skip to content

Instantly share code, notes, and snippets.

@astadmistry
Last active September 3, 2020 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astadmistry/cbaba31771a8b20bed4ca163cf65061a to your computer and use it in GitHub Desktop.
Save astadmistry/cbaba31771a8b20bed4ca163cf65061a to your computer and use it in GitHub Desktop.
import Pkg
import Base: SHA1
using SHA, UUIDs
function uuid5(namespace::UUID, key::String)
data = [reinterpret(UInt8, [namespace.value]); codeunits(key)]
u = reinterpret(UInt128, sha1(data)[1:16])[1]
u &= 0xffffffffffff0fff3fffffffffffffff
u |= 0x00000000000050008000000000000000
return UUID(u)
end
uuid5(namespace::UUID, key::AbstractString) = uuid5(namespace, String(key))
const uuid_dns = UUID(0x6ba7b810_9dad_11d1_80b4_00c04fd430c8)
const uuid_julia_project = uuid5(uuid_dns, "julialang.org")
const uuid_package = uuid5(uuid_julia_project, "package")
"""
generate_project_toml([name::String])
Generate Project.toml file for the existing current project at `\$PWD`.
It activates the generated Project.toml and then adds packages based on
REQUIRE file.
"""
function generate_project_toml(name::AbstractString=guess_project_name())
pkg_UUID = uuid5(uuid_package, name)
if isfile("Project.toml")
error("Project.toml exists.")
end
if !isfile("REQUIRE")
error("REQUIRE file not found.")
end
deps = read_require("REQUIRE")
pkg = Base.identify_package(name)
project_toml = """
name = $(repr(name))
uuid = "$(pkg_UUID)"
"""
write("Project.toml", project_toml)
@info "Activating \$PWD (to run `Pkg.add`)"
Pkg.activate(pwd())
Pkg.add(deps)
end
"""
guess_project_name() :: String
Guess project name using Git. It works even in worktree repository.
"""
function guess_project_name()
path = abspath(rstrip(read(`git rev-parse --git-dir`, String)))
prev = ""
while path != prev
if basename(path) == ".git"
return basename(dirname(path))[1:end - 3]
end
prev, path = path, dirname(path)
end
error("Project not found")
end
function read_require(path)
deps = String[]
for line in readlines(path)
name, = split(line)
if name == "julia"
continue
end
push!(deps, name)
end
return deps
end
#=
REf:
https://discourse.julialang.org/t/convert-require-to-project-toml-and-manifest-toml/17775/3
https://github.com/JuliaLang/Pkg.jl/commit/34ec22f5405ffc7f8f25a4b4f7178e887f718051
https://github.com/JuliaLang/Pkg.jl/blob/34ec22f5405ffc7f8f25a4b4f7178e887f718051/src/Types.jl
https://github.com/JuliaLang/Pkg.jl/search?p=1&q=gen_project.jl&type=Issues
https://github.com/JuliaLang/Pkg.jl/issues/223
https://github.com/JuliaLang/Pkg.jl/issues/1886
https://github.com/JuliaLang/Pkg.jl/issues/1632
=#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment