Permalink
Browse files

sprockets adapter

  • Loading branch information...
1 parent 605e131 commit e4cb6778440a954be633fe762d761d34ac141cf0 @datapimp committed Mar 11, 2013
View
12 Gemfile
@@ -8,6 +8,14 @@ gem "ejs"
gem "coffee-script"
gem "haml_assets"
-group :development do
- gem "pry"
+group :development, :test do
+ gem 'rspec'
+ gem "rake"
+ gem 'fakeredis'
+ gem 'guard'
+ gem 'rb-fsevent'
+ gem 'growl'
+ gem 'guard-bundler'
+ gem 'guard-rspec'
+ gem 'pry'
end
View
@@ -6,29 +6,60 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.6.1)
+ diff-lcs (1.2.1)
ejs (1.1.1)
execjs (1.4.0)
multi_json (~> 1.0)
+ fakeredis (0.4.1)
+ redis (~> 3.0.0)
+ growl (1.0.3)
+ guard (1.6.2)
+ listen (>= 0.6.0)
+ lumberjack (>= 1.0.2)
+ pry (>= 0.9.10)
+ terminal-table (>= 1.4.3)
+ thor (>= 0.14.6)
+ guard-bundler (1.0.0)
+ bundler (~> 1.0)
+ guard (~> 1.1)
+ guard-rspec (2.4.1)
+ guard (>= 1.1)
+ rspec (~> 2.11)
haml (4.0.0)
tilt
haml_assets (0.2.1)
haml
tilt
hike (1.2.1)
+ listen (0.7.3)
+ lumberjack (1.0.2)
method_source (0.8.1)
multi_json (1.6.1)
pry (0.9.12)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.4)
rack (1.5.2)
+ rake (10.0.3)
+ rb-fsevent (0.9.3)
+ redis (3.0.2)
+ rspec (2.13.0)
+ rspec-core (~> 2.13.0)
+ rspec-expectations (~> 2.13.0)
+ rspec-mocks (~> 2.13.0)
+ rspec-core (2.13.0)
+ rspec-expectations (2.13.0)
+ diff-lcs (>= 1.1.3, < 2.0)
+ rspec-mocks (2.13.0)
sass (3.2.7)
slop (3.4.3)
sprockets (2.9.0)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
+ terminal-table (1.4.5)
+ thor (0.17.0)
tilt (1.3.5)
PLATFORMS
@@ -37,8 +68,16 @@ PLATFORMS
DEPENDENCIES
coffee-script
ejs
+ fakeredis
+ growl
+ guard
+ guard-bundler
+ guard-rspec
haml
haml_assets
pry
+ rake
+ rb-fsevent
+ rspec
sass
sprockets
@@ -0,0 +1,3 @@
+#= require 'underscore-min'
+#= require 'backbone-events'
+
@@ -1 +1,13 @@
-#= require 'backbone-events'
+#= require ./dependencies
+
+#= require_self
+#= require ./util
+
+root = @
+
+if typeof(CodeSync) isnt "undefined"
+ CodeSync = exports
+else
+ CodeSync = root.CodeSync = {}
+
+CodeSync.VERSION = "0.0.1"
@@ -0,0 +1,43 @@
+CodeSync.util ||= {}
+
+
+loadedScripts = {}
+scriptTimers = {}
+
+CodeSync.util.loadScript = (url, options={}, callback) ->
+ loaded = loadedScripts
+ timers = scriptTimers
+
+ if _.isFunction(options) and !callback?
+ callback = options
+ options = {}
+
+ head= document.getElementsByTagName('head')[0];
+ script = document.createElement("script")
+ script.url = url
+ script.type = "text/javascript"
+
+ that = @
+ onLoad = ()->
+ if typeof(callback) is "function"
+ callback.call(that, url, options, script)
+
+ head.removeChild(script)
+ loaded[url] = true
+
+ if options.once is true && loaded[url]
+ return false
+
+ head.appendChild(script)
+
+ script.onreadystatechange = ()->
+ if script.readyState is "loaded" or script.readyState is "complete"
+ onLoad()
+
+ script.onload = onLoad
+
+ if navigator?.userAgent.match(/WebKit/)
+ timers[url] = setInterval ()->
+ onLoad()
+ clearInterval(timers[url])
+ , 10
View
@@ -4,6 +4,7 @@ module CodeSync
require 'code_sync/version'
require 'code_sync/publisher'
require 'code_sync/server'
+ require 'code_sync/temp_asset'
require 'code_sync/sprockets_adapter'
require 'code_sync/watcher'
end
View
@@ -1,4 +1,10 @@
module CodeSync
module Server
+
+ attr_reader :assets
+
+ def initialize options={}
+ @assets = CodeSync::SprocketsAdapter.new()
+ end
end
end
@@ -4,23 +4,48 @@ module CodeSync
class SprocketsAdapter
attr_accessor :env, :options
- def initialize options={}
+ def initialize sprockets, options=nil
+ if options.nil? and sprockets.is_a?(Hash)
+ options = sprockets
+ sprockets = nil
+ end
+
@options = options
- options[:root] ||= Dir.pwd()
- @env = Sprockets::Environment.new(options[:root])
+ @env = sprockets || Sprockets::Environment.new(options[:root] ||= Dir.pwd())
+
+ append_asset_paths()
+ end
+
+ def compile content, options={}
+ create_asset(content,options).to_s
+ end
- append_asset_paths() if env.paths.length == 0
+ def create_asset content, options={}
+ TempAsset.create_from(content, options.merge(env: env))
+ end
+ def method_missing meth, *args, &block
+ env.send(meth, *args, &block)
end
protected
+ def tmpdir
+ @tmpdir ||= Dir.tmpdir
+ end
+
+ def append_temporary_path
+ env.prepend_path(tmpdir)
+ end
+
def append_asset_paths base_path=nil
base_path ||= env.root
+ env.append_path tmpdir
+
%w{lib vendor app}.each do |base|
- path = File.join(env.root, base, 'assets' )
+ path = File.join(env.root, base, 'assets')
if File.exists?(path)
%w{images stylesheets javascripts}.each do |type|
@@ -0,0 +1,33 @@
+require 'sprockets' unless defined?(Sprockets)
+
+module CodeSync
+ class TempAsset < Sprockets::BundledAsset
+ def self.create_from content, options={}
+ environment, filename, extension = options.values_at(:env, :filename, :extension)
+
+ filename = filename || "compiled"
+
+ if options[:type]
+ filename += "-#{options[:type]}"
+
+ extension ||= case options[:type]
+ when "coffeescript"
+ ".coffee"
+ when "scss"
+ ".css.scss"
+ else
+ options[:type]
+ end
+ end
+
+ tempfile = Tempfile.new([filename, extension])
+
+ tempfile.write(content) && tempfile.rewind
+
+ pathname = Pathname.new(tempfile.path)
+ logical_path = File.basename(pathname)
+
+ new(environment, logical_path, pathname)
+ end
+ end
+end
@@ -0,0 +1,44 @@
+require "spec_helper"
+
+describe CodeSync::SprocketsAdapter do
+ let(:coffeescript) do
+ asset = File.join(CodeSync.spec_root,'support','site','app','assets','javascripts','spec_application_javascript.coffee')
+ content = IO.read(asset)
+ end
+
+ let(:env) do
+ site = File.join(CodeSync.spec_root, 'support', 'site')
+ CodeSync::SprocketsAdapter.new(root: site)
+ end
+
+ let(:manifest) do
+ asset = File.join(CodeSync.spec_root,'support','site','app','assets','javascripts','manifest.coffee')
+ IO.read(asset)
+ end
+
+ it "should be able to find an asset" do
+ env.find_asset('spec_application_javascript.coffee').should_not be_nil
+ end
+
+ it "should be able to compile an asset by filename" do
+ env.find_asset("spec_application_javascript.coffee").to_s.should match(/SpecApplication.prototype.boot/)
+ end
+
+ it "should be able to compile an asset by path" do
+ path = File.join(CodeSync.spec_root,'support','site','app','assets','javascripts','spec_application_javascript.coffee')
+ env.find_asset(path).to_s.should match(/SpecApplication.prototype.boot/)
+ end
+
+ it "should be able to compile a coffeescript string into an asset" do
+ env.compile(coffeescript, type:"coffeescript").should match("SpecApplication.prototype.boot")
+ end
+
+ it "should be able to compile a javascript manifest" do
+ compiled = env.compile(manifest, type:"coffeescript")
+
+ compiled.should match("SpecApplication.prototype.boot")
+ compiled.should match("SpecLib")
+ compiled.should match("SpecVendorJavascript")
+ end
+
+end
View
@@ -1,22 +1,21 @@
-require 'machinist/active_record'
-require 'sham'
-require 'faker'
+unless defined?(CodeSync)
+ Bundler.require(:test)
+end
+
+require 'code_sync'
-require File.expand_path('../support/schema.rb', __FILE__)
-require File.expand_path('../support/models.rb', __FILE__)
+module CodeSync
+ class << self
+ attr_accessor :spec_root
+ end
-Sham.define do
- name { Faker::Name.name }
- salary {|index| 30000 + (index * 1000)}
+ self.spec_root = File.dirname(__FILE__)
end
RSpec.configure do |config|
config.before(:suite) do
- Models.make
- end
- config.before(:all) { Sham.reset(:before_all) }
- config.before(:each) { Sham.reset(:before_each) }
+ end
end
-require 'smooth'
+
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
+#= require "spec_vendor_javascript"
+#= require "spec_library_javascript"
+#= require "spec_application_javascript"
@@ -0,0 +1,4 @@
+SpecApplication = new Spec.Application()
+
+SpecApplication::boot = ()->
+ console.log "booooot"
@@ -0,0 +1,5 @@
+#header {
+ h1 {
+ color: pink;
+ }
+}
@@ -0,0 +1,3 @@
+SpecLib =
+ util:
+ method: ()-> true
@@ -0,0 +1,5 @@
+#footer {
+ a {
+ color: blue;
+ }
+}
@@ -0,0 +1,5 @@
+var SpecVendorJavascript = {
+ init: function(){
+ console.log("This feels dirty.");
+ }
+};
@@ -0,0 +1,3 @@
+h3.vendor-class {
+ color: redr
+}
Oops, something went wrong.

0 comments on commit e4cb677

Please sign in to comment.