Skip to content

Commit bb29783

Browse files
author
Soham Dasgupta
committed
feat: add version updater
1 parent 7273f9a commit bb29783

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
## v0.1.0 (2023-09-24)
1+
## Unreleased
2+
3+
### Features
4+
5+
- Add automated version check at startup
6+
- Add tray menu for version manual checking
7+
8+
## [v0.1.0](https://github.com/thesobercoder/polygon/releases/v0.1.0) (2023-09-24)
29

310
### Features
411

polygon.ahk

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55

66
;-- Globals
77
global APP_VERSION := "0.1.0"
8+
global APP_VERSION_NAME := "v" . APP_VERSION
89
global APP_NAME := "Polygon"
9-
global APP_URL := "https://github.com/thesobercoder/polygon"
10-
global APP_FEEDBACK_URL := "https://github.com/thesobercoder/polygon/issues/new"
10+
global APP_REPO_OWNER := "thesobercoder"
11+
global APP_REPO_NAME := "polygon"
12+
global APP_URL := "https://github.com/" . APP_REPO_OWNER . "/" . APP_REPO_NAME
13+
global APP_FEEDBACK_URL := APP_URL . "/issues/new"
14+
global APP_UPDATE_URL := APP_URL . "/releases/latest"
1115
global APP_INI_FILE := "polygon.ini"
1216
global APP_INI_SECTION_SHORTCUT := "Shortcut"
1317
global APP_INI_SECTION_TOAST := "Toast"
@@ -35,16 +39,49 @@ global APP_SHORTCUT_BOTTOMRIGHT := IniRead(APP_INI_FILE, APP_INI_SECTION_SHORTCU
3539
;--Tooltip
3640
A_IconTip := APP_NAME
3741

42+
;-- Register global error logging
43+
OnError(LogError)
44+
45+
;-- On startup check for version update
46+
CheckForUpdate()
47+
3848
;-- Context Menu
3949
tray := A_TrayMenu
4050
tray.Delete()
4151
tray.Add("Help", ShowHelp)
4252
tray.Add("Version", ShowVersion)
53+
tray.Add("Check for Updates", CheckForUpdate)
4354
tray.Add("Feedback", SubmitFeedback)
4455
tray.Add("Restart", Restart)
4556
tray.Add("Exit", Terminate)
4657
tray.Default := "Version"
4758

59+
LogError(exception, mode)
60+
{
61+
; Get the user's application data folder
62+
PolygonDataFolder := A_AppData . "\Polygon"
63+
PolygonLogPath := PolygonDataFolder . "\errorlog.txt"
64+
65+
; Create the folder if it doesn't exist
66+
if (!DirExist(PolygonDataFolder))
67+
{
68+
DirCreate(PolygonDataFolder)
69+
}
70+
71+
; Display a message to the user
72+
result := MsgBox('Polygon encountered an error. The error has been logged in the "errorlog.txt" file. Open folder location?', APP_NAME, "YesNo Iconx")
73+
if (result := "Yes")
74+
{
75+
Run(PolygonDataFolder)
76+
}
77+
78+
; Append the error message to the log file
79+
FileAppend("Error on line " . exception.Line . ": " . exception.Message . "`n", PolygonLogPath)
80+
81+
return true
82+
}
83+
84+
4885
Toast(Message, r, l, t, b)
4986
{
5087
;-- Return if toast is not enabled
@@ -95,6 +132,74 @@ Toast(Message, r, l, t, b)
95132
}
96133
}
97134

135+
ParseVersionString(version)
136+
{
137+
regex := "v(\d+)\.(\d+)\.(\d+)"
138+
if (RegExMatch(version, regex, &parsedVersion))
139+
{
140+
if (parsedVersion.Count > 0)
141+
{
142+
;-- Extract the version here
143+
major := parsedVersion[1]
144+
minor := parsedVersion[2]
145+
patch := parsedVersion[3]
146+
147+
return Number(major) * 10000 + Number(minor) * 100 + Number(patch)
148+
}
149+
}
150+
return 0
151+
}
152+
153+
CheckForUpdate(args*)
154+
{
155+
version := GetLatestGitHubRelease(APP_REPO_OWNER, APP_REPO_NAME)
156+
157+
; Check if the request was successful
158+
if (version)
159+
{
160+
newVersion := ParseVersionString(version)
161+
currentVersion := ParseVersionString(APP_VERSION_NAME)
162+
163+
;-- Check if the latest version is greater
164+
if (newVersion > currentVersion)
165+
{
166+
result := MsgBox("A new version " . newVersion . " is available. Do you want to update?", APP_NAME, "YesNo Iconi")
167+
if (result == "Yes")
168+
{
169+
Run(APP_UPDATE_URL)
170+
return
171+
}
172+
}
173+
}
174+
175+
if (args && args.Length > 0 && args[1] == "Check for Updates")
176+
{
177+
MsgBox("You already have the latest version.", APP_NAME, "Iconi")
178+
}
179+
}
180+
181+
GetLatestGitHubRelease(owner, repo)
182+
{
183+
req := ComObject("Msxml2.XMLHTTP")
184+
req.open("GET", "https://api.github.com/repos/" . owner . "/" . repo . "/releases/latest", false)
185+
req.send()
186+
if req.status != 200
187+
{
188+
MsgBox("Error checking for update. Please try after some time.", APP_NAME, "Iconx")
189+
return
190+
}
191+
192+
res := JSONParse(req.responseText)
193+
return res.tag_name
194+
195+
JSONParse(str)
196+
{
197+
htmlfile := ComObject("htmlfile")
198+
htmlfile.write('<meta http-equiv="X-UA-Compatible" content="IE=edge">')
199+
return htmlfile.parentWindow.JSON.parse(str)
200+
}
201+
}
202+
98203
SubmitFeedback(*)
99204
{
100205
Run(APP_FEEDBACK_URL)
@@ -117,7 +222,7 @@ Terminate(*)
117222

118223
ShowVersion(*)
119224
{
120-
MsgBox("Version " . APP_VERSION, APP_NAME, "iconi")
225+
MsgBox("Version " . APP_VERSION, APP_NAME, "Iconi")
121226
}
122227

123228
;-- Map Hotkeys

0 commit comments

Comments
 (0)