Skip to content
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

Created plugin boilerplate and new-plugin script #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pids
*.pid
*.seed
*.pid.lock
*.sw[op]

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ I will make releases of each plugin when/if your PR is accepted.

Please add yourself to the Contributors list below if you add a new plugin!

You can use the `npm run new-plugin` command to easily generate a new plugin skeleton from boilerplate. This should give you at least a reasonable head start on your plugin in a way that is generally compatible with the conventions in this library.

**One easy way to contribute is to copy over plugins found at https://github.com/swagger-api/swagger-ui/issues/5027#issuecomment-438745785.**

## Versioning

Each plugin uses [SemVer](http://semver.org/) for versioning.
Expand All @@ -23,6 +27,7 @@ A plugin's version is unrelated to its Swagger UI compatibility: compatible vers
## Contributors

* **Kyle Shockey**: *Initial work; plugin maintenance*
* **Kael Shipman**: *Plugin boilerplate and script; limited ports of other people's plugins from around the web.*

## Built With

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"@babel/preset-env": "^7.1.0",
"babel-plugin-add-module-exports": "^1.0.0",
"lerna": "^3.4.3"
},
"scripts": {
"new-plugin": "./scripts/new-plugin.sh"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

running this locally shows a couple of weird behaviors (shown is two runs, with names Kyle Plugin and Test Plugin):

  1. my plugin's name seems to be mangled at the first character
  2. a runaway file copy seems to be creating copies with trailing singlequotes

image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, looks like this was a bad sed line. Frustratingly, mac sed requires you to supply a backup prefix. I tried to get around that by just supplying an empty string, which I think would normally work, but I did it in a weird way.

Anyway, I believe it's now fixed. Grab a look and see what you think.

}
}
12 changes: 12 additions & 0 deletions packages/.boilerplate/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"ie": "11"
}
}
]
]
}
3 changes: 3 additions & 0 deletions packages/.boilerplate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
build
*.sw[op]
54 changes: 54 additions & 0 deletions packages/.boilerplate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Swagger UI '{proper-name}' Plugin
==================================================================



## Usage

### Require

First, install the module with npm:
```
$ npm install --save swagger-ui-plugin-{name-kebab-case}
```

Next, require it in your client-side application:

```js
const {name-pascal-case}Plugin = require('swagger-ui-plugin-{name-kebab-case}');

SwaggerUI({
// your options here...
plugins: [
{name-pascal-case}Plugin
]
})
```

### `<script>`

