Skip to content

Instantly share code, notes, and snippets.

@fishman
Created October 25, 2012 03:28
Show Gist options
  • Save fishman/3950260 to your computer and use it in GitHub Desktop.
Save fishman/3950260 to your computer and use it in GitHub Desktop.
A Phonegap development workflow with Rack and Sprockets - http://abhishiv.tumblr.com/post/15994872384/phonegap-and-sprockets-sitting-in-a-tree
require 'sprockets'
require 'rack/rewrite'
project_root = File.expand_path(File.dirname(__FILE__))
assets = Sprockets::Environment.new(project_root) do |env|
env.logger = Logger.new(STDOUT)
end
assets.append_path(File.join(project_root, 'assets'))
assets.append_path(File.join(project_root, 'assets', 'javascripts'))
assets.append_path(File.join(project_root, 'assets', 'stylesheets'))
assets.append_path(File.join(project_root, 'assets', 'images'))
map "/build" do
run assets
end
use Rack::Static, :urls => [""]
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}
source :rubygems
gem 'rake'
gem 'rack'
gem 'rack-rewrite'
gem 'thin'
gem 'sprockets', '2.2.0'
gem 'sass'
gem 'coffee-script'
require 'rubygems'
require 'bundler'
require 'pathname'
require 'logger'
require 'fileutils'
Bundler.require
module Sprockets
class StaticCompiler
attr_accessor :env, :target, :paths
def initialize(env, target, paths, options = {})
@env = env
@target = target
@paths = paths
@digest = options.key?(:digest) ? options.delete(:digest) : true
@manifest = options.key?(:manifest) ? options.delete(:manifest) : true
@manifest_path = options.delete(:manifest_path) || target
@zip_files = options.delete(:zip_files) || /\.(?:css|html|js|svg|txt|xml)$/
end
def compile
manifest = {}
env.each_logical_path do |logical_path|
next unless compile_path?(logical_path)
if asset = env.find_asset(logical_path)
manifest[logical_path] = write_asset(asset)
end
end
write_manifest(manifest) if @manifest
end
def write_manifest(manifest)
FileUtils.mkdir_p(@manifest_path)
File.open("#{@manifest_path}/manifest.yml", 'wb') do |f|
YAML.dump(manifest, f)
end
end
def write_asset(asset)
path_for(asset).tap do |path|
filename = File.join(target, path)
FileUtils.mkdir_p File.dirname(filename)
asset.write_to(filename)
end
end
def compile_path?(logical_path)
paths.each do |path|
case path
when Regexp
return true if path.match(logical_path)
when Proc
return true if path.call(logical_path)
else
return true if File.fnmatch(path.to_s, logical_path)
end
end
false
end
def path_for(asset)
@digest ? asset.digest_path : asset.logical_path
end
end
end
ROOT = Pathname(File.dirname(__FILE__))
LOGGER = Logger.new(STDOUT)
BUNDLES = %w( application.css application.js )
BUILD_DIR = ROOT.join("build")
SOURCE_DIR = ROOT.join("assets")
task :compile do
enviorment = Sprockets::Environment.new(ROOT) do |env|
env.logger = LOGGER
end
enviorment.append_path(SOURCE_DIR.join('javascripts').to_s)
enviorment.append_path(SOURCE_DIR.join('stylesheets').to_s)
enviorment.append_path(SOURCE_DIR.join('images').to_s)
comp = Sprockets::StaticCompiler.new enviorment, BUILD_DIR, [/\.(png|jpg)$/, /^(application|ie)\.(css|js)$/], {:digest => false, :zip_files => false, :manifest => false }
comp.compile
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment