Skip to content

Instantly share code, notes, and snippets.

@masnick
Created April 3, 2015 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masnick/1c06a4ebf3c3f4f4f950 to your computer and use it in GitHub Desktop.
Save masnick/1c06a4ebf3c3f4f4f950 to your computer and use it in GitHub Desktop.
Barebones LiveReload with guard example
source 'https://rubygems.org'
gem 'guard'
gem 'guard-livereload', '~> 2.4', require: false
gem 'rack-livereload'
gem 'sinatra'
gem 'thin'
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features)
## Uncomment to clear the screen before every task
# clearing :on
## Guard internally checks for changes in the Guardfile and exits.
## If you want Guard to automatically start up again, run guard in a
## shell loop, e.g.:
##
## $ while bundle exec guard; do echo "Restarting Guard..."; done
##
## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
guard 'livereload' do
watch(%r{index\.html})
# You'll have to customize this depending on what files you want to trigger LiveReload
# watch(%r{app/views/.+\.(erb|haml|slim)$})
# watch(%r{app/helpers/.+\.rb})
# watch(%r{public/.+\.(css|js|html)})
# watch(%r{config/locales/.+\.yml})
# # Rails Assets Pipeline
# watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" }
end
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/something.js"></script>
</head>
<body>
hello world
</body>
</html>
require 'rubygems'
require 'bundler'
Bundler.require :default, (ENV["RACK_ENV"] || "development").to_sym
use Rack::LiveReload, :host => 'yourcomputer.local'
set :bind, 'localhost' # only available on local computer
# set :public_folder, 'assets'
# set :public_folder, 'img'
set :public_folder, 'js'
get '/' do
File.read('index.html')
end
# Serve static assets
get %r{/(assets|img|js)/(.*)} do
fullpath = File.expand_path(File.join(settings.root, params[:captures][0], params[:captures][1]))
# Checks to make sure the requested path is a subdirectory of assets, img, or js (not /img/../../something_else)
if /^#{settings.root}\/(assets|img|js)/ =~ fullpath.to_s then
send_file fullpath
else
halt 404
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment