Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'plugins' of github.com:quirkey/soca

  • Loading branch information...
commit d4cd7af61c7d6c8d3c5a42bcb2a08348d9725dff 2 parents 433356c + 336b750
@quirkey authored
View
6 Gemfile
@@ -11,9 +11,9 @@ gem 'mustache', '~>0.99'
gem 'coffee-script', '~>2.2'
group :development do
- gem 'rake'
- gem 'jeweler'
+ gem 'rake'
+ gem 'jeweler'
end
group :test do
- gem "shoulda", ">= 0"
+ gem "shoulda", ">= 0"
end
View
8 lib/soca/plugin.rb
@@ -1,6 +1,7 @@
module Soca
class Plugin
attr_reader :pusher
+ attr_accessor :options
def self.name(plugin_name)
@@plugins ||= {}
@@ -11,12 +12,9 @@ def self.plugins
@@plugins ||= {}
end
- def initialize(pusher)
+ def initialize(pusher, options = {})
@pusher = pusher
- end
-
- def run(options = {})
- raise "you need to subclass plugin and provide your own logic, please"
+ @options = options
end
def logger
View
6 lib/soca/plugins/coffeescript.rb
@@ -14,7 +14,7 @@ class CoffeeScript < Soca::Plugin
# * :vars - Additional variables to interpolate. By default the `env` and
# `config` are interpolated.
#
- def run(options = {})
+ def before_build
file_patterns = options[:files] || '**/*.coffee'
files = Dir[*[file_patterns].flatten]
vars = {
@@ -30,11 +30,11 @@ def run(options = {})
parts = basename.split(/\./)
new_file = (parts.length > 2 ? parts[0..-2].join('.') : parts[0]) + ".js"
- File.open(File.join(dir, new_file), 'w') do |f|
+ output_dir = options[:output_dir] || dir
+ File.open(File.join(output_dir, new_file), 'w') do |f|
f << ::CoffeeScript.compile(File.read(file), vars)
end
Soca.logger.debug "Wrote to #{new_file}"
-
end
end
View
10 lib/soca/plugins/compass.rb
@@ -8,13 +8,11 @@ class Compass < Soca::Plugin
name 'compass'
- def run(options = {})
- @options = options
-
+ def before_build
Soca.logger.info "compiling compass"
unless Soca.debug
- options = {:logger => ::Compass::NullLogger.new}.merge(options)
+ options = {:logger => ::Compass::NullLogger.new}.merge(self.options)
end
compass = ::Compass::Compiler.new(app_dir, compass_from, compass_to, ::Compass.sass_engine_options.merge(options || {}))
@@ -24,11 +22,11 @@ def run(options = {})
private
def compass_from
- @options.has_key?(:from) ? File.join(app_dir, @options[:from]) : File.join(app_dir, 'sass')
+ options.has_key?(:from) ? File.join(app_dir, options[:from]) : File.join(app_dir, 'sass')
end
def compass_to
- @options.has_key?(:to) ? File.join(app_dir, @options[:to]) : File.join(app_dir, 'css')
+ options.has_key?(:to) ? File.join(app_dir, options[:to]) : File.join(app_dir, 'css')
end
end
end
View
4 lib/soca/plugins/jim.rb
@@ -6,11 +6,13 @@ class Jim < Soca::Plugin
name 'jim'
- def run(options = {})
+ def before_build
jimfile = File.join(app_dir, 'Jimfile')
::Jim.logger = logger
logger.debug "bundling js"
bundler = ::Jim::Bundler.new(File.read(jimfile), ::Jim::Index.new(app_dir))
+ # set the base dir relative to the app
+ bundler.bundle_dir = app_dir + bundler.bundle_dir
bundler.bundle!
end
View
5 lib/soca/plugins/macro.rb
@@ -7,16 +7,17 @@ class Macro < Soca::Plugin
name 'macro'
- def run(options = {})
+ def before_push
@pusher.document['views'].each do |view,code|
['map','reduce'].each{|part| macro_expand_on(part,code) if code[part]}
end
end
+ private
def macro_expand_on(part,code)
code[part] = code[part].split("\n").inject(" ") do |res,line|
if line =~ /\/\/ !code (.*)/
- res += "\n#{File.read($1)}\n"
+ res += "\n#{File.read(File.join(pusher.app_dir, $1))}\n"
else
res += "#{line}\n"
end
View
2  lib/soca/plugins/mustache.rb
@@ -14,7 +14,7 @@ class Mustache < Soca::Plugin
# * :vars - Additional variables to interpolate. By default the `env` and
# `config` are interpolated.
#
- def run(options = {})
+ def before_build
file_patterns = options[:files] || '*.mustache'
files = Dir[*[file_patterns].flatten]
vars = {
View
39 lib/soca/pusher.rb
@@ -30,13 +30,13 @@ def load_couchapprc
def build
@document = {}
- run_hook_file!(:before_build)
+ run_hooks!(:before_build)
logger.debug "building app JSON"
Dir.glob(app_dir + '**/**') do |path|
next if File.directory?(path)
@document = map_file(path, @document)
end
- run_hook_file!(:after_build)
+ run_hooks!(:after_build)
@document
end
@@ -81,13 +81,13 @@ def get_current_revision
def push!
create_db!
build
- run_hook_file!(:before_push)
+ run_hooks!(:before_push)
get_current_revision
document['_rev'] = revision if revision
post_body = JSON.generate(document)
logger.debug "pushing document to #{push_url}"
put!(push_url, post_body)
- run_hook_file!(:after_push)
+ run_hooks!(:after_push)
end
def purge!
@@ -107,6 +107,11 @@ def logger
Soca.logger
end
+ def run_hooks!(hook)
+ run_hook_file!(hook)
+ run_plugin_hooks!(hook)
+ end
+
def run_hook_file!(hook)
hook_file_path = File.join(app_dir, 'hooks', "#{hook}.rb")
if File.readable? hook_file_path
@@ -118,10 +123,28 @@ def run_hook_file!(hook)
end
end
- def plugin(plugin_name, options = {})
- require "soca/plugins/#{plugin_name}"
- p = Soca::Plugin.plugins[plugin_name].new(self)
- p.run(options)
+ def run_plugin_hooks!(hook)
+ plugins.each do |plugin_name, plugin|
+ if plugin.respond_to?(hook)
+ plugin.send(hook)
+ end
+ end
+ end
+
+ def plugins
+ return @plugins if @plugins
+ if config['plugins']
+ @plugins = config['plugins'].map do |plugin_config|
+ plugin_name, plugin_options = [plugin_config].flatten
+ require "soca/plugins/#{plugin_name}"
+ plugin_options ||= {}
+ plugin_options.each {|k, v| plugin_options[k.to_sym] = v }
+ [plugin_name, Soca::Plugin.plugins[plugin_name].new(self, plugin_options)]
+ end
+ else
+ @plugins = []
+ end
+ @plugins
end
private
View
15 test/test_compass_plugin.rb
@@ -7,8 +7,7 @@ def app_path(relative='')
context 'compass plugin' do
setup do
- pusher = Soca::Pusher.new(app_path)
- @plugin = Soca::Plugins::Compass.new(pusher)
+ @pusher = Soca::Pusher.new(app_path)
end
context 'given no options' do
@@ -17,7 +16,8 @@ def app_path(relative='')
with(@test_app_dir, app_path('sass'), app_path('css'), anything).
returns(mock(:run => true))
- @plugin.run
+ @plugin = Soca::Plugins::Compass.new(@pusher)
+ @plugin.before_build
end
end
@@ -27,7 +27,8 @@ def app_path(relative='')
with(@test_app_dir, app_path('app/sass'), app_path('css'), anything).
returns(mock(:run => true))
- @plugin.run :from => 'app/sass'
+ @plugin = Soca::Plugins::Compass.new(@pusher, :from => 'app/sass')
+ @plugin.before_build
end
end
@@ -37,7 +38,8 @@ def app_path(relative='')
with(@test_app_dir, app_path('sass'), app_path('snoop_dogg/css'), anything).
returns(mock(:run => true))
- @plugin.run :to => 'snoop_dogg/css'
+ @plugin = Soca::Plugins::Compass.new(@pusher, :to => 'snoop_dogg/css')
+ @plugin.before_build
end
end
@@ -47,7 +49,8 @@ def app_path(relative='')
with(@test_app_dir, app_path('app/sass'), app_path('app/css'), anything).
returns(mock(:run => true))
- @plugin.run :from => 'app/sass', :to => 'app/css'
+ @plugin = Soca::Plugins::Compass.new(@pusher, :from => 'app/sass', :to => 'app/css')
+ @plugin.before_build
end
end
end
View
18 test/test_soca_pusher.rb
@@ -21,6 +21,8 @@ class TestSocaPusher < Test::Unit::TestCase
context "build" do
setup do
+ js_path = File.join(@test_app_dir, 'js', 'default.js')
+ File.unlink(js_path) if File.readable?(js_path)
@app_file = @push.build
end
@@ -55,6 +57,11 @@ class TestSocaPusher < Test::Unit::TestCase
assert @app_file['rewrites']
assert @app_file['rewrites'][0]['from']
end
+
+ should "run hooks with arbitrary info" do
+ assert_equal "test", @push.config["arbitrary_setting"]
+ end
+
end
context "push_url" do
@@ -69,12 +76,19 @@ class TestSocaPusher < Test::Unit::TestCase
end
context "push" do
- should "create the db and push" do
+ setup do
+ @push.push!
+ end
+
+ before_should "create the db and push" do
put_response = Typhoeus::Response.new(:code => 201, :headers => "", :body => '{"ok":"true"}')
Typhoeus::Request.expects(:put).twice.returns(put_response)
get_response = Typhoeus::Response.new(:code => 200, :headers => "", :body => '{"_id":"_design/test_app", "_rev": "2"}')
Typhoeus::Request.expects(:get).returns(get_response)
- @push.push!
+ end
+
+ should "run hooks" do
+ assert_equal "test", @push.config["before_push_setting"]
end
end
View
6 test/testapp/config.js
@@ -8,5 +8,9 @@
"templates": "_attachments/templates",
"db": "",
"config.js": ""
- }
+ },
+ "plugins": [
+ "macro",
+ "jim"
+ ]
}
View
2  test/testapp/hooks/before_build.rb
@@ -1 +1 @@
-plugin 'jim'
+config["arbitrary_setting"] = "test"
View
2  test/testapp/hooks/before_push.rb
@@ -1 +1 @@
-plugin 'macro'
+config["before_push_setting"] = "test"
Please sign in to comment.
Something went wrong with that request. Please try again.