Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Shell script to update 3rd party libs
#!/bin/sh
# This shell script is used to bootstrap the app and update external libraries
#
# ====== IMPORTANT ======
#
# it may break application if 3rd party libs aren't backwards compatible
# or if libs were edited locally, use with care !!!
# Settings ----------
SWAP_FOLDER="_update_swap"
BASE_FOLDER="."
JS_FOLDER="$BASE_FOLDER/js"
CSS_FOLDER="$BASE_FOLDER/css"
# if $IS_SVN is `true` it will avoid deleting ".svn" folders
# set to `false` if using Git/Mercurial or SVN 1.7+
IS_SVN=false
# if $IS_HANDLEBARS it will download RequireJS handlebars plugin if not it
# downloads the hogan plugin
IS_HANDLEBARS=true
if [ ! -d "$JS_FOLDER" ]; then
mkdir -p "$JS_FOLDER/lib/jquery/"
mkdir -p "$JS_FOLDER/lib/require/"
fi
if [ ! -d "$CSS_FOLDER" ]; then
mkdir -p "$CSS_FOLDER"
START_CSS=true
fi
# Update ----------
echo "\n == UPDATING LIBS =="
echo "\n -- updating jquery.js --\n"
curl http://code.jquery.com/jquery.js > "$JS_FOLDER/lib/jquery/jquery.js"
echo "\n -- updating signals.js --\n"
curl https://raw.github.com/millermedeiros/js-signals/master/dist/signals.js > "$JS_FOLDER/lib/signals.js"
echo "\n -- updating crossroads.js --\n"
curl https://raw.github.com/millermedeiros/crossroads.js/master/dist/crossroads.js > "$JS_FOLDER/lib/crossroads.js"
# RequireJS ----------
echo "\n -- updating require.js --\n"
curl https://raw.github.com/jrburke/requirejs/master/require.js > "$JS_FOLDER/lib/require/require.js"
echo "\n -- updating require/text.js --\n"
curl https://raw.github.com/requirejs/text/master/text.js > "$JS_FOLDER/lib/require/text.js"
echo "\n -- updating require/async.js --\n"
curl https://raw.github.com/millermedeiros/requirejs-plugins/master/src/async.js > "$JS_FOLDER/lib/require/async.js"
echo "\n -- updating require/json.js --\n"
curl https://raw.github.com/millermedeiros/requirejs-plugins/master/src/json.js > "$JS_FOLDER/lib/require/json.js"
echo "\n -- updating require/image.js --\n"
curl https://raw.github.com/millermedeiros/requirejs-plugins/master/src/image.js > "$JS_FOLDER/lib/require/image.js"
echo "\n -- updating require/mdown.js --\n"
curl https://raw.github.com/millermedeiros/requirejs-plugins/master/src/mdown.js > "$JS_FOLDER/lib/require/mdown.js"
curl https://raw.github.com/millermedeiros/requirejs-plugins/master/lib/Markdown.Converter.js > "$JS_FOLDER/lib/require/Markdown.Converter.js"
if $IS_HANDLEBARS; then
echo "\n -- updating require/hbs.js --\n"
curl https://raw.github.com/SlexAxton/require-handlebars-plugin/master/hbs.js > "$JS_FOLDER/lib/require/hbs.js"
curl https://raw.github.com/SlexAxton/require-handlebars-plugin/master/Handlebars.js > "$JS_FOLDER/lib/Handlebars.js"
if [ ! -d "$JS_FOLDER/lib/require/hbs" ]; then
mkdir "$JS_FOLDER/lib/require/hbs"
fi
curl https://raw.github.com/SlexAxton/require-handlebars-plugin/master/hbs/i18nprecompile.js > "$JS_FOLDER/lib/require/hbs/i18nprecompile.js"
curl https://raw.github.com/SlexAxton/require-handlebars-plugin/master/hbs/json2.js > "$JS_FOLDER/lib/require/hbs/json2.js"
curl https://raw.github.com/SlexAxton/require-handlebars-plugin/master/hbs/underscore.js > "$JS_FOLDER/lib/require/hbs/underscore.js"
else
echo "\n -- updating require/hgn.js --\n"
curl https://raw.github.com/millermedeiros/requirejs-hogan-plugin/master/hgn.js > "$JS_FOLDER/lib/require/hgn.js"
curl https://raw.github.com/millermedeiros/requirejs-hogan-plugin/master/hogan.js > "$JS_FOLDER/lib/hogan.js"
# text plugin is a dependency
curl https://raw.github.com/requirejs/text/master/text.js > "$JS_FOLDER/lib/require/text.js"
fi
# Other Libs -----------
echo "\n -- updating moutjs --\n"
if $IS_SVN; then
find "$JS_FOLDER/lib/mout" ! -wholename '*.svn*' -type f -delete
else
rm -rf "$JS_FOLDER/lib/mout"
fi
git clone --depth 1 https://github.com/mout/mout.git "$SWAP_FOLDER/mout"
cp -R "$SWAP_FOLDER/mout/src/." "$JS_FOLDER/lib/mout/"
mv "$SWAP_FOLDER/mout/README.md" "$JS_FOLDER/lib/mout/"
echo "\n -- updating MM_js_lib --\n"
if $IS_SVN; then
find "$JS_FOLDER/lib/millermedeiros" ! -wholename '*.svn*' -type f -delete
else
rm -rf "$JS_FOLDER/lib/millermedeiros"
fi
git clone --depth 1 https://github.com/millermedeiros/MM_js_lib.git "$SWAP_FOLDER/millermedeiros"
cp -R "$SWAP_FOLDER/millermedeiros/src/." "$JS_FOLDER/lib/millermedeiros/"
mv "$SWAP_FOLDER/millermedeiros/README.markdown" "$JS_FOLDER/lib/millermedeiros/"
echo "\n -- updating basis.css --\n"
if $IS_SVN; then
find "$CSS_FOLDER/bss" ! -wholename '*.svn*' -type f -delete
else
rm -rf "$CSS_FOLDER/bss"
fi
git clone --depth 1 https://github.com/millermedeiros/basis.css.git "$SWAP_FOLDER/basis"
if $START_CSS; then
cp -R "$SWAP_FOLDER/basis/css/." "$CSS_FOLDER"
else
# we update only the "bss" folder otherwise it might overwrite user styles
cp -R "$SWAP_FOLDER/basis/css/bss/." "$CSS_FOLDER/bss/"
fi
mv "$SWAP_FOLDER/basis/README.md" "$CSS_FOLDER/bss/"
# Finish -----------
echo "\n -- clean swap folder --\n"
rm -rf "$SWAP_FOLDER"
echo "\n == FINISHED UPDATE == \n"
Owner

