Skip to content

Commit 10dd16a

Browse files
author
Bart Koelman
authored
Merge pull request #848 from bart-degreed/gen-examples
Generate examples in documentation
2 parents bb1ad7a + 8178856 commit 10dd16a

40 files changed

+193
-208
lines changed

Build.ps1

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Gets the version suffix from the repo tag
22
# example: v1.0.0-preview1-final => preview1-final
3-
function Get-Version-Suffix-From-Tag
4-
{
3+
function Get-Version-Suffix-From-Tag {
54
$tag=$env:APPVEYOR_REPO_TAG_NAME
65
$split=$tag -split "-"
76
$suffix=$split[1..2]
@@ -32,25 +31,25 @@ CheckLastExitCode
3231

3332
Write-Output "APPVEYOR_REPO_TAG: $env:APPVEYOR_REPO_TAG"
3433

35-
If($env:APPVEYOR_REPO_TAG -eq $true) {
34+
if ($env:APPVEYOR_REPO_TAG -eq $true) {
3635
$revision = Get-Version-Suffix-From-Tag
3736
Write-Output "VERSION-SUFFIX: $revision"
3837

39-
IF ([string]::IsNullOrWhitespace($revision)){
38+
if ([string]::IsNullOrWhitespace($revision)) {
4039
Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts"
4140
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts
4241
CheckLastExitCode
4342
}
44-
Else {
43+
else {
4544
Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$revision"
4645
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$revision
4746
CheckLastExitCode
4847
}
4948
}
50-
Else {
49+
else {
5150
$packageVersionSuffix="beta1-$revision"
5251
Write-Output "VERSION-SUFFIX: $packageVersionSuffix"
5352
Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$packageVersionSuffix"
5453
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$packageVersionSuffix
5554
CheckLastExitCode
56-
}
55+
}

appveyor.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ environment:
99

1010
# REF: https://github.com/docascode/docfx-seed/blob/master/appveyor.yml
1111
before_build:
12-
- ps: |
13-
if(-Not $env:APPVEYOR_PULL_REQUEST_TITLE)
14-
{
12+
- pwsh: |
13+
if (-Not $env:APPVEYOR_PULL_REQUEST_TITLE) {
14+
# https://dotnet.github.io/docfx/tutorial/docfx_getting_started.html
1515
git checkout $env:APPVEYOR_REPO_BRANCH -q
1616
choco install docfx -y
1717
}
1818
1919
after_build:
20-
- ps: |
21-
CD ./docs
22-
if(-Not $env:APPVEYOR_PULL_REQUEST_TITLE)
23-
{
20+
- pwsh: |
21+
if (-Not $env:APPVEYOR_PULL_REQUEST_TITLE) {
22+
CD ./docs
23+
& ./generate-examples.ps1
2424
& docfx docfx.json
25-
if ($lastexitcode -ne 0){
25+
if ($lastexitcode -ne 0) {
2626
throw [System.Exception] "docfx build failed with exit code $lastexitcode."
2727
}
2828
@@ -61,8 +61,8 @@ services:
6161
- postgresql96
6262

6363
build_script:
64-
- ps: dotnet --version
65-
- ps: .\Build.ps1
64+
- pwsh: dotnet --version
65+
- pwsh: .\Build.ps1
6666

6767
test: off
6868

docs/api/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ This section documents the package API and is generated from the XML source comm
66

77
- [`JsonApiOptions`](JsonApiDotNetCore.Configuration.JsonApiOptions.yml)
88
- [`IResourceGraph`](JsonApiDotNetCore.Configuration.IResourceGraph.yml)
9-
- [`ResourceDefinition<TResource>`](JsonApiDotNetCore.Resources.ResourceDefinition-1.yml)
9+
- [`JsonApiResourceDefinition<TResource>`](JsonApiDotNetCore.Resources.JsonApiResourceDefinition-1.yml)

docs/generate-examples.ps1

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#Requires -Version 7.0
2+
3+
# This script generates response documents for ./request-examples
4+
5+
function Kill-WebServer {
6+
$tcpConnections = Get-NetTCPConnection -LocalPort 14141 -ErrorAction SilentlyContinue
7+
if ($tcpConnections -ne $null) {
8+
Write-Output "Stopping web server"
9+
Get-Process -Id $tcpConnections.OwningProcess | Stop-Process
10+
}
11+
}
12+
13+
function Start-Webserver {
14+
Write-Output "Starting web server"
15+
Start-Job -ScriptBlock { dotnet run --project ..\src\Examples\GettingStarted\GettingStarted.csproj } | Out-Null
16+
}
17+
18+
Kill-WebServer
19+
Start-Webserver
20+
21+
Remove-Item -Force -Path .\request-examples\*.json
22+
23+
Start-Sleep -Seconds 10
24+
25+
$scriptFiles = Get-ChildItem .\request-examples\*.ps1
26+
foreach ($scriptFile in $scriptFiles) {
27+
$jsonFileName = [System.IO.Path]::GetFileNameWithoutExtension($scriptFile.Name) + "_Response.json"
28+
29+
Write-Output "Writing file: $jsonFileName"
30+
& $scriptFile.FullName > .\request-examples\$jsonFileName
31+
32+
if ($LastExitCode -ne 0) {
33+
throw [System.Exception] "Example request from '$($scriptFile.Name)' failed."
34+
}
35+
}
36+
37+
Kill-WebServer

docs/generate.sh

Lines changed: 0 additions & 43 deletions
This file was deleted.

docs/request-examples/000-CREATE_Person.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

docs/request-examples/001-CREATE_Article.sh

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -s -f http://localhost:14141/api/books

docs/request-examples/002-GET_Articles.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -s -f http://localhost:14141/api/people/1

docs/request-examples/003-GET_Article.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -s -f http://localhost:14141/api/books?include=author

docs/request-examples/004-GET_Articles_With_Authors.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -s -f http://localhost:14141/api/books?fields=publishYear

docs/request-examples/005-PATCH_Article.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -s -f "http://localhost:14141/api/people?filter=contains(name,'Shell')"

docs/request-examples/006-DELETE_Article.sh

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -s -f http://localhost:14141/api/books?sort=-publishYear

docs/request-examples/007-__SEED__.sh

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl -s -f "http://localhost:14141/api/books?page%5Bsize%5D=1&page%5Bnumber%5D=2"

docs/request-examples/008-GET_Articles_With_Filter_Eq.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/request-examples/009-GET_Articles_With_Filter_Like.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
curl -s -f http://localhost:14141/api/people `
2+
-H "Content-Type: application/vnd.api+json" `
3+
-d '{
4+
\"data\": {
5+
\"type\": \"people\",
6+
\"attributes\": {
7+
\"name\": \"Alice\"
8+
}
9+
}
10+
}'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
curl -s -f http://localhost:14141/api/books `
2+
-H "Content-Type: application/vnd.api+json" `
3+
-d '{
4+
\"data\": {
5+
\"type\": \"books\",
6+
\"attributes\": {
7+
\"title\": \"Valperga\",
8+
\"publishYear\": 1823
9+
},
10+
\"relationships\": {
11+
\"author\": {
12+
\"data\": {
13+
\"type\": \"people\",
14+
\"id\": \"1\"
15+
}
16+
}
17+
}
18+
}
19+
}'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
curl -s -f http://localhost:14141/api/books/1 `
2+
-H "Content-Type: application/vnd.api+json" `
3+
-X PATCH `
4+
-d '{
5+
\"data\": {
6+
\"type\": \"books\",
7+
\"id\": "1",
8+
\"attributes\": {
9+
\"publishYear\": 1820
10+
}
11+
}
12+
}'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
curl -s -f http://localhost:14141/api/books/1 `
2+
-X DELETE

docs/request-examples/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
To update these requests:
44

5-
1. Add a bash (.sh) file prefixed by a number that is used to determine the order the scripts are executed. The bash script should execute a request and output the response. Example:
5+
1. Add a PowerShell (.ps1) script prefixed by a number that is used to determine the order the scripts are executed. The script should execute a request and output the response. Example:
66
```
7-
curl -vs http://localhost:5001/api/articles
7+
curl -s http://localhost:14141/api/books
88
```
99

1010
2. Add the example to `index.md`. Example:
1111
```
12-
## Get Article with Author
12+
### Get with relationship
1313
14-
[!code-sh[GET Request](004-GET_Articles_With_Authors.sh)]
15-
[!code-json[GET Response](004-GET_Articles_With_Authors-Response.json)]
14+
[!code-ps[REQUEST](003_GET_Books-including-Author.ps1)]
15+
[!code-json[RESPONSE](003_GET_Books-including-Author_Response.json)]
1616
```
1717

18-
3. Run `./generate.sh`
18+
3. Run `./generate-examples.ps1`
1919
4. Verify the results by running `docfx --serve`

0 commit comments

Comments
 (0)