From 06826443274e34e7b23bc254568db71854f0a35c Mon Sep 17 00:00:00 2001 From: Sunny Young Date: Mon, 8 Dec 2025 14:52:08 +0800 Subject: [PATCH] reorder code blocks --- Sources/WeChatTweak/Config.swift | 4 +- Sources/WeChatTweak/main.swift | 156 ++++++++++++++++--------------- 2 files changed, 84 insertions(+), 76 deletions(-) diff --git a/Sources/WeChatTweak/Config.swift b/Sources/WeChatTweak/Config.swift index 7587bfc..57e366f 100644 --- a/Sources/WeChatTweak/Config.swift +++ b/Sources/WeChatTweak/Config.swift @@ -9,6 +9,8 @@ import Foundation import MachO struct Config: Decodable { + static let `default` = URL(string:"https://raw.githubusercontent.com/sunnyyoung/WeChatTweak/refs/heads/master/config.json")! + enum Arch: String, Decodable { case arm64 case x86_64 @@ -81,7 +83,7 @@ struct Config: Decodable { let version: String let targets: [Target] - static func load(from url: URL) async throws -> [Config] { + static func load(url: URL = Self.default) async throws -> [Config] { if url.isFileURL { return try JSONDecoder().decode( [Config].self, diff --git a/Sources/WeChatTweak/main.swift b/Sources/WeChatTweak/main.swift index 85e620d..bf21b68 100644 --- a/Sources/WeChatTweak/main.swift +++ b/Sources/WeChatTweak/main.swift @@ -8,96 +8,104 @@ import Foundation import Dispatch import ArgumentParser -struct Versions: AsyncParsableCommand { - static let configuration = CommandConfiguration(abstract: "List all supported WeChat versions") +// MARK: Versions +extension Tweak { + struct Versions: AsyncParsableCommand { + static let configuration = CommandConfiguration(abstract: "List all supported WeChat versions") - mutating func run() async throws { - try await Config.load(from: Tweak.config).forEach({ print($0.version) }) - Darwin.exit(EXIT_SUCCESS) + mutating func run() async throws { + try await Config.load().forEach({ print($0.version) }) + Darwin.exit(EXIT_SUCCESS) + } } } -struct Patch: AsyncParsableCommand { - enum Error: LocalizedError { - case invalidApp - case invalidConfig - case invalidVersion - case unsupportedVersion +// MARK: Patch +extension Tweak { + struct Patch: AsyncParsableCommand { + enum Error: LocalizedError { + case invalidApp + case invalidConfig + case invalidVersion + case unsupportedVersion - var errorDescription: String? { - switch self { - case .invalidApp: - return "Invalid app path" - case .invalidConfig: - return "Invalid patch config" - case .invalidVersion: - return "Invalid app version" - case .unsupportedVersion: - return "Unsupported WeChat version" - } - } - } - - static let configuration = CommandConfiguration(abstract: "Patch WeChat.app") - - @Option( - name: .shortAndLong, - help: "Path of WeChat.app", - transform: { - guard FileManager.default.fileExists(atPath: $0) else { - throw Error.invalidApp - } - return URL(fileURLWithPath: $0) - } - ) - var app: URL = URL(fileURLWithPath: "/Applications/WeChat.app", isDirectory: true) - - @Option( - name: .shortAndLong, - help: "Local path or Remote URL of config.json", - transform: { - if FileManager.default.fileExists(atPath: $0) { - return URL(fileURLWithPath: $0) - } else { - guard let url = URL(string: $0) else { - throw Error.invalidConfig + var errorDescription: String? { + switch self { + case .invalidApp: + return "Invalid app path" + case .invalidConfig: + return "Invalid patch config" + case .invalidVersion: + return "Invalid app version" + case .unsupportedVersion: + return "Unsupported WeChat version" } - return url } } - ) - var config: URL = Tweak.config - mutating func run() async throws { - print("------ Version ------") - guard let version = try await Command.version(app: self.app) else { - throw Error.invalidVersion - } - print("WeChat version: \(version)") + static let configuration = CommandConfiguration(abstract: "Patch WeChat.app") - print("------ Config ------") - guard let config = (try await Config.load(from: self.config)).first(where: { $0.version == version }) else { - throw Error.unsupportedVersion - } - print("Matched config: \(config)") - - print("------ Patch ------") - try await Command.patch( - app: self.app, - config: config + @Option( + name: .shortAndLong, + help: "Path of WeChat.app", + transform: { + guard FileManager.default.fileExists(atPath: $0) else { + throw Error.invalidApp + } + return URL(fileURLWithPath: $0) + } ) - print("Done!") + var app: URL = URL(fileURLWithPath: "/Applications/WeChat.app", isDirectory: true) - print("------ Resign ------") - try await Command.resign( - app: self.app + @Option( + name: .shortAndLong, + help: "Local path or Remote URL of config.json", + transform: { + if FileManager.default.fileExists(atPath: $0) { + return URL(fileURLWithPath: $0) + } else { + guard let url = URL(string: $0) else { + throw Error.invalidConfig + } + return url + } + } ) - print("Done!") + var config: URL = Config.default - Darwin.exit(EXIT_SUCCESS) + mutating func run() async throws { + print("------ Version ------") + guard let version = try await Command.version(app: self.app) else { + throw Error.invalidVersion + } + print("WeChat version: \(version)") + + print("------ Config ------") + guard let config = (try await Config.load()).first(where: { $0.version == version }) else { + throw Error.unsupportedVersion + } + print("Matched config: \(config)") + + print("------ Patch ------") + try await Command.patch( + app: self.app, + config: config + ) + print("Done!") + + print("------ Resign ------") + try await Command.resign( + app: self.app + ) + print("Done!") + + Darwin.exit(EXIT_SUCCESS) + } } + } +// MARK: Tweak struct Tweak: AsyncParsableCommand { static let configuration = CommandConfiguration( commandName: "wechattweak", @@ -108,8 +116,6 @@ struct Tweak: AsyncParsableCommand { ] ) - static let config = URL(string:"https://raw.githubusercontent.com/sunnyyoung/WeChatTweak/refs/heads/master/config.json")! - mutating func run() async throws { print(Tweak.helpMessage()) Darwin.exit(EXIT_SUCCESS)