Last active
December 17, 2015 23:58
-
-
Save uu59/5692993 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -- coding: utf-8 | |
require "grill" | |
Grill.implant <<-G | |
source "https://rubygems.org" | |
gem "sprockets" | |
gem "fssm" | |
gem "sass" | |
gem "coffee-script" | |
gem "pry" | |
G | |
def start(root) | |
pid = Process.fork do | |
env = Sprockets::Environment.new(root) | |
generated = [] | |
FSSM.monitor(root, "**/*", :directries => false) do | |
delete do |_, file| | |
path = File.join(root, file) | |
end | |
[:update, :create].each do |event| | |
send(event) do |_, file| | |
path = File.join(root, file) | |
if path == File.expand_path(".", __FILE__) | |
puts "Going to restart the server.." | |
Process.kill(:INT, $$) | |
next | |
end | |
if generated.include?(path) | |
puts "Ignore: #{path} is generated file" | |
next | |
end | |
dir = File.dirname(path) | |
env.append_path(dir) unless env.paths.include?(dir) | |
begin | |
asset = env.find_asset(File.basename(file)) | |
ext = env.extension_for_mime_type(asset.content_type) | |
dest = "#{path.gsub(%r!/(.*?)\..*$!, "/\\1")}#{ext}" | |
File.open(dest, "w") do |f| | |
f.write asset.to_s | |
end | |
generated << dest | |
generated.uniq! | |
puts "Generate: #{path} -> #{dest}" | |
rescue => e | |
p e | |
end | |
end | |
end | |
end | |
end | |
end | |
puts "Start watcher" | |
root = File.expand_path(ARGV.first || File.dirname(__FILE__)) | |
pid = start(root) | |
Process.waitpid pid | |
Bundler.with_clean_env do | |
exec(ENV, "ruby", *[$0, ARGV].flatten) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment