Skip to content

Instantly share code, notes, and snippets.

@s-u
Last active August 29, 2015 14:04
Show Gist options
  • Save s-u/031b2894684a7d2bddfd to your computer and use it in GitHub Desktop.
Save s-u/031b2894684a7d2bddfd to your computer and use it in GitHub Desktop.
Cache external dependencies in rCharts
#!/bin/sh
#
# Usage: cache.sh [<prefix>]
#
# This file is intended to be run in the rCharts source
# root. It will plow through rCharts sources and
# pick out all external dependencies, download them
# with cache-* prefix as files and modify .yml
# files to reference them instead of the originals.
# All original .yml files are kept as .yml.orig
#
# <prefix> is the prefix that will be used when referencing
# the cached files, the default is "/misc/" but can be anything
# including things like "http://foo.bar/path/"
#
# Copyright (c) 2014, Simon Urbanek
# License: BSD 2-clause
#
# Updated: 2014-11-19 The URLs are now entriely out of whack in rCharts
# since they omit scheme even in cases where the servers don't exist
# so we have to treat everythign as http: just to make sure.
#
PREFIX="$1"
if [ -z "$PREFIX" ]; then
echo No prefix specificed, using /misc/
echo use - if you want no prefix
PREFIX=/misc/
fi
if [ "x$PREFIX" = "x-" ]; then
PREFIX=''
fi
echo Creating a list of JS depenedencies ...
for i in `find . -name \*yml -o -name \*yml.orig`; do echo $i >&2; sed 's!\([ "\t]\)//!\1http://!g' $i | sed -n "s/.*http:/https:/p" | sed 's:".*::'; done | sort | uniq > /tmp/all-https
wc -l /tmp/all-https
echo Downloading dependencies ...
for i in `cat /tmp/all-https`; do
path=`echo $i | sed -e 's,^https://,,'`
name=`echo $path | sed 's:/:-:g'`
curl -s -S -o "cache-$name" "http://$path"
done
echo Modifying links ...
for i in `find . -name \*yml`; do
if [ ! -e "$i.orig" ]; then
mv "$i" "$i.orig"
fi
sed 's!\([ "\t]\)//!\1http://!g' "$i.orig" | sed -e '/http:/s,/,-,g' -e '/http:--/s,http:--,'"$PREFIX"'cache-,' > "$i"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment