From 642f1ae2793d5e6e10fe46221edaab800f741fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=97=AD?= <34770031+Blinue@users.noreply.github.com> Date: Wed, 11 Oct 2023 22:05:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=AE=80=E5=8C=96=E5=92=8C?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=20publish.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publish.py | 97 +++++++++++++++++++++--------------------------------- 1 file changed, 37 insertions(+), 60 deletions(-) diff --git a/publish.py b/publish.py index 1f9f0711e..0d1f43030 100644 --- a/publish.py +++ b/publish.py @@ -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 @@ -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 @@ -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") @@ -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 失败") ##################################################################### @@ -103,6 +70,7 @@ def __init__(self, uuid_): os.chdir(os.getcwd() + "\\publish") + # 删除文件,忽略错误 def remove_file(file): try: @@ -110,6 +78,7 @@ def remove_file(file): except: pass + # 删除文件夹,忽略错误 def remove_folder(folder): try: @@ -117,6 +86,7 @@ def remove_folder(folder): except: pass + remove_file("Microsoft.Web.WebView2.Core.dll") for pattern in ["*.pdb", "*.lib", "*.exp", "*.winmd", "*.xml", "*.xbf", "dummy.*"]: @@ -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) @@ -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: @@ -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(""" + print( + """ @@ -190,10 +165,12 @@ def remove_folder(folder): -""", file=f) +""", + 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")