Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility functions #17

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -140,17 +131,13 @@ jobs:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633
with:
persist-credentials: false
- uses: julia-actions/setup-julia@f2258781c657ad9b4b88072c5eeaf9ec8c370874
- uses: julia-actions/install-juliaup@5e96cfab3bb0500aa42f2843f46c24b91cfcc3cd
with:
version: ${{ matrix.version }}
julia-version: "${{ matrix.version }}"
- run: juliaup default ${{ 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:
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ artifact that can be installed as a custom Julia package registry by users.

## Usage

You can use the below code to generate a starter bundler project

```julia
import Pkg
Pkg.activate(; temp = true)
Pkg.add(; url = "https://github.com/PumasAI/PackageBundler.jl")
import PackageBundler
PackageBundler.generate("path/to/my/project")
```

This will create a new project with some starter configuration to get you
started. See below for how to configure a project manually.

Create a `PackageBundler.toml` file. This file contains the list of project
environments to bundle and the specific packages that should have their source
code stripped and bundled instead as serialized AST artifacts.
Expand Down
1 change: 1 addition & 0 deletions src/PackageBundler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,6 @@ end

include("openssl.jl")
include("code_stripping.jl")
include("utilities.jl")

end # module PackageBundler
122 changes: 122 additions & 0 deletions src/utilities.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"""
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

function generate(
directory::AbstractString;
name::AbstractString = basename(directory),
uuid::Base.UUID = Base.UUID(rand(UInt128)),
)
Base.isidentifier(name) || error("'$name' is not valid. Must be a Julia identifier.")
ispath(directory) && error("The path '$directory' already exists.")
mkpath(directory)
run(
`$(Base.julia_cmd()) --project=$directory -e 'pushfirst!(LOAD_PATH, "@stdlib"); import Pkg; Pkg.add(; url = "https://github.com/PumasAI/PackageBundler.jl")'`,
)
mkpath(joinpath(directory, "environments"))
write(
joinpath(directory, "PackageBundler.toml"),
"""
name = "$name"
uuid = "$uuid"
environments = [
# Add your environments here.
"environments/...",
]
outputs = ["build"]
key = "key"
clean = true
multiplexers = ["juliaup", "asdf"]

# Registries that you want to strip code from.
# [registries]

# Packages that you want to strip code from.
# [packages]
""",
)
write(
joinpath(directory, "bundle.jl"),
"""
import PackageBundler
PackageBundler.bundle(joinpath(@__DIR__, "PackageBundler.toml"))
""",
)
write(
joinpath(directory, "instantiate.jl"),
"""
import PackageBundler
PackageBundler.instantiate(joinpath(@__DIR__, "environments"))
""",
)
end
Loading