-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini.py
152 lines (118 loc) · 5.03 KB
/
mini.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
#!/usr/bin/env python3
import os
import re
import json
import argparse
from pathlib import Path
class MudletBuilder:
def __init__(self, project_root, output_file):
self.project_root = Path(project_root)
self.output_file = Path(output_file)
self.scripts_config = None
def load_scripts_config(self):
"""Load the scripts.json configuration file."""
config_path = self.project_root / 'scripts.json'
if not config_path.exists():
raise FileNotFoundError(f"scripts.json not found in {self.project_root}")
with open(config_path, 'r', encoding='utf-8') as f:
self.scripts_config = json.load(f)
def minify(self, content):
"""Basic Lua minification."""
# Remove comments (but keep shebangs)
lines = content.split('\n')
processed_lines = []
in_multiline_comment = False
for line in lines:
# Handle multiline comments
if in_multiline_comment:
if ']]' in line:
line = line[line.find(']]') + 2:]
in_multiline_comment = False
else:
continue
if '--[[' in line:
if ']]' in line:
# Single-line multi-line comment
line = line[:line.find('--[[')] + line[line.find(']]') + 2:]
else:
line = line[:line.find('--[[')]
in_multiline_comment = True
# Handle single-line comments
if not in_multiline_comment and '--' in line:
line = line[:line.find('--')]
if line.strip():
processed_lines.append(line)
content = '\n'.join(processed_lines)
# Remove unnecessary whitespace
content = re.sub(r'\s+', ' ', content)
content = re.sub(r';\s+', ';', content)
content = re.sub(r'\s*([=+\-*/<>()])\s*', r'\1', content)
# Preserve some readability with newlines between statements
content = re.sub(r'([\n;])\s*', r'\1\n', content)
return content.strip()
def process_scripts(self):
"""Process all scripts in the order specified by scripts.json."""
if not self.scripts_config:
self.load_scripts_config()
processed_content = []
total_size = 0
# Add a header comment
header = f"-- Generated by MudletBuilder\n"
processed_content.append(header)
# Process each script entry
for script_entry in self.scripts_config:
# Skip if not active
if script_entry.get("isActive", "").lower() != "yes":
print(f"Skipping inactive script: {script_entry['name']}")
continue
# Skip folders
if script_entry.get("isFolder", "").lower() == "yes":
print(f"Skipping folder: {script_entry['name']}")
continue
script_name = f"{script_entry['name']}.lua"
script_path = self.project_root / script_name
if not script_path.exists():
print(f"Warning: Script not found: {script_path}")
continue
print(f"Processing: {script_name}")
with open(script_path, 'r', encoding='utf-8') as f:
content = f.read()
total_size += len(content)
# Add a comment to mark the start of each file
processed_content.append(f"\n-- File: {script_name}")
processed_content.append(content)
return '\n'.join(processed_content), total_size
def build(self, minify=True):
"""Build the final output."""
print(f"Building Mudlet project from {self.project_root}")
# Process all scripts
content, original_size = self.process_scripts()
# Minify if requested
if minify:
minified_content = self.minify(content)
else:
minified_content = content
# Create output directory if it doesn't exist
self.output_file.parent.mkdir(parents=True, exist_ok=True)
# Write the output
with open(self.output_file, 'w', encoding='utf-8') as f:
f.write(minified_content)
# Print build statistics
print(f"\nBuild completed: {self.output_file}")
print(f"Original size: {original_size:,} bytes")
print(f"Final size: {len(minified_content):,} bytes")
if minify:
print(f"Reduction: {(1 - len(minified_content)/original_size)*100:.1f}%")
def main():
parser = argparse.ArgumentParser(description='Build and minify Mudlet project')
parser.add_argument('project_root', help='Root directory of the Mudlet project')
parser.add_argument('output_file', help='Output file path')
parser.add_argument('--no-minify', action='store_true', help='Skip minification')
args = parser.parse_args()
builder = MudletBuilder(
project_root=args.project_root,
output_file=args.output_file
)
builder.build(minify=not args.no_minify)
if __name__ == '__main__':
main()