Skip to content

Write Project.toml with the same section sorting as Pkg #74

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/Preferences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ function set_preferences!(target_toml::String, pkg_name::String, pairs::Pair{Str
d = Base.parsed_toml(target_toml)
end
prefs = d
if endswith(target_toml, "Project.toml")
is_project_toml = endswith(target_toml, "Project.toml")
if is_project_toml
if !haskey(prefs, "preferences")
prefs["preferences"] = Dict{String,Any}()
end
Expand All @@ -158,11 +159,34 @@ function set_preferences!(target_toml::String, pkg_name::String, pairs::Pair{Str
prefs[pkg_name] = process_sentinel_values!(prefs[pkg_name])
end
open(target_toml, "w") do io
TOML.print(io, d, sorted=true)
if is_project_toml
project_toml_print(io, d)
else
TOML.print(io, d; sorted=true)
end
end
return nothing
end

# Taken from Pkg.jl v1.11.4 (but we don't want to depend on Pkg.jl directly just for this)
# https://github.com/JuliaLang/Pkg.jl/blob/v1.11.4/src/project.jl#L235
# with `preferences` added before `deps`
const _project_key_order = ["name", "uuid", "keywords", "license", "desc", "preferences", "deps", "weakdeps", "sources", "extensions", "compat"]
project_key_order(key::String) = something(findfirst(x -> x == key, _project_key_order), length(_project_key_order) + 1)
function project_toml_print(io::IO, d::Dict)
@static if VERSION <= v"1.11-"
TOML.print(io, d; sorted=true, by=key -> (project_key_order(key), key))
else
kw = (sorted=true, by=key -> (project_key_order(key), key))
if haskey(d, "sources")
inline_tables = IdSet{Dict{String}}(v for v in values(d["sources"]))
kw = merge(kw, (; inline_tables))
end
TOML.print(io, d; kw...)
end
end


"""
set_preferences!(uuid_or_module_or_name_or_tuple, prefs::Pair{String,Any}...;
export_prefs=false, active_project_only=true, force=false)
Expand Down
4 changes: 4 additions & 0 deletions test/PkgV_1_11_plus/LocalPkg/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "LocalPkg"
uuid = "8fa365b9-38ac-4f5f-96f8-37668e8e369e"
authors = ["Preferences.jl contributors"]
version = "0.1.0"
5 changes: 5 additions & 0 deletions test/PkgV_1_11_plus/LocalPkg/src/LocalPkg.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module LocalPkg

greet() = print("Hello World!")

end # module LocalPkg
26 changes: 26 additions & 0 deletions test/PkgV_1_11_plus/Project-reference-after.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name = "PkgV_1_11_plus"
uuid = "6fde81da-fe91-40db-985f-5f1fb79db309"
authors = ["Preferences.jl contributors"]
version = "0.1.0"

[preferences.PkgV_1_11_plus]
TEST_PREF = true

[deps]
Example = "7876af07-990d-54b4-ab0e-23690620f79a"
LocalPkg = "fcf55292-0d03-4e8a-9e0b-701580031fc3"

[sources]
Example = {url = "https://github.com/JuliaLang/Example.jl"}
LocalPkg = {path = "LocalPkg"}

[compat]
Example = "0.5"
Test = "1.11.0"
julia = "1.11"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
23 changes: 23 additions & 0 deletions test/PkgV_1_11_plus/Project-reference-before.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name = "PkgV_1_11_plus"
uuid = "6fde81da-fe91-40db-985f-5f1fb79db309"
authors = ["Preferences.jl contributors"]
version = "0.1.0"

[deps]
Example = "7876af07-990d-54b4-ab0e-23690620f79a"
LocalPkg = "fcf55292-0d03-4e8a-9e0b-701580031fc3"

[sources]
Example = {url = "https://github.com/JuliaLang/Example.jl"}
LocalPkg = {path = "LocalPkg"}

[compat]
Example = "0.5"
Test = "1.11.0"
julia = "1.11"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
23 changes: 23 additions & 0 deletions test/PkgV_1_11_plus/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name = "PkgV_1_11_plus"
uuid = "6fde81da-fe91-40db-985f-5f1fb79db309"
authors = ["Preferences.jl contributors"]
version = "0.1.0"

[deps]
Example = "7876af07-990d-54b4-ab0e-23690620f79a"
LocalPkg = "fcf55292-0d03-4e8a-9e0b-701580031fc3"

[sources]
Example = {url = "https://github.com/JuliaLang/Example.jl"}
LocalPkg = {path = "LocalPkg"}

[compat]
Example = "0.5"
Test = "1.11.0"
julia = "1.11"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
5 changes: 5 additions & 0 deletions test/PkgV_1_11_plus/src/PkgV_1_11_plus.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module PkgV_1_11_plus

greet() = print("Hello World!")

end # module PkgV_1_11_plus
25 changes: 25 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,28 @@ end
end
end
end

# https://github.com/JuliaPackaging/Preferences.jl/issues/52
# Need v1.11+ for Project.toml `[sources]` section to be supported.
if VERSION > v"1.11.0-"
@testset "Project.toml writing" begin
Pkg.activate("PkgV_1_11_plus") do
@eval using PkgV_1_11_plus
proj_path = joinpath("PkgV_1_11_plus", "Project.toml")
proj_before = read(proj_path, String)
ref_before = read(joinpath("PkgV_1_11_plus", "Project-reference-before.toml"), String)
@test proj_before == ref_before
try
set_preferences!(PkgV_1_11_plus, "TEST_PREF" => true; export_prefs=true)
proj_after = read(proj_path, String)
ref_after = read(joinpath("PkgV_1_11_plus", "Project-reference-after.toml"), String)
@assert ref_before != ref_after
@test proj_after == ref_after
finally
open(proj_path, "w") do io
write(io, proj_before)
end
end
end
end
end # if VERSION
Loading