6
6
import shutil
7
7
import glob
8
8
import warnings
9
+ import sys
10
+ import importlib
11
+ import uuid
9
12
10
13
from ._all_keywords import julia_keywords
11
14
from ._py_components_generation import reorder_props
12
15
16
+ #uuid of Dash Julia package. Used as base for component package uuid
17
+ jl_dash_uuid = "1b08a953-4be3-4667-9a23-3db579824955"
13
18
14
19
# Declaring longer string templates as globals to improve
15
20
# readability, make method logic clearer to anyone inspecting
16
21
# code below
17
22
jl_component_string = '''
23
+ export {funcname}
24
+
18
25
"""
19
26
{funcname}(;kwargs...)
20
27
{funcname}(children::Any;kwargs...)
50
57
{funcname}(children_maker::Function; kwargs...) = {funcname}(children_maker(); kwargs...)
51
58
''' # noqa:E501
52
59
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 )
53
111
54
112
def stringify_wildcards (wclist , no_symbol = False ):
55
113
if no_symbol :
@@ -306,6 +364,69 @@ def format_fn_name(prefix, name):
306
364
return name .lower ()
307
365
308
366
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
+
309
430
def generate_class_string (name , props , description , project_shortname , prefix ):
310
431
# Ensure props are ordered with children first
311
432
filtered_props = reorder_props (filter_props (props ))
@@ -374,6 +495,7 @@ def generate_module(
374
495
project_shortname ,
375
496
components ,
376
497
metadata ,
498
+ pkg_data ,
377
499
prefix ,
378
500
** kwargs
379
501
):
@@ -398,3 +520,9 @@ def generate_module(
398
520
399
521
for sourcemap in glob .glob ("{}/*.map" .format (project_shortname )):
400
522
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
+
0 commit comments