Skip to content

Commit

Permalink
fix: if unable to run gzip, tries a powershell solution instead. fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Ehrendreich authored and Will Ehrendreich committed Oct 9, 2023
1 parent 32dec44 commit 84ed2b2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 21 deletions.
80 changes: 59 additions & 21 deletions lua/codeium/io.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions lua/powershell/gzip.ps1
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 84ed2b2

Please sign in to comment.