diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 2d3f445..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,56 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "PowerShell", - "request": "launch", - "name": "PowerShell Pester Tests", - "script": "Import-Module -Name '.\\' -Force ; Invoke-Pester", // Change to '.\\ModuleName.psd1' if Git name different - "args": [""], - "cwd": "${workspaceFolder}" - }, - { - "type": "PowerShell", - "request": "launch", - "name": "PowerShell Launch Current File", - "script": "${file}", - "args": [], - "cwd": "${file}" - }, - { - "type": "PowerShell", - "request": "launch", - "name": "PowerShell Launch Current File in Temporary Console", - "script": "${file}", - "args": [], - "cwd": "${file}", - "createTemporaryIntegratedConsole": true - }, - { - "type": "PowerShell", - "request": "launch", - "name": "PowerShell Launch Current File w/Args Prompt", - "script": "${file}", - "args": [ - "${command:SpecifyScriptArgs}" - ], - "cwd": "${file}" - }, - { - "type": "PowerShell", - "request": "attach", - "name": "PowerShell Attach to Host Process", - "processId": "${command:PickPSHostProcess}", - "runspaceId": 1 - }, - { - "type": "PowerShell", - "request": "launch", - "name": "PowerShell Interactive Session", - "cwd": "" - } - ] -} \ No newline at end of file diff --git a/Invoke-NotebookFormatter.ps1 b/Invoke-NotebookFormatter.ps1 new file mode 100644 index 0000000..ab6e7cb --- /dev/null +++ b/Invoke-NotebookFormatter.ps1 @@ -0,0 +1,61 @@ +function Invoke-NotebookFormatter { + <# + .Synopsis + #> + + param( + [Parameter(ValueFromPipelineByPropertyName = $true)] + [Alias('Fullname')] + $Path = $pwd + ) + + Begin { + $found = (Get-Module -ListAvailable PSScriptAnalyzer) + if (!$found) { + Write-Host -ForegroundColor Red "PSScriptAnalyzer not found, 'Install-Module PSScriptAnalyzer' and rerun" + } + } + + Process { + if ($found) { + if ([System.Uri]::IsWellFormedUriString($Path, [System.UriKind]::Absolute)) { + try { + $data = Invoke-RestMethod $Path + } + catch { + throw "$($Path) is not a valid Jupyter Notebook" + } + } + else { + $json = Get-Content $Path + $data = $json | ConvertFrom-Json + } + } + + $cells = $data.cells + $totalCells = $cells.count + for ($idx = 0; $idx -lt $totalCells; $idx++) { + $cell = $cells[$idx] + if ($cell.cell_type) { + if ($cell.metadata.dotnet_interactive.language -eq 'pwsh') { + $result = Invoke-Formatter (-join $cell.source) + $lines = $result.split("`n") + + $cell.source = $( + foreach ($line in $lines) { + $line + "`n" + } + ) + } + } + } + + $targetPath = Split-Path $Path + $LeafBase = Split-Path $Path -LeafBase + $Extension = Split-Path $Path -Extension + + $targetFile = "{0}\{1}.formatted{2}" -f $targetPath, $LeafBase, $Extension + + ConvertTo-Json $data -Depth 15 | Set-Content -Path $targetFile + } +} \ No newline at end of file diff --git a/PowerShellNotebook.psd1 b/PowerShellNotebook.psd1 index 06b5748..51904ca 100644 --- a/PowerShellNotebook.psd1 +++ b/PowerShellNotebook.psd1 @@ -80,6 +80,7 @@ For detailed instructions and examples, click through the "Project Site" link or 'Get-ParsedSql', 'Get-ParsedSqlOffsets', 'Invoke-ExecuteNotebook', + 'Invoke-NotebookFormatter', 'Invoke-PowerShellNotebook', 'loadScriptDomModules', 'New-CodeCell', diff --git a/PowerShellNotebook.psm1 b/PowerShellNotebook.psm1 index 929daa1..4cfd9b8 100644 --- a/PowerShellNotebook.psm1 +++ b/PowerShellNotebook.psm1 @@ -11,6 +11,7 @@ . $PSScriptRoot\GetNotebookContent.ps1 . $PSScriptRoot\GetNotebookDisplayData.ps1 . $PSScriptRoot\GetParameterInsertionIndex.ps1 +. $PSScriptRoot\Invoke-NotebookFormatter.ps1 . $PSScriptRoot\InvokeExecuteNotebook.ps1 . $PSScriptRoot\InvokePowerShellNotebook.ps1 . $PSScriptRoot\New-InteractiveNotebook.ps1 diff --git a/testFormatter.ipynb b/testFormatter.ipynb new file mode 100644 index 0000000..b351048 --- /dev/null +++ b/testFormatter.ipynb @@ -0,0 +1,114 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "dotnet_interactive": { + "language": "pwsh" + } + }, + "outputs": [], + "source": [ + "if($a) { 'yes'\r\n", + "} else { 'nox' \r\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + } + }, + "outputs": [], + "source": [ + "var x = 1;\r\n", + "x" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "----\r\n", + "# Some Markdown" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "dotnet_interactive": { + "language": "pwsh" + } + }, + "outputs": [], + "source": [ + "for($i = 0; $i -lt 10; $i++) { $i*2; \"test\" 1\r\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "dotnet_interactive": { + "language": "pwsh" + } + }, + "outputs": [], + "source": [ + "function foo {\r\n", + " \"hello\"\r\n", + " }" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "dotnet_interactive": { + "language": "pwsh" + } + }, + "outputs": [], + "source": [ + "function foo {\r\n", + "\"hello\"\r\n", + " }" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "dotnet_interactive": { + "language": "pwsh" + } + }, + "outputs": [], + "source": [ + "$h=@{\r\n", + "1='123'\r\n", + "123123='asfd'\r\n", + "}" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "PowerShell", + "language": "powershell", + "name": "powershell" + }, + "language_info": { + "name": "powershell", + "version": "" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file