Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to generate #2492

Merged
merged 9 commits into from
May 25, 2021
6 changes: 5 additions & 1 deletion src/Pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,11 @@ See also [`PackageSpec`](@ref)
"""
const develop = API.develop

# TODO: Will probably be deprecated for something in PkgDev
"""
Pkg.generate(pkgname::String)

Create a minimal project called `pkgname` in the current folder. For more featureful package creation, please see `PkgTemplates.jl`.
"""
const generate = API.generate

"""
Expand Down
4 changes: 2 additions & 2 deletions src/REPLMode/command_declarations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,14 @@ packages will not be upgraded at all.
""",
],
PSA[:name => "generate",
:api => API.generate_deprecated,
:api => API.generate,
:arg_count => 1 => 1,
:arg_parser => ((x,y) -> map(expanduser, unwrap(x))),
:description => "generate files for a new project",
:help => md"""
generate pkgname

Create a project called `pkgname` in the current folder.
Create a minimal project called `pkgname` in the current folder. For more featureful package creation, please see `PkgTemplates.jl`.
Copy link
Sponsor Member

@johnnychen94 johnnychen94 Apr 27, 2021

Choose a reason for hiding this comment

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

I prefer to not ship this redirection in the docstring, but instead in the documentation. We have no idea whether it is still PkgTemplates.jl five years later. If someone is still using Julia 1.7 five years later for unknown reasons, he doesn't get directed to the wrong place then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, but keep in mind that currently, ] generate is deprecated in favor of PkgTemplates.jl with the deprecation warning: Pkg.generate is deprecated. Please use PkgTemplates.jl instead.

Incidentally, I think that makes a good case for having this "core" functionality built-in to Pkg.jl.

""",
],
PSA[:name => "precompile",
Expand Down
40 changes: 18 additions & 22 deletions src/generate.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

function generate_deprecated(varargs...; kwargs...)
Base.depwarn("Pkg.generate is deprecated. Please use PkgTemplates.jl instead.", Symbol("Pkg.generate"))
return generate(varargs...; kwargs...)
end

generate(path::String; kwargs...) = generate(Context(), path; kwargs...)
function generate(ctx::Context, path::String; kwargs...)
Context!(ctx; kwargs...)
dir, pkg = dirname(path), basename(path)
function generate(path::String; io::IO=DEFAULT_IO[])
base = basename(path)
pkg = endswith(lowercase(base), ".jl") ? chop(base, tail=3) : base
Base.isidentifier(pkg) || pkgerror("$(repr(pkg)) is not a valid package name")
isdir(path) && pkgerror("$(abspath(path)) already exists")
printpkgstyle(ctx.io, :Generating, " project $pkg:")
uuid = project(ctx, pkg, dir)
entrypoint(ctx, pkg, dir)
printpkgstyle(io, :Generating, " project $pkg:")
uuid = project(io, pkg, path)
entrypoint(io, pkg, path)
return Dict(pkg => uuid)
end

function genfile(f::Function, ctx::Context, pkg::String, dir::String, file::String)
path = joinpath(dir, pkg, file)
println(ctx.io, " $(Base.contractuser(path))")
function genfile(f::Function, io::IO, dir::AbstractString, file::AbstractString)
path = joinpath(dir, file)
println(io, " $(Base.contractuser(path))")
mkpath(dirname(path))
open(f, path, "w")
return
end

function project(ctx::Context, pkg::String, dir::String)
function project(io::IO, pkg::AbstractString, dir::AbstractString)
mkpath(dir)

name = email = nothing
gitname = LibGit2.getconfig("user.name", "")
isempty(gitname) || (name = gitname)
Expand All @@ -51,24 +47,24 @@ function project(ctx::Context, pkg::String, dir::String)
authors = ["$name " * (email === nothing ? "" : "<$email>")]

uuid = UUIDs.uuid4()
genfile(ctx, pkg, dir, "Project.toml") do io
genfile(io, dir, "Project.toml") do file_io
toml = Dict{String,Any}("authors" => authors,
"name" => pkg,
"uuid" => string(uuid),
"version" => "0.1.0",
)
TOML.print(io, toml, sorted=true, by=key -> (Types.project_key_order(key), key))
TOML.print(file_io, toml, sorted=true, by=key -> (Types.project_key_order(key), key))
end
return uuid
end

function entrypoint(ctx::Context, pkg::String, dir)
genfile(ctx, pkg, dir, "src/$pkg.jl") do io
print(io,
function entrypoint(io::IO, pkg::AbstractString, dir)
genfile(io, dir, "src/$pkg.jl") do file_io
print(file_io,
"""
module $pkg

greet() = print("Hello World!")
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Why change this? I like having some function in here that does something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just because of this comment. Personally, I always found greet() = print("Hello World!") a bit weird, but I have no strong preference one way or the other.

Copy link

Choose a reason for hiding this comment

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

this function should not remove, there is nothing weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I reverted it

Choose a reason for hiding this comment

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

boo 😔😔😔😔😔

Copy link
Contributor Author

@pazner pazner May 12, 2021

Choose a reason for hiding this comment

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

Can't satisfy everyone 😄. We can do an emoji vote here to decide.

Choose a reason for hiding this comment

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

put it in the main issue this is buried

# module contents

end # module
"""
Expand Down
2 changes: 1 addition & 1 deletion test/new.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,7 @@ end
isolate() do
Pkg.REPLMode.TEST_MODE[] = true
api, arg, opts = first(Pkg.pkg"generate Foo")
@test api == Pkg.API.generate_deprecated
@test api == Pkg.API.generate
@test arg == "Foo"
@test isempty(opts)
mktempdir() do dir
Expand Down