From 84ed2b25067fd5768e3997468ef1e13d3432b3ff Mon Sep 17 00:00:00 2001 From: Will Ehrendreich Date: Mon, 9 Oct 2023 18:38:08 -0500 Subject: [PATCH] fix: if unable to run gzip, tries a powershell solution instead. fixes #76 --- lua/codeium/io.lua | 80 ++++++++++++++++++++++++++++++----------- lua/powershell/gzip.ps1 | 26 ++++++++++++++ 2 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 lua/powershell/gzip.ps1 diff --git a/lua/codeium/io.lua b/lua/codeium/io.lua index 3284a3e..ebf5be1 100644 --- a/lua/codeium/io.lua +++ b/lua/codeium/io.lua @@ -301,31 +301,69 @@ function M.generate_uuid() end function M.gunzip(path, callback) - local os = M.get_system_info() - - if os.is_windows then + if not M.executable("gzip") then + local function expandFile(infile) + local scriptDirectory = debug.getinfo(1, "S").source:match("^@(.*/)[^/]+$") + local command = "& { . " + .. vim.fn.shellescape(scriptDirectory .. "../powershell/gzip.ps1") + .. "; Expand-File " + .. vim.fn.shellescape(infile) + .. "}" + local output = vim.fn.system(command) + + if vim.v.shell_error ~= 0 then + error("Failed to expand file: " .. output) + end + end + local shell = vim.o.shell + local shellcmdflag = vim.o.shellcmdflag + local shellredir = vim.o.shellredir + local shellpipe = vim.o.shellpipe + local shellquote = vim.o.shellquote + local shellxquote = vim.o.shellxquote + + local pwshCoreAvailable = vim.fn.executable("pwsh") + + local isPowershell = vim.o.shell == "pwsh" + or vim.o.shell == "pwsh.exe" + or vim.o.shell == "powershell" + or vim.o.shell == "powershell.exe" + if not isPowershell then + if pwshCoreAvailable then + vim.o.shell = "pwsh" + else + vim.o.shell = "powershell" + end + vim.o.shellcmdflag = + "-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;" + vim.o.shellredir = "2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode" + vim.o.shellpipe = "2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode" + vim.o.shellquote = "" + vim.o.shellxquote = "" + end + isPowershell = vim.o.shell == "pwsh" + or vim.o.shell == "pwsh.exe" + or vim.o.shell == "powershell" + or vim.o.shell == "powershell.exe" + if isPowershell then + expandFile(path) + callback() + else + callback(nil, "gzip could not be found, powershell was unable to be run") + end + vim.o.shell = shell + vim.o.shellcmdflag = shellcmdflag + vim.o.shellredir = shellredir + vim.o.shellpipe = shellpipe + vim.o.shellquote = shellquote + vim.o.shellxquote = shellxquote + else M.job({ - "powershell.exe", - "-noprofile", - "-command", - "\"Expand-Archive -Path '", + "gzip", + "-d", path, - "' -DestinationPath '", - ".", - "'\"", on_exit = callback, }):start() - else - if not M.executable("gzip") then - callback(nil, "gzip could not be found") - else - M.job({ - "gzip", - "-d", - path, - on_exit = callback, - }):start() - end end end diff --git a/lua/powershell/gzip.ps1 b/lua/powershell/gzip.ps1 new file mode 100644 index 0000000..0c2cddb --- /dev/null +++ b/lua/powershell/gzip.ps1 @@ -0,0 +1,26 @@ +Function Expand-File +{ + Param( + $infile, + $outfile = ($infile -replace '\.gz$','') + ) + + $in = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read) + $output = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None) + $gzipStream = New-Object System.IO.Compression.GzipStream $in, ([IO.Compression.CompressionMode]::Decompress) + + $buffer = New-Object byte[](1024) + while($true) + { + $read = $gzipstream.Read($buffer, 0, 1024) + if ($read -le 0) + {break + } + $output.Write($buffer, 0, $read) + } + + $gzipStream.Close() + $output.Close() + $in.Close() + Remove-Item $infile +}