-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify.py
executable file
·84 lines (62 loc) · 2.93 KB
/
minify.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
#!/usr/bin/env python3
# All CSS content in the UI bundle directory is copied to 'site.css' then minified.
# Antora uses 'site.css' (improving site performance), while designers continue to use
# the original, non-minified CSS files (enhancing human readability).
import os
import subprocess
import hashlib
from utils.root import get_project_root
# Get the project root directory
PROJECT_ROOT = get_project_root()
DIRECTORIES = [] # If more than one UI bundle, add its name (i.e. 'docs', 'widget'). Otherwise, leave blank.
# Get the path to the 'current_directory'. If 'current_directory' is empty,
# use the default UI bundle directory, i.e. 'ui-bundle/css/'.
def get_directory_path(current_directory):
if current_directory == "":
return os.path.join(PROJECT_ROOT, "ui-bundle/css/")
else:
return os.path.join(PROJECT_ROOT, f"ui-bundle-{current_directory}/css/")
def merge_and_minify_css_files(directory, output_file):
merged_content = ""
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.css') and file != 'site.css':
with open(os.path.join(root, file), 'r') as f:
merged_content += f.read()
# Use postcess to minify css files.
with open(output_file, 'w') as f:
process = subprocess.Popen(['npx', 'postcss', '--use', 'postcss-import', 'postcss-clean', '--no-map'],
stdin=subprocess.PIPE, stdout=f)
process.communicate(input=merged_content.encode())
def remove_comments_and_append_header(file_path):
with open(file_path, 'r') as f:
content = f.readlines()
# Remove comments
content = [line for line in content if not line.strip().startswith("/*") and not line.strip().endswith("*/")]
# Add header
header = "/* DO NOT EDIT: 'site.css' is auto-generated from the minified output of " \
"'default-styles.css' and 'custom-styles.css'. */\n"
content.insert(0, header)
with open(file_path, 'w') as f:
f.writelines(content)
def calculate_md5(file_path):
hasher = hashlib.md5()
with open(file_path, 'rb') as f:
buf = f.read()
hasher.update(buf)
return hasher.hexdigest()
# Minify the 'site.css' file located in 'DIRECTORIES', but only if
# changes where made to one or more CSS file in that directory.
def main():
for directory in DIRECTORIES:
temp_file = os.path.join(get_directory_path(directory), "temp.css")
merge_and_minify_css_files(get_directory_path(directory), temp_file)
remove_comments_and_append_header(temp_file)
original_checksum = calculate_md5(os.path.join(get_directory_path(directory), "site.css"))
new_checksum = calculate_md5(temp_file)
if original_checksum != new_checksum:
os.rename(temp_file, os.path.join(get_directory_path(directory), "site.css"))
else:
os.remove(temp_file)
if __name__ == "__main__":
main()