Friendiiverse/Bundle.py

61 lines
1.9 KiB
Python
Raw Normal View History

2023-04-19 00:16:35 +02:00
#!/usr/bin/env python3
import os
2023-04-24 15:03:51 +02:00
from base64 import b64encode
from mimetypes import guess_type
2023-04-19 00:16:35 +02:00
from pathlib import Path
os.chdir(os.path.dirname(os.path.abspath(__file__)))
2023-04-19 00:16:35 +02:00
os.makedirs('./Dist', exist_ok=True)
os.chdir('./Build')
2023-04-20 15:17:00 +02:00
with open(f'./Friendiiverse.html', 'r') as Base:
2023-04-19 00:16:35 +02:00
Base = Base.read()
2023-04-20 15:17:00 +02:00
def FragReplace(Find, Replace, Pattern='*.*'):
2023-04-19 00:16:35 +02:00
global Base
for File in Path('./').rglob(Pattern):
2023-04-20 15:17:00 +02:00
File = str(File)
2023-04-19 00:16:35 +02:00
with open(File, 'r') as Frag:
Frag = Frag.read()
#if Pattern.endswith('*.js') and not File.startswith('Lib/'):
# Frag = MinifyJs(Frag)
Frag = Replace.format(File=File, Frag=Frag)
2023-04-19 00:16:35 +02:00
for Prefix in ('', './'):
2023-04-24 15:03:51 +02:00
Name = Prefix + File
Base = Base.replace(Find.format(File=Name), Frag)
BaseNew = ''
Split = '<script data-build-json="true">'
Frags = Base.split(Split)
BaseNew += Frags[0] + Split
Split = '</script>'
Frags = Frags[1].split(Split)
for File in Path('./Assets').rglob('*.*'):
File = str(File)
Mime = guess_type(File)
Mime = (Mime[0] if Mime else 'application/octet-stream')
with open(File, 'rb') as Frag:
Frag = b64encode(Frag.read()).decode()
for Prefix in ('', './'):
Name = Prefix + '/'.join(File.split('/')[1:])
Frags[0] = Frags[0].replace(f'"{Name}"', f'"data:{Mime};base64,{Frag}"')
BaseNew += Split.join(Frags)
Base = BaseNew
2023-04-23 19:46:01 +02:00
2023-04-20 15:17:00 +02:00
FragReplace('<link rel="stylesheet" href="{File}"/>', '<style data-source="{File}">{Frag}</style>', '*.css')
FragReplace('<script src="{File}"></script>', '<script data-source="{File}">{Frag}</script>', '*.js')
2023-04-19 00:16:35 +02:00
2023-04-26 15:33:51 +02:00
ScriptFrags = ''
for File in Path('./Polyfill').rglob('*.js'):
File = str(File)
#Folder = '/'.join(File.split('/')[:-1])
#File = File.split('/')[-1]
ScriptFrags += f'<script>{open(File, "r").read()}</script>'
Base = Base.replace('<script folder="./Polyfill"></script>', ScriptFrags)
2023-04-20 15:17:00 +02:00
os.chdir('..')
2023-04-19 00:16:35 +02:00
with open('./Dist/Friendiiverse.html', 'w') as Build:
Build.write(Base)