Skip to content

Instantly share code, notes, and snippets.

@WardCunningham
Last active January 22, 2017 23:02
Show Gist options
  • Save WardCunningham/fced775fcedcee133b32 to your computer and use it in GitHub Desktop.
Save WardCunningham/fced775fcedcee133b32 to your computer and use it in GitHub Desktop.
Script to create a stub plugin with stub documentation
#!/bin/sh
# Script to create a stub plugin with build script, tests and documentation.
# See http://plugins.fed.wiki.org/make-plugin-script.html
# Usage: ./mkplugin.sh CoolThing
if [ $# -eq 0 ]
then
echo "Usage: ./mkplugin.sh <new-plugin-name>"
echo "e.g. ./mkplugin.sh CoolThing"
exit 0
fi
name=`echo $1 | tr '[A-Z]' '[a-z]'`
repo="wiki-plugin-$name"
date=`date -u +%s`
msec=000
if [ "$1" == "$name" ]
then
echo "Expected capitalized name"
echo "e.g. CoolThing"
exit 2
fi
if [ -e $repo ]
then
echo "plugin repository directory already exists: $repo"
exit 3
fi
mkdir $repo
mkdir $repo/client
cat <<EOF > $repo/client/$name.coffee
expand = (text)->
text
.replace /&/g, '&amp;'
.replace /</g, '&lt;'
.replace />/g, '&gt;'
.replace /\*(.+?)\*/g, '<i>\$1</i>'
emit = (\$item, item) ->
\$item.append """
<p style="background-color:#eee;padding:15px;">
#{expand item.text}
</p>
"""
bind = (\$item, item) ->
\$item.dblclick -> wiki.textEditor \$item, item
window.plugins.$name = {emit, bind} if window?
module.exports = {expand} if module?
EOF
mkdir $repo/server
cat <<EOF > $repo/server/server.coffee
# $name plugin, server-side component
# These handlers are launched with the wiki server.
startServer = (params) ->
app = params.app
argv = params.argv
app.get '/plugin/$name/:thing', (req, res) ->
thing = req.params.thing
res.json {thing}
module.exports = {startServer}
EOF
mkdir $repo/test
cat <<EOF > $repo/test/test.coffee
# build time tests for $name plugin
# see http://mochajs.org/
$name = require '../client/$name'
expect = require 'expect.js'
describe '$name plugin', ->
describe 'expand', ->
it 'can make itallic', ->
result = $name.expand 'hello *world*'
expect(result).to.be 'hello <i>world</i>'
EOF
mkdir $repo/pages
title='"About '"$1"' Plugin"'
id1=`cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 16`
id2=`cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 16`
read -r -d '' story <<EOF
[
{
"type": "paragraph",
"id": "$id1",
"text": "Here we describe the purpose of the plugin and include a sample."
},
{
"type": "$name",
"id": "$id2",
"text": "This is text in the new plugin. You can *double-click* to edit it too."
}
]
EOF
read -r -d '' journal <<EOF
[
{
"type": "create",
"item": {
"title": $title,
"story": $story
},
"date": $date$msec,
"certificate": "from mkplugin.sh"
}
]
EOF
cat <<EOF > $repo/pages/about-$name-plugin
{
"title": $title,
"story": $story,
"journal": $journal
}
EOF
cat <<EOF > $repo/gruntfile.js
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-git-authors');
grunt.initConfig({
coffee: {
client: {
expand: true,
options: {
sourceMap: true
},
src: ['client/*.coffee', 'test/*.coffee', 'server/*.coffee'],
ext: '.js'
}
},
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: ['test/**/*.js']
}
},
watch: {
all: {
files: ['client/*.coffee', 'test/*.coffee', 'server/*.coffee'],
tasks: ['coffee','mochaTest']
}
}
});
grunt.registerTask('build', ['coffee', 'mochaTest']);
grunt.registerTask('default', ['build']);
};
EOF
cat <<EOF > $repo/package.json
{
"name": "wiki-plugin-$name",
"version": "0.1.0",
"description": "Federated Wiki - $1 Plugin",
"keywords": [
"$name",
"wiki",
"federated wiki",
"plugin"
],
"author": {
"name": "Your Name",
"email": "you@example.com",
"url": "http://example.com"
},
"contributors": [],
"scripts": {
"test": "mocha"
},
"devDependencies": {
"grunt": "^1.0.1",
"grunt-contrib-coffee": "^1.0.0",
"grunt-contrib-watch": "^1.0.0",
"grunt-git-authors": "^3.2.0",
"mocha": "^3.1.0",
"expect.js": "^0.3.1",
"grunt-mocha-test": "^0.13.2"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/fedwiki/wiki-plugin-$name.git"
},
"bugs": {
"url": "https://github.com/fedwiki/wiki-plugin-$name/issues"
},
"engines": {
"node": ">=0.10"
}
}
EOF
cat <<EOF > $repo/factory.json
{
"name": "$1",
"title": "$1 Plugin",
"category": "other"
}
EOF
cat <<EOF > $repo/ReadMe.md
# Federated Wiki - $1 Plugin
This plugin, type: $name, extends the markup of the federated wiki.
## Build
npm install
grunt build
## License
MIT
EOF
cat <<EOF > $repo/.gitignore
/*/*.js
/*/*.map
node_modules
npm-debug.log
status
EOF
cat <<EOF > $repo/.npmignore
/**/*.coffee
Gruntfile.js
test
status
EOF
echo
echo "Package: cd $repo/"
echo "Commit: git init; git add .; git commit -m mkplugin"
echo "Install: npm install"
echo "Build: grunt build; grunt watch"
echo "Link: npm link; (cd ../wiki; npm link $repo)"
echo "Serve: (restart once to notice new plugin)"
echo "Browse: open localhost:3000/about-$name-plugin.html"
echo "Develop: vi client/$name.coffee"
echo "Document: wiki -d . -p 4000 --security_legacy true"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment