Skip to content

Commit

Permalink
rewrite to avoid external dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
joshday committed Feb 9, 2024
1 parent fa9c441 commit 39727d6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 58 deletions.
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
114 changes: 59 additions & 55 deletions src/DepotDelivery.jl
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 39727d6

Please sign in to comment.