Skip to content

Commit

Permalink
Merge branch 'meta-dev' into meta
Browse files Browse the repository at this point in the history
  • Loading branch information
mrFq1 committed Apr 1, 2023
2 parents 0f44258 + b963eb6 commit e9949d2
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 126 deletions.
4 changes: 4 additions & 0 deletions ClashX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
015F1E91288E42A50052B20A /* ClashMetaConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 015F1E90288E42A50052B20A /* ClashMetaConfig.swift */; };
015F1E92288E60D30052B20A /* MetaTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0162E74E2864B819007218A6 /* MetaTask.swift */; };
0162E74F2864B819007218A6 /* MetaTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0162E74E2864B819007218A6 /* MetaTask.swift */; };
016BEAB029D80103001586C5 /* AlphaMetaDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 016BEAAF29D80102001586C5 /* AlphaMetaDownloader.swift */; };
018F88F9286DD0CB004DD0F7 /* DualTitleMenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 018F88F8286DD0CB004DD0F7 /* DualTitleMenuItem.swift */; };
01943259287D19BC008CC51A /* ClashRuleProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01943258287D19BC008CC51A /* ClashRuleProvider.swift */; };
019A239628657A7A00AE5698 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 019A239528657A7A00AE5698 /* main.swift */; };
Expand Down Expand Up @@ -143,6 +144,7 @@
015F1E90288E42A50052B20A /* ClashMetaConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashMetaConfig.swift; sourceTree = "<group>"; };
0162E74D2864B818007218A6 /* com.metacubex.ClashX.ProxyConfigHelper-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "com.metacubex.ClashX.ProxyConfigHelper-Bridging-Header.h"; sourceTree = "<group>"; };
0162E74E2864B819007218A6 /* MetaTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetaTask.swift; sourceTree = "<group>"; };
016BEAAF29D80102001586C5 /* AlphaMetaDownloader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlphaMetaDownloader.swift; sourceTree = "<group>"; };
018F88F8286DD0CB004DD0F7 /* DualTitleMenuItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DualTitleMenuItem.swift; sourceTree = "<group>"; };
01943258287D19BC008CC51A /* ClashRuleProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashRuleProvider.swift; sourceTree = "<group>"; };
019A239528657A7A00AE5698 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -344,6 +346,7 @@
491C250021BD55B900AB5D44 /* Utils */,
492C4868210EE6B9004554A0 /* ApiRequest.swift */,
015F1E90288E42A50052B20A /* ClashMetaConfig.swift */,
016BEAAF29D80102001586C5 /* AlphaMetaDownloader.swift */,
);
path = General;
sourceTree = "<group>";
Expand Down Expand Up @@ -750,6 +753,7 @@
49CF3B2120CD7463001EBF94 /* AppDelegate.swift in Sources */,
496BDEE021196F1E00C5207F /* Logger.swift in Sources */,
49722FEF211F338B00650A41 /* FileEvent.swift in Sources */,
016BEAB029D80103001586C5 /* AlphaMetaDownloader.swift in Sources */,
49D176A72355FE680093DD7B /* NetworkChangeNotifier.swift in Sources */,
4913C82321157D0200F6B87C /* Notification.swift in Sources */,
015F1E91288E42A50052B20A /* ClashMetaConfig.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
20 changes: 1 addition & 19 deletions ClashX.xcodeproj/xcshareddata/xcschemes/ClashX.xcscheme
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
version = "1.7">
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<PreActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Run Script"
scriptText = "export PATH=&quot;$PATH:/opt/homebrew/bin&quot;&#10;if which swiftlint &gt; /dev/null; then&#10; cd $PROJECT_DIR &amp;&amp; swiftlint --fix &amp;&amp; swiftlint&#10;else&#10; echo &quot;warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint&quot;&#10;fi&#10;">
<EnvironmentBuildable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "49CF3B1C20CD7463001EBF94"
BuildableName = "ClashX Meta.app"
BlueprintName = "ClashX Meta"
ReferencedContainer = "container:ClashX.xcodeproj">
</BuildableReference>
</EnvironmentBuildable>
</ActionContent>
</ExecutionAction>
</PreActions>
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
Expand Down
143 changes: 37 additions & 106 deletions ClashX/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

if !chmodX(corePath.path) {
return (nil, "chmod +x failed.")
}

if let msg = testMetaCore(corePath.path) {
Logger.log("version: \(msg.version)")
}
Expand Down Expand Up @@ -609,6 +605,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func testMetaCore(_ path: String) -> (version: String, date: Date?)? {
guard chmodX(path) else { return nil }

let proc = Process()
proc.executableURL = .init(fileURLWithPath: path)
Expand All @@ -629,9 +626,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return nil
}

let outs = out.replacingOccurrences(of: "\n", with: "").split(separator: " ").map(String.init)
Logger.log("test core path: \(path)")
Logger.log("-v out: \(out)")

let outs = out
.split(separator: "\n")
.first {
$0.starts(with: "Clash Meta")
}?.split(separator: " ")
.map(String.init)

guard outs.count == 13,
guard let outs,
outs.count == 13,
outs[0] == "Clash",
outs[1] == "Meta",
outs[3] == "darwin" else {
Expand All @@ -640,17 +646,19 @@ class AppDelegate: NSObject, NSApplicationDelegate {

let version = outs[2]

let dateString = [outs[7], outs[8], outs[9], outs[10], outs[12]].joined(separator: "-")
let f = DateFormatter()
f.dateFormat = "E-MMM-d-HH:mm:ss-yyyy"
f.timeZone = .init(abbreviation: outs[11])
let date = f.date(from: dateString)
let dateString = [outs[7], outs[8], outs[9], outs[10], outs[12]].joined(separator: "-")
let f = DateFormatter()
f.dateFormat = "E-MMM-d-HH:mm:ss-yyyy"
f.timeZone = .init(abbreviation: outs[11])
let date = f.date(from: dateString)

return (version: version, date: date)
return (version: version, date: date)
}

func validateDefaultCore() -> Bool {
guard let path = Paths.defaultCorePath()?.path else { return false }
guard let path = Paths.defaultCorePath()?.path,
chmodX(path) else { return false }

#if DEBUG
return true
#endif
Expand Down Expand Up @@ -1300,101 +1308,24 @@ extension AppDelegate {
}

@IBAction func updateAlphaMeta(_ sender: NSMenuItem) {
guard let helperURL = Paths.alphaCorePath() else {
return
}
sender.isEnabled = false
struct ReleasesResp: Decodable {
let assets: [Asset]
struct Asset: Decodable {
let name: String
let downloadUrl: String
let contentType: String
let state: String

enum CodingKeys: String, CodingKey {
case name,
state,
downloadUrl = "browser_download_url",
contentType = "content_type"
}
}
}

func GetMachineHardwareName() -> String? {
var sysInfo = utsname()
let retVal = uname(&sysInfo)

guard retVal == EXIT_SUCCESS else { return nil }

let machineMirror = Mirror(reflecting: sysInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
return identifier
}

let assetName: String? = {
switch GetMachineHardwareName() {
case "x86_64":
return "darwin-amd64"
case "arm64":
return "darwin-arm64"
default:
return nil
}
}()
let fm = FileManager.default

func dlResult(_ info: String) {
sender.isEnabled = true
NSUserNotificationCenter.default.post(title: "Clash Meta Core", info: info)
}

AF.request("https://api.github.com/repos/MetaCubeX/Clash.Meta/releases/tags/Prerelease-Alpha").responseDecodable(of: ReleasesResp.self) {
guard let assets = $0.value?.assets,
let assetName = assetName,
let asset = assets.first(where: {
$0.name.contains(assetName) &&
$0.state == "uploaded" &&
$0.contentType == "application/gzip"
}) else {
dlResult("Decode alpha release info failed")
return
}

if let v = self.testMetaCore(helperURL.path),
asset.name.contains(v.version) {
dlResult("Not found update")
return
}

self.updateAlphaVersion(nil)

AF.download(asset.downloadUrl).response {
guard let gzPath = $0.fileURL?.path,
let contentData = fm.contents(atPath: gzPath)
else {
dlResult("Download file failed")
return
}
do {
try? fm.removeItem(at: helperURL)

try fm.createDirectory(at: helperURL.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil)
try contentData.gunzipped().write(to: helperURL)
guard let version = self.testMetaCore(helperURL.path)?.version else {
dlResult("Test downloaded file failed")
return
}
self.updateAlphaVersion(version)
dlResult(NSLocalizedString("Version: ", comment: "") + version)
} catch let error {
dlResult("Something error \(error.localizedDescription)")
}
}
}
AlphaMetaDownloader.alphaAsset().then {
AlphaMetaDownloader.checkVersion($0)
}.then {
AlphaMetaDownloader.downloadCore($0)
}.then {
AlphaMetaDownloader.replaceCore($0)
}.done {
self.updateAlphaVersion($0)
let msg = NSLocalizedString("Version: ", comment: "") + $0
NSUserNotificationCenter.default.post(title: "Clash Meta Core", info: msg)
}.ensure {
sender.isEnabled = true
}.catch {
let error = $0 as? AlphaMetaDownloader.errors
NSUserNotificationCenter.default.post(title: "Clash Meta Core", info: error?.des() ?? "")
}
}

func updateAlphaVersion(_ version: String?) {
Expand Down
Loading

0 comments on commit e9949d2

Please sign in to comment.