|
1 | 1 | #!/bin/bash |
2 | 2 | set -euxo pipefail |
3 | 3 |
|
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, ...` |
6 | 7 |
|
7 | 8 | julia -e ' |
8 | 9 | using Pkg, TOML |
9 | 10 |
|
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#.#" |
12 | 12 | Pkg.activate("@v#.#") |
13 | 13 |
|
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()) |
22 | 19 | end |
23 | 20 |
|
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}()) |
27 | 31 |
|
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)) |
33 | 34 |
|
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 |
38 | 40 | 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()) |
52 | 54 | end |
53 | 55 | end |
54 | 56 | end |
55 | 57 |
|
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 |
57 | 70 | 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 |
58 | 80 | ' |
0 commit comments