-
Notifications
You must be signed in to change notification settings - Fork 9
/
DocFx.ps1
89 lines (74 loc) · 2.4 KB
/
DocFx.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
# 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 )
{
Push-Location $folder
$path = Get-ChildItem -Recurse "docfx.json" | select -First 1
Pop-Location
$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
Start-Process http://localhost:8080
}
# 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
}
}