-
Notifications
You must be signed in to change notification settings - Fork 18
/
setup.ahk
305 lines (268 loc) · 8.89 KB
/
setup.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
; Script is meant to create the installer, not act as one.
if (!A_IsCompiled) {
msg := "Build the application, then use 'setup.exe'"
Alert(0x10, "ERROR", msg)
ExitApp 1
}
; Defaults
ListLines Off
SetBatchLines -1
DetectHiddenWindows On
Process Priority,, High
; Preload the turtle...
Run PowerShell -C Exit,, Hide
; Arguments
verbose := !quiet := false
if (A_Args[1] ~= "i)-quiet")
verbose := !quiet := true
; Upgrade notice
if (verbose && A_OSVersion = "WIN_7")
{
msg := "In January of 2020, Microsoft stopped the support of Windows 7."
. " Is highly recommended to upgrade your OS."
Alert(0x10, "Officially unsupported OS", msg)
}
conn := DllCall("Wininet\InternetCheckConnection"
, "Str","https://github.com"
, "Ptr",1
, "Ptr",0)
if (!FileExist(A_ProgramFiles "\Auto-Type\bw.exe") && !conn)
{
Alert(0x10, "Error", "Internet is required to download Bitwarden CLI.")
ExitApp 1
}
projectRoot := "https://github.com/anonymous1184/bitwarden-autotype/"
; Check if latest version
if (A_IsCompiled && !Update_IsLatest())
{
msg := "There is a new version of this application.`n`nIs NOT recommend"
. "ed the usage of older versions. Do you want to go to GitHub "
. "and download the current release?"
Alert(0x44, "Outdated installer", msg)
IfMsgBox Yes
{
Run % projectRoot "releases/latest"
ExitApp
}
}
; Registry key
key := "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Auto-Type"
RegRead isInstalled, % key
; Ask
if (verbose && !isInstalled)
{
Alert_Labels("", "&Exit")
Alert(0x24, "Install?", "Do you want to install Bitwarden Auto-Type?")
IfMsgBox No ; Relabeled as `Exit`
ExitApp
}
; Close if running
WinKill ahk_exe bw-at.exe
; Download progress
Gui New, +AlwaysOnTop +HwndHwnd +LastFound -SysMenu
Gui Font, s11 q5, Consolas
Gui Add, Text,, Getting version...
Gui Show,, > Bitwarden CLI
Hotkey IfWinActive, % "ahk_id" hWnd
Hotkey !F4, WinExist
; If exists...
FileGetVersion offline, % A_ProgramFiles "\Auto-Type\bw.exe"
; Releases file
UrlDownloadToFile https://api.github.com/repos/bitwarden/cli/releases/latest
, % A_Temp "\bw-releases.json"
if (ErrorLevel && !offline)
{
Alert(0x11, "Error", "Bitwarden CLI release couldn't be retrieved.")
ExitApp 1
}
; Destination
FileCreateDir % A_ProgramFiles "\Auto-Type"
; Uninstaller
FileInstall uninstall.exe, % A_ProgramFiles "\Auto-Type\uninstall.exe", % true
; App
FileInstall bw-at.exe, % A_ProgramFiles "\Auto-Type\bw-at.exe", % true
signComplete := false
SetTimer Signature, -1
; Check for latest
asset := {}
assets := {}
JSON._init()
FileRead buffer, % A_Temp "\bw-releases.json"
FileDelete % A_Temp "\bw-releases.json"
try
assets := JSON.Load(buffer).assets
for i,asset in assets
{
if RegExMatch(asset.name, "windows-\K.+(?=.zip)", online)
break
asset := {}
}
; No release for Windows, use last known
if !asset.HasKey("browser_download_url")
{
online := "1.18.1"
url := "https://github.com/bitwarden/cli/releases/download"
. "/v" online "/bw-windows-" online ".zip"
asset := { "size":18788019, "browser_download_url":url }
}
; Check if already latest
if CheckVersion(offline, online)
Gui % hWnd ":Destroy"
else
{
WinSetTitle % "> Bitwarden CLI v" online
; Download
SetTimer Download, -1
SetTimer Percentage, 1
}
; Start Menu
start := A_AppDataCommon "\Microsoft\Windows\Start Menu\Programs\Auto-Type"
FileCreateDir % start
FileCreateShortcut % A_ProgramFiles "\Auto-Type\bw-at.exe"
, % start "\Auto-Type.lnk"
FileCreateShortcut % A_ProgramFiles "\Auto-Type\uninstall.exe"
, % start "\Uninstall.lnk"
FileCreateShortcut % A_ProgramFiles "\Auto-Type\bw-at.exe"
, % A_DesktopCommon "\Auto-Type.lnk"
pretty := Quote("BW_PRETTY=true")
appDataDir := Quote("BITWARDENCLI_APPDATA_DIR=%AppData%\Auto-Type")
FileCreateShortcut % A_ComSpec, % start "\BW CLI.lnk"
, % A_ProgramFiles "\Auto-Type"
, % "/K " Quote("set " pretty " && set " appDataDir " && bw status")
IniWrite https://github.com/anonymous1184/bitwarden-autotype, % start
. "\Project Page.url", InternetShortcut, URL
IniWrite % A_WinDir "\System32\shell32.dll", % start "\Project Page.url"
, InternetShortcut, IconFile
IniWrite 14, % start "\Project Page.url", InternetShortcut, IconIndex
; Size calculation
installSize := 0
for i,dir in [start, A_ProgramFiles "\Auto-Type"]
{
loop files, % dir "\*"
{
FileGetSize size, % A_LoopFileLongPath
installSize += size
}
}
; Uninstall information
FileGetVersion version, % A_ProgramFiles "\Auto-Type\bw-at.exe"
; https://docs.microsoft.com/en-us/windows/win32/msi/uninstall-registry-key
; https://nsis.sourceforge.io/Add_uninstall_information_to_Add/Remove_Programs
RegWrite REG_SZ, % key,, % A_Now
RegWrite REG_SZ, % key, Comments, Bitwarden Auto-Type capability via its CLI.
RegWrite REG_SZ, % key, DisplayIcon, % A_ProgramFiles "\Auto-Type\bw-at.exe"
RegWrite REG_SZ, % key, DisplayName, Bitwarden Auto-Type
RegWrite REG_SZ, % key, DisplayVersion, % version
RegWrite REG_DWORD, % key, EstimatedSize, % Format("{:#x}", installSize // 1024)
RegWrite REG_SZ, % key, HelpLink, % projectRoot "#readme"
RegWrite REG_SZ, % key, InstallDate, % A_YYYY A_MM A_DD
RegWrite REG_SZ, % key, InstallLocation, % A_ProgramFiles "\Auto-Type"
RegWrite REG_DWORD, % key, Language, % Format("{:#x}", 1033) ; || SZ "x64;1033"
RegWrite REG_DWORD, % key, NoModify, 0x1
RegWrite REG_DWORD, % key, NoRepair, 0x1
RegWrite REG_SZ, % key, Publisher, u/anonymous1184
uninstaller := Quote(A_ProgramFiles "\Auto-Type\uninstall.exe")
RegWrite REG_SZ, % key, QuietUninstallString, % uninstaller " -quiet:1"
RegWrite REG_SZ, % key, UninstallString, % uninstaller
RegWrite REG_SZ, % key, URLInfoAbout, % projectRoot "/issues"
RegWrite REG_SZ, % key, URLUpdateInfo, % projectRoot "/releases/latest"
version := StrSplit(version, ".")
RegWrite REG_SZ, % key, Version, % version[1] "." version[2] "." version[3]
RegWrite REG_DWORD, % key, VersionMajor, % Format("{:#x}", version[1])
RegWrite REG_DWORD, % key, VersionMinor, % Format("{:#x}", version[2])
/*
If installing immediately after an uninstall that could not remove directories,
those directories are queued for deletion on the next reboot. Entries contain a
double line-ending that AHK can't handle, thus reg.exe is used for querying the
values and then manually parsed and imported them.
*/
key := "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager"
cmd := "reg query " Quote(key) " /v PendingFileRenameOperations /se *"
pending := GetStdStream(cmd)
if InStr(pending, "Auto-Type")
{
regData := ""
for _,file in StrSplit(pending, "\??\", "*`r`n")
{
if (FileExist(file) && !InStr(file, "\Auto-Type"))
regData .= "\??\" file "`n`n"
}
RegWrite REG_MULTI_SZ, % key, PendingFileRenameOperations, % regData
}
; PowerShell
while !signComplete
ToolTip Please wait...
ToolTip
; Acknowledge
Alert(0x40, (isInstalled ? "Update" : "Installation") " complete!"
, "Application will be launched now.")
; Run, unelevated
app := A_ProgramFiles "\Auto-Type\bw-at.exe" (isInstalled ? "" : " -settings")
DllCall("wdc\WdcRunTaskAsInteractiveUser", "Str",app, "Ptr",0)
ExitApp
Signature:
/*TODO: Move into assets\ after the bug is fixed:
* https://www.autohotkey.com/boards/viewtopic.php?f=14&t=94956
*/
FileInstall bw-at.ps1, % A_Temp "\bw-at.ps1", % true
RunWait % "PowerShell -ExecutionPolicy Bypass -File .\bw-at.ps1 "
. Quote("Auto-Type") " "
. Quote(A_ProgramFiles "\Auto-Type\bw-at.exe")
. " start"
, % A_Temp, Hide UseErrorLevel
if (ErrorLevel)
{
done := UIAccess(A_ProgramFiles "\Auto-Type\bw-at.exe", false)
if (!done)
{
Run % A_ProgramFiles "\Auto-Type\uninstall.exe -quiet"
ExitApp
}
}
signComplete := true
FileDelete % A_Temp "\bw-at.ps1"
return
Download:
UrlDownloadToFile % asset.browser_download_url, % A_Temp "\bw.zip"
SetTimer Percentage, Delete
Gui % hWnd ":Destroy"
; Unzip
Zip_Extract(A_Temp "\bw.zip", A_ProgramFiles "\Auto-Type")
FileDelete % A_Temp "\bw.zip"
return
Percentage:
FileGetSize current, % A_Temp "\bw.zip"
downloaded := Round(current / asset.size * 100, 2)
GuiControl % hWnd ":", Static1, % "Downloaded: " downloaded "%"
return
#NoEnv
#NoTrayIcon
#KeyHistory 0
;@Ahk2Exe-IgnoreBegin
#SingleInstance Force
#Warn All, OutputDebug
;@Ahk2Exe-IgnoreEnd
/*@Ahk2Exe-Keep
#SingleInstance Ignore
*/
; Includes
#Include %A_ScriptDir%
#Include <JSON>
;@Ahk2Exe-Base %A_ScriptDir%\assets\bw-at.bin, setup.exe, CP65001
;@Ahk2Exe-SetCompanyName u/anonymous1184
;@Ahk2Exe-SetCopyright Copyleft 2020
;@Ahk2Exe-SetDescription Bitwarden Auto-Type Installer
;@Ahk2Exe-SetLanguage 0x0409
;@Ahk2Exe-SetMainIcon %A_ScriptDir%\assets\bw-at.ico
;@Ahk2Exe-SetName Bitwarden Auto-Type
;@Ahk2Exe-SetOrigFilename setup.ahk
;@Ahk2Exe-SetProductVersion 1.1.4.3
;@Ahk2Exe-SetVersion 1.1.4.3
;@Ahk2Exe-UpdateManifest 1, Auto-Type, 1.1.4.3, 0
; BinMod
;@Ahk2Exe-PostExec "%A_ScriptDir%\assets\BinMod.exe" "%A_WorkFileName%"
;@Ahk2Exe-Cont "2.AutoHotkeyGUI.Auto-Type-GUI"
;@Ahk2Exe-PostExec "%A_ScriptDir%\assets\BinMod.exe" "%A_WorkFileName%"
;@Ahk2Exe-Cont "22.>AUTOHOTKEY SCRIPT<.$APPLICATION SOURCE"
;@Ahk2Exe-PostExec "%A_ScriptDir%\assets\BinMod.exe" "%A_WorkFileName%" /SetUTC