From efdb79b3cf9380ce206c7ce0f308e9350831ef24 Mon Sep 17 00:00:00 2001 From: MichaelHatherly Date: Thu, 18 Apr 2024 23:10:29 +0100 Subject: [PATCH] Add `instantiate` utility function --- .github/workflows/CI.yml | 20 ++--------- src/PackageBundler.jl | 1 + src/utilities.jl | 73 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 src/utilities.jl diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 20f1e83..b38d34d 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -44,19 +44,10 @@ jobs: - uses: julia-actions/install-juliaup@5e96cfab3bb0500aa42f2843f46c24b91cfcc3cd with: julia-version: "1.10.2" - - run: | - juliaup default 1.10.2 - juliaup add 1.10.1 - - - run: julia +1.10.1 --project=test/multiversion/envs/CommonMark_1.10.1 -e 'import Pkg; Pkg.instantiate()' - env: - JULIA_PKG_PRECOMPILE_AUTO: "0" - - - run: julia +1.10.2 --project=test/multiversion/envs/CommonMark_1.10.2 -e 'import Pkg; Pkg.instantiate()' - env: - JULIA_PKG_PRECOMPILE_AUTO: "0" + - run: juliaup default 1.10.2 - run: julia --project -e 'import Pkg; Pkg.instantiate()' + - run: julia --project -e 'import PackageBundler; PackageBundler.instantiate("test/multiversion/envs")' - run: julia --project test/multiversion/bundle.jl - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 @@ -145,12 +136,7 @@ jobs: version: ${{ matrix.version }} - uses: julia-actions/cache@dc1a3cdeacb521b0ca93cfc66143fcadb15a5bd0 - uses: julia-actions/julia-buildpkg@90dd6f23eb49626e4e6612cb9d64d456f86e6a1c - - # `PackageBundler.bundle()` expects the environments to be bundled to have - # already been instantiated (hence downloaded) prior to running it. We - # don't need to precompile it though. - - run: julia --project=test/envs/CustomEnv -e 'ENV["JULIA_PKG_PRECOMPILE_AUTO"]=0; import Pkg; Pkg.instantiate()' - + - run: julia --project -e 'import PackageBundler; PackageBundler.instantiate("test/envs")' - uses: julia-actions/julia-runtest@79a7e100883947123f8263c5f06e6c0ea3eb972f - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 with: diff --git a/src/PackageBundler.jl b/src/PackageBundler.jl index 81f917d..4a2e60a 100644 --- a/src/PackageBundler.jl +++ b/src/PackageBundler.jl @@ -289,5 +289,6 @@ end include("openssl.jl") include("code_stripping.jl") +include("utilities.jl") end # module PackageBundler diff --git a/src/utilities.jl b/src/utilities.jl new file mode 100644 index 0000000..93472c1 --- /dev/null +++ b/src/utilities.jl @@ -0,0 +1,73 @@ +""" + instantiate(environments::String) + +Scan the directory `environments` for subdirectories containing a +`Manifest.toml` file and instantiate them all using the appropriate `julia` +version as specified in the `Manifest.toml` file. This uses either `juliaup` or +`asdf` to switch between `julia` versions as needed. Make sure to have either of +those installed, as well as the expected `julia` versions. +""" +function instantiate(environments::String) + if ispath(environments) + if isfile(environments) + error("The path '$environments' is a file, not a directory.") + end + else + error("The path '$environments' does not exist.") + end + for each in readdir(environments; join = true) + manifest = joinpath(each, "Manifest.toml") + if isfile(manifest) + @info "Instantiating $each" + toml = TOML.parsefile(manifest) + julia_version = toml["julia_version"] + if !isnothing(Sys.which("juliaup")) + @info "Checking whether Julia '$julia_version' is installed, if not, installing it." + run(`juliaup add $julia_version`) + withenv("JULIA_PKG_PRECOMPILE_AUTO" => 0) do + run( + `julia +$(julia_version) --project=$each -e 'import Pkg; Pkg.instantiate()'`, + ) + end + elseif !isnothing(Sys.which("asdf")) + # Using the `ASDF_JULIA_VERSION` environment variable to control the + # `julia` version used doesn't appear to have an effect. Instead + # compute the exact path to the binary and use that instead. + asdf_dir = get( + ENV, + "ASDF_DATA_DIR", + get(ENV, "ASDF_DIR", joinpath(homedir(), ".asdf")), + ) + if isdir(asdf_dir) + julia_path = joinpath( + asdf_dir, + "installs", + "julia", + "$(julia_version)", + "bin", + "julia", + ) + if !isfile(julia_path) + @info "Installing Julia '$julia_version', since it does not appear to be installed." + run(`asdf install julia $julia_version`) + end + if isfile(julia_path) + withenv("JULIA_PKG_PRECOMPILE_AUTO" => 0) do + run( + `$julia_path --project=$each -e 'import Pkg; Pkg.instantiate()'`, + ) + end + else + error( + "The `julia` binary for version $julia_version failed to install.", + ) + end + else + error("`asdf` is not installed in a known location. $asdf_dir") + end + else + error("`asdf` or `juliaup` is required to instantiate the environments.") + end + end + end +end \ No newline at end of file