I've been using shell scripts similar to this for the past couple years to install/update 3rd party libs. I usually keep the whole code commented out and just enable the parts that I want to run that way I avoid updating the wrong libs.

This could be replaced by a package manager like:

But I kinda like the "simplicity" of the shell script specially since most of my dependencies are solved with simple curl calls and I don't execute this script that often.

Owner

Another thing that is good to note is that I ALWAYS keep the dependencies on my version control system so future devs don't need to execute this script. - I rarely update libs after deploy (only for bug fixes).

Package managers are great and solve lots of problems (resolving dependencies, downloading proper files, etc..) but I still haven't found a client side package manager that fits my desires/needs. I like to organize my files on a very specific way and I don't like a lot of "garbage" inside the 3rd party libraries folder (git clone isn't enough). I also like to use the unminified version of the scripts during development so I can fix things if needed and have proper line diffs in case I update the lib to a newer version or change the code myself.

Package management for the client-side is a very complex "problem" that might not be solved in the near future, specially since we have so many different kinds of dependencies (images, css, fonts, js) and so many different project structures (AMD, CJS, multiple script tags, namespaces, globals, etc). There is no easy way to sandbox each component so we can have multiple versions running simultaneously without conflicts (solving this in JS is easier than CSS) and we probably won't agree about the best format anytime soon...

Since it takes me less than 15min to create a script like this for each project, and it works for my own needs, I don't think I will invest my time trying to come up with yet-another-client-side-package-manager.

Interesting thoughts on client-side package managers. Thanks for posting this up here!

Always use double quotes. Please add some.

Especially stuff like:

rm -rf $JS_FOLDER/lib/mout

…is potentially dangerous. (What if $JS_FOLDER contains a space?)

I started using Bower for a project, but I didn't like to download the entire repository (docs, tests, README etc), I just wanted the minified and unminified version of the lib.

Maybe this behaviour happened because the lib didn't implement the "component.json" properly, but event if it did, as far as I know, the "component.json" only allow to specify 1 "js" file to be "downloaded".

Owner

update: I’ve been using http://volojs.org/ more often instead of the ad-hoc shell scripts, it's flexible enough and let me do basically the same thing.

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