-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild.py
171 lines (142 loc) · 5.68 KB
/
build.py
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
import click
import shutil
import os
import os.path as path
import subprocess
@click.command()
@click.option(
"--target",
type=click.Choice(["merge-plugins-hide", "sync-mod-order", "link-deploy", "all"]),
help="The target plugin to build",
default="all",
prompt="Select the plugin to build",
)
@click.option(
"--zip",
is_flag=True,
help="Zip the built plugin",
)
def cli(target: str, zip: bool):
paths = {
"src": path.join(path.dirname(__file__), "src"),
"out": path.join(path.dirname(__file__), "target", target),
"build": path.join(path.dirname(__file__), "build"),
}
plugin = target.replace("-", "_")
# Separate all to use the old code for now while i work out the per-plugin code
if target == "all":
return build_all(zip)
click.echo("Copying source files to the build folder...")
if path.exists(paths["out"]):
shutil.rmtree(paths["out"])
os.makedirs(paths["out"])
for file in ["common.py", f"{plugin}.py"]:
shutil.copy2(path.join(paths["src"], file), paths["out"])
with open(path.join(paths["src"], "plugin.__init__.py"), "r") as file:
with open(path.join(paths["out"], "__init__.py"), "w") as out_file:
out_file.write(file.read().replace("PLUGIN", plugin))
shutil.copy2("LICENSE", paths["out"])
click.echo("Source files copied successfully")
# Run scour on all svg files
click.echo("Setting up resources...")
if path.exists(paths["build"]):
shutil.rmtree(paths["build"])
os.makedirs(paths["build"])
# Remove unused files from resources.qrc and write it to the build folder
with open(path.join(paths["src"], "resources.qrc"), "r") as in_file:
with open(path.join(paths["build"], "resources.qrc"), "w") as out_file:
out_file.writelines(
[line for line in in_file if "<file>" not in line or plugin in line]
)
svg_files = [
path.join(dirpath, filename)
for dirpath, _, filenames in os.walk(paths["src"])
for filename in filenames
if filename.endswith(".svg")
]
for file in svg_files:
scour_file(file, path.join(paths["build"], path.basename(file)))
click.echo("Resources ready for packaging")
# Run rcc and write the output to resources.py
click.echo("Running rcc...")
rcc_data = compile_rcc(path.join(paths["build"], "resources.qrc"))
with open(path.join(paths["out"], "resources.py"), "w") as file:
file.write(rcc_data)
shutil.rmtree(paths["build"])
click.echo("rcc executed successfully")
if zip:
click.echo("Zipping the plugin...")
shutil.make_archive(
paths["out"], "zip", path.dirname(paths["out"]), path.basename(paths["out"])
)
click.echo("Plugin zipped successfully")
click.echo("Script completed successfully")
def build_all(zip: bool):
target = "deorder-plugins"
paths = {
"src": path.join(path.dirname(__file__), "src"),
"out": path.join(path.dirname(__file__), "target", target),
"build": path.join(path.dirname(__file__), "build"),
}
# Set up output folder
click.echo("Copying source files to the build folder...")
if path.exists(paths["out"]):
shutil.rmtree(paths["out"])
os.makedirs(paths["out"])
# Copy all .py files and LICENSE to the build folder
excluded_files = ["resources.py", "plugin.__init__.py"]
for file in os.listdir(paths["src"]):
if file.endswith(".py") and file not in excluded_files:
shutil.copy2(path.join(paths["src"], file), paths["out"])
shutil.copy2("LICENSE", paths["out"])
click.echo("Source files copied successfully")
# Run scour on all svg files
click.echo("Setting up resources...")
if path.exists(paths["build"]):
shutil.rmtree(paths["build"])
os.makedirs(paths["build"])
shutil.copy2(path.join(paths["src"], "resources.qrc"), paths["build"])
svg_files = [
path.join(dirpath, filename)
for dirpath, _, filenames in os.walk(paths["src"])
for filename in filenames
if filename.endswith(".svg")
]
for file in svg_files:
scour_file(file, path.join(paths["build"], path.basename(file)))
click.echo("Resources ready for packaging")
# Run rcc.exe with the specified parameters
click.echo("Running rcc...")
rcc_data = compile_rcc(path.join(paths["build"], "resources.qrc"))
with open(path.join(paths["out"], "resources.py"), "w") as file:
file.write(rcc_data)
shutil.rmtree(paths["build"])
click.echo("rcc executed successfully")
if zip:
click.echo("Zipping the plugin...")
shutil.make_archive(
paths["out"], "zip", path.dirname(paths["out"]), path.basename(paths["out"])
)
click.echo("Plugin zipped successfully")
click.echo("Script completed successfully")
def scour_file(input: str, output: str):
return subprocess.run(
f"scour -i {input} -o {output} --quiet --enable-id-stripping --remove-descriptive-elements --enable-comment-stripping --indent=none --no-line-breaks".split(),
check=True,
)
def compile_rcc(resources_qrc: str):
try:
rcc = subprocess.run(
f"pyside6-rcc -g python -compress 2 -threshold 30 {resources_qrc}".split(),
check=True,
capture_output=True,
text=True,
)
rcc_output = rcc.stdout
return rcc_output.replace("from PySide6", "from PyQt6")
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Error occurred while running rcc: {e.stderr}")
except Exception as e:
raise RuntimeError(f"An error occurred: {str(e)}")
if __name__ == "__main__":
cli()