docs.sheetjs.com/tests/math/polars.ps1
2026-03-09 00:32:12 -04:00

96 lines
4.4 KiB
PowerShell

#!/usr/bin/env pwsh
# https://docs.sheetjs.com/docs/demos/math/pandas
$oldDir = Get-Location
$tempDir = Join-Path -Path $env:TEMP -ChildPath "sheetjs-polars"
if (Test-Path -Path $tempDir) { Remove-Item -Path $tempDir -Recurse -Force }
New-Item -ItemType Directory -Path $tempDir | Out-Null
Set-Location -Path $tempDir
Invoke-WebRequest -Uri "https://www.7-zip.org/a/7z2501-extra.7z" -OutFile "7z2501-extra.7z"
$7zrPath = "7zr.exe"
if (-not (Test-Path $7zrPath)) {
Invoke-WebRequest -Uri "https://www.7-zip.org/a/7zr.exe" -OutFile $7zrPath
}
.\7zr.exe x 7z2501-extra.7z -y -o"7za" | Out-Null
Move-Item -Path "7za\7za.exe" -Destination "7za.exe" -Force
Remove-Item -Path "7z2501-extra.7z" -Force
Remove-Item -Path "7za" -Recurse -Force
Invoke-WebRequest -Uri "https://duktape.org/duktape-2.7.0.tar.xz" -OutFile "duktape-2.7.0.tar.xz"
.\7za.exe x duktape-2.7.0.tar.xz -y | Out-Null
.\7za.exe x duktape-2.7.0.tar -y | Out-Null
$vsVersions = @("2022\Community", "2022\Professional", "2022\Enterprise", "2019\Community")
foreach ($vsVersion in $vsVersions) {
$vcvarsPath = "${env:ProgramFiles}\Microsoft Visual Studio\$vsVersion\VC\Auxiliary\Build\vcvars64.bat"
if (Test-Path -Path $vcvarsPath) { break }
}
$tempEnvFile = [System.IO.Path]::GetTempFileName()
$vcvarsArg = if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { 'arm64' } else { '' }
Push-Location "$vcvarsPath\..\.."
if ($vcvarsArg) {
cmd.exe /c "`"$vcvarsPath`" $vcvarsArg && set > `"$tempEnvFile`""
} else {
cmd.exe /c "`"$vcvarsPath`" && set > `"$tempEnvFile`""
}
Pop-Location
Get-Content "$tempEnvFile" | ForEach-Object { if ($_ -match '^([^=]+)=(.*)$') {
$name = $matches[1]
$value = $matches[2]
if ($name -ne 'PATH') {
Set-Item -Path "Env:$name" -Value $value
} else {
$env:PATH = "$value;$env:PATH"
}
} }
Remove-Item "$tempEnvFile" -Force -ErrorAction SilentlyContinue
$dukSrcDir = Join-Path -Path (Join-Path -Path $tempDir -ChildPath "duktape-2.7.0") -ChildPath "src"
$dukConfigPath = Join-Path -Path $dukSrcDir -ChildPath "duk_config.h"
$dukConfigContent = [System.IO.File]::ReadAllText($dukConfigPath)
$exportDefs = "`n`n#define DUK_EXTERNAL_DECL extern __declspec(dllexport)`n#define DUK_EXTERNAL __declspec(dllexport)`n"
$dukConfigContent += $exportDefs
[System.IO.File]::WriteAllText($dukConfigPath, $dukConfigContent)
$dukBuildDir = Join-Path -Path $tempDir -ChildPath "duktape-2.7.0"
$dukDllPath = Join-Path -Path $dukBuildDir -ChildPath "duktape.dll"
cl /O2 /W3 /I"$dukSrcDir" /LD /DDUK_SINGLE_FILE /DDUK_F_DLL_BUILD /DDUK_F_WINDOWS /DDUK_COMPILING_DUKTAPE "/Fe:$dukDllPath" "$dukSrcDir\duktape.c"
Copy-Item $dukDllPath -Destination $tempDir
Invoke-WebRequest -Uri "https://cdn.sheetjs.com/xlsx-latest/package/dist/shim.min.js" -OutFile "shim.min.js"
Invoke-WebRequest -Uri "https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js" -OutFile "xlsx.full.min.js"
Invoke-WebRequest -Uri "https://docs.sheetjs.com/pres.numbers" -OutFile "pres.numbers"
Invoke-WebRequest -Uri "https://docs.sheetjs.com/pandas/sheetjs.py" -OutFile "sheetjs.py"
Invoke-WebRequest -Uri "https://docs.sheetjs.com/pandas/SheetJSPandas.py" -OutFile "SheetJSPandas.py"
$sheetjspy = Get-Content "sheetjs.py" -Raw
$sheetjspy = $sheetjspy -replace 'libduktape\.207\.20700\.so', '.\\duktape.dll'
$sheetjspy = $sheetjspy -replace 'with open\(path, "r"\)', 'with open(path, "rb")'
$sheetjspy = $sheetjspy -replace 'def str_to_c\(s\):\r?\n b = s\.encode\("utf8"\)\r?\n return \[c_char_p\(b\), len\(b\)\]', "def str_to_c(s):`r`n if type(s) == bytes:`r`n b = s`r`n else:`r`n b = s.encode(`"utf8`")`r`n return [c_char_p(b), len(b)]"
$sheetjspy = $sheetjspy -replace 'from pandas import read_csv', 'from polars import read_csv'
$sheetjspy = $sheetjspy -replace 'json = df\.to_json\(orient="records"\)', 'json = df.write_json()'
$sheetjspy | Set-Content "sheetjs.py"
$sheetJsPandasContent = Get-Content "SheetJSPandas.py" -Raw
$sheetJsPandasContent = $sheetJsPandasContent -replace 'print\(df\.info\(\)\)', 'print(df)'
$sheetJsPandasContent = $sheetJsPandasContent -replace 'SheetJSPandas\.xlsb', 'SheetJSPolars.xlsb'
$sheetJsPandasContent | Set-Content "SheetJSPandas.py"
$polarsInstalled = Get-Module -ListAvailable -Name polars -ErrorAction SilentlyContinue
if (-not $polarsInstalled) { pip install polars }
python SheetJSPandas.py pres.numbers
npm init -y
npm i --save xlsx-cli
.\node_modules\.bin\xlsx-cli SheetJSPolars.xlsb
Set-Location $oldDir
Remove-Item -Path $tempDir -Recurse -Force