Move some code around

This commit is contained in:
Chris de Graaf 2019-09-27 11:54:00 +07:00
parent a373a25c05
commit 719538e9e8
No known key found for this signature in database
GPG Key ID: 150FFDD9B0073C7B
4 changed files with 77 additions and 79 deletions

View File

@ -41,6 +41,7 @@ abstract type Plugin end
include("template.jl")
include("plugin.jl")
include("show.jl")
include("interactive.jl")
# Run some function with a project activated at the given path.

View File

@ -1,37 +1,3 @@
function Base.show(io::IO, ::MIME"text/plain", p::T) where T <: Plugin
indent = get(io, :indent, 0)
print(io, repeat(' ', indent), T)
ns = fieldnames(T)
isempty(ns) || print(io, ":")
foreach(ns) do n
println(io)
print(io, repeat(' ', indent + 2), n, ": ", show_field(getfield(p, n)))
end
end
show_field(x) = repr(x)
if Sys.iswindows()
show_field(x::AbstractString) = replace(repr(contractuser(x)), "\\\\" => "\\")
else
show_field(x::AbstractString) = repr(contractuser(x))
end
function Base.show(io::IO, m::MIME"text/plain", t::Template)
println(io, "Template:")
foreach(fieldnames(Template)) do n
n === :plugins || println(io, repeat(' ', 2), n, ": ", show_field(getfield(t, n)))
end
if isempty(t.plugins)
print(io, " plugins: None")
else
print(io, repeat(' ', 2), "plugins:")
foreach(sort(t.plugins; by=string)) do p
println(io)
show(IOContext(io, :indent => 4), m, p)
end
end
end
"""
interactive(::Type{T<:Plugin}) -> T

View File

@ -1,6 +1,49 @@
const TEMPLATES_DIR = normpath(joinpath(@__DIR__, "..", "templates"))
const DEFAULT_PRIORITY = 1000
"""
@with_defaults struct T #= ... =# end
Wraps Parameters.jl's [`@with_kw_noshow`](https://mauro3.github.io/Parameters.jl/stable/api/#Parameters.@with_kw_noshow-Tuple{Any}) to generate keyword constructors,
## Example
```julia
struct Foo <: Plugin
file::String = "/dev/null" <- "Path to the file to use"
n::Int <- "This one has no default, but this is the interactive prompt"
xyz::String = "Without a prompt, defaultkw is not implemented for this field"
end
```
"""
macro with_defaults(ex::Expr)
T = esc(ex.args[2].args[1]) # This assumes T <: U.
# This is a bit nasty.
funcs = Expr[]
foreach(filter(arg -> arg isa Expr, ex.args[3].args)) do arg
if iscall(arg, :<) && iscall(arg.args[3], :-) # x::T <- "prompt"
name = QuoteNode(arg.args[2].args[1])
prompt = arg.args[2]
push!(funcs, :(PkgTemplates.prompt(::Type{$T}, ::Val{$name}) = $(esc(prompt))))
elseif arg.head === :(=)
rhs = arg.args[2]
if iscall(rhs, :<) && iscall(rhs.args[3], :-) # x::T = "foo" <- "prompt"
name = QuoteNode(arg.args[1].args[1])
prompt = rhs.args[3].args[2]
default = arg.args[2] = rhs.args[2]
push!(
funcs,
:(PkgTemplates.prompt(::Type{$T}, ::Val{$name}) = $(esc(prompt))),
:(PkgTemplates.defaultkw(::Type{$T}, ::Val{$name}) = $(esc(default))),
)
end
end
end
return Expr(:block, esc(with_kw(ex, __module__, false)), funcs...)
end
"""
A simple plugin that, in general, creates a single file.
"""
@ -240,51 +283,6 @@ function prompt end
iscall(x, ::Symbol) = false
iscall(ex::Expr, s::Symbol) = ex.head === :call && ex.args[1] === s
"""
@with_defaults struct T #= ... =# end
Wraps Parameters.jl's [`@with_kw_noshow`](https://mauro3.github.io/Parameters.jl/stable/api/#Parameters.@with_kw_noshow-Tuple{Any}) to generate keyword constructors,
TODO explain prompt syntax.
## Example
```julia
struct Foo <: Plugin
file::String = "/dev/null" <- "Path to the file to use"
n::Int <- "This one has no default, but this is the interactive prompt"
xyz::String = "Without a prompt, defaultkw is not implemented for this field"
end
```
"""
macro with_defaults(ex::Expr)
T = esc(ex.args[2].args[1]) # This assumes T <: U.
# This is a bit nasty.
funcs = Expr[]
foreach(filter(arg -> arg isa Expr, ex.args[3].args)) do arg
if iscall(arg, :<) && iscall(arg.args[3], :-) # x::T <- "prompt"
name = QuoteNode(arg.args[2].args[1])
prompt = arg.args[2]
push!(funcs, :(PkgTemplates.prompt(::Type{$T}, ::Val{$name}) = $(esc(prompt))))
elseif arg.head === :(=)
rhs = arg.args[2]
if iscall(rhs, :<) && iscall(rhs.args[3], :-) # x::T = "foo" <- "prompt"
name = QuoteNode(arg.args[1].args[1])
prompt = rhs.args[3].args[2]
default = arg.args[2] = rhs.args[2]
push!(
funcs,
:(PkgTemplates.prompt(::Type{$T}, ::Val{$name}) = $(esc(prompt))),
:(PkgTemplates.defaultkw(::Type{$T}, ::Val{$name}) = $(esc(default))),
)
end
end
end
return Expr(:block, esc(with_kw(ex, __module__, false)), funcs...)
end
include(joinpath("plugins", "project_file.jl"))
include(joinpath("plugins", "src_dir.jl"))
include(joinpath("plugins", "tests.jl"))

33
src/show.jl Normal file
View File

@ -0,0 +1,33 @@
function Base.show(io::IO, m::MIME"text/plain", t::Template)
println(io, "Template:")
foreach(fieldnames(Template)) do n
n === :plugins || println(io, repeat(' ', 2), n, ": ", show_field(getfield(t, n)))
end
if isempty(t.plugins)
print(io, " plugins: None")
else
print(io, repeat(' ', 2), "plugins:")
foreach(sort(t.plugins; by=string)) do p
println(io)
show(IOContext(io, :indent => 4), m, p)
end
end
end
function Base.show(io::IO, ::MIME"text/plain", p::T) where T <: Plugin
indent = get(io, :indent, 0)
print(io, repeat(' ', indent), T)
ns = fieldnames(T)
isempty(ns) || print(io, ":")
foreach(ns) do n
println(io)
print(io, repeat(' ', indent + 2), n, ": ", show_field(getfield(p, n)))
end
end
show_field(x) = repr(x)
if Sys.iswindows()
show_field(x::AbstractString) = replace(repr(contractuser(x)), "\\\\" => "\\")
else
show_field(x::AbstractString) = repr(contractuser(x))
end