forked from DSharpPlus/DSharpPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild-docs.ps1
executable file
·491 lines (417 loc) · 12.5 KB
/
rebuild-docs.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/usr/bin/env pwsh
# Rebuild-docs
#
# Rebuilds the documentation for DSharpPlus project, and places artifacts in specified directory.
#
# Author: Emzi0767
# Version: 2017-09-11 14:20
#
# Arguments:
# .\rebuild-docs.ps1 <path to docfx> <output path> <docs project path>
#
# Run as:
# .\rebuild-docs.ps1 .\path\to\docfx\project .\path\to\output project-docs
param
(
[parameter(Mandatory = $true)]
[string] $DocsPath,
[parameter(Mandatory = $true)]
[string] $OutputPath,
[parameter(Mandatory = $true)]
[string] $PackageName
)
# Backup the environment
$current_path = $Env:PATH
$current_location = Get-Location
# Tool paths
$docfx_path = Join-Path "$current_location" "docfx"
$sevenzip_path = Join-Path "$current_location" "7zip"
# Restores the environment
function Restore-Environment()
{
Write-Host "Restoring environment variables"
$Env:PATH = $current_path
Set-Location -path "$current_location"
if (Test-Path "$docfx_path")
{
Remove-Item -recurse -force "$docfx_path"
}
if (Test-Path "$sevenzip_path")
{
Remove-Item -recurse -force "$sevenzip_path"
}
}
# Downloads and installs latest version of DocFX
function Install-DocFX([string] $target_dir_path)
{
Write-Host "Installing DocFX"
# Check if the target directory exists
# If it does, remove it
if (Test-Path "$target_dir_path")
{
Write-Host "Target directory exists, deleting"
Remove-Item -recurse -force "$target_dir_path"
}
# Create target directory
$target_dir = New-Item -type directory "$target_dir_path"
$target_fn = "docfx.zip"
# Form target path
$target_dir = $target_dir.FullName
$target_path = Join-Path "$target_dir" "$target_fn"
# Download release info from Chocolatey API
try
{
Write-Host "Getting latest DocFX release"
$release_json = Invoke-WebRequest -uri "https://chocolatey.org/api/v2/package-versions/docfx" | ConvertFrom-JSON
$release_json = $release_json | % { [System.Version]::Parse($_) } | Sort-Object -Descending
}
catch
{
Return 1
}
# Set TLS version to 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Download the release
# Since GH releases are unreliable, we have to try up to 3 times
$tries = 0
$fail = $true
while ($tries -lt 3)
{
# Prepare the assets
$release = $release_json[$tries] # Pick the next available release
$release_version = $release.ToString() # Convert to string
$release_asset = "https://github.com/dotnet/docfx/releases/download/v$release_version/docfx.zip"
# increment try counter
$tries = $tries + 1
try
{
Write-Host "Downloading DocFX $release_version to $target_path"
Invoke-WebRequest -uri "$release_asset" -outfile "$target_path"
# No failure, carry on
Write-Host "DocFX version $release_version downloaded successfully"
$fail = $false
Break
}
catch
{
Write-Host "Downloading DocFX version $release_version failed, trying next ($tries / 3)"
#Return 1
}
}
# Check if we succedded in downloading
if ($fail)
{
Return 1
}
# Switch directory
Set-Location -Path "$target_dir"
# Extract the release
try
{
Write-Host "Extracting DocFX"
Expand-Archive -path "$target_path" -destinationpath "$target_dir"
}
catch
{
Return 1
}
# Remove the downloaded zip
Write-Host "Removing temporary files"
Remove-Item "$target_path"
# Add DocFX to PATH
Write-Host "Adding DocFX to PATH"
if ($Env:OS -eq $null)
{
$Env:DOCFX_PATH = "$target_dir"
}
else
{
$Env:PATH = "$target_dir;$current_path"
}
Set-Location -path "$current_location"
Return 0
}
# Downloads and installs latest version of 7-zip CLI
function Install-7zip([string] $target_dir_path)
{
# First, download 7-zip 9.20 CLI to extract latest CLI
# http://www.7-zip.org/a/7za920.zip
Write-Host "Installing 7-zip"
# Check if the target directory exists
# If it does, remove it
if (Test-Path "$target_dir_path")
{
Write-Host "Target directory exists, deleting"
Remove-Item -recurse -force "$target_dir_path"
}
# Create target directory
$target_dir = New-Item -type directory "$target_dir_path"
$target_fn = "7za920.zip"
# Form target path
$target_dir = $target_dir.FullName
$target_path = Join-Path "$target_dir" "v920"
$target_dir_920 = New-Item -type directory "$target_path"
$target_dir_920 = $target_dir_920.FullName
$target_path = Join-Path "$target_dir_920" "$target_fn"
# Download the 9.20 CLI
try
{
Write-Host "Downloading 7-zip 9.20 CLI to $target_path"
Invoke-WebRequest -uri "http://www.7-zip.org/a/7za920.zip" -outfile "$target_path"
Set-Location -Path "$target_dir_920"
}
catch
{
Return 1
}
# Extract the 9.20 CLI
try
{
Write-Host "Extracting 7-zip latest CLI"
Expand-Archive -path "$target_path" -destinationpath "$target_dir_920"
}
catch
{
Return 1
}
# Temporarily add the 9.20 CLI to PATH
Write-Host "Adding 7-zip 9.20 CLI to PATH"
$old_path = $Env:PATH
$Env:PATH = "$target_dir_920;$old_path"
# Next, download latest CLI
# http://www.7-zip.org/a/7z1604-extra.7z
# Form target path
$target_version = "19.00"
$target_fn = "7z1900-extra.7z"
$target_path = Join-Path "$target_dir" "$target_fn"
# Download the latest CLI
try
{
Write-Host "Downloading 7-zip $target_version CLI to $target_path"
Invoke-WebRequest -uri "http://www.7-zip.org/a/$target_fn" -outfile "$target_path"
Set-Location -Path "$target_dir"
}
catch
{
Return 1
}
# Extract the latest CLI
Write-Host "Extracting 7-zip $target_version CLI"
& 7za x "$target_path" | Out-Host
if ($LastExitCode -ne 0)
{
Return $LastExitCode
}
# Remove the 9.20 CLI from PATH
Write-Host "Removing 7-zip 9.20 CLI from PATH"
$Env:PATH = "$old_path"
# Remove temporary files and 9.20 CLI
Write-Host "Removing temporary files"
Remove-Item -recurse -force "$target_dir_920"
Remove-Item -recurse -force "$target_path"
# Add the latest CLI to PATH
Write-Host "Adding 7-zip $target_version CLI to PATH"
$target_dir = Join-Path "$target_dir" "x64"
$Env:PATH = "$target_dir;$old_path"
Set-Location -path "$current_location"
Return 0
}
# Builds the documentation using available DocFX
function Build-Docs([string] $target_dir_path)
{
# Check if documentation source path exists
if (-not (Test-Path "$target_dir_path"))
{
#Write-Host "Specified path does not exist"
Return 65536
}
# Check if documentation source path is a directory
$target_path = Get-Item "$target_dir_path"
if (-not ($target_path -is [System.IO.DirectoryInfo]))
{
#Write-Host "Specified path is not a directory"
Return 65536
}
# Form target path
$target_path = $target_path.FullName
# Form component paths
$docs_site = Join-Path "$target_path" "_site"
$docs_api = Join-Path "$target_path" "api"
$docs_obj = Join-Path "$target_path" "obj"
# Check if API documentation source path exists
if (-not (Test-Path "$docs_api"))
{
#Write-Host "API build target directory does not exist"
Return 32768
}
# Check if API documentation source path is a directory
$docs_api_dir = Get-Item "$docs_api"
if (-not ($docs_api_dir -is [System.IO.DirectoryInfo]))
{
#Write-Host "API build target directory is not a directory"
Return 32768
}
# Purge old API documentation
Write-Host "Purging old API documentation"
Set-Location -path "$docs_api"
Remove-Item "*.yml"
Set-Location -path "$current_location"
# Check if old built site exists
# If it does, remove it
if (Test-Path "$docs_site")
{
Write-Host "Purging old products"
Remove-Item -recurse -force "$docs_site"
}
# Create target directory for the built site
$docs_site = New-Item -type directory "$docs_site"
$docs_site = $docs_site.FullName
# Check if old object cache exists
# If it does, remove it
if (Test-Path "$docs_obj")
{
Write-Host "Purging object cache"
Remove-Item -recurse -force "$docs_obj"
}
# Create target directory for the object cache
$docs_obj = New-Item -type directory "$docs_obj"
$docs_obj = $docs_obj.FullName
# Enter the documentation directory
Set-Location -path "$target_path"
# Check OS
# Null means non-Windows
if ($Env:OS -eq $null)
{
# Generate new API documentation
& mono "$Env:DOCFX_PATH/docfx.exe" docfx.json | Out-Host
# Check if successful
if ($LastExitCode -eq 0)
{
# Build new documentation site
& mono "$Env:DOCFX_PATH/docfx.exe" build docfx.json | Out-Host
}
}
else
{
# Generate new API documentation
& docfx docfx.json | Out-Host
# Check if successful
if ($LastExitCode -eq 0)
{
# Build new documentation site
& docfx build docfx.json | Out-Host
}
}
# Exit back
Set-Location -path "$current_location"
# Check if building was a success
if ($LastExitCode -eq 0)
{
Return 0
}
else
{
Return $LastExitCode
}
}
# Packages the build site to a .tar.xz archive
function Package-Docs([string] $target_dir_path, [string] $output_dir_path, [string] $pack_name)
{
# Form target path
$target_path = Get-Item "$target_dir_path"
$target_path = $target_path.FullName
$target_path = Join-Path "$target_path" "_site"
# Form output path
$output_path_dir = Get-Item "$output_dir_path"
$output_path_dir = $output_path_dir.FullName
$output_path = Join-Path "$output_path_dir" "$pack_name"
# Enter target path
Set-Location -path "$target_path"
# Check if target .tar exists
# If it does, remove it
if (Test-Path "$output_path.tar")
{
Write-Host "$output_path.tar exists, deleting"
Remove-Item "$output_path.tar"
}
# Package .tar archive
Write-Host "Packaging docs to $output_path.tar"
& 7za -r a "$output_path.tar" * | Out-Host
# Check if prepackaging was a success
if ($LastExitCode -ne 0)
{
Return $LastExitCode
}
# Go to package's location
Set-Location -path "$output_path_dir"
# Check if target .tar.xz exists
# If it does, remove it
if (Test-Path "$output_path.tar.xz")
{
Write-Host "$output_path.tar.xz exists, deleting"
Remove-Item "$output_path.tar.xz"
}
# Package .tar.xz
Write-Host "Packaging docs to $output_path.tar.xz"
& 7za -sdel -mx9 a "$pack_name.tar.xz" "$pack_name.tar" | Out-Host
# Exit back
Set-Location -path "$current_location"
# Check if packaging was a success
if ($LastExitCode -eq 0)
{
Return 0
}
else
{
Return $LastExitCode
}
}
# Install DocFX
$result = Install-DocFX "$docfx_path"
if ($result -ne 0)
{
Write-Host "Installing DocFX failed"
Restore-Environment
$host.SetShouldExit(1)
Exit 1
}
# Install 7-zip, if Windows
if ($Env:OS -ne $null)
{
$result = Install-7zip "$sevenzip_path"
if ($result -ne 0)
{
Write-Host "Installing 7-zip failed"
Restore-Environment
$host.SetShouldExit(1)
Exit 1
}
}
# Build and package docs
# At this point nothing should fail as everything is already set up
$result = Build-Docs "$DocsPath"
if ($result -eq 0)
{
$result = Package-Docs "$DocsPath" "$OutputPath" "$PackageName"
if ($result -ne 0)
{
Write-Host "Packaging API documentation failed"
}
}
else
{
Write-Host "Building API documentation failed"
}
# Restore the environment
Restore-Environment
# All was well, exit with success
if ($result -eq 0)
{
Write-Host "All operations completed"
Exit 0
}
else
{
$host.SetShouldExit($result)
Exit $result
}