-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathConvertToPowerShellNoteBook.ps1
141 lines (122 loc) · 6.1 KB
/
ConvertToPowerShellNoteBook.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function ConvertTo-PowerShellNoteBook {
<#
.Synopsis
Convert PowerShell scripts (ps1 files) to interactive notebooks (ipynb files)
.Description
Convert PowerShell scripts on disk or the internet to interactive notebooks that can be run in Azure Data Studio or with `Invoke-ExecuteMethod`
.Example
ConvertTo-PowerShellNoteBook -InputFileName c:\Temp\demo.txt -OutputNotebookName c:\Temp\demo.ipynb
.Example
ConvertTo-PowerShellNoteBook 'https://raw.githubusercontent.com/dfinke/PowerShellNotebook/master/__tests__/DemoFiles/demo_SingleCommentSingleLineCodeBlock.ps1'
.Example
$(
'https://raw.githubusercontent.com/dfinke/PowerShellNotebook/master/__tests__/DemoFiles/demo_SingleCommentSingleLineCodeBlock.ps1'
dir *.ps1
) | ConvertTo-PowerShellNoteBook
#>
param(
[parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)]
[Alias('FullName', 'Path')]
$InputFileName,
$OutputNotebookName
)
Process {
#region Parsing section.
Write-Progress -Activity "Converting PowerShell file to Notebook" -Status "Converting $($InputFileName)"
if ([System.Uri]::IsWellFormedUriString($InputFileName, [System.UriKind]::Absolute)) {
$s = Invoke-RestMethod -Uri $InputFileName
}
else {
$InputFileName = Resolve-Path $InputFileName
$s = Get-Content -Raw $InputFileName
}
# if no $OutputNotebookName, grab the filename and replace the ps1
# should work with both filenames and uri's
if (!$OutputNotebookName) {
$OutputNotebookName = (Split-Path -leaf $InputFileName) -replace '.ps1', '.ipynb'
$OutputNotebookName = $pwd.Path + "\" + $OutputNotebookName
}
try {
# $CommentRanges = [System.Management.Automation.PSParser]::Tokenize((Get-Content -Raw $InputFileName), [ref]$null).Where( { $_.Type -eq 'Comment' }) |
$CommentRanges = [System.Management.Automation.PSParser]::Tokenize($s, [ref]$null).Where( { $_.Type -eq 'Comment' }) |
Select-Object -Property Start, Length, Type, Content
}
Catch {
"This is not a valid PowerShell file"
}
$BlocksWitGaps = @()
$Previous = $null
foreach ($CommentBlock in $CommentRanges ) {
$BlockOffsets = [ordered]@{
Start = $CommentBlock.Start;
StopOffset = $CommentBlock.Start + $CommentBlock.Length;
Length = $CommentBlock.Length;
GapLength = [int] $CommentBlock.Start - $Previous.StopOffset;
PreviousStart = $Previous.Start;
PreviousStopOffset = $Previous.StopOffset;
Type = 'Code';
GapText = IF ($CommentBlock.Start - $Previous.StopOffset -gt 1) { [string] $s.Substring($Previous.StopOffset, ($CommentBlock.Start - $Previous.StopOffset)).trimstart() }else { [string] '' };
Content = $CommentBlock.Content
}
$Previous = $BlockOffsets
$BlocksWitGaps += [pscustomobject] $BlockOffsets
}
$AllBlocks = @()
$Previous = $null
$AllBlocks = $CommentRanges
<# Catch anything missed from the tail of the file, add it to $AllBlocks #>
if ($BlocksWitGaps.Count -eq 1) {
<# This step handles ading the psobjects together if $CommentBlock isn't an array. #>
$AllBlocks = @($CommentBlock; [pscustomobject][Ordered]@{
Start = ($CommentBlock.Start + $CommentBlock.Length);
Length = $s.Length - ($CommentBlock.Start + $CommentBlock.Length);
Type = 'Gap';
Content = $s.Substring(($CommentBlock.Start + $CommentBlock.Length), ($s.Length - ($CommentBlock.Start + $CommentBlock.Length))).trimstart()
})
}
else {
if (($CommentBlock.Start + $CommentBlock.Length) -lt $s.Length) {
$AllBlocks += [pscustomobject][ordered]@{
Start = ($CommentBlock.Start + $CommentBlock.Length);
Length = $s.Length - ($CommentBlock.Start + $CommentBlock.Length);
Type = 'Gap';
Content = $s.Substring(($CommentBlock.Start + $CommentBlock.Length), ($s.Length - ($CommentBlock.Start + $CommentBlock.Length))).trimstart()
}
}
}
foreach ($GapBlock in $BlocksWitGaps ) {
$GapOffsets = [ordered]@{
Start = $GapBlock.PreviousStopOffset;
StopOffset = $GapBlock.Start;
Length = $GapBlock.GapLength;
Type = 'Gap';
Content = $GapBlock.GapText.trimstart()
}
$Previous = $GapOffsets
$AllBlocks += if ($GapOffsets.Length -gt 0) { [pscustomobject] $GapOffsets }
}
#endregion
#region Notebook creation section.
New-PSNotebook -NoteBookName $OutputNotebookName -DotNetInteractive {
foreach ($Block in $AllBlocks | Sort-Object Start ) {
switch ($Block.Type) {
'Comment' {
$TextBlock = $s.Substring($Block.Start, $Block.Length) -replace ("\n", " `n ")
if ($TextBlock.Trim().length -gt 0) {
Add-NotebookMarkdown -markdown (-join $TextBlock)
}
}
'Gap' {
$GapBlock = $s.Substring($Block.Start, $Block.Length)
if ($GapBlock.Trim().length -gt 0) {
Add-NotebookCode -code (-join $GapBlock.trimstart().trimend()) # -replace ("\n", " `n ")
}
}
}
}
}
# Set $OutputNotebookName to $null otherwise if the $InputFileName is being piped to, it won't get reset
$OutputNotebookName = $null
#endregion
}
}