```html
<!-- Load Swagger UI and related -->
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css"></link>
<script src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js"> </script>

<!-- Load the {name-pascal-case} Plugin -->
<script src="https://unpkg.com/swagger-ui-plugin-{name-kebab-case}"> </script>

<script>
window.onload = function() {
SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: "#swagger-ui",

// your options here...

plugins: [
{name-pascal-case}Plugin
],
});
}
</script>
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const swaggerUiPlugin{name-pascal-case} = require('..');

describe('swagger-ui-plugin-{name-kebab-case}', () => {
it('needs tests');
});
36 changes: 36 additions & 0 deletions packages/.boilerplate/example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!doctype html>
<html>
<head>
<!-- Load React (if necessary) -->
<!-- <script src="https://unpkg.com/react@17/umd/react.production.min.js"></script>-->

<!-- Load Swagger UI -->
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css"></link>
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js"></script>

<!-- Load your plugin -->
<script src="./plugin.js"></script>

<!-- Set it up on-load -->
<script>
window.onload = function() {
window.ui = SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: "#swagger-ui",
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
],
plugins: [
{name-pascal-case}Plugin
]
});
}
</script>
</head>

<body>
<div id="swagger-ui"></div>
</body>
</html>

37 changes: 37 additions & 0 deletions packages/.boilerplate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "swagger-ui-plugin-{name-kebab-case}",
"version": "1.0.0",
"description": "{short-desc}",
"author": "{author}",
"homepage": "https://github.com/shockey/swagger-ui-plugins#readme",
"license": "MIT",
"main": "build/index.js",
"directories": {
"build": "build",
"test": "__tests__"
},
"files": [
"build"
],
"repository": {
"type": "git",
"url": "git+https://github.com/shockey/swagger-ui-plugins.git"
},
"scripts": {
"start": "npx http-server example/",
"build": "npx babel src -d build && cp build/index.js example/plugin.js",
"prepublishOnly": "npm run build",
"test": "echo \"Error: run tests from root\" && exit 1"
},
"bugs": {
"url": "https://github.com/shockey/swagger-ui-plugins/issues"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11"
},
"dependencies": {
"@babel/preset-react": "^7.12.13"
}
}
56 changes: 56 additions & 0 deletions packages/.boilerplate/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// From: https://raw.githubusercontent.com/chilts/umd-template/master/template.js
; ((f) => {
// module name and requires
var name = '{name-pascal-case}Plugin';
var requires = [];

// CommonJS
if (typeof exports === "object" && typeof module !== "undefined") {
module.exports = f.apply(null, requires.map(function (r) { return require(r); }));

// RequireJS
} else if (typeof define === "function" && define.amd) {
define(requires, f);

// <script>
} else {
var g;
if (typeof window !== "undefined") {
g = window;
} else if (typeof global !== "undefined") {
g = global;
} else if (typeof self !== "undefined") {
g = self;
} else {
// works providing we're not in "use strict";
// needed for Java 8 Nashorn
// see https://github.com/facebook/react/issues/3037
g = this;
}
g[name] = f.apply(null, requires.map(function (r) { return g[r]; }));
}

})(() => {
// Module source
return (system) => {
return {



/**
*
*
*
*
* Your plugin here
*
*
*
*
*/



}
};
});
143 changes: 143 additions & 0 deletions scripts/new-plugin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/bin/bash

set -e

SUCCESS=0

# Get bold/plain variables using tput, if available
if command -v tput &>/dev/null; then
bold="$(tput bold)"
reset="$(tput sgr0)"
fi

function bold() {
echo "${bold}$@${reset}"
}

function revert() {
if [ "$SUCCESS" -eq 0 ]; then
if [ -n "$PLUGIN_KEBAB_CASE" ]; then
if [ -e "packages/$PLUGIN_KEBAB_CASE" ]; then
rm -Rf "packages/$PLUGIN_KEBAB_CASE"
fi
fi
fi
}

function gather_info() {
echo "$(bold "Plugin Name")"
echo "What should the name of your plugin be? Some examples are 'Hide Empty Tags' and 'Disable Try-It-Out Without Servers'."
echo
read -p "Name: " PLUGIN_NAME
echo
echo "$(bold "Plugin Author")"
echo "Who should be listed as the plugin author. This is usually something like 'Your Name<your.name@example.com>'."
echo
read -p "Author: " PLUGIN_AUTHOR
echo
echo "$(bold "Plugin Description")"
echo "Come up with a short description for package.json so others know what this plugin is for."
echo
read -p "Description: " PLUGIN_SHORT_DESC
echo

# Generate derivative name values
PLUGIN_PROPER_NAME="$([ -n "$PLUGIN_PROPER_NAME" ] && echo "$PLUGIN_PROPER_NAME" || echo "$PLUGIN_NAME" | sed 's/ +/ /g' | sed 's/.*/\L&/g' | sed 's/[a-z]*/\u&/g' | sed "s/'[ST]/\\L&/g")"

echo "$(bold "Good. Now we're going to generate a few name derivatives for you.") Make sure they look ok, or change them if they don't."
echo
echo "$(bold "Proper Name")"
echo "This will be your plugin's name in the Readme"
echo
read -p "Proper name: [${PLUGIN_PROPER_NAME}] " ANS
if [ -n "$ANS" ]; then
PLUGIN_PROPER_NAME="$ANS"
fi
PLUGIN_KEBAB_CASE="$([ -n "$PLUGIN_KEBAB_CASE" ] && echo "$PLUGIN_KEBAB_CASE" || echo "$PLUGIN_PROPER_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9 ]//g' | sed -r 's/ +/-/g')"
echo
echo "$(bold "Kebab-Case Name")"
echo "There are a variety of places where your plugin will be referenced by a 'kebab-case' variant. Enter that here or accept the default."
echo
read -p "Kebab-case name: [${PLUGIN_KEBAB_CASE}] " ANS
if [ -n "$ANS" ]; then
PLUGIN_KEBEB_CASE="$ANS"
fi
echo
PLUGIN_PASCAL_CASE="$([ -n "$PLUGIN_PASCAL_CASE" ] && echo "$PLUGIN_PASCAL_CASE" || echo "$PLUGIN_PROPER_NAME" | sed 's/[^a-zA-Z0-9 ]//g' | sed -r 's/ +//g')"
echo "$(bold "PascalCase Name")"
echo "There are a variety of places where your plugin will be referenced by a 'PascalCase' variant. Enter that here or accept the default."
echo
read -p "PascalCase name: [${PLUGIN_PASCAL_CASE}] " ANS
if [ -n "$ANS" ]; then
PLUGIN_PASCAL_CASE="$ANS"
fi
echo
}

function gather_loop() {
while true; do
# Gather
gather_info

# Then validate
local MISSING=
if [ -z "$PLUGIN_NAME" ]; then MISSING="$MISSING,name"; fi
if [ -z "$PLUGIN_AUTHOR" ]; then MISSING="$MISSING,author"; fi
if [ -z "$PLUGIN_SHORT_DESC" ]; then MISSING="$MISSING,short-description"; fi
if [ -n "$MISSING" ]; then
echo "Hm... Looks like we didn't get all the info we needed. We're missing the following fields: $(bold "$(echo "$MISSING" | sed s/^,//)"). Please try again."
else
return 0
fi
done
}

trap "revert" EXIT

# Intro
echo "Hi! Sounds like you want to make a new Swagger UI plugin. Please fill in the following information and we'll get you set up."
echo

while true; do
# Gather info
gather_loop

# Now generate from boilerplate
echo "$(bold "REVIEW") - Here's what we got:"
echo
echo "$(bold "Name:") $PLUGIN_PROPER_NAME"
echo "$(bold "Author:") $PLUGIN_AUTHOR"
echo "$(bold "Short Description:") $PLUGIN_SHORT_DESC"
echo "$(bold "Kebab-Case Name:") $PLUGIN_KEBAB_CASE"
echo "$(bold "PascalCase Name:") $PLUGIN_PASCAL_CASE"
echo
read -p "Does that look right? (Y/n) " ANS
ANS="$(echo "$ANS" | tr '[:upper:]' '[:lower:]')"
if [ "$ANS" == "n" ]; then
echo "Ok, let's try again"
sleep 1
else
echo "$(bold "Great!") generating a new plugin from the boilerplate...."
sleep 1
break
fi
done


cp -R packages/.boilerplate "packages/${PLUGIN_KEBAB_CASE}"

sed -i'.delete' s/'{author}'/"$PLUGIN_AUTHOR"/g $(find packages/${PLUGIN_KEBAB_CASE} -type f)
sed -i'.delete' s/'{proper-name}'/"$PLUGIN_PROPER_NAME"/g $(find packages/${PLUGIN_KEBAB_CASE} -type f)
sed -i'.delete' s/'{name-kebab-case}'/"$PLUGIN_KEBAB_CASE"/g $(find packages/${PLUGIN_KEBAB_CASE} -type f)
sed -i'.delete' s/'{name-pascal-case}'/"$PLUGIN_PASCAL_CASE"/g $(find packages/${PLUGIN_KEBAB_CASE} -type f)
sed -i'.delete' s/'{short-desc}'/"$PLUGIN_SHORT_DESC"/g $(find packages/${PLUGIN_KEBAB_CASE} -type f)
find "packages/${PLUGIN_KEBAB_CASE}" -name '*.delete' -delete

while read f; do
NEW_NAME="$(echo "$f" | sed s/'{name-kebab-case}'/"$PLUGIN_KEBAB_CASE"/g)"
mv "$f" "$NEW_NAME"
done < <(find "packages/${PLUGIN_KEBAB_CASE}" -name '*{name-kebab-case}*')

echo "$(bold "Ok! Your new plugin is ready for development at ./packages/$PLUGIN_KEBAB_CASE".)"

SUCCESS=1