-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-vba-library.py
More file actions
359 lines (315 loc) · 10.5 KB
/
generate-vba-library.py
File metadata and controls
359 lines (315 loc) · 10.5 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from copy import copy
from tempfile import gettempdir, TemporaryDirectory
from typing import Union
from zebra_vba_packager import (
Config,
Source,
compile_xl,
runmacro_xl,
backup_last_50_paths,
pack
)
from locate import this_dir
from textwrap import dedent
import os
import shutil
from contextlib import suppress
from pathlib import Path
backup_dir = Path(gettempdir(), "BackupVBALib")
with TemporaryDirectory() as tempdir:
if (f := this_dir().joinpath("VBALib")).exists():
backup_last_50_paths(backup_dir, f)
if (f := this_dir().joinpath("VBALib.xlsb")).exists():
backup_last_50_paths(backup_dir, f)
output = this_dir().joinpath("VBALib")
def to_ln(s: Union[str, bytes]):
#if s are bytes
if isinstance(s, bytes):
return s.replace(b"\r\n", b"\n")
else:
return s.replace("\r\n", "\n")
def mid_process(source):
for pth in source.temp_transformed.rglob("*.bas"):
with pth.open("rb") as f:
txt_lines = to_ln(f.read()).split(b"\n")
do_overwrite = False
for i, line in enumerate(txt_lines):
if line.strip().startswith(b"Public"):
if line.strip().split()[2].lower() == b"as":
do_overwrite = True
ii = line.lower().find(b"public")
line = line[0:ii] + b"Private" + line[ii + len(b"public") :]
txt_lines[i] = line
if do_overwrite:
with pth.open("wb") as f:
f.write((b"\r\n".join(txt_lines)))
def office64_ptr_compatability(bin_lines):
bin_lines = copy(bin_lines)
# strip trailing ending lines
i = -1
while (i := i + 1) < len(bin_lines):
if bin_lines[i].strip().lower().split()[1:2] == [b"declare"]:
j = i
for j in range(i, len(bin_lines)):
if not bin_lines[j].strip().endswith(b"_"):
break
line = b" ".join(
[bin_lines[ii].strip().rstrip(b"_") for ii in range(i, j + 1)]
)
bin_lines[i] = line
for ii in range(i, j):
bin_lines.pop(i + 1)
i = -1
while (i := i + 1) < len(bin_lines):
if bin_lines[i].strip().lower().split()[1:2] == [b"declare"]:
txt64 = (
bin_lines[i]
.replace(b" Declare ", b" Declare PtrSafe ")
.replace(b"Long", b"LongPtr")
)
all_pointer_declare.append(
dedent(
f"""
#If VBA7 Then
{txt64.decode('utf-8')}
#Else
{bin_lines[i].decode('utf-8')}
#End If
"""
)
.strip()
.encode("utf-8")
)
bin_lines.pop(i)
i -= 1
return bin_lines
def functions_start(bin_lines):
i = -1
while (i := i + 1) < len(bin_lines):
funcdecl = bin_lines[i].lower().strip().split()[:2]
if (
funcdecl[:1] == [b"function"]
or funcdecl[:1] == [b"sub"]
or funcdecl == [b"private", b"function"]
or funcdecl == [b"private", b"sub"]
or funcdecl == [b"public", b"function"]
or funcdecl == [b"public", b"sub"]
):
break
return i
# Seperate step for this library
common_lib = Config(
Source(
git_source="https://github.com/nylen/vba-common-library.git",
git_rev="1e21b0d", # 2014
glob_include=["*.cls", "*.bas"],
glob_exclude=["*VBALib_VERSION*"],
auto_bas_namespace=False,
auto_cls_rename=False,
)
)
common_lib.run()
all_bas_lines = []
all_pointer_declare = []
all_precode_declare = []
for fpath in common_lib.output_dir.glob("*.bas"):
if "z__" in str(fpath) and str(fpath).lower().endswith(".bas"):
continue
with fpath.open("rb") as f:
txt_lines = to_ln(f.read()).split(b"\n")
txt_lines = office64_ptr_compatability(txt_lines)
i = functions_start(txt_lines)
all_precode_declare.extend(
[j for j in txt_lines[1:i] if not j.lower().strip().startswith(b"option ")]
)
all_bas_lines.extend(txt_lines[i:])
os.remove(fpath)
for fpath in common_lib.output_dir.glob("*.cls"):
with fpath.open("rb") as f:
txt = f.read()
with fpath.open("wb") as f:
f.write(
txt.replace(b"Attribute VB_Exposed = False", b"Attribute VB_Exposed = True")
)
frm1 = b" Dim bytesRead As Long"
to1 = b"""
#If VBA7 Then
Dim bytesRead As LongPtr
#Else
Dim bytesRead As Long
#End If
"""
frm2 = b" Dim ret As Long"
to2 = b"""
#If VBA7 Then
Dim ret As LongPtr
#Else
Dim ret As Long
#End If
"""
with common_lib.output_dir.joinpath("concatenated.bas").open("wb") as f:
f.write(
b"\r\n".join(
[b'Attribute VB_Name = "VLib"']
+ [b"Option Explicit"]
+ all_pointer_declare
+ all_precode_declare
+ all_bas_lines
)
.replace(frm1, to1)
.replace(frm2, to2)
)
# Find all function/sub names and attach "VLib." in front ot them
vlib_funcnames = set()
for i in all_bas_lines:
fline = i.strip().split()
with suppress(IndexError):
if fline[0].lower() in (b"function", b"sub") or fline[1].lower() in (
b"function",
b"sub",
):
vlib_funcnames.add(fline[1].decode("utf-8").split("(")[0])
vlib_funcnames.add(fline[2].decode("utf-8").split("(")[0])
vlib_namespace_fix = [(i, "VLib." + i) for i in vlib_funcnames]
vlib_renames = [
(
lambda x: x.lower().startswith("vbalib_excel"),
lambda x: "z_VLib" + x[len("vbalib_excel") :],
),
(
lambda x: x.lower().startswith("vbalib_"),
lambda x: "z_VLib" + x[len("vbalib_") :],
),
]
common_lib_output_dir2 = Path(str(common_lib.output_dir) + "2")
shutil.rmtree(common_lib_output_dir2, ignore_errors=True)
shutil.copytree(common_lib.output_dir, common_lib_output_dir2)
# -----------------------------------------------------------------------
# Aggregate all the rest of the sources
# -----------------------------------------------------------------------
Config(
## TODO: why doesn't auto_cls_rename work?
# Source(
# git_source="https://github.com/sancarn/stdVBA.git",
# git_rev="50fdb99", # 2021-07
# glob_include=['src/*.cls'],
# auto_cls_rename=True
# ),
Source(
git_source="https://github.com/VirtualActuary/SHA256-and-MD5-for-VBA.git",
glob_include=["*.cls"],
rename_overwrites={"HS256": "z__Hash"},
),
Source(
git_source="https://github.com/ws-garcia/VBA-CSV-interface.git",
git_rev="1b23247", # 2022-03
glob_include=["**/src/*.cls"],
rename_overwrites={
"CSVArrayList": "z_WsCsvArrayList",
"CSVcallBack": "z_WsCsvCallBack",
"CSVdialect": "z_WsCsvDialect",
"CSVexpressions": "z_WsCsvExpressions",
"CSVparserConfig": "z_WsCsvParserConfig",
"CSVSniffer": "z_WsCsvSniffer",
"CSVTextStream": "z_WsCsvTextStream",
"CSVudFunctions": "z_WsCsvUdFunctions",
"CSVinterface": "z_WsCsvInterface", # useful
},
),
Source(
git_source="https://github.com/GustavBrock/VBA.Compress.git",
git_rev="052b889", # 2020
glob_include=["**/*.bas"],
mid_process=mid_process,
rename_overwrites={
"FileCompress": "Compress",
},
),
# The following two projects are dependant on each other:
Source(
git_source="https://github.com/VBA-tools/VBA-JSON.git",
git_rev="v2.3.1", # 2019
glob_include=["**/JsonConverter.bas"],
mid_process=mid_process,
rename_overwrites={
"JsonConverter": "Json", # bas file
},
),
Source(
git_source="https://github.com/sdkn104/VBA-CSV.git",
git_rev="48d98d6", # 2020
glob_include=["**/CSVUtils.bas"],
mid_process=mid_process,
rename_overwrites={
"CSVUtils": "CsvUtils",
},
),
Source(
git_source="https://github.com/todar/VBA-Strings",
git_rev="6d25dad", # 2020
glob_include=["*.bas"],
rename_overwrites={"StringFunctions": "StrUtils"},
),
Source(
path_source=common_lib.output_dir,
glob_include=["*.cls"],
glob_exclude=["z__*"],
rename_overwrites=vlib_namespace_fix + vlib_renames,
),
Source(
path_source=common_lib_output_dir2,
glob_include=["*.bas"],
glob_exclude=["z__*"],
rename_overwrites=vlib_renames,
),
Source(
git_source="https://github.com/VirtualActuary/MiscVBAFunctions.git",
git_rev="v2.5.0",
glob_include=[
"MiscVBAFunctions/**/*.bas",
"MiscVBAFunctions/**/*.cls",
"**/thisworkbook.txt",
],
glob_exclude=["**/Test__*", "**/Casing.bas"],
combine_bas_files="Fn",
auto_bas_namespace=True,
auto_cls_rename=False,
),
#Source(path_source=this_dir().joinpath("add_examples"), auto_bas_namespace=False),
).run(output)
# Add empty xlsx file
shutil.copy2(
empty_src := this_dir().joinpath("add_empty_workbook", "empty.xlsx"),
empty_dest := output.joinpath("empty.xlsx"),
)
# Add examples
shutil.copytree(
this_dir().joinpath("add_examples"),
examples_dest := output.joinpath("examples")
)
# Make library into an .xlsb file
compile_xl(output, xl_final := this_dir().joinpath("VBALib.xlsb"))
runmacro_xl(xl_final)
os.remove(empty_dest)
shutil.rmtree(examples_dest)
with suppress(OSError):
os.remove(zip_dest := this_dir().joinpath("VBALib.zip"))
pack(output, zip_dest, compression_type="zip")
"""
Usefull VBA projects to choose from
https://github.com/sancarn/stdVBA.git (looks promising)
https://github.com/sdkn104/VBA-CSV
https://github.com/VBA-tools
https://github.com/GustavBrock/VBA.Compress
https://github.com/AllenMattson/VBA (???)
https://github.com/carvetighter/VBA-Code-Library
https://github.com/Zadigo/vba_codes
https://github.com/topics/vba-modules (further collection)
https://github.com/Greedquest/VBA-Toolbox (Next level stuff, but bit risky:
https://github.com/Greedquest/VBA-Toolbox/blob/master/ToolboxSource/TextWriter.cls
https://github.com/nylen/vba-common-library (maybe use tables?)
https://github.com/vbaidiot/ariawase
https://github.com/omegastripes/VBA-JSON-parser
https://github.com/VBA-tools/VBA-JSON.git
https://github.com/x-vba/xlib <--------------- << Combine into single namespace >>
"""