Skip to content

Commit

Permalink
refactor: 简化和格式化 publish.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Oct 11, 2023
1 parent 0b58d65 commit 642f1ae
Showing 1 changed file with 37 additions and 60 deletions.
97 changes: 37 additions & 60 deletions publish.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import ctypes, sys
from ctypes import windll, wintypes
import uuid
import subprocess
import sys
import os
import subprocess
import glob
import shutil
from xml.etree import ElementTree
Expand All @@ -12,7 +10,7 @@
if os.environ["GITHUB_ACTIONS"].lower() == "true":
# 不知为何在 Github Actions 中运行时默认编码为 ANSI,并且 print 需刷新流才能正常显示
for stream in [sys.stdout, sys.stderr]:
stream.reconfigure(encoding='utf-8')
stream.reconfigure(encoding="utf-8")
except:
pass

Expand All @@ -22,52 +20,16 @@
#
#####################################################################

class FOLDERID:
ProgramFilesX86 = uuid.UUID('{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}')

# 包装 SHGetKnownFolderPath,来自 https://gist.github.com/mkropat/7550097
def get_known_folder_path(folderid):
class GUID(ctypes.Structure):
_fields_ = [
("Data1", wintypes.DWORD),
("Data2", wintypes.WORD),
("Data3", wintypes.WORD),
("Data4", wintypes.BYTE * 8)
]

def __init__(self, uuid_):
ctypes.Structure.__init__(self)
self.Data1, self.Data2, self.Data3, self.Data4[0], self.Data4[1], rest = uuid_.fields
for i in range(2, 8):
self.Data4[i] = rest>>(8 - i - 1)*8 & 0xff

CoTaskMemFree = windll.ole32.CoTaskMemFree
CoTaskMemFree.restype = None
CoTaskMemFree.argtypes = [ctypes.c_void_p]

SHGetKnownFolderPath = windll.shell32.SHGetKnownFolderPath
SHGetKnownFolderPath.argtypes = [
ctypes.POINTER(GUID),
wintypes.DWORD,
wintypes.HANDLE,
ctypes.POINTER(ctypes.c_wchar_p)
]

fid = GUID(folderid)
pPath = ctypes.c_wchar_p()
if SHGetKnownFolderPath(ctypes.byref(fid), 0, wintypes.HANDLE(0), ctypes.byref(pPath)) != 0:
raise FileNotFoundError()
path = pPath.value
CoTaskMemFree(pPath)
return path

programFilesX86Path = get_known_folder_path(FOLDERID.ProgramFilesX86);

programFilesX86Path = os.path.expandvars("%ProgramFiles(x86)%")
vswherePath = programFilesX86Path + "\\Microsoft Visual Studio\\Installer\\vswhere.exe"
if not os.access(vswherePath, os.X_OK):
raise Exception("未找到 vswhere")

process = subprocess.run(vswherePath + " -latest -requires Microsoft.Component.MSBuild -find MSBuild\\**\\Bin\\MSBuild.exe", capture_output=True)
process = subprocess.run(
vswherePath
+ " -latest -requires Microsoft.Component.MSBuild -find MSBuild\\**\\Bin\\MSBuild.exe",
capture_output=True,
)
msbuildPath = str(process.stdout, encoding="utf-8").splitlines()[0]
if not os.access(msbuildPath, os.X_OK):
raise Exception("未找到 msbuild")
Expand All @@ -80,19 +42,24 @@ def __init__(self, uuid_):

os.chdir(os.path.dirname(__file__))

if os.system("\"" + msbuildPath + "\" /p:Configuration=Release;Platform=x64 src\\CONAN_INSTALL") != 0:
cmd = f'"{msbuildPath}" /p:Configuration=Release;Platform=x64 src\\CONAN_INSTALL'
if os.system(cmd) != 0:
raise Exception("编译 CONAN_INSTALL 失败")

if os.system("\"" + msbuildPath + "\" /p:Configuration=Release;Platform=x64;OutDir=..\\..\\publish\\ src\\Effects") != 0:
cmd = f'"{msbuildPath}" /p:Configuration=Release;Platform=x64;OutDir=..\\..\\publish\\ src\\Effects'
if os.system(cmd) != 0:
raise Exception("编译 Effects 失败")

if os.system("\"" + msbuildPath + "\" /p:Configuration=Release;Platform=x64;OutDir=..\\..\\publish\\ src\\Magpie.Core") != 0:
cmd = f'"{msbuildPath}" /p:Configuration=Release;Platform=x64;OutDir=..\\..\\publish\\ src\\Magpie.Core'
if os.system(cmd) != 0:
raise Exception("编译 Magpie.Core 失败")

