From 39727d6ccb6c640ef45f6a7cf7053dc5c9808d49 Mon Sep 17 00:00:00 2001 From: Josh Day Date: Fri, 9 Feb 2024 11:33:47 -0500 Subject: [PATCH] rewrite to avoid external dependency --- Project.toml | 2 - src/DepotDelivery.jl | 114 ++++++++++++++++++++++--------------------- test/runtests.jl | 4 +- 3 files changed, 62 insertions(+), 58 deletions(-) diff --git a/Project.toml b/Project.toml index bf8c6c1..ea75b69 100644 --- a/Project.toml +++ b/Project.toml @@ -6,12 +6,10 @@ version = "0.1.0" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" -Malt = "36869731-bdee-424d-aa32-cab38c994e3b" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" [compat] -Malt = "1" julia = "1" [extras] diff --git a/src/DepotDelivery.jl b/src/DepotDelivery.jl index 53b6272..0d2c3bc 100644 --- a/src/DepotDelivery.jl +++ b/src/DepotDelivery.jl @@ -1,72 +1,76 @@ module DepotDelivery -import Malt +using Dates, InteractiveUtils, Pkg, TOML -export build_project - -#-----------------------------------------------------------------------------# utils -global worker::Malt.Worker - -function __init__() - global worker = Malt.Worker() +@kwdef struct State + depot_path = copy(DEPOT_PATH) + precomp = get(ENV, "JULIA_PKG_PRECOMPILE_AUTO", nothing) + project = Base.current_project() +end +function restore!!(s::State) + Pkg.activate(s.project) + isnothing(s.precomp) || (ENV["JULIA_PKG_PRECOMPILE_AUTO"] = s.precomp) + append!(empty!(DEPOT_PATH), s.depot_path) end -#-----------------------------------------------------------------------------# build_project -function build_project(file::String; platform = Base.BinaryPlatforms.HostPlatform()) - Malt.remote_eval_fetch(worker, quote - file = $(abspath(file)) - path = mktempdir() - push!(empty!(DEPOT_PATH), path) - ENV["JULIA_PKG_PRECOMPILE_AUTO"] = "0" - - import Pkg, TOML, Dates, InteractiveUtils - - config = mkpath(joinpath(path, "config")) - dev = mkpath(joinpath(path, "dev")) - - project = TOML.parsefile(file) +#-----------------------------------------------------------------------------# build +function build(path::String; platform = Base.BinaryPlatforms.HostPlatform()) + state = State() + path = abspath(path) + depot = mktempdir() + try + proj_file = joinpath(path, "Project.toml") + proj_file = isfile(proj_file) ? proj_file : joinpath(path, "JuliaProject.toml") + proj = TOML.parsefile(proj_file) + name = proj["name"] build_spec = Dict( :datetime => Dates.now(), :versioninfo => (buf = IOBuffer(); InteractiveUtils.versioninfo(buf); String(take!(buf))), - :project_file => abspath(file), - :project => project, - :platform => $(string(platform)) + :project_file => proj_file, + :project => proj, + :platform => string(platform) ) + mkdir(joinpath(depot, "config")) + mkdir(joinpath(depot, "dev")) + push!(empty!(DEPOT_PATH), depot) + ENV["JULIA_PKG_PRECOMPILE_AUTO"] = "0" # Needed when building for non-host platforms - @info "Copying project to `\$depot/dev/$(project["name"])`" - dev_proj = joinpath(dev, project["name"]) - cp(dirname(file), dev_proj) - - Pkg.activate(dev_proj) - Pkg.instantiate(; platform = $platform, verbose=true) + cp(path, joinpath(depot, "dev", name)) Pkg.activate() - Pkg.develop(project["name"]) - - @info "Writing `\$depot/config/depot_build.toml`" - open(io -> TOML.print(io, build_spec), joinpath(config, "depot_build.toml"), "w") - - @info "Writing `\$depot/config/depot_startup.jl`" - open(joinpath(config, "depot_startup.jl"), "w") do io - println(io, """ - import Pkg - - ENV["JULIA_DEPOT_PATH"] = "$path" # Needed for Distributed.jl to work - - push!(empty!(DEPOT_PATH), "$path") + Pkg.develop(path=joinpath(depot, "dev", name)) + Pkg.instantiate() + + open(io -> TOML.print(io, build_spec), joinpath(depot, "config", "depot_build.toml"), "w") + open(io -> print(io, startup_script(depot, name)), joinpath(depot, "config", "depot_startup.jl"), "w") + catch ex + @warn "DepotDelivery.build failed" + rethrow(ex) + finally + restore!!(state) + end + + return depot +end - @info "`using $(project["name"])`" - using $(project["name"]) - """) - end - path - end) +#-----------------------------------------------------------------------------# startup_script +function startup_script(depot_path::String, proj_name::String) + """ + import Pkg + ENV["JULIA_DEPOT_PATH"] = "$depot_path" # Needed for Distributed.jl to work + push!(empty!(DEPOT_PATH), "$depot_path") + using $proj_name + @info "Depot `$depot_path` initialized with project `$proj_name`." + """ end -#-----------------------------------------------------------------------------# test_build -function test_build(path::String; kw...) - Malt.remote_eval_fetch(worker, quote - include(joinpath($path, "config", "depot_startup.jl")) - end) +#-----------------------------------------------------------------------------# test +function test(depot_path::String) + script = """ + @info "Loading the depot_startup.jl script" + include("$(joinpath(depot_path, "config", "depot_startup.jl"))") + """ + process = run(`$(Base.julia_cmd()) -e $script`) + process.exitcode == 0 end end # DepotDelivery module diff --git a/test/runtests.jl b/test/runtests.jl index 08d2eaf..2e5277c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,6 @@ using DepotDelivery using Test -path = DepotDelivery.build_project(joinpath(@__DIR__, "..", "Project.toml")) +depot = DepotDelivery.build(joinpath(@__DIR__, "..")) + +@test DepotDelivery.test(depot)