Skip to content

Commit

Permalink
Merge pull request #45 from PumasAI/mh/env_exeflags
Browse files Browse the repository at this point in the history
Support `julia.env` frontmatter
  • Loading branch information
jkrumbiegel authored Feb 29, 2024
2 parents 36b5971 + f5fc5a9 commit 42cd3b8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ mutable struct File
worker::Malt.Worker
path::String
exeflags::Vector{String}
env::Vector{String}

function File(path::String)
function File(path::String, options::Union{String,Dict{String,Any}})
if isfile(path)
_, ext = splitext(path)
if ext in (".jl", ".qmd")
path = isabspath(path) ? path : abspath(path)

_, frontmatter = raw_text_chunks(path)
exeflags = frontmatter["julia"]["exeflags"]
options = _parsed_options(options)
_, file_frontmatter = raw_text_chunks(path)
merged_options = _extract_relevant_options(file_frontmatter, options)
exeflags, env = _exeflags_and_env(merged_options)

worker = cd(() -> Malt.Worker(; exeflags), dirname(path))
file = new(worker, path, exeflags)
worker = cd(() -> Malt.Worker(; exeflags, env), dirname(path))
file = new(worker, path, exeflags, env)
init!(file)
return file
else
Expand All @@ -31,6 +34,12 @@ mutable struct File
end
end

function _exeflags_and_env(options)
exeflags = options["format"]["metadata"]["julia"]["exeflags"]
env = String.(options["format"]["metadata"]["julia"]["env"])
return exeflags, env
end

struct Server
workers::Dict{String,File}

Expand All @@ -48,10 +57,10 @@ function init!(file::File)
end

function refresh!(file::File, options::Dict)
exeflags = options["format"]["metadata"]["julia"]["exeflags"]
if exeflags != file.exeflags
exeflags, env = _exeflags_and_env(options)
if exeflags != file.exeflags || env != file.env
Malt.stop(file.worker)
file.worker = cd(() -> Malt.Worker(; exeflags), dirname(file.path))
file.worker = cd(() -> Malt.Worker(; exeflags, env), dirname(file.path))
file.exeflags = exeflags
init!(file)
end
Expand Down Expand Up @@ -443,9 +452,11 @@ end

function default_frontmatter()
D = Dict{String,Any}
exeflags = JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_EXEFLAGS", "[]"), Vector{String})
env = JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_ENV", "[]"), Vector{String})
return D(
"fig-format" => "png",
"julia" => D("exeflags" => []),
"julia" => D("exeflags" => exeflags, "env" => env),
"execute" => D("error" => true),
)
end
Expand Down Expand Up @@ -746,8 +757,8 @@ is_julia_toplevel(node) =
node.t.info == "{julia}" &&
node.parent.t isa CommonMark.Document

function loadfile!(server::Server, path::String)
file = File(path)
function loadfile!(server::Server, path::String, options::Union{String,Dict{String,Any}})
file = File(path, options)
server.workers[path] = file
return file
end
Expand All @@ -761,7 +772,7 @@ function run!(
)
file = get!(server.workers, file) do
@debug "file not loaded, loading first." file
loadfile!(server, file)
loadfile!(server, file, options)
end
return evaluate!(file, output; showprogress, options)
end
Expand Down
13 changes: 13 additions & 0 deletions test/examples/env_and_exeflags.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: env
julia:
env: ["FOO=BAR"]
---

```{julia}
ENV["FOO"]
```

```{julia}
printstyled("red"; color = :red)
```
25 changes: 25 additions & 0 deletions test/testsets/env_and_exeflags.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
include("../utilities/prelude.jl")

test_example(joinpath(@__DIR__, "../examples/env_and_exeflags.qmd")) do json
cells = json["cells"]
@test length(cells) == 4

cell = cells[2]
@test cell["outputs"][1]["data"]["text/plain"] == "\"BAR\""

cell = cells[4]
@test cell["outputs"][1]["text"] == "red"
end

withenv("QUARTONOTEBOOKRUNNER_EXEFLAGS" => """["--color=yes"]""") do
test_example(joinpath(@__DIR__, "../examples/env_and_exeflags.qmd")) do json
cells = json["cells"]
@test length(cells) == 4

cell = cells[2]
@test cell["outputs"][1]["data"]["text/plain"] == "\"BAR\""

cell = cells[4]
@test cell["outputs"][1]["text"] == "\e[31mred\e[39m"
end
end

0 comments on commit 42cd3b8

Please sign in to comment.