Skip to content

Commit

Permalink
yay
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Nov 11, 2024
1 parent 9020254 commit 9b8164c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Pluto.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ include("./webserver/WebServer.jl")
const reset_notebook_environment = PkgUtils.reset_notebook_environment
const update_notebook_environment = PkgUtils.update_notebook_environment
const activate_notebook_environment = PkgUtils.activate_notebook_environment
const has_notebook_environment = PkgUtils.has_notebook_environment
const will_use_pluto_pkg = PkgUtils.will_use_pluto_pkg
export reset_notebook_environment
export update_notebook_environment
export activate_notebook_environment
export has_notebook_environment
export will_use_pluto_pkg

include("./precompile.jl")

Expand Down
22 changes: 10 additions & 12 deletions src/packages/PkgUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ using Markdown

export activate_notebook

ensure_has_nbpkg(notebook::Notebook) = if notebook.nbpkg_ctx === nothing
ensure_has_nbpkg(notebook::Notebook) = if !will_use_pluto_pkg(notebook)

# TODO: update_save the notebook to init packages and stuff?
error("""
This notebook is not using Pluto's package manager. This means that either:
1. The notebook contains Pkg.activate or Pkg.add calls, or
2. The notebook was created before Pluto 0.15.
This notebook is not using Pluto's package manager. This means that the notebook contains Pkg.activate or Pkg.add call.
Open the notebook using Pluto to get started.
Open the notebook using Pluto to see what's up.
""")
else
for f in [notebook |> project_file, notebook |> manifest_file]
Expand Down Expand Up @@ -107,21 +105,21 @@ end

"""
```julia
has_notebook_environment(notebook_path::String)::Bool
will_use_pluto_pkg(notebook_path::String)::Bool
```
Does the notebook file contain an embedded `Project.toml` and `Manifest.toml`?
Will this notebook use the Pluto package manager? `false` means that the notebook contains `Pkg.activate` or another deactivator.
"""
has_notebook_environment(path::String) = has_notebook_environment(load_notebook_nobackup(path))
function has_notebook_environment(notebook::Notebook)
will_use_pluto_pkg(path::String) = will_use_pluto_pkg(load_notebook_nobackup(path))
function will_use_pluto_pkg(notebook::Notebook)
ctx = notebook.nbpkg_ctx
ctx === nothing && return false
# if one of the two files is not empty:
if !isempty(PkgCompat.read_project_file(ctx)) || !isempty(PkgCompat.read_manifest_file(ctx))
if ctx !== nothing && !isempty(PkgCompat.read_project_file(ctx)) || !isempty(PkgCompat.read_manifest_file(ctx))
return true
end

# fallback, when nbpkg is defined buy there are no files: check if the notebook would use one (i.e. that Pkg.activate is not used).
# otherwise, check for Pkg.activate:
# when nbpkg_ctx is defined but the files are empty: check if the notebook would use one (i.e. that Pkg.activate is not used).
topology = Pluto.updated_topology(notebook.topology, notebook, notebook.cells)
return Pluto.use_plutopkg(topology)
end
Expand Down
10 changes: 4 additions & 6 deletions test/packages/PkgUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,21 @@ without_pluto_version(s) = replace(s, r"# v.*" => "")



@testset "has_notebook_environment" begin
@testset "will_use_pluto_pkg" begin
file = Pluto.PkgUtils.testnb()
before = read(file, String)
@assert :has_notebook_environment in names(Pluto)
@assert :will_use_pluto_pkg in names(Pluto)

###
@test Pluto.has_notebook_environment(file)
@test Pluto.will_use_pluto_pkg(file)


after = read(file, String)
@test before == after


file2 = Pluto.PkgUtils.testnb("pkg_cell.jl")

@info "huh" Pluto.load_notebook_nobackup(file2).nbpkg_ctx
@test !Pluto.has_notebook_environment(file2)
@test !Pluto.will_use_pluto_pkg(file2)
end
end

Expand Down

0 comments on commit 9b8164c

Please sign in to comment.