Skip to content

Generate examples in documentation #848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Gets the version suffix from the repo tag
# example: v1.0.0-preview1-final => preview1-final
function Get-Version-Suffix-From-Tag
{
function Get-Version-Suffix-From-Tag {
$tag=$env:APPVEYOR_REPO_TAG_NAME
$split=$tag -split "-"
$suffix=$split[1..2]
Expand Down Expand Up @@ -32,25 +31,25 @@ CheckLastExitCode

Write-Output "APPVEYOR_REPO_TAG: $env:APPVEYOR_REPO_TAG"

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

IF ([string]::IsNullOrWhitespace($revision)){
if ([string]::IsNullOrWhitespace($revision)) {
Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts"
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts
CheckLastExitCode
}
Else {
else {
Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$revision"
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$revision
CheckLastExitCode
}
}
Else {
else {
$packageVersionSuffix="beta1-$revision"
Write-Output "VERSION-SUFFIX: $packageVersionSuffix"
Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$packageVersionSuffix"
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$packageVersionSuffix
CheckLastExitCode
}
}
20 changes: 10 additions & 10 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ environment:

# REF: https://github.com/docascode/docfx-seed/blob/master/appveyor.yml
before_build:
- ps: |
if(-Not $env:APPVEYOR_PULL_REQUEST_TITLE)
{
- pwsh: |
if (-Not $env:APPVEYOR_PULL_REQUEST_TITLE) {
# https://dotnet.github.io/docfx/tutorial/docfx_getting_started.html
git checkout $env:APPVEYOR_REPO_BRANCH -q
choco install docfx -y
}

after_build:
- ps: |
CD ./docs
if(-Not $env:APPVEYOR_PULL_REQUEST_TITLE)
{
- pwsh: |
if (-Not $env:APPVEYOR_PULL_REQUEST_TITLE) {
CD ./docs
& ./generate-examples.ps1
& docfx docfx.json
if ($lastexitcode -ne 0){
if ($lastexitcode -ne 0) {
throw [System.Exception] "docfx build failed with exit code $lastexitcode."
}

Expand Down Expand Up @@ -61,8 +61,8 @@ services:
- postgresql96

build_script:
- ps: dotnet --version
- ps: .\Build.ps1
- pwsh: dotnet --version
- pwsh: .\Build.ps1

test: off

Expand Down
2 changes: 1 addition & 1 deletion docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ This section documents the package API and is generated from the XML source comm

- [`JsonApiOptions`](JsonApiDotNetCore.Configuration.JsonApiOptions.yml)
- [`IResourceGraph`](JsonApiDotNetCore.Configuration.IResourceGraph.yml)
- [`ResourceDefinition<TResource>`](JsonApiDotNetCore.Resources.ResourceDefinition-1.yml)
- [`JsonApiResourceDefinition<TResource>`](JsonApiDotNetCore.Resources.JsonApiResourceDefinition-1.yml)
37 changes: 37 additions & 0 deletions docs/generate-examples.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#Requires -Version 7.0

# This script generates response documents for ./request-examples

function Kill-WebServer {
$tcpConnections = Get-NetTCPConnection -LocalPort 14141 -ErrorAction SilentlyContinue
if ($tcpConnections -ne $null) {
Write-Output "Stopping web server"
Get-Process -Id $tcpConnections.OwningProcess | Stop-Process
}
}

function Start-Webserver {
Write-Output "Starting web server"
Start-Job -ScriptBlock { dotnet run --project ..\src\Examples\GettingStarted\GettingStarted.csproj } | Out-Null
}

Kill-WebServer
Start-Webserver

Remove-Item -Force -Path .\request-examples\*.json

Start-Sleep -Seconds 10

$scriptFiles = Get-ChildItem .\request-examples\*.ps1
foreach ($scriptFile in $scriptFiles) {
$jsonFileName = [System.IO.Path]::GetFileNameWithoutExtension($scriptFile.Name) + "_Response.json"

Write-Output "Writing file: $jsonFileName"
& $scriptFile.FullName > .\request-examples\$jsonFileName

if ($LastExitCode -ne 0) {
throw [System.Exception] "Example request from '$($scriptFile.Name)' failed."
}
}

Kill-WebServer
43 changes: 0 additions & 43 deletions docs/generate.sh

This file was deleted.

10 changes: 0 additions & 10 deletions docs/request-examples/000-CREATE_Person.sh

This file was deleted.

18 changes: 0 additions & 18 deletions docs/request-examples/001-CREATE_Article.sh

This file was deleted.

1 change: 1 addition & 0 deletions docs/request-examples/001_GET_Books.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -s -f http://localhost:14141/api/books
1 change: 0 additions & 1 deletion docs/request-examples/002-GET_Articles.sh

This file was deleted.

1 change: 1 addition & 0 deletions docs/request-examples/002_GET_Person-by-ID.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -s -f http://localhost:14141/api/people/1
1 change: 0 additions & 1 deletion docs/request-examples/003-GET_Article.sh

This file was deleted.

1 change: 1 addition & 0 deletions docs/request-examples/003_GET_Books-including-Author.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -s -f http://localhost:14141/api/books?include=author
1 change: 0 additions & 1 deletion docs/request-examples/004-GET_Articles_With_Authors.sh

This file was deleted.

1 change: 1 addition & 0 deletions docs/request-examples/004_GET_Books-PublishYear.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -s -f http://localhost:14141/api/books?fields=publishYear
11 changes: 0 additions & 11 deletions docs/request-examples/005-PATCH_Article.sh

This file was deleted.

1 change: 1 addition & 0 deletions docs/request-examples/005_GET_People-Filter_Partial.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -s -f "http://localhost:14141/api/people?filter=contains(name,'Shell')"
2 changes: 0 additions & 2 deletions docs/request-examples/006-DELETE_Article.sh

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -s -f http://localhost:14141/api/books?sort=-publishYear
29 changes: 0 additions & 29 deletions docs/request-examples/007-__SEED__.sh

This file was deleted.

1 change: 1 addition & 0 deletions docs/request-examples/007_GET_Books-paginated.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -s -f "http://localhost:14141/api/books?page%5Bsize%5D=1&page%5Bnumber%5D=2"
1 change: 0 additions & 1 deletion docs/request-examples/008-GET_Articles_With_Filter_Eq.sh

This file was deleted.

1 change: 0 additions & 1 deletion docs/request-examples/009-GET_Articles_With_Filter_Like.sh

This file was deleted.

10 changes: 10 additions & 0 deletions docs/request-examples/010_CREATE_Person.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
curl -s -f http://localhost:14141/api/people `
-H "Content-Type: application/vnd.api+json" `
-d '{
\"data\": {
\"type\": \"people\",
\"attributes\": {
\"name\": \"Alice\"
}
}
}'
19 changes: 19 additions & 0 deletions docs/request-examples/011_CREATE_Book-with-Author.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
curl -s -f http://localhost:14141/api/books `
-H "Content-Type: application/vnd.api+json" `
-d '{
\"data\": {
\"type\": \"books\",
\"attributes\": {
\"title\": \"Valperga\",
\"publishYear\": 1823
},
\"relationships\": {
\"author\": {
\"data\": {
\"type\": \"people\",
\"id\": \"1\"
}
}
}
}
}'
12 changes: 12 additions & 0 deletions docs/request-examples/012_PATCH_Book.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
curl -s -f http://localhost:14141/api/books/1 `
-H "Content-Type: application/vnd.api+json" `
-X PATCH `
-d '{
\"data\": {
\"type\": \"books\",
\"id\": "1",
\"attributes\": {
\"publishYear\": 1820
}
}
}'
2 changes: 2 additions & 0 deletions docs/request-examples/013_DELETE_Book.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
curl -s -f http://localhost:14141/api/books/1 `
-X DELETE
12 changes: 6 additions & 6 deletions docs/request-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

To update these requests:

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:
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:
```
curl -vs http://localhost:5001/api/articles
curl -s http://localhost:14141/api/books
```

2. Add the example to `index.md`. Example:
```
## Get Article with Author
### Get with relationship

[!code-sh[GET Request](004-GET_Articles_With_Authors.sh)]
[!code-json[GET Response](004-GET_Articles_With_Authors-Response.json)]
[!code-ps[REQUEST](003_GET_Books-including-Author.ps1)]
[!code-json[RESPONSE](003_GET_Books-including-Author_Response.json)]
```

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