forked from adityadr/PowerShellNotebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertToSQLNoteBook.ps1
More file actions
93 lines (81 loc) · 3.1 KB
/
Copy pathConvertToSQLNoteBook.ps1
File metadata and controls
93 lines (81 loc) · 3.1 KB
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
function ConvertTo-SQLNoteBook {
<#
.Example
ConvertTo-PowerShellNoteBook -InputFileName c:\Temp\demo.txt -OutputNotebookName c:\Temp\demo.ipynb
#>
param(
$InputFileName,
$OutputNotebookName
)
<#
New-SQLNotebook -NoteBookName $OutputNotebookName {
$content = Get-Content $InputFileName | Out-String
$insideBlockComment = $false
$lines = [regex]::split($content,'\r\n')
foreach ($line in $lines) {
if($line.Length -eq 0) { continue }
if ($insideBlockComment) {
$blockComment += $line
if ($line -match '\*/') {
$insideBlockComment = $false
Add-NotebookMarkdown $blockComment
Write-Verbose "block comment - $blockComment" -Verbose
$blockComment = ''
continue
}
Write-Verbose "block comment - $blockComment" -Verbose
}
elseif ($line -match '/\*') {
$insideBlockComment = $true;
$blockComment += $line
#Write-Verbose "comment - $line" -Verbose
}else{
if ($line -match '--') {
# Write-Verbose "comment - $line" -Verbose
Add-NotebookMarkdown $line
}
else{
Add-NotebookCode $line
#Write-Verbose "code - $line" -Verbose
}
}
}
}
#>
New-SQLNotebook -NoteBookName $OutputNotebookName {
$s = Get-Content -Raw ( Resolve-Path $InputFileName )
#$s.GetType()
<# Doug's code for extracting the comment blocks. #>
$locations=@()
$pos=$s.IndexOf("/*")
while ($pos -ge 0) {
$locations+=[pscustomobject]@{startPos=$pos;endPos=$null}
$pos=$s.IndexOf("/*", $pos+2)
}
$count=0
$pos=$s.IndexOf("*/")
while ($pos -ge 0) {
$locations[$count].endPos=$pos
$pos=$s.IndexOf("*/", $pos+1)
$count++
}
<# My basic attempt #>
$PreviousLocation = $null
<# The line below cpits out a code block, in the event the file stars with code. #>
Add-NotebookCode ($s.Substring(0, ($locations[0].startPos)))
foreach($location in $locations)
{
$start=$location.startPos
$length=$location.endPos-$location.startPos+2
<# The line below spits out the comment blocks #>
Add-NotebookMarkdown $s.Substring($start+2, $length-4)
<# The line below spits out the code blocks #>
Add-NotebookCode ($s.Substring($PreviousLocation.endPos, ($location.startPos - $PreviousLocation.endPos)))
$PreviousLocation=$location
}
Add-NotebookCode ($s.Substring($location.endPos+2, ($s.Length-$location.endPos-2)))
<# The line above grabs the last code block from the .SQL file. #>
<# When you need to debug, the list of comment-block locations is in the variable below #>
$locations
}
}