From 15e410edae6532010544c944cdf5546a6b1ebbf1 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Sun, 3 Jun 2018 17:37:36 +0200 Subject: [PATCH] fix preview of generate so it does not create any files (#330) --- stdlib/Pkg/src/REPLMode.jl | 2 +- stdlib/Pkg/src/generate.jl | 21 +++++++++++++-------- stdlib/Pkg/test/pkg.jl | 10 +++++++++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/stdlib/Pkg/src/REPLMode.jl b/stdlib/Pkg/src/REPLMode.jl index a44c7aa63e7a2..74540a5ee29e8 100644 --- a/stdlib/Pkg/src/REPLMode.jl +++ b/stdlib/Pkg/src/REPLMode.jl @@ -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}) diff --git a/stdlib/Pkg/src/generate.jl b/stdlib/Pkg/src/generate.jl index 6f2528c9899ae..bcaf8876f50c3 100644 --- a/stdlib/Pkg/src/generate.jl +++ b/stdlib/Pkg/src/generate.jl @@ -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) @@ -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 @@ -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 diff --git a/stdlib/Pkg/test/pkg.jl b/stdlib/Pkg/test/pkg.jl index f449dca197145..e3e427f9d5753 100644 --- a/stdlib/Pkg/test/pkg.jl +++ b/stdlib/Pkg/test/pkg.jl @@ -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