-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetNotebookToPs.ps1
42 lines (40 loc) · 2.04 KB
/
SetNotebookToPs.ps1
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
param (
[Parameter(ValueFromPipeline = $true)]
$Path,
$OutputPath,
[switch]$FixOutput,
[switch]$SetMetaData
)
process {
$nb = Get-Content $Path | ConvertFrom-Json -Depth 10
$nb.metadata.kernelspec.language = 'PowerShell' # "C#" / "F#"
$nb.metadata.kernelspec.name = '.net-powershell' # ".net-csharp" / ".net-fsharp"
$nb.metadata.kernelspec.display_name = '.NET (PowerShell)' # ".NET (C#)" / ".NET (F#)"
$nb.metadata.language_info.file_extension = '.ps1' # ".cs" / "".fs"
$nb.metadata.language_info.mimetype = 'text/x-powershell' # "text/x-csharp" / "text/x-fsharp"
$nb.metadata.language_info.name = 'PowerShell' # "csharp" / "fsharp"
$nb.metadata.language_info.pygments_lexer = 'powerShell' # "csharp" / "fsharp"
$nb.metadata.language_info.version = '7.0' # "8.0" / "4.5"
foreach ($cell in $nb.cells.where({$_.cell_Type -eq 'code' -and
$_.source[0] -match '^#!pwsh'})) {
$cell.source = $cell.source[1..$cell.source.count]
}
if ($FixOutput) {
foreach ($cell in $nb.cells.where({$_.cell_Type -eq 'code' -and
$_.Outputs.output_type -eq 'unknown' -and #all outputtypes are 'unknown'
-not $_.Outputs.output_type -ne 'unknown'})) {
$h = @{'name' = 'stdout'
'output_type' = 'stream'
'text' = $cell.outputs.data.'text/plain' -replace '[\r\n]+$',"`n"
}
$cell.outputs = ,$h
}
}
if ($SetMetaData) {
foreach ($cell in $j.cells.Where({$_.cell_type -eq "code"})) {
$cell['metadata'] = @{'dotnet_interactive' = @{'language'='pwsh'} }
}
}
if (-not $OutputPath) {$OutputPath = $Path}
ConvertTo-Json $nb -Depth 10 | Out-File -Encoding utf8 -path $OutputPath
}