Skip to content

Instantly share code, notes, and snippets.

@deepakjois
Created December 4, 2011 02:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save deepakjois/1428926 to your computer and use it in GitHub Desktop.
Save deepakjois/1428926 to your computer and use it in GitHub Desktop.
A convenient way to generate Github gists and link them to JSFiddle.
#!/bin/sh
# Usage: generate-fiddle <folder>
# Creates a folder and adds some files commonly used with JSFiddle
set -e
# Create folder
mkdir $1
cd $1
# Create files
touch fiddle.manifest fiddle.js fiddle.html fiddle.css
# Write a basic manifest
echo "author: $(git config --get user.name)
description: <description>" > fiddle.manifest
#!/bin/sh
#/ Dependencies: Ruby w/ json gem (For JSON string conversion), Perl and curl
#/ Usage: post-fiddle-via-gist [ --auth <username:password> ] [ --framework <framework> ] [ --version <version> ]
#/
#/ A convenient way to generate Github Gists and link them to JSFiddle.
#/
#/ * Creates a Github gist from the files named fiddle.*
#/ in the current folder.
#/
#/ * Generates a JSFiddle URL from the Gist ID.
#/
#/ * Copies the JSFiddle URL to the clipboard.
#/
#/ Options:
#/ --auth: Specify using <username:password>. Uses basic authorization when
#/ making Github API call. If --auth is not provided, it looks for
#/ a Github OAuth token using:
#/ % git config --get github.token
#/ Make sure you set the token using:
#/ % git config --global github.token OAUTH_TOKEN
#/ More info at http://developer.github.com/v3/#authentication
#/
#/ --framework: JSFiddle Framework; e.g. jquery,mootools,yui, prototype, extjs, etc.
#/
#/ --version: Framework version to use; e.g. 1.2, etc.
#/
# The most important line in any shell program.
set -e
# Help technique stolen from shocco
function help() {
grep '^#/' <"$0" | cut -c4-
exit 0
}
# Layout for Gist
function layout () {
cat<<EOF
{
"description": "$gist_desc",
"public": true,
"files": {
$(cat)
}
}
EOF
}
# Convert input stream to JSON string
# FIXME do pure shell JSON conversion instead of pulling in Ruby
function toJson () {
ruby -rrubygems -rjson -e "print STDIN.read.to_json"
}
auth=
framework=library
version=pure
while [ $# -gt 0 ]; do
case "$1" in
-h | --help) help;;
--auth) shift; auth="$1";;
--framework) shift; framework="$1";;
--version) shift; version="$1";;
-*) echo "UnknownOption: '$1'" 1>&2; exit 1;;
*) echo "UnknownArgument: $*" 1>&2; exit 1;;
esac
shift
done
# Validate framework to use with JSFiddle
if [ -z $framework ] || [ -z $version ]; then
echo "You must provide a framework and version to use with your fiddle, using --framework and --version"
exit 1
fi
# Get Github configuration
GITHUB_USER=$(git config --get github.user)
: ${GITHUB_USER:?"Please configure github user using git config --global github.user 'your-github-username'"}
: GITHUB_TOKEN=$(git config --get github.token)
if [ -z $auth ] && [ -z $GITHUB_TOKEN ]; then
# No form of Github authentication provided
echo "No form of Github authorization provided." >&2
echo "Please provide basic auth using --auth <username:password>, or" >&2
echo "configure Github OAuth token using git config --global github.token 'your-github-token'" >&2
exit 1
fi
# Arguments to curl.
CURL_ARGS="-s --url https://api.github.com/gists -d @-"
if [ -z $auth ]; then
# Configure OAuth token header
CURL_ARGS="-H \"Authorization: token $GITHUB_TOKEN\" $CURL_ARGS"
else
# Configure username and password
CURL_ARGS="-u $auth $CURL_ARGS"
fi
# Check fiddle manifest
[ ! -e fiddle.manifest ] && {
echo "No fiddle.manifest" >&2
exit 1
}
# Pull Description
gist_desc=$(cat fiddle.manifest | grep description)
gist_desc=${gist_desc#description\:\ }
echo "Posting gist : $gist_desc" >&2
GIST_ID=$(
# Pipe the content of each file starting with 'fiddle' in current folder
(
for line in $(ls fiddle*)
do
printf $line | toJson
printf ': { "content" : '
cat $line | toJson
printf " }"
echo
done
) |
# Insert commas between each JSON field
sed '$!{s/$/,/;}' |
# Insert gist template for posting to API
layout |
# Invoke Curl to make Github API call
command curl $CURL_ARGS |
# Extract Gist ID
grep html_url | perl -n -e '/.*\/(\d+)/ && print $1'
)
echo "Gist ID is : $GIST_ID" >&2
# Generate JSFiddle URL
FIDDLE_URL="http://jsfiddle.net/gh/gist/$framework/$version/$GIST_ID"
echo "Fiddle URL is $FIDDLE_URL" >&2
# Copy to clipboard
echo $FIDDLE_URL | pbcopy FIDDLE_URL
@gr00nd
Copy link

gr00nd commented Aug 26, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment