-
Notifications
You must be signed in to change notification settings - Fork 206
feat: New Mix Task for Generating Static Markdown Swagger Page for ExDoc #679
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
Open
bradschwartz
wants to merge
2
commits into
open-api-spex:master
Choose a base branch
from
bradschwartz:feat/task/generate-static-page
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
defmodule Mix.Tasks.Openapi.StaticDocs do | ||
@shortdoc "Generate a static file that renders the OpenAPI spec using the Swagger UI" | ||
@moduledoc """ | ||
Generate a static file that renders the OpenAPI spec using the Swagger UI. Uses | ||
assets from https://unpkg.com/swagger-ui-dist. | ||
|
||
## Examples | ||
|
||
$ mix openapi.static_docs --spec PhoenixAppWeb.ApiSpec --output-file swagger.md | ||
$ mix openapi.static_docs --spec PhoenixAppWeb.ApiSpec --output-file swagger.md --swagger-version 5.27.1 | ||
|
||
## Usage with ExDoc | ||
|
||
To use with ExDoc, you can add the following to your `mix.exs` file: | ||
|
||
```elixir | ||
def docs do | ||
[ | ||
extras: ["guides/swagger.md"] | ||
] | ||
end | ||
|
||
def aliases do | ||
[ | ||
"docs": [ | ||
"cmd mkdir -p guides/", | ||
"openapi.static_docs --spec PhoenixAppWeb.ApiSpec --output-file guides/swagger.md --swagger-version 4.4.0", | ||
"docs" | ||
] | ||
] | ||
end | ||
``` | ||
|
||
## Command line options | ||
|
||
Accepts all the same options as `Mix.Tasks.Openapi.Spec.Json` plus: | ||
|
||
* `--output-file` - The file to write the static file to (defaults to `swagger.md`) | ||
|
||
* `--swagger-version` - The version of the Swagger UI to use (defaults to `5.27.1`) | ||
""" | ||
use Mix.Task | ||
|
||
require Mix.Generator | ||
|
||
alias Mix.Tasks.Openapi.Spec.Json | ||
|
||
@recursive true | ||
@default_static_filename "swagger.md" | ||
@default_swagger_version "5.27.1" | ||
|
||
@static_template """ | ||
# <%= spec_name %> | ||
|
||
<head> | ||
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@<%= swagger_version %>/swagger-ui.css" /> | ||
</head> | ||
|
||
<body> | ||
<div id="swagger-ui"></div> | ||
<script src="https://unpkg.com/swagger-ui-dist@<%= swagger_version %>/swagger-ui-bundle.js" crossorigin></script> | ||
<script src="https://unpkg.com/swagger-ui-dist@<%= swagger_version %>/swagger-ui-standalone-preset.js" crossorigin></script> | ||
<script> | ||
window.onload = () => { | ||
window.ui = SwaggerUIBundle({ | ||
spec: <%= json_spec %>, | ||
dom_id: '#swagger-ui', | ||
}); | ||
}; | ||
</script> | ||
</body> | ||
""" | ||
|
||
@flags [ | ||
output_file: :string, | ||
swagger_version: :string, | ||
spec: :string | ||
] | ||
|
||
@impl true | ||
def run(argv) do | ||
{opts, _, _} = OptionParser.parse(argv, switches: @flags) | ||
|
||
json_spec_filename = Json.run(argv) | ||
|
||
output_file = Keyword.get(opts, :output_file, @default_static_filename) | ||
swagger_version = Keyword.get(opts, :swagger_version, @default_swagger_version) | ||
|
||
json_spec = File.read!(json_spec_filename) | ||
|
||
rendered_template = | ||
EEx.eval_string(@static_template, | ||
spec_name: opts[:spec], | ||
json_spec: json_spec, | ||
swagger_version: swagger_version | ||
) | ||
|
||
Mix.Generator.create_file(output_file, rendered_template, force: true, quiet: true) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i found it easier to update here on successful writes to return the new filename, instead of having the new task have to explicitly be aware of the
:filename
switch for the json task. Happy to go with that if preferable tho