Skip to content

Commit d771176

Browse files
authored
Generate Project.toml for Dash.jl components (#1253)
1 parent dad9261 commit d771176

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

dash/development/_jl_components_generation.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@
66
import shutil
77
import glob
88
import warnings
9+
import sys
10+
import importlib
11+
import uuid
912

1013
from ._all_keywords import julia_keywords
1114
from ._py_components_generation import reorder_props
1215

16+
#uuid of Dash Julia package. Used as base for component package uuid
17+
jl_dash_uuid = "1b08a953-4be3-4667-9a23-3db579824955"
1318

1419
# Declaring longer string templates as globals to improve
1520
# readability, make method logic clearer to anyone inspecting
1621
# code below
1722
jl_component_string = '''
23+
export {funcname}
24+
1825
"""
1926
{funcname}(;kwargs...)
2027
{funcname}(children::Any;kwargs...)
@@ -50,6 +57,57 @@
5057
{funcname}(children_maker::Function; kwargs...) = {funcname}(children_maker(); kwargs...)
5158
''' # noqa:E501
5259

60+
jl_package_file_string = '''
61+
module {package_name}
62+
using Dash
63+
64+
const resources_path = realpath(joinpath( @__DIR__, "..", "deps"))
65+
const version = "{version}"
66+
67+
{component_includes}
68+
69+
function __init__()
70+
Dash.register_package(
71+
Dash.ResourcePkg(
72+
"{project_shortname}",
73+
resources_path,
74+
version = version,
75+
[
76+
{resources_dist}
77+
]
78+
)
79+
)
80+
end
81+
end
82+
'''
83+
84+
jl_projecttoml_string = '''
85+
name = "{package_name}"
86+
uuid = "{package_uuid}"
87+
{authors}
88+
version = "{version}"
89+
90+
[deps]
91+
Dash = "{dash_uuid}"
92+
93+
[compact]
94+
julia = "1.1"
95+
Dash = ">=0.1"
96+
'''
97+
98+
jl_component_include_string = 'include("{name}.jl")'
99+
100+
jl_resource_tuple_string = '''Dash.Resource(
101+
relative_package_path = {relative_package_path},
102+
external_url = {external_url},
103+
dynamic = {dynamic},
104+
async = {async_string},
105+
type = :{type}
106+
)'''
107+
108+
def jl_package_name(namestring):
109+
s = namestring.split("_")
110+
return "".join(w.capitalize() for w in s)
53111

54112
def stringify_wildcards(wclist, no_symbol=False):
55113
if no_symbol:
@@ -306,6 +364,69 @@ def format_fn_name(prefix, name):
306364
return name.lower()
307365

308366

367+
def generate_metadata_strings(resources, type):
368+
def noting_or_string(v):
369+
return '"{}"'.format(v) if v else "nothing"
370+
return [jl_resource_tuple_string.format(
371+
relative_package_path = noting_or_string(resource.get("relative_package_path", "")),
372+
external_url = noting_or_string(resource.get("external_url", "")),
373+
dynamic = str(resource.get("dynamic", 'nothing')).lower(),
374+
type = type,
375+
async_string = ":{}".format(str(resource.get("async")).lower())
376+
if "async" in resource.keys()
377+
else 'nothing'
378+
) for resource in resources]
379+
380+
381+
def generate_package_file(project_shortname, components, pkg_data, prefix):
382+
package_name = jl_package_name(project_shortname)
383+
384+
sys.path.insert(0, os.getcwd())
385+
mod = importlib.import_module(project_shortname)
386+
js_dist = getattr(mod, "_js_dist", [])
387+
css_dist = getattr(mod, "_css_dist", [])
388+
project_ver = pkg_data.get("version")
389+
390+
resources_dist = ",\n".join(
391+
generate_metadata_strings(js_dist, "js") + generate_metadata_strings(css_dist, "css")
392+
)
393+
394+
package_string = jl_package_file_string.format(
395+
package_name = package_name,
396+
component_includes = "\n".join(
397+
[jl_component_include_string.format(name = format_fn_name(prefix, comp_name)) for comp_name in components]
398+
),
399+
resources_dist = resources_dist,
400+
version = project_ver,
401+
project_shortname = project_shortname
402+
403+
)
404+
file_path = os.path.join("src", package_name + ".jl")
405+
with open(file_path, "w") as f:
406+
f.write(package_string)
407+
print("Generated {}".format(file_path))
408+
409+
def generate_toml_file(project_shortname, pkg_data):
410+
package_author = pkg_data.get("author", "")
411+
project_ver = pkg_data.get("version")
412+
package_name = jl_package_name(project_shortname)
413+
u = uuid.UUID(jl_dash_uuid)
414+
package_uuid = uuid.UUID(hex=u.hex[:-12] + hex(hash(package_name))[-12:])
415+
416+
authors_string = 'authors = ["{}"]'.format(package_author) if package_author else ""
417+
418+
toml_string = jl_projecttoml_string.format(
419+
package_name = package_name,
420+
package_uuid = package_uuid,
421+
version = project_ver,
422+
authors = authors_string,
423+
dash_uuid = jl_dash_uuid
424+
)
425+
file_path = "Project.toml"
426+
with open(file_path, "w") as f:
427+
f.write(toml_string)
428+
print("Generated {}".format(file_path))
429+
309430
def generate_class_string(name, props, description, project_shortname, prefix):
310431
# Ensure props are ordered with children first
311432
filtered_props = reorder_props(filter_props(props))
@@ -374,6 +495,7 @@ def generate_module(
374495
project_shortname,
375496
components,
376497
metadata,
498+
pkg_data,
377499
prefix,
378500
**kwargs
379501
):
@@ -398,3 +520,9 @@ def generate_module(
398520

399521
for sourcemap in glob.glob("{}/*.map".format(project_shortname)):
400522
shutil.copy(sourcemap, "deps/")
523+
524+
generate_package_file(project_shortname, components, pkg_data, prefix)
525+
generate_toml_file(project_shortname, pkg_data)
526+
527+
528+

dash/development/component_generator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ def generate_components(
111111
)
112112

113113
if jlprefix is not False:
114+
if pkg_data is None:
115+
with open("package.json", "r") as f:
116+
pkg_data = safe_json_loads(f.read())
117+
114118
generator_methods.append(
115119
functools.partial(generate_struct_file, prefix=jlprefix)
116120
)
@@ -140,6 +144,7 @@ def generate_components(
140144
project_shortname,
141145
components,
142146
metadata,
147+
pkg_data,
143148
jlprefix
144149
)
145150

0 commit comments

Comments
 (0)