-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExportNotebookToSqlScript.ps1
76 lines (60 loc) · 2.41 KB
/
ExportNotebookToSqlScript.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
function Export-NotebookToSqlScript {
<#
.SYNOPSIS
Exports all code blocks from a SQL Notebook to a SQL script
.DESCRIPTION
Exports from either a local notebook or one on the internet
.Example
Export-NotebookToSqlScript .\BPCheck.ipynb
Get-Content .\BPCheck.SQL
Converts a local copy of the BPCheck.ipynb Jupyter Notebook into a .SQL file, and gets the content of the
resulting .SQL file.
.Example
Export-NotebookToSqlScript "https://raw.githubusercontent.com/microsoft/tigertoolbox/master/BPCheck/BPCheck.ipynb"
Get-Content .\BPCheck.sql
Downloads the latest version of the BPCheck Jupyter Notebook from the TigerToolbox repository, converts it
into a .SQL file (named BPCheck.SQL), and gets the content.
.Example
Export-NotebookToSqlScript "https://raw.githubusercontent.com/microsoft/tigertoolbox/master/BPCheck/BPCheck.ipynb"
Open-EditorFile .\BPCheck.sql
Downloads the latest version of the BPCheck Jupyter Notebook from the TigerToolbox repository, converts it
into a .SQL file (named BPCheck.SQL), and when run from the PowerShell Integrated Console (in either VS Code or
Azure Data Studio), opens it as a new Notebook window.
#>
[CmdletBinding()]
param(
$FullName,
$outPath = "./",
$IncludeTextCells=$true
)
Write-Progress -Activity "Exporting SQL Notebook" -Status $FullName
if ([System.Uri]::IsWellFormedUriString($FullName, [System.UriKind]::Absolute)) {
$outFile = $FullName.split('/')[-1]
}
else {
$outFile = (Split-Path -Leaf $FullName)
}
$outFile = $outFile -replace ".ipynb", ".sql"
$fullOutFileName = $outPath + $outFile
$heading = @"
/*
Created from: $($FullName)
Created by: Export-NotebookToSqlScript
Created on: {0:D} {0:t}
*/
"@ -f (Get-Date)
$heading | Set-Content $fullOutFileName
if($IncludeTextCells -eq $false)
{$sourceBlocks = Get-NotebookContent $FullName -JustCode}
else{$sourceBlocks = Get-NotebookContent $FullName}
$result = foreach ($sourceBlock in $sourceBlocks)
{
switch ($sourceBlock.Type) {
'code' {($sourceBlock.Source)}
'markdown' {"/* "+($sourceBlock.Source)+" */"}
}
""
}
$result | Add-Content $fullOutFileName
Write-Verbose "$($outFile) created"
}