Skip to content

Commit 097179a

Browse files
committed
新增一键更新功能
1 parent e46ab3b commit 097179a

File tree

6 files changed

+174
-8
lines changed

6 files changed

+174
-8
lines changed

XYDevTool/XYDevTool.xcodeproj/project.pbxproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
B4A3B06E281EC26500C34DD8 /* script.pl in Resources */ = {isa = PBXBuildFile; fileRef = B4A3B06C281EC26500C34DD8 /* script.pl */; };
3737
B4A3B070281EC5D000C34DD8 /* mac_icon.sh in Resources */ = {isa = PBXBuildFile; fileRef = B4A3B06F281EC5D000C34DD8 /* mac_icon.sh */; };
3838
B4A3B074281EC72600C34DD8 /* STPrivilegedTask.m in Sources */ = {isa = PBXBuildFile; fileRef = B4A3B073281EC72600C34DD8 /* STPrivilegedTask.m */; };
39+
B4AD7761283FB333003FE01D /* autoupdate.sh in Resources */ = {isa = PBXBuildFile; fileRef = B4AD7760283FB333003FE01D /* autoupdate.sh */; };
3940
B4D23BC9283D1A620081F400 /* NSTextView+LineNumberRulerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4D23BC8283D1A620081F400 /* NSTextView+LineNumberRulerView.swift */; };
4041
B4DB4BA1283936AC00AF654D /* UpgradeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4DB4B9F283936AC00AF654D /* UpgradeViewController.swift */; };
4142
B4DB4BA2283936AC00AF654D /* UpgradeViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = B4DB4BA0283936AC00AF654D /* UpgradeViewController.xib */; };
@@ -81,6 +82,7 @@
8182
B4A3B072281EC72600C34DD8 /* STPrivilegedTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STPrivilegedTask.h; sourceTree = "<group>"; };
8283
B4A3B073281EC72600C34DD8 /* STPrivilegedTask.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPrivilegedTask.m; sourceTree = "<group>"; };
8384
B4A3B07A281EC7E300C34DD8 /* XYDevTool-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "XYDevTool-Bridging-Header.h"; sourceTree = "<group>"; };
85+
B4AD7760283FB333003FE01D /* autoupdate.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = autoupdate.sh; sourceTree = "<group>"; };
8486
B4D23BC8283D1A620081F400 /* NSTextView+LineNumberRulerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSTextView+LineNumberRulerView.swift"; sourceTree = "<group>"; };
8587
B4DB4B9F283936AC00AF654D /* UpgradeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpgradeViewController.swift; sourceTree = "<group>"; };
8688
B4DB4BA0283936AC00AF654D /* UpgradeViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = UpgradeViewController.xib; sourceTree = "<group>"; };
@@ -222,6 +224,7 @@
222224
B4A3B06A281EC16C00C34DD8 /* sh */ = {
223225
isa = PBXGroup;
224226
children = (
227+
B4AD7760283FB333003FE01D /* autoupdate.sh */,
225228
B4A3AD9B28217171001FD2B3 /* ios_icon.sh */,
226229
B4A3B06F281EC5D000C34DD8 /* mac_icon.sh */,
227230
B4A3B06C281EC26500C34DD8 /* script.pl */,
@@ -382,6 +385,7 @@
382385
B4A3B067281D66CB00C34DD8 /* json2model.js in Resources */,
383386
B48BC727281D07500093D91F /* Main.storyboard in Resources */,
384387
B4A3B06D281EC26500C34DD8 /* script.sh in Resources */,
388+
B4AD7761283FB333003FE01D /* autoupdate.sh in Resources */,
385389
);
386390
runOnlyForDeploymentPostprocessing = 0;
387391
};
@@ -611,7 +615,7 @@
611615
"$(inherited)",
612616
"@executable_path/../Frameworks",
613617
);
614-
MARKETING_VERSION = 1.3.0;
618+
MARKETING_VERSION = 1.4.0;
615619
PRODUCT_BUNDLE_IDENTIFIER = com.xiaoyou.XYDevTool;
616620
PRODUCT_NAME = "$(TARGET_NAME)";
617621
PROVISIONING_PROFILE_SPECIFIER = "";
@@ -644,7 +648,7 @@
644648
"$(inherited)",
645649
"@executable_path/../Frameworks",
646650
);
647-
MARKETING_VERSION = 1.3.0;
651+
MARKETING_VERSION = 1.4.0;
648652
PRODUCT_BUNDLE_IDENTIFIER = com.xiaoyou.XYDevTool;
649653
PRODUCT_NAME = "$(TARGET_NAME)";
650654
PROVISIONING_PROFILE_SPECIFIER = "";

XYDevTool/XYDevTool/FEATURES/JSONFormatter/JSONFormatter.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
</constraints>
6060
<buttonCell key="cell" type="push" title="压缩" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="mJq-FT-YQm">
6161
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
62-
<font key="font" size="13" name=".PingFangSC-Regular"/>
62+
<font key="font" metaFont="system"/>
6363
</buttonCell>
6464
<connections>
6565
<action selector="compressionAction:" target="bU7-R8-ocO" id="JxY-9J-5Za"/>

