22
33import glob
44import os
5+ import re
56import shutil
67import subprocess
78from hashlib import md5
1112import requests
1213from bs4 import BeautifulSoup
1314
15+ # Load version from version.py
16+ version = None
17+ with open ("version.py" , "r" ) as version_file :
18+ for line in version_file :
19+ if line .startswith ("version" ):
20+ version = line .split ("=" )[1 ].strip ().strip ('"' )
21+
1422
1523def get_files (path , extension , recursive = False ):
1624 """Generate filepaths for each file into path with the target extension."""
@@ -25,25 +33,28 @@ def get_files(path, extension, recursive=False):
2533
2634def find_list_resources (tag , attribute , soup ):
2735 """Find resources based off of their tag and attributes."""
28- list = []
36+ resources = []
2937 for x in soup .findAll (tag ):
3038 try :
31- list .append (x [attribute ])
39+ resources .append (x [attribute ])
3240 except KeyError :
3341 pass
34- return list
42+ return resources
3543
3644
3745for f in [* get_files (os .getcwd (), "html.jinja2" , recursive = True ), * get_files (os .getcwd (), "html" , recursive = True )]:
3846 with open (f , "r" ) as reader :
39- html : str = reader .read ()
47+ html = reader .read ()
4048 soup = BeautifulSoup (html , features = "html.parser" )
4149
4250 image_src = find_list_resources ("img" , "src" , soup )
4351 script_src = find_list_resources ("script" , "src" , soup )
4452 css_link = find_list_resources ("link" , "href" , soup )
53+
54+ # Update HTML file links
4555 for link in [* css_link , * script_src , * image_src ]:
4656 if "http://" in link or "https://" in link :
57+ # External link
4758 pre = ""
4859 if "wiki/" in f :
4960 pre = "."
@@ -52,19 +63,20 @@ def find_list_resources(tag, attribute, soup):
5263 req = requests .get (link , allow_redirects = False )
5364 open (f".{ file_name } " , "wb" ).write (req .content )
5465 html = html .replace (link , f".{ pre } { file_name } " )
55- with open (f , "w" ) as writer :
56- writer .write (html )
66+ elif link .endswith (".js" ):
67+ # Local JavaScript file - append version query parameter
68+ html = html .replace (link , f"{ link } ?v={ version } " )
69+
70+ # Update JavaScript blocks
71+ pattern = r'(\{ src: [\'"])(\.\/static\/js\/.*?\.js)([\'"], defer: true \})'
72+ updated_html = re .sub (pattern , rf"\1\2?v={ version } \3" , html )
73+
74+ # Write the modified HTML content back to the file
75+ with open (f , "w" ) as writer :
76+ writer .write (updated_html )
5777
58- # subprocess.run(["css-html-js-minify", "static/styles/", "--overwrite"])
59- # subprocess.run(["pyminify", "-i", "."])
6078subprocess .run (["python3" , "setup.py" , "bdist_wheel" ])
6179shutil .copyfile ("dist/dk64rando-1.0.0-py3-none-any.whl" , "static/py_libraries/dk64rando-1.0.0-py3-none-any.whl" )
62- with open ("./static/py_libraries/dk64rando-1.0.0-py3-none-any.whl" , "rb" ) as file :
63- wheel = file .read ()
64- hash = md5 (wheel ).hexdigest ()
65- with open ("version.py" , "a" ) as version :
66- version .write (f'\n whl_hash = "{ hash } "' )
67-
6880
6981# Create the file Gemfile
7082with open ("Gemfile" , "w" ) as file :
0 commit comments