-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite to avoid external dependency
- Loading branch information
Showing
3 changed files
with
62 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |