Skip to content

Commit

Permalink
Internal file watching
Browse files Browse the repository at this point in the history
  • Loading branch information
essenciary committed Jun 22, 2022
1 parent f236f7a commit e1cb4a4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Genie"
uuid = "c43c736e-a2d1-11e8-161f-af95117fbd1e"
authors = ["Adrian Salceanu <[email protected]>"]
version = "5.0.0"
version = "5.1.0"

[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
Expand Down Expand Up @@ -36,16 +36,16 @@ YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
ArgParse = "1"
EzXML = "1"
FilePathsBase = "0.9"
HTTP = "0.8, 0.9"
HTTP = "0.9"
HttpCommon = "0.5"
Inflector = "1"
JSON3 = "1"
JuliaFormatter = "0.12 - 0.22, 1"
JuliaFormatter = "1"
Millboard = "0.2"
Nettle = "0.5"
OrderedCollections = "1"
Reexport = "0.2, 1"
Revise = "2, 3"
Reexport = "1"
Revise = "3"
YAML = "0.4"
julia = "1.6"

Expand Down
3 changes: 2 additions & 1 deletion files/new_app/config/env/dev.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Genie.Configuration.config!(
server_handle_static_files = true,
path_build = "build",
format_julia_builds = true,
format_html_output = true
format_html_output = true,
watch = true
)

ENV["JULIA_REVISE"] = "auto"
4 changes: 4 additions & 0 deletions src/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ Base.@kwdef mutable struct Settings
format_html_indentation_string::String = " "

autoload::Vector{Symbol} = Symbol[:initializers, :helpers, :libs, :resources, :plugins, :routes]

watch::Bool = false
watch_extensions::Vector{String} = String["jl", "html", "md", "js", "css"]
watch_handlers::Dict{Any,Vector{Function}} = Dict()
end

end
3 changes: 3 additions & 0 deletions src/Genie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using Reexport
include("HTTPUtils.jl")
include("Exceptions.jl")
include("Repl.jl")
include("Watch.jl")
include("Loader.jl")
include("Secrets.jl")
include("Util.jl")
Expand Down Expand Up @@ -100,6 +101,7 @@ function loadapp(path::String = "."; autostart::Bool = false, dbadapter::Union{N
include(joinpath($path, $(Genie.BOOTSTRAP_FILE_NAME)))
end)

Genie.config.watch && Genie.Watch.watch(path)
autostart && (Core.eval(Main, :(up())))

nothing
Expand Down Expand Up @@ -171,6 +173,7 @@ function genie(; context = @__MODULE__) :: Union{Nothing,Sockets.TCPServer}

Secrets.load(context = context)
Loader.load(context = context)
Genie.config.watch && Watch.watch(pwd())
run(server = EARLYBINDING)

EARLYBINDING
Expand Down
63 changes: 63 additions & 0 deletions src/Watch.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module Watch

using Revise
using Genie

const _task = Ref{Core.Task}()
const WATCHED_FOLDERS = Ref{Vector{String}}(String[])

function collect_watched_files(files::Vector{String} = String[],
extensions::Vector{String} = Genie.config.watch_extensions) :: Vector{String}
result = String[]

for f in files
try
push!(result, Genie.Util.walk_dir(f, only_extensions = extensions)...)
catch ex
@error ex
end
end

result |> unique!
end

function handlers()
Genie.config.watch_handlers |> values |> collect
end

function watch(files::Vector{String}, extensions::Vector{String} = Genie.config.watch_extensions) :: Nothing
push!(WATCHED_FOLDERS[], files...)
WATCHED_FOLDERS[] = unique(WATCHED_FOLDERS[])

@info "Monitoring $files for changes"

Revise.revise()

_task[] = @async entr(collect_watched_files(files, extensions); all = true, postpone = true) do
@info "Detected files changes"

for fg in handlers()
for f in fg
try
Base.invokelatest(f)
catch ex
@error ex
end
end
end
end

nothing
end

watch(files::String, extensions::Vector{String} = Genie.config.watch_extensions) = watch(String[files], extensions)
watch(files...; extensions::Vector{String} = Genie.config.watch_extensions) = watch(String[files...], extensions)
watch() = watch(String[])

function unwatch(files::Vector{String}) :: Nothing
filter!(e -> !(e in files), WATCHED_FOLDERS[])
watch()
end
unwatch(files...) = unwatch(String[files...])

end

0 comments on commit e1cb4a4

Please sign in to comment.