-
Notifications
You must be signed in to change notification settings - Fork 2
/
pack.py
110 lines (93 loc) · 3.11 KB
/
pack.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
import os
import shutil
import datetime
if os.path.exists("release"):
shutil.rmtree("release")
print("Deleting release directory.")
print("Recreating release directory.")
os.mkdir("release")
print("Copying base files.")
shutil.copy("main.lua", "release/main.lua")
shutil.copy("LICENSE", "release/LICENSE")
shutil.copy("id.txt", "release/id.txt")
shutil.copy("info.txt", "release/info.txt")
shutil.copy("preview.jpg", "release/preview.jpg")
print("Creating source directory.")
os.mkdir("release/source")
print("Copying project files.")
# define the function, (it copies a tree btw.)
def copytree(src, dst, symlinks=False, ignore=None):
if not os.path.exists(dst):
os.makedirs(dst)
shutil.copystat(src, dst)
lst = os.listdir(src)
if ignore:
excl = ignore(src, lst)
lst = [x for x in lst if x not in excl]
for item in lst:
s = os.path.join(src, item)
d = os.path.join(dst, item)
if symlinks and os.path.islink(s):
if os.path.lexists(d):
os.remove(d)
os.symlink(os.readlink(s), d)
try:
st = os.lstat(s)
mode = stat.S_IMODE(st.st_mode)
os.lchmod(d, mode)
except:
pass # lchmod not available
elif os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
# call the function ;)
copytree("source", "release/source")
# remove junk code basically
print("Removing comments, unnecessary spaces, and newlines from .lua files.")
# array
lua_files = []
# populate array with all of the source files
for root, dirs, files in os.walk("release/source"):
for file in files:
if file.endswith(".lua"):
lua_files.append(os.path.join(root, file))
# do the optimizations
# 04.11.2023, we save around 34KB ~ 36KB
for file in lua_files:
print(file)
with open(file, "r") as f:
lines = f.readlines()
with open(file, "w") as f:
for line in lines:
line = line.split('--', 1)[0].lstrip().rstrip() # remove comments and trailing spaces
if line:
f.write(line + "\n")
print("")
print("Updating local.lua")
# Location of local.lua file
file = "release/source/utils/local.lua"
# Get the current date
date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Check if file exists
if os.path.isfile(file):
# Replace the line
with open(file, "r") as f:
lines = f.readlines()
with open(file, "w") as f:
for line in lines:
if 'fProjectName = "Tearware Github Preview"' in line:
line = line.replace('fProjectName = "Tearware Github Preview"', 'fProjectName = "Tearware"')
f.write(line)
print("Line replaced successfully in", file)
else:
print("Error:", file, "not found")
print("Adding 'Packaged on", date, "to main.lua")
file = "release/main.lua"
if os.path.isfile(file):
with open(file, "a") as f:
f.write("\n-- Packaged on " + date + "\n")
print("Line added successfully in", file)
else:
print("Error:", file, "not found")
print("Finished.")