Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spike - Formats script text for only pwsh cells #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions .vscode/launch.json

This file was deleted.

61 changes: 61 additions & 0 deletions Invoke-NotebookFormatter.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions PowerShellNotebook.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions PowerShellNotebook.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
114 changes: 114 additions & 0 deletions testFormatter.ipynb
Original file line number Diff line number Diff line change
@@ -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
}