Skip to content

Commit bbb9e6d

Browse files
authored
allow generate into an empty directory (#4430)
1 parent a1818b9 commit bbb9e6d

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/generate.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

33
function generate(path::String; io::IO = stderr_f())
4-
base = basename(path)
4+
# Handle "." to generate in current directory
5+
abspath_path = abspath(path)
6+
# Remove trailing path separator to ensure basename works correctly
7+
abspath_path = rstrip(abspath_path, ('/', '\\'))
8+
base = basename(abspath_path)
59
pkg = endswith(lowercase(base), ".jl") ? chop(base, tail = 3) : base
610
Base.isidentifier(pkg) || pkgerror("$(repr(pkg)) is not a valid package name")
7-
isdir(path) && pkgerror("$(abspath(path)) already exists")
11+
12+
if isdir(abspath_path)
13+
# Allow generating in existing directory only if it's effectively empty for our purposes
14+
files = readdir(abspath_path)
15+
# Filter out common hidden files that are okay to have
16+
relevant_files = filter(f -> f != ".git" && f != ".gitignore", files)
17+
if !isempty(relevant_files)
18+
pkgerror("$(abspath_path) already exists and is not empty")
19+
end
20+
end
21+
822
printpkgstyle(io, :Generating, " project $pkg:")
923
uuid = project(io, pkg, path)
1024
entrypoint(io, pkg, path)

test/new.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,24 @@ end
28012801
end
28022802
end
28032803
end
2804+
# Test generate . (issue #2821)
2805+
isolate(loaded_depot = true) do
2806+
cd_tempdir() do dir
2807+
mkdir("MyNewPkg")
2808+
cd("MyNewPkg") do
2809+
Pkg.generate(".")
2810+
@test isfile("Project.toml")
2811+
@test isfile("src/MyNewPkg.jl")
2812+
@test Pkg.Types.read_project("Project.toml").name == "MyNewPkg"
2813+
end
2814+
2815+
mkdir("NonEmpty")
2816+
write("NonEmpty/existing.txt", "content")
2817+
cd("NonEmpty") do
2818+
@test_throws Pkg.Types.PkgError Pkg.generate(".")
2819+
end
2820+
end
2821+
end
28042822
end
28052823

28062824
#

0 commit comments

Comments
 (0)