if os.system("\"" + msbuildPath + "\" /p:Configuration=Release;Platform=x64;BuildProjectReferences=false;OutDir=..\\..\\publish\\ src\\Magpie") != 0:
cmd = f'"{msbuildPath}" /p:Configuration=Release;Platform=x64;BuildProjectReferences=false;OutDir=..\\..\\publish\\ src\\Magpie'
if os.system(cmd) != 0:
raise Exception("编译 Magpie 失败")

if os.system("\"" + msbuildPath + "\" /p:Configuration=Release;Platform=x64;OutDir=..\\..\\publish\\ src\\Updater") != 0:
cmd = f'"{msbuildPath}" /p:Configuration=Release;Platform=x64;OutDir=..\\..\\publish\\ src\\Updater'
if os.system(cmd) != 0:
raise Exception("编译 Updater 失败")

#####################################################################
Expand All @@ -103,20 +70,23 @@ def __init__(self, uuid_):

os.chdir(os.getcwd() + "\\publish")


# 删除文件,忽略错误
def remove_file(file):
try:
os.remove(file)
except:
pass


# 删除文件夹,忽略错误
def remove_folder(folder):
try:
shutil.rmtree(folder)
except:
pass


remove_file("Microsoft.Web.WebView2.Core.dll")

for pattern in ["*.pdb", "*.lib", "*.exp", "*.winmd", "*.xml", "*.xbf", "dummy.*"]:
Expand All @@ -125,7 +95,7 @@ def remove_folder(folder):

for file in glob.glob("*.pri"):
if file != "resources.pri":
remove_file(file)
remove_file(file)

for folder in ["Microsoft.UI.Xaml", "Magpie.App"]:
remove_folder(folder)
Expand All @@ -140,24 +110,28 @@ def remove_folder(folder):
#####################################################################

# 取最新的 Windows SDK
windowsSdkDir = sorted(glob.glob(programFilesX86Path + "\\Windows Kits\\10\\bin\\10.*"))[-1];
windowsSdkDir = sorted(
glob.glob(programFilesX86Path + "\\Windows Kits\\10\\bin\\10.*")
)[-1]
makepriPath = windowsSdkDir + "\\x64\\makepri.exe"
if not os.access(makepriPath, os.X_OK):
raise Exception("未找到 makepri")

# 将 resources.pri 的内容导出为 xml
if os.system("\"" + makepriPath + "\" dump /dt detailed /o") != 0:
if os.system(f'"{makepriPath}" dump /dt detailed /o') != 0:
raise Exception("dump 失败")

xmlTree = ElementTree.parse("resources.pri.xml")

# 在 xml 中删除冗余资源
for resourceNode in xmlTree.getroot().findall("ResourceMap/ResourceMapSubtree/ResourceMapSubtree/ResourceMapSubtree/NamedResource"):
for resourceNode in xmlTree.getroot().findall(
"ResourceMap/ResourceMapSubtree/ResourceMapSubtree/ResourceMapSubtree/NamedResource"
):
name = resourceNode.get("name")

if not name.endswith(".xbf"):
continue

# 我们仅需 19h1 和 21h1 的资源,分别用于 Win10 和 Win11
for key in ["compact", "Compact", "v1", "rs2", "rs3", "rs4", "rs5"]:
if key in name:
Expand All @@ -168,7 +142,8 @@ def remove_folder(folder):
xmlTree.write("resources.pri.xml", encoding="utf-8")

with open("priconfig.xml", "w", encoding="utf-8") as f:
print("""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
print(
"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<resources targetOsVersion="10.0.0" majorVersion="1">
<packaging>
<autoResourcePackage qualifier="Scale" />
Expand All @@ -190,10 +165,12 @@ def remove_folder(folder):
</default>
<indexer-config type="priinfo" emitStrings="true" emitPaths="true" emitEmbeddedData="true" />
</index>
</resources>""", file=f)
</resources>""",
file=f,
)

# 将 xml 重新封装成 pri
os.system("\"" + makepriPath + "\" new /pr . /cf priconfig.xml /in Magpie.App /o")
os.system(f'"{makepriPath}" new /pr . /cf priconfig.xml /in Magpie.App /o')

os.remove("resources.pri.xml")
os.remove("priconfig.xml")
Expand Down

0 comments on commit 642f1ae

Please sign in to comment.