diff --git a/native/win_install.ps1 b/native/win_install.ps1 new file mode 100644 index 00000000..ad9fb57f --- /dev/null +++ b/native/win_install.ps1 @@ -0,0 +1,214 @@ +Param ( + [switch]$debug = $false +) + +# +# Constants +# +$win_tridactyl_dir_name = ".tridactyl" +$batch_file_name = "native_main.bat" +$manifest_file_name = "tridactyl.json" +$manifest_replace_str = "REPLACE_ME_WITH_SED" +$native_messenger_binary_name = "native_main.py" + +$native_messenger_files_http_base = ` + "https://raw.githubusercontent.com/cmcaine/tridactyl/master/native" + +$manifest_registry_path = ` + "HKCU:\Software\Mozilla\NativeMessagingHosts\tridactyl" + + +function Check-Path() +{ + Param($Path) + if((Test-Path $Path)) + { + Write-Host " - $Path created successfully!" + } + else + { + Write-Host " - $Path creating failed, quitting ..." + } +} + +# +# Prepare `.tridactyl` directory +# +$win_tridactyl_dir_path = "$env:USERPROFILE\$win_tridactyl_dir_name" +Write-Host "[+] Preparing $win_tridactyl_dir_name ..." + +if ($debug) +{ + Write-Host " - [debug] Removing $win_tridactyl_dir_path" + Remove-Item ` + -Force ` + -Recurse ` + -Path $win_tridactyl_dir_path +} + +If(!(test-path $win_tridactyl_dir_path)) +{ + New-Item -ItemType Directory ` + -Force ` + -Path $win_tridactyl_dir_path | Out-Null + + Check-Path -Path $win_tridactyl_dir_path +} +else +{ + Write-Host " - $win_tridactyl_dir_path exists, moving on ..." +} + +# +# Prepare `native_main.py` +# +$download_start_time = [long] (Get-Date ` + -Date ((Get-Date).ToUniversalTime()) ` + -UFormat %s) + +$native_messenger_binary_path = [string]::Format("{0}\{1}", + $win_tridactyl_dir_path, + $native_messenger_binary_name) + +$native_messenger_binary_url = [string]::Format("{0}/{1}?{2}", + $native_messenger_files_http_base, + $native_messenger_binary_name, + $download_start_time) + +Write-Host "[+] Downloading $native_messenger_binary_url ..." + +try { + $retval = Invoke-WebRequest ` + -Uri $native_messenger_binary_url ` + -OutFile $native_messenger_binary_path +} catch { + Write-Host ` + "Invoke-WebRequest StatusCode:" ` + $_.Exception.Response.StatusCode.value__ + + Write-Host ` + "Invoke-WebRequest StatusDescription:" ` + $_.Exception.Response.StatusDescription + + Write-Host "[+] Downloading failed, quitting ..." + exit +} + +Check-Path -Path $native_messenger_binary_path + +# +# Prepare `win_tridactyl.bat` +# +$batch_file_path = [string]::Format("{0}\{1}", + $win_tridactyl_dir_path, + $batch_file_name) +Write-Host "[+] Preparing $batch_file_path ..." + +$batch_file_content = @" +@echo off + +call python -u $win_tridactyl_dir_path\native_main.py +"@ + +$batch_file_content ` + | Out-File ` + -Encoding ascii ` + -FilePath $batch_file_path + +Check-Path -Path $batch_file_path + +# +# Prepare `tridactyl.json` +# +$download_start_time = [long] (Get-Date ` + -Date ((Get-Date).ToUniversalTime()) ` + -UFormat %s) + +$manifest_file_path = [string]::Format("{0}\{1}", + $win_tridactyl_dir_path, + $manifest_file_name) + +Write-Host "[+] Preparing $manifest_file_path ..." + +#$manifest_file_content = @" +#{ +# "name": "tridactyl", +# "description": "Tridactyl native command handler", +# "path": "win_tridactyl.bat", +# "type": "stdio", +# "allowed_extensions": [ +# "tridactyl.vim@cmcaine.co.uk", +# "tridactyl.vim.betas@cmcaine.co.uk" +# ] +#} +#"@ +# +#$manifest_file_content ` +# | Out-File ` +# -Encoding ascii ` +# -FilePath $manifest_file_path + +$manifest_file_url = [string]::Format("{0}/{1}?{2}", + $native_messenger_files_http_base, + $manifest_file_name, + $download_start_time) + +Write-Host "[+] Downloading $manifest_file_url ..." + +try { + $retval = Invoke-WebRequest ` + -Uri $manifest_file_url ` + -OutFile $manifest_file_path +} catch { + Write-Host ` + "Invoke-WebRequest StatusCode:" ` + $_.Exception.Response.StatusCode.value__ + + Write-Host ` + "Invoke-WebRequest StatusDescription:" ` + $_.Exception.Response.StatusDescription + + Write-Host "[+] Downloading failed, quitting ..." + exit +} + +Check-Path -Path $manifest_file_path + +(Get-Content ` + $manifest_file_path).replace($manifest_replace_str, $batch_file_name) ` + | Set-Content -Encoding ascii $manifest_file_path + +$found_pattern = (Select-String ` + -Path $manifest_file_path ` + -Pattern $manifest_replace_str ` + -CaseSensitive ` + | select Line) + +if (! $found_pattern) +{ + Write-Host " - $manifest_file_path patched successfully!" +} + +# +# Add registry entry +# +Write-Host "[+] Adding registry key $manifest_registry_path ..." +New-Item ` + -Path $manifest_registry_path ` + -Force | Out-Null + +New-ItemProperty ` + -Path $manifest_registry_path ` + -Name "(Default)" ` + -Value $manifest_file_path ` + -Force | Out-Null + +#Get-ItemProperty ` +# -Path $manifest_registry_path ` +# -Name "(Default)" + +if ((Get-ItemProperty ` + -Path $manifest_registry_path)."(Default)" = $manifest_file_path) +{ + Write-Host " - Registry key looks good!" +} diff --git a/src/config.ts b/src/config.ts index 96d534c6..ee07bc54 100644 --- a/src/config.ts +++ b/src/config.ts @@ -262,6 +262,8 @@ const DEFAULTS = o({ browser: "firefox", nativeinstallcmd: "curl -fsSl https://raw.githubusercontent.com/cmcaine/tridactyl/master/native/install.sh | bash", + win_nativeinstallcmd: + "Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/gsbabil/tridactyl/master/native/win_install.ps1'))", profiledir: "auto", // Container settings diff --git a/src/excmds.ts b/src/excmds.ts index 5f8f3c72..108a6f24 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -308,9 +308,15 @@ export async function native() { */ //#background export async function installnative() { - const installstr = await config.get("nativeinstallcmd") - await clipboard("yank", installstr) - fillcmdline("# Installation command copied to clipboard. Please paste and run it in your shell to install the native messenger.") + if ((await browser.runtime.getPlatformInfo()).os === "win") { + const installstr = await config.get("win_nativeinstallcmd") + await clipboard("yank", installstr) + fillcmdline("# Installation command copied to clipboard. Please paste and run it in Powershell to install the native messenger.") + } else { + const installstr = await config.get("nativeinstallcmd") + await clipboard("yank", installstr) + fillcmdline("# Installation command copied to clipboard. Please paste and run it in your shell to install the native messenger.") + } } /** @@ -353,7 +359,12 @@ export async function updatenative(interactive = true) { if (interactive) logger.error("Updating the native messenger on OSX is broken. Please use `:installnative` instead.") return } - await Native.run(await config.get("nativeinstallcmd")) + if ((await browser.runtime.getPlatformInfo()).os === "win") { + await Native.run(await config.get("win_nativeinstallcmd")) + } else { + await Native.run(await config.get("nativeinstallcmd")) + } + if (interactive) native() } }