Skip to content

Instantly share code, notes, and snippets.

@sjkelly
Created June 23, 2019 01:02
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 sjkelly/47c9de1b21658ec78def097e8975cf0a to your computer and use it in GitHub Desktop.
Save sjkelly/47c9de1b21658ec78def097e8975cf0a to your computer and use it in GitHub Desktop.
Generate Project.toml Julia (for old Pkg)
# https://discourse.julialang.org/t/convert-require-to-project-toml-and-manifest-toml/17775/2
"""
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())
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))
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment