-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathComponentPackages.jl
228 lines (189 loc) · 7.69 KB
/
ComponentPackages.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
module ComponentPackages
import JSON, JSON2, HTTP
include("ComponentMetas.jl")
using .ComponentMetas
const ROOT_PATH = realpath(joinpath( @__DIR__, ".."))
const META_FILENAME = joinpath(ROOT_PATH, "components_meta.json")
struct ComponentPackage
name ::String
package_name ::String
source_path ::String
scripts ::NamedTuple{(:dev, :prod), Tuple{Vector{String}, Vector{String}}}
has_components ::Bool
end
macro _make_packages()
raw_meta = open(META_FILENAME, "r") do f
JSON2.read(f, Vector{ComponentPackage})
end
package_pairs = map(raw_meta) do p
return :($(Symbol(p.name)) = $(p))
end
return esc(:($(package_pairs...),))
end
const _components_packages = @_make_packages
function components_js_include(prefix = ""; debug=false)
type = debug ? :dev : :prod
join(
map(values(_components_packages)) do p
join(
map(p.scripts[type]) do script
"""<script src="$(prefix)_dash-component-suites/$(p.package_name)/$(script)"></script>"""
end, "\n"
)
end
)
end
function components_js_sources(prefix = ""; debug=false)
type = debug ? :dev : :prod
result = []
for p in values(_components_packages)
for script in p.scripts[type]
push!(result, "$(prefix)_dash-component-suites/$(p.package_name)/$(script)")
end
end
return result
end
macro register_js_sources(path, prefix)
registers = []
for package in values(_components_packages)
package_uri = "_dash-component-suites/$(package.package_name)/"
package_path = ROOT_PATH * package.source_path
part = quote
full_package_uri = $(prefix) * $(package_uri)
if startswith($(path), full_package_uri)
script_name = replace($(path), full_package_uri=>"")
filename = $(package_path) * script_name
try
return HTTP.Response(200, ["Content-Type" => "application/javascript"], body = read(filename))
catch
return HTTP.Response(404)
end
end
end
push!(registers, part)
end
return esc(quote
$(registers...)
end)
end
function package_namespace(package_name)
return _components_packages[Symbol(package_name)].package_name
end
function load_package_components(package_name)
package_symbol = Symbol(package_name)
if !haskey(_components_packages, package_symbol)
error("undefined package $(package_symbol)")
end
package = _components_packages[Symbol(package_name)]
meta_filename = joinpath(
ROOT_PATH,
Sys.iswindows() ? replace(package.source_path[2:end-1], "/" =>"\\") : package.source_path[2:end],
"metadata.json"
)
return load_components_meta(meta_filename)
raw_meta = JSON.parsefile(meta_filename, dicttype = OrderedDict{String, Any})
components = Dict{Symbol, ComponentMeta}()
for (script, meta) in raw_meta
name = match(r"([a-zA-Z0-9_]+)\.react\.js", script).captures[1]
components[Symbol(name)] = ComponentMeta(name, meta["description"], parse_props(meta["props"]))
break
end
return components
end
function component_doc_list()
result = ""
for package in values(_components_packages)
if package.has_components
result *= "## Component package: `$(package.name)`:\n"
components = load_package_components(Symbol(package.name))
names = sort(collect(keys(components)))
names = map(names) do name
lowercase("`$(package.name)_$(name)`")
end
result *= join(names, ", ") * "\n"
end
end
return result
end
function make_component(package, component)
maker_name = Symbol(lowercase(package.name),"_", lowercase(component.name))
props = collect(keys(component.properties))
add_signs = ""
if any(x->x==:children, props)
add_signs = """\n $(maker_name)(children::Any;kwags...)\n $(maker_name)(children_maker::Function;kwags...)\n"""
end
avaible_props = Set{Symbol}()
wild_props = Set{Symbol}()
for prop in values(component.properties)
if endswith(prop.name, "*")
push!(wild_props, Symbol(prop.name[1:end-1]))
else
push!(avaible_props, Symbol(prop.name))
end
end
wild_regs = Regex("^(?<prop>$(join(wild_props, "|")))")
docstr = """ $(maker_name)(;kwags...)
$(add_signs)
$(component.description)
# Arguments
$(join(map(values(component.properties)) do prop
"- `$(prop.name)` - $(prop.description)"
end,"\n"))
"""
comp_maker = quote
avaible_props = $(avaible_props)
wild_props = $(wild_props)
wild_regs = $(wild_regs)
result = Component($(component.name), $(package.package_name), Dict{Symbol, Any}(), avaible_props, $(wild_props))
for (prop, value) in pairs(kwargs)
m = match(wild_regs, string(prop))
if (length(wild_props) == 0 || isnothing(m)) && !(prop in avaible_props)
throw(ArgumentError("Invalid property $(string(prop)) for component " * $(string(maker_name))))
end
push!(result.props, prop=>Front.to_dash(value))
end
end
makers = Vector{Expr}()
push!(makers,esc(quote
export $(maker_name)
function $(maker_name) end
@doc $(docstr) $(maker_name)
function $(maker_name)(;kwargs...)
$(comp_maker)
return result
end
end))
if any(x->x==:children, props)
props_nochildren = filter(x->x!=:children, props)
push!(makers,esc(quote
function $(maker_name)(children::Any; kwargs...)
$(comp_maker)
push!(result.props, :children=>Front.to_dash(children))
return result
end
function $(maker_name)(children_maker::Function; kwargs...)
$(comp_maker)
push!(result.props, :children=>Front.to_dash(children_maker()))
return result
end
end))
end
return quote
$(makers...)
end
end
macro reg_components()
components_code::Vector{Expr} = []
for package in values(_components_packages)
if package.has_components
components = ComponentPackages.load_package_components(package.name)
for component in values(components)
push!(components_code, make_component(package, component))
end
end
end
return quote
$(components_code...)
end
end
end