XYDevTool/XYDevTool/FEATURES/Upgrade/UpgradeUtils.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ class UpgradeUtils {
2828
}
2929
}
3030

31+
class Assets: Codable {
32+
/** https://github.com/xiaoyouPrince/XYDevTool/releases/download/1.3.0/XYDevTool.app.zip */
33+
var browser_download_url: String?
34+
35+
/** XYDevTool.app.zip */
36+
var name: String?
37+
}
38+
3139
class Version: Codable {
3240

3341
/** https://api.github.com/repos/xiaoyouPrince/XYDevTool/releases/66602654 */
@@ -52,4 +60,10 @@ class Version: Codable {
5260

5361
/** 新增 JSON 格式化功能 ... */
5462
var body: String?
63+
64+
var assets: [Assets]?
65+
66+
func app_zip_url() -> URL {
67+
URL(string: assets!.first!.browser_download_url!)!
68+
}
5569
}

XYDevTool/XYDevTool/FEATURES/Upgrade/UpgradeViewController.swift

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,106 @@ class UpgradeViewController: NSViewController {
2626
upgradeBtn.title = "更新"
2727
}
2828

29+
30+
@IBAction func oneKeyUpgrade(_ sender: Any) {
31+
32+
downLoad(url: versionInfo!.app_zip_url()) { success, downLoadPath in
33+
print(success, "hhhhhhh")
34+
if let path = downLoadPath {
35+
self.doWithProcess(path)
36+
}else{
37+
showAlert(msg: "下载失败,重试/尝试手动更新")
38+
}
39+
}
40+
}
41+
2942
@IBAction func upgradeBtnAction(_ sender: NSButton) {
3043
if let htmlURL = versionInfo?.html_url,
3144
let url = URL(string: htmlURL) {
3245
NSWorkspace.shared.open(url)
3346
}
3447
}
48+
49+
50+
func doWithProcess(_ path: String){
51+
52+
let cmd = "/bin/sh autoupdate.sh \(path)"
53+
// var cmd = "/bin/sh autoupdate.sh"
54+
55+
var components = cmd.components(separatedBy: .whitespaces)
56+
57+
let task = Process()
58+
task.launchPath = components[0]
59+
components.remove(at: 0)
60+
task.arguments = components
61+
if let path = Bundle.main.resourcePath{
62+
task.currentDirectoryPath = path
63+
}
64+
65+
let outputPipe = Pipe()
66+
task.standardOutput = outputPipe
67+
task.standardError = outputPipe
68+
let readHandle = outputPipe.fileHandleForReading
69+
70+
task.launch()
71+
// task.waitUntilExit()
72+
task.resume()
73+
74+
let outputData = readHandle.readDataToEndOfFile()
75+
let outputString = String(data: outputData, encoding: .utf8)
76+
77+
print("outputString -- ",outputString)
78+
79+
let exitStr = String(format: "Exit status: %d", task.terminationStatus)
80+
81+
print("exitStr -- ",exitStr)
82+
83+
84+
}
85+
86+
func downLoad(url: URL, completed: @escaping (Bool, String?)->()) {
87+
88+
let config = URLSessionConfiguration.default
89+
let session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
90+
91+
session.downloadTask(with: url) { theUrl, response, error in
92+
if (error != nil) {
93+
print(error)
94+
completed(false,nil)
95+
}else{
96+
print(response, theUrl)
97+
// 放到指定地点
98+
99+
let targetURL = URL(fileURLWithPath: Bundle.main.resourcePath!.appending("/XYDevTool.zip"))
100+
do {
101+
try FileManager.default.moveItem(at: theUrl!, to:targetURL )
102+
}catch{
103+
print("出现异常-- \(error)")
104+
}
105+
106+
completed(true, "XYDevTool.zip")
107+
}
108+
}.resume()
109+
}
110+
}
111+
112+
extension UpgradeViewController: URLSessionDownloadDelegate {
113+
114+
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
115+
showAlert(msg: "下载完成")
116+
}
117+
118+
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64){
119+
print("已经下载- \(bytesWritten)/\(totalBytesExpectedToWrite)")
120+
}
121+
122+
123+
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64){
124+
125+
}
126+
127+
35128
}
129+
130+
131+

