Use `RelocatableFolders` for assets folders.

pull/426/head
Mike 2021-06-18 09:50:38 +01:00
parent 1dfb8d76d6
commit d4c65729e4
No known key found for this signature in database
GPG Key ID: 2398501382945A1B
3 changed files with 5 additions and 67 deletions

View File

@ -12,9 +12,8 @@ Mustache = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
RelocatableFolders = "05181044-ff0b-4ac5-8273-598c1e38db00"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
@ -24,7 +23,6 @@ JSON = "0.21"
Mustache = "0.4.1, 0.5, 1"
Plots = "0.28, 0.29, 1.0"
Requires = "1.0"
Scratch = "1.1"
YAML = "0.3, 0.4"
julia = "1.2"

View File

@ -1,57 +0,0 @@
module RelocatableFolders
import Scratch, SHA
export @folder_str
macro folder_str(path)
dir = string(__source__.file)
dir = isfile(dir) ? dirname(dir) : pwd()
return :($(Folder)($__module__, $dir, $(esc(path))))
end
struct Folder <: AbstractString
mod::Module
path::String
hash::String
files::Dict{String,Vector{UInt8}}
function Folder(mod::Module, dir, path::AbstractString)
path = isabspath(path) ? path : normpath(joinpath(dir, path))
isdir(path) || throw(ArgumentError("not a directory: `$path`"))
files = Dict{String,Vector{UInt8}}()
ctx = SHA.SHA1_CTX()
for (root, _, fs) in walkdir(path), f in fs
fullpath = joinpath(root, f)
include_dependency(fullpath)
SHA.update!(ctx, codeunits(fullpath))
content = read(fullpath)
SHA.update!(ctx, content)
files[relpath(fullpath, path)] = content
end
return new(mod, path, string(Base.SHA1(SHA.digest!(ctx))), files)
end
end
Base.show(io::IO, path::Folder) = print(io, repr(path.path))
Base.ncodeunits(f::Folder) = ncodeunits(getpath(f))
Base.isvalid(f::Folder, index::Integer) = isvalid(getpath(f), index)
Base.iterate(f::Folder) = iterate(getpath(f))
Base.iterate(f::Folder, state::Integer) = iterate(getpath(f), state)
Base.String(f::Folder) = String(getpath(f))
function getpath(f::Folder)
isdir(f.path) && return f.path
dir = Scratch.get_scratch!(f.mod, f.hash)
if !isempty(f.files) && !ispath(joinpath(dir, first(keys(f.files))))
cd(dir) do
for (file, blob) in f.files
mkpath(dirname(file))
write(file, blob)
end
end
end
return dir
end
end # module

View File

@ -1,16 +1,13 @@
module Weave
using Highlights, Mustache, Requires, Pkg, REPL
include("RelocatableFolders.jl")
using .RelocatableFolders
using Highlights, Mustache, Requires, Pkg, REPL, RelocatableFolders
# directories
const PKG_DIR = normpath(@__DIR__, "..")
const TEMPLATE_DIR = folder"../templates"
const STYLESHEET_DIR = folder"../stylesheets"
const TEMPLATE_DIR = @path joinpath(PKG_DIR, "templates")
const STYLESHEET_DIR = @path joinpath(PKG_DIR, "stylesheets")
# keeps paths of sample documents for easy try
const EXAMPLE_FOLDER = folder"../examples"
const EXAMPLE_FOLDER = @path joinpath(PKG_DIR, "examples")
# constant names
const WEAVE_OPTION_NAME = "weave_options"