Skip to content

Commit

Permalink
Merge pull request #16 from PumasAI/mh/artifacts
Browse files Browse the repository at this point in the history
Allow creating `Artifacts.toml` and associated `.tar.gz` as `outputs`
  • Loading branch information
MichaelHatherly authored Apr 16, 2024
2 parents 3ee45b1 + 7a42f95 commit 5978de2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
55 changes: 40 additions & 15 deletions src/PackageBundler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ should your dependencies change.
The `outputs` field is the target for the bundle. It can be a directory, a
tarball, or an `Artifacts.toml` file. Or it can be an array of multiple targets.
For `Artifacts.toml` files the `artifacts_url` field must be specified, which
points at the URL where the tarball can be downloaded from.
The `key` field is the name of the private and public key files. The private key
is used to sign the bundled packages and the public key is included in the
Expand Down Expand Up @@ -101,10 +103,7 @@ listed below. The valid names for handlers are:
- `code_injector.jl`: A function that injects extra code into the bundled
packages. Takes `filename` as an argument.
"""
function bundle(
config::AbstractString = "PackageBundler.toml";
clean::Bool = false,
)
function bundle(config::AbstractString = "PackageBundler.toml"; clean::Bool = false)
config = abspath(config)
endswith(config, ".toml") || error("Config file must be a TOML file: `$config`.")
isfile(config) || error("Config file not found: `$config`.")
Expand Down Expand Up @@ -164,6 +163,9 @@ function bundle(
outputs = String.(vcat(outputs))
outputs = isempty(outputs) ? [name] : outputs

# Artifact URL for `Artifacts.toml` files.
artifact_url = get(config, "artifacts_url", nothing)::Union{String,Nothing}

# Multiplexers. The list of multiplexer programs to try to use to select
# specific versions of Julia for each environment based on it's
# `julia_version`.
Expand Down Expand Up @@ -208,18 +210,41 @@ function bundle(
end
isdir(output) || mkpath(output)
cp(temp_dir, output; force = true)
else
if is_artifacts
# TODO: implement.
error("unsupported output type: $output")
else
@info "Generating tarball bundle." output
isdir(dirname(output)) || mkpath(dirname(output))
tar_gz = open(output, write = true)
tar = CodecZlib.GzipCompressorStream(tar_gz)
Tar.create(temp_dir, tar)
close(tar)
elseif is_tarball
@info "Generating tarball bundle." output
isdir(dirname(output)) || mkpath(dirname(output))
tar_gz = open(output, write = true)
tar = CodecZlib.GzipCompressorStream(tar_gz)
Tar.create(temp_dir, tar)
close(tar)
elseif is_artifacts
@info "Generating Artifacts.toml bundle." output
if isnothing(artifact_url)
error("`artifacts_url` must be specified for Artifacts.toml output.")
end
artifact_toml = joinpath(temp_dir, "Artifacts.toml")
product_hash = Pkg.Artifacts.create_artifact() do artifact_dir
cp(temp_dir, artifact_dir; force = true)
end
artifact_name = "$name.tar.gz"
temp_artifact = joinpath(temp_dir, artifact_name)
download_hash = Pkg.Artifacts.archive_artifact(product_hash, temp_artifact)
Pkg.Artifacts.bind_artifact!(
artifact_toml,
name,
product_hash,
force = true,
download_info = Tuple[("$artifact_url/$artifact_name", download_hash)],
)
if clean && isdir(dirname(output))
@warn "Cleaning directory." dirname(output)
rm(dirname(output); recursive = true)
end
isdir(dirname(output)) || mkpath(dirname(output))
cp(artifact_toml, output; force = true)
cp(temp_artifact, joinpath(dirname(output), artifact_name); force = true)
else
error("Invalid output target: $output")
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion test/PackageBundler.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
name = "LocalCustomRegistry"
uuid = "a0e467e0-8c5a-11e4-9d3f-823b93f75cbb"
environments = ["envs/CustomEnv"]
outputs = ["build/LocalCustomRegistry", "build/LocalCustomRegistry.tar.gz"]
outputs = [
"build/LocalCustomRegistry",
"build/LocalCustomRegistry.tar.gz",
"build/LocalCustomRegistryArtifacts/Artifacts.toml"
]
artifacts_url = "URL_GOES_HERE"
key = "key"
clean = true

Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[deps]
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
8 changes: 7 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Test, PackageBundler
using Test, TOML, PackageBundler

@testset "PackageBundler" begin
key = PackageBundler.keypair()
Expand All @@ -16,6 +16,12 @@ using Test, PackageBundler
remove_script = joinpath(registry_path, "remove.jl")
@test isfile(remove_script)

artifact_toml = TOML.parsefile(
joinpath(@__DIR__, "build", "LocalCustomRegistryArtifacts", "Artifacts.toml"),
)
@test artifact_toml["LocalCustomRegistry"]["download"][1]["url"] ==
"URL_GOES_HERE/LocalCustomRegistry.tar.gz"

mktempdir() do temp_depot
withenv("JULIA_DEPOT_PATH" => temp_depot, "JULIA_PKG_SERVER" => "") do
run(
Expand Down

0 comments on commit 5978de2

Please sign in to comment.