diff --git a/src/Generator.jl b/src/Generator.jl index 4f736775e..30958e9a3 100755 --- a/src/Generator.jl +++ b/src/Generator.jl @@ -260,8 +260,9 @@ function write_secrets_file(app_path::String = ".") :: Nothing open(joinpath(secrets_path, Genie.Secrets.SECRETS_FILE_NAME), "w") do f write(f, """try - Genie.Secrets.secret_token!("$(Genie.Secrets.secret())") - catch + Genie.Util.isprecompiling() || Genie.Secrets.secret_token!("$(Genie.Secrets.secret())") + catch ex + @error "Failed to generate secrets file: $ex" end """) end diff --git a/src/Watch.jl b/src/Watch.jl index cf2fa9512..8dffed2f7 100644 --- a/src/Watch.jl +++ b/src/Watch.jl @@ -6,10 +6,10 @@ using Logging using Dates const WATCHED_FOLDERS = Ref{Vector{String}}(String[]) -const WATCHING = Ref{Bool}(false) +const WATCHING = Ref{Vector{UInt}}(UInt[]) const WATCH_HANDLERS = Ref{Dict{String,Vector{Function}}}(Genie.config.watch_handlers) -function collect_watched_files(files::Vector{String} = WATCHED_FOLDERS[], extensions::Vector{String} = Genie.config.watch_extensions) :: Vector{String} +function collect_watched_files(files::Vector{S} = WATCHED_FOLDERS[], extensions::Vector{S} = Genie.config.watch_extensions)::Vector{S} where {S<:AbstractString} result = String[] for f in files @@ -19,7 +19,7 @@ function collect_watched_files(files::Vector{String} = WATCHED_FOLDERS[], extens result |> sort |> unique end -function watchpath(path::Union{String,Vector{String}}) +function watchpath(path::Union{S,Vector{S}}) where {S<:AbstractString} isa(path, Vector) || (path = String[path]) push!(WATCHED_FOLDERS[], path...) end @@ -28,31 +28,37 @@ function delete_handlers(key::Any) delete!(WATCH_HANDLERS[], string(key)) end -function handlers!(key::String, handlers::Vector{Function}) +function add_handler!(key::S, handler::F) where {F<:Function, S<:AbstractString} + haskey(WATCH_HANDLERS[], key) || (WATCH_HANDLERS[][key] = Function[]) + push!(WATCH_HANDLERS[][key], handler) +end + +function handlers!(key::S, handlers::Vector{<: Function}) where {S<:AbstractString} WATCH_HANDLERS[][key] = handlers end -function handlers() :: Vector{Function} +function handlers() :: Vector{<: Function} WATCH_HANDLERS[] |> values |> collect |> Iterators.flatten |> collect end -function watch(files::Vector{String}, extensions::Vector{String} = Genie.config.watch_extensions; handlers::Vector{Function} = handlers()) :: Nothing - push!(WATCHED_FOLDERS[], files...) - WATCHED_FOLDERS[] = unique(WATCHED_FOLDERS[]) - last_watched = now() +function watch(files::Vector{<: AbstractString}, extensions::Vector{<: AbstractString} = Genie.config.watch_extensions; handlers::Vector{<: Function} = handlers()) :: Nothing + last_watched = now() - Millisecond(Genie.config.watch_frequency) # to trigger the first watch Genie.Configuration.isdev() && Revise.revise() - if ! WATCHING[] - WATCHING[] = true + if ! (hash(handlers) in WATCHING[]) + push!(WATCHING[], hash(handlers)) else + @warn "Handlers already registered" return end - entr(collect_watched_files(WATCHED_FOLDERS[], extensions); all = true, postpone = true) do + entr(collect_watched_files(WATCHED_FOLDERS[], extensions); all = true) do now() - last_watched > Millisecond(Genie.config.watch_frequency) || return last_watched = now() + @show handlers + try for f in handlers Base.invokelatest(f) @@ -67,16 +73,16 @@ function watch(files::Vector{String}, extensions::Vector{String} = Genie.config. nothing end -function watch(handler::Function, files::Union{String,Vector{String}}, extensions::Vector{String} = Genie.config.watch_extensions) +function watch(handler::F, files::Union{A,Vector{A}}, extensions::Vector{A} = Genie.config.watch_extensions) where {F<:Function, A<:AbstractString} isa(files, Vector) || (files = String[files]) watch(files, extensions; handlers = Function[handler]) end -watch(files::String, extensions::Vector{String} = Genie.config.watch_extensions; handlers::Vector{Function} = handlers()) = watch(String[files], extensions; handlers) -watch(files...; extensions::Vector{String} = Genie.config.watch_extensions, handlers::Vector{Function} = handlers()) = watch(String[files...], extensions; handlers) +watch(files::A, extensions::Vector{A} = Genie.config.watch_extensions; handlers::Vector{F} = handlers()) where {F<:Function, A<:AbstractString} = watch(String[files], extensions; handlers) +watch(files...; extensions::Vector{A} = Genie.config.watch_extensions, handlers::Vector{F} = handlers()) where {F<:Function, A<:AbstractString} = watch(String[files...], extensions; handlers) watch() = watch(String[]) -function unwatch(files::Vector{String}) :: Nothing +function unwatch(files::Vector{A})::Nothing where {A<:AbstractString} filter!(e -> !(e in files), WATCHED_FOLDERS[]) nothing