Skip to content

Commit

Permalink
fix preview of generate so it does not create any files (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Jun 12, 2018
1 parent 529761a commit 1e83aaa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion stdlib/Pkg/src/REPLMode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ function do_generate!(ctx::Context, tokens::Vector{Token})
cmderror("`generate` takes a name of the project to create")
end
end
API.generate(pkg)
API.generate(ctx, pkg)
end

function do_precompile!(ctx::Context, tokens::Vector{Token})
Expand Down
21 changes: 13 additions & 8 deletions stdlib/Pkg/src/generate.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

function generate(path::String)
generate(path::String; kwargs...) = generate(Context(), path; kwargs...)
function generate(ctx::Context, path::String; kwargs...)
Context!(ctx; kwargs...)
ctx.preview && preview_info()
dir, pkg = dirname(path), basename(path)
isdir(path) && cmderror("$(abspath(path)) already exists")
printstyled("Generating"; color=:green, bold=true)
print(" project $pkg:\n")
project(pkg, dir)
entrypoint(pkg, dir)
project(pkg, dir; preview=ctx.preview)
entrypoint(pkg, dir; preview=ctx.preview)
ctx.preview && preview_info()
return
end

function genfile(f::Function, pkg::String, dir::String, file::String)
function genfile(f::Function, pkg::String, dir::String, file::String; preview::Bool)
path = joinpath(dir, pkg, file)
println(stdout, " $path")
preview && return
mkpath(dirname(path))
open(f, path, "w")
return
end

function project(pkg::String, dir::String)
function project(pkg::String, dir::String; preview::Bool)
name = email = nothing
gitname = LibGit2.getconfig("user.name", "")
isempty(gitname) || (name = gitname)
Expand Down Expand Up @@ -46,7 +51,7 @@ function project(pkg::String, dir::String)

authorstr = "[\"$name " * (email == nothing ? "" : "<$email>") * "\"]"

genfile(pkg, dir, "Project.toml") do io
genfile(pkg, dir, "Project.toml"; preview=preview) do io
print(io,
"""
authors = $authorstr
Expand All @@ -60,8 +65,8 @@ function project(pkg::String, dir::String)
end
end

function entrypoint(pkg::String, dir)
genfile(pkg, dir, "src/$pkg.jl") do io
function entrypoint(pkg::String, dir; preview::Bool)
genfile(pkg, dir, "src/$pkg.jl"; preview=preview) do io
print(io,
"""
module $pkg
Expand Down
10 changes: 9 additions & 1 deletion stdlib/Pkg/test/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,19 @@ temp_pkg_dir() do project_path
end
end


@testset "parse package url win" begin
@test typeof(Pkg.REPLMode.parse_package("https://github.com/abc/ABC.jl"; context=Pkg.REPLMode.CMD_ADD)) == PackageSpec
end

@testset "preview generate" begin
mktempdir() do tmp
cd(tmp) do
Pkg.generate("Foo"; preview=true)
@test !isdir(joinpath(tmp, "Foo"))
end
end
end

include("repl.jl")

end # module

0 comments on commit 1e83aaa

Please sign in to comment.