XYDevTool/XYDevTool/FEATURES/Upgrade/UpgradeViewController.xib

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,49 @@
4848
</textFieldCell>
4949
</textField>
5050
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="kvZ-am-lSo">
51-
<rect key="frame" x="372" y="21" width="66" height="37"/>
51+
<rect key="frame" x="352" y="21" width="86" height="37"/>
5252
<constraints>
5353
<constraint firstAttribute="height" constant="30" id="QnU-Nm-JnD"/>
54-
<constraint firstAttribute="width" constant="60" id="gfm-Bl-FOG"/>
54+
<constraint firstAttribute="width" constant="80" id="gfm-Bl-FOG"/>
5555
</constraints>
56-
<buttonCell key="cell" type="bevel" title="更新" bezelStyle="regularSquare" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="ioY-oC-Cs5">
56+
<buttonCell key="cell" type="bevel" title="手动更新" bezelStyle="regularSquare" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="ioY-oC-Cs5">
5757
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
58-
<font key="font" metaFont="system"/>
58+
<font key="font" size="13" name=".PingFangSC-Regular"/>
5959
</buttonCell>
6060
<connections>
6161
<action selector="upgradeBtnAction:" target="-2" id="ars-Bl-RTj"/>
6262
</connections>
6363
</button>
64+
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="LXt-Fh-S8G">
65+
<rect key="frame" x="253" y="21" width="86" height="37"/>
66+
<constraints>
67+
<constraint firstAttribute="width" constant="80" id="68k-EY-Pog"/>
68+
<constraint firstAttribute="height" constant="30" id="U9i-Yg-CUb"/>
69+
</constraints>
70+
<buttonCell key="cell" type="bevel" title="一键更新" bezelStyle="regularSquare" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="jcc-fc-USy">
71+
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
72+
<font key="font" size="13" name="STSongti-SC-Regular"/>
73+
</buttonCell>
74+
<connections>
75+
<action selector="oneKeyUpgrade:" target="-2" id="shr-XE-kXr"/>
76+
</connections>
77+
</button>
6478
</subviews>
6579
<constraints>
6680
<constraint firstItem="JJw-ki-yrg" firstAttribute="top" secondItem="aRn-4n-Alr" secondAttribute="top" constant="23" id="033-Ns-c8y"/>
6781
<constraint firstAttribute="trailing" secondItem="kvZ-am-lSo" secondAttribute="trailing" constant="35" id="Cme-EF-zff"/>
6882
<constraint firstItem="JJw-ki-yrg" firstAttribute="centerX" secondItem="aRn-4n-Alr" secondAttribute="centerX" id="N7F-zi-5sX"/>
6983
<constraint firstItem="kvZ-am-lSo" firstAttribute="top" relation="greaterThanOrEqual" secondItem="Kcw-gQ-ohQ" secondAttribute="bottom" constant="30" id="NN2-7D-mkl"/>
84+
<constraint firstItem="LXt-Fh-S8G" firstAttribute="centerY" secondItem="kvZ-am-lSo" secondAttribute="centerY" id="PPc-Jo-o0n"/>
7085
<constraint firstItem="Kcw-gQ-ohQ" firstAttribute="top" secondItem="bng-d5-Ffn" secondAttribute="bottom" constant="40" id="RC5-JL-QVO"/>
86+
<constraint firstItem="kvZ-am-lSo" firstAttribute="leading" secondItem="LXt-Fh-S8G" secondAttribute="trailing" constant="19" id="Vp5-Xl-H81"/>
7187
<constraint firstItem="Kcw-gQ-ohQ" firstAttribute="trailing" secondItem="aRn-4n-Alr" secondAttribute="trailing" constant="-35" id="X3i-Bx-tEG"/>
7288
<constraint firstItem="bng-d5-Ffn" firstAttribute="top" secondItem="JJw-ki-yrg" secondAttribute="bottom" constant="10" id="Y6c-Vt-iSI"/>
7389
<constraint firstItem="bng-d5-Ffn" firstAttribute="centerX" secondItem="JJw-ki-yrg" secondAttribute="centerX" id="biK-ST-LdQ"/>
7490
<constraint firstAttribute="bottom" secondItem="kvZ-am-lSo" secondAttribute="bottom" constant="25" id="gnr-h2-XwU"/>
7591
<constraint firstItem="Kcw-gQ-ohQ" firstAttribute="leading" secondItem="aRn-4n-Alr" secondAttribute="leading" constant="35" id="j6r-gX-OG8"/>
7692
</constraints>
77-
<point key="canvasLocation" x="139" y="156"/>
93+
<point key="canvasLocation" x="46" y="122"/>
7894
</customView>
7995
</objects>
8096
</document>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
3+
# feature: 自动更新脚本
4+
# date:2022/05/26 20:08
5+
# authre: xiaoyou
6+
7+
# 应用内不能直接下载,这样直接使用文件吧
8+
9+
#S_PATH="https://github.com/xiaoyouPrince/XYDevTool/releases/download/1.3.0/XYDevTool.zip"
10+
11+
# download
12+
#wget $S_PATH
13+
mv $1 XYDevTool.zip
14+
tar xf XYDevTool.zip
15+
16+
# quit old
17+
ps aux | grep "XYDevTool.app" > pids
18+
for pid in `awk '{print $2}' pids`; do
19+
kill "$pid"
20+
done
21+
22+
rm pids
23+
24+
# copy history
25+
# /Applications/XYDevTool.app/Contents/Resources/history.json
26+
cp /Applications/XYDevTool.app/Contents/Resources/history.json XYDevTool.app/Contents/Resources/
27+
28+
rm -rf /Applications/XYDevTool.app
29+
30+
# copy new app
31+
# Desktop/XYDevToolApp/XYDevTool.app
32+
mv XYDevTool.app /Applications/XYDevTool.app
33+
34+
# run new
35+
open /Applications/XYDevTool.app
36+

0 commit comments

Comments
 (0)