File size: 3,593 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# This scripts modifies AllPlugins.vue to automatically load all the plugins.

import glob
import os
import re

print("Running add_plugins.py\n")

core_plugins = glob.glob("../src/Plugins/Core/**/*Plugin.vue", recursive=True)
optional_plugins = glob.glob("../src/Plugins/Optional/**/*Plugin.vue", recursive=True)

# Sort by basename
core_plugins.sort(key=lambda x: os.path.basename(x))
optional_plugins.sort(key=lambda x: os.path.basename(x))

# Remove if os.basename is AboutPlugin.vue (handled special case in
# AllPlugins.vue). Same with HelpPlugin.vue
core_plugins = [
    x
    for x in core_plugins
    if os.path.basename(x) not in ["AboutPlugin.vue", "HelpPlugin.vue"]
]


all_plugins = "../src/Plugins/AllPlugins.vue"

# Make a backup of all_plugins
with open(all_plugins, "r") as file:
    content = file.read()
with open(f"{all_plugins}.bak", "w") as file:
    file.write(content)

# Modify the content
template1 = "<!-- TEMPLATE1 START -->\n"
for core_plugin in core_plugins:
    plugin_name = os.path.basename(core_plugin).replace(".vue", "").replace(".ts", "")
    template1 += f'    <{plugin_name} @onPluginSetup="onPluginSetup"></{plugin_name}>\n'
template1 += "\n"
for optional_plugin in optional_plugins:
    plugin_name = (
        os.path.basename(optional_plugin).replace(".vue", "").replace(".ts", "")
    )
    template1 += f'    <{plugin_name} @onPluginSetup="onPluginSetup"></{plugin_name}>\n'
template1 += "    <!-- TEMPLATE1 END -->"
# print(template1)

# This section is now empty because imports are handled dynamically inside defineAsyncComponent.
template2 = "// TEMPLATE2 START\n// TEMPLATE2 END"
# template2 = "// TEMPLATE2 START\n"
# for core_plugin in core_plugins:
#     plugin_name = os.path.basename(core_plugin).replace(".vue", "").replace(".ts", "")
#     plugin_path = core_plugin.replace("../src/Plugins/", "./")
#     template2 += f'import {plugin_name} from "{plugin_path}";\n'
# template2 += "\n"
# for optional_plugin in optional_plugins:
#     plugin_name = (
#         os.path.basename(optional_plugin).replace(".vue", "").replace(".ts", "")
#     )
#     plugin_path = optional_plugin.replace("../src/Plugins/", "./")
#     template2 += f'import {plugin_name} from "{plugin_path}";\n'
# template2 += "// TEMPLATE2 END"

template3 = "// TEMPLATE3 START\n"
for core_plugin in core_plugins:
    plugin_name = os.path.basename(core_plugin).replace(".vue", "").replace(".ts", "")
    # Use Webpack's alias '@' for the src directory to create a robust path
    plugin_path = core_plugin.replace("../src/", "@/")
    template3 += f'    {plugin_name}: defineAsyncComponent(() => import("{plugin_path}")),\n'
template3 += "\n"
for optional_plugin in optional_plugins:
    plugin_name = (
        os.path.basename(optional_plugin).replace(".vue", "").replace(".ts", "")
    )
    plugin_path = optional_plugin.replace("../src/", "@/")
    template3 += f'    {plugin_name}: defineAsyncComponent(() => import("{plugin_path}")),\n'
template3 += " // TEMPLATE3 END"
# print(template3)

# Insert template1
content = re.sub(
    r"<!-- TEMPLATE1 START -->.*<!-- TEMPLATE1 END -->",
    template1,
    content,
    flags=re.DOTALL,
)

# Insert template2
content = re.sub(
    r"// TEMPLATE2 START.*// TEMPLATE2 END", template2, content, flags=re.DOTALL,
)

# Insert template3
content = re.sub(
    r"// TEMPLATE3 START.*// TEMPLATE3 END", template3, content, flags=re.DOTALL,
)
# print(content)

while "\n\n\n" in content:
    content = content.replace("\n\n\n", "\n\n")

# Write the modified content
with open(all_plugins, "w") as file:
    file.write(content)