Ported over code to run docfx from jupyter notebook

This commit is contained in:
Alexander Kostikov 2020-07-28 16:45:02 -07:00
Родитель 440e5e972f
Коммит f30cac9193
4 изменённых файлов: 86 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,83 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
function Start-DocFx
{
<#
.SYNOPSIS
Start docfx in current folder or $env:DefaultDocFxPath.
Reuse existing docfx instance already running if possible.
.PARAMETER Force
Don't reuse anything and don't use defaults.
Just open a new docfx in the current folder.
.EXAMPLE
Start-DocFx
Tries to reopen a currently opened docfx.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions', '',
Justification = 'Intended to be this way')]
param
(
[switch] $Force
)
# Test if docfx is installed
if( -not (Get-Command docfx.exe -ea Ignore) )
{
throw "docfx.exe must be discoverable via PATH environment variable"
}
# Cleanup cleanup jobs =)
$cleanupJobName = "Start-DocFx cleanup"
Get-Job $cleanupJobName -ea Ignore | where State -eq Completed | Remove-Job
# Helper function
function Open-DocFx( $folder = $pwd )
{
$path = Get-ChildItem -Recurse docfx.json | select -First 1
$ps = Start-Process `
-FilePath "pwsh" `
-ArgumentList ('-Command "docfx ' + $path + ' --serve"') `
-WorkingDirectory $folder `
-WindowStyle Hidden `
-PassThru
Start-Job -Name $cleanupJobName {
Start-Sleep -Seconds 60
$ps | Stop-Process
} | Out-Null
}
# When need to open new docfx in current folder
if( $Force )
{
"Open new docfx in current folder $pwd"
Open-DocFx
return
}
# Trying to reuse opened docfx if possible
if( Get-Process docfx -ea Ignore )
{
"Found existing docfx, reopening default URL"
Start-Process http://localhost:8080
return
}
# Run notebook from default location if possible
if( $env:DefaultDocFxPath )
{
"Open new docfx in `$env:DefaultDocFxPath = $env:DefaultDocFxPath"
Open-DocFx $env:DefaultDocFxPath
}
else
{
"Open new docfx in current folder $pwd, note that you can use `$env:DefaultDocFxPath instead if you define it"
Open-DocFx
}
}

Просмотреть файл

@ -64,6 +64,7 @@
"Get-Parameter", "Use-Project", "Use-Filter",
"Get-Ini", "Show-Ini", "ConvertFrom-Ini", "Import-Ini",
# DoxFx
"Start-DocFx",
# Files
"Resolve-ScriptPath", "Get-FileEncoding",
# Functional

Просмотреть файл

@ -30,6 +30,7 @@ Set-Alias any Test-Any
Set-Alias call Set-CmdEnvironment
Set-Alias construct ConvertTo-PsObject
Set-Alias default Use-Default
Set-Alias dfx Start-DocFx
Set-Alias first Get-First
Set-Alias f Use-Filter
Set-Alias gite Open-GitExtensions

Просмотреть файл

@ -6,7 +6,7 @@ function Start-JupyterNotebook
<#
.SYNOPSIS
Start Jupyter Notebook in current folder or $env:DefaultJupyterNotebookPath.
Reuse existing notebook already running if possible
Reuse existing notebook already running if possible.
.PARAMETER Force
Don't reuse anything and don't use defaults.