diff --git a/DocFx.ps1 b/DocFx.ps1 index e69de29..7d35d91 100644 --- a/DocFx.ps1 +++ b/DocFx.ps1 @@ -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 + } +} \ No newline at end of file diff --git a/PSToolset.psd1 b/PSToolset.psd1 index 9ede80e..9100e2f 100644 --- a/PSToolset.psd1 +++ b/PSToolset.psd1 @@ -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 diff --git a/PSToolset.psm1 b/PSToolset.psm1 index 20d790e..dabe858 100644 --- a/PSToolset.psm1 +++ b/PSToolset.psm1 @@ -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 diff --git a/Python.ps1 b/Python.ps1 index 1de3a28..65f6081 100644 --- a/Python.ps1 +++ b/Python.ps1 @@ -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.