Skip to content

Commit 6c28134

Browse files
feat(binder): enhance postBuild script for improved dependency handling and environment setup
1 parent e2edce7 commit 6c28134

1 file changed

Lines changed: 60 additions & 38 deletions

File tree

binder/postBuild

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,80 @@
11
#!/bin/bash
22
set -euxo pipefail
33

4-
# Pre-seed Julia's default environment for this Julia version (e.g. @v1.11)
5-
# and install everything listed in binder/Project.toml (plus your local package).
4+
# Seed Julia's default environment (@v#.#) with *your* unregistered package
5+
# and all deps listed in binder/Project.toml (fall back to root Project.toml).
6+
# After this, in Pluto: `import Pkg; Pkg.activate(); using LineCableModels, ...`
67

78
julia -e '
89
using Pkg, TOML
910
10-
# 1) Activate default env for the running Julia version
11-
# (@v#.# expands to ~/.julia/environments/v<major>.<minor>)
11+
# 1) Activate the default versioned env, not a folder named "@v#.#"
1212
Pkg.activate("@v#.#")
1313
14-
# 2) Always dev the local repo (your unregistered package) into the default env
15-
# so `using LineCableModels` works from anywhere.
16-
if isfile("Project.toml")
17-
try
18-
Pkg.develop(path=".")
19-
catch e
20-
@warn "Pkg.develop(path=\".\") failed; check repo root" exception=(e, catch_backtrace())
21-
end
14+
# 2) Develop the local repo (assume postBuild is running at repo root)
15+
try
16+
Pkg.develop(path=".")
17+
catch e
18+
@warn "Pkg.develop(path=\".\") failed" exception=(e, catch_backtrace())
2219
end
2320
24-
# 3) Read declared deps from binder/Project.toml (fallback to root Project.toml)
25-
projfile = isfile("binder/Project.toml") ? "binder/Project.toml" :
26-
(isfile("Project.toml") ? "Project.toml" : nothing)
21+
# Helper to add deps from a Project.toml
22+
function add_deps_from(project_file::String)
23+
if !isfile(project_file)
24+
@info "No $project_file found"; return
25+
end
26+
proj = TOML.parsefile(project_file)
27+
deps = get(proj, "deps", Dict{String,Any}())
28+
compat = get(proj, "compat", Dict{String,Any}())
29+
extras = get(proj, "extras", Dict{String,Any}()) # in case you keep dev/test stuff here
30+
targets = get(proj, "targets", Dict{String,Any}())
2731
28-
deps = Dict{String,Any}()
29-
if projfile !== nothing
30-
proj = TOML.parsefile(projfile)
31-
deps = get(proj, "deps", deps)
32-
end
32+
# Collect package names to install (deps + optionally selected extras)
33+
names = Set{String}(keys(deps))
3334
34-
# 4) Add each dependency by name if it is not the local package we just devved.
35-
for name in sort(collect(keys(deps)))
36-
if name == "LineCableModels"
37-
continue
35+
# If you keep things like IJulia/Pluto in [extras] with a "binder" target, include them
36+
if haskey(targets, "binder") && isa(targets["binder"], Vector)
37+
for extra in targets["binder"]
38+
extra in keys(extras) && push!(names, extra)
39+
end
3840
end
39-
try
40-
Pkg.add(name)
41-
catch e
42-
# If this dep exists as a subdir, try dev it; else warn.
43-
if isdir(name)
44-
@info "Pkg.add($name) failed, trying Pkg.develop(path=\"$name\")"
45-
try
46-
Pkg.develop(path=name)
47-
catch e2
48-
@warn "Could not add or dev dependency $name" exception=(e2, catch_backtrace())
49-
end
50-
else
51-
@warn "Could not add dependency $name (not found as a subdir)."
41+
42+
for name in sort(collect(names))
43+
# Skip your own devved package if it appears in deps
44+
if name == "LineCableModels"
45+
continue
46+
end
47+
ver = get(compat, name, nothing)
48+
spec = ver === nothing ? Pkg.PackageSpec(name=name) :
49+
Pkg.PackageSpec(name=name, version=ver)
50+
try
51+
Pkg.add(spec)
52+
catch e
53+
@warn "Pkg.add failed for $name" exception=(e, catch_backtrace())
5254
end
5355
end
5456
end
5557
56-
# 5) Precompile the default env so Pluto loads fast
58+
# Prefer binder/Project.toml, fall back to repo root
59+
add_deps_from("binder/Project.toml")
60+
add_deps_from("Project.toml")
61+
62+
# 3) Nice-to-haves commonly needed in Binder + Pluto
63+
try
64+
Pkg.add(["IJulia", "Pluto"])
65+
catch e
66+
@warn "Optional adds failed" exception=(e, catch_backtrace())
67+
end
68+
69+
# 4) Precompile for faster startup
5770
Pkg.precompile()
71+
72+
# 5) Make sure the IJulia kernel points to the default env (useful in JupyterLab)
73+
try
74+
using IJulia
75+
envpath = Base.load_path_expand("@v#.#")
76+
IJulia.installkernel("Julia (@v#.#)", env=envpath)
77+
catch e
78+
@warn "IJulia kernel install failed (non-fatal)" exception=(e, catch_backtrace())
79+
end
5880
'

0 commit comments

Comments
 (0)