diff --git a/Rakefile b/Rakefile index c40e5c2..ca850ff 100644 --- a/Rakefile +++ b/Rakefile @@ -59,7 +59,7 @@ spec = s.homepage = 'http://code.whytheluckystiff.net/camping/' s.executables = ['camping'] - s.add_dependency('activesupport', '>1.3.1') + s.add_dependency('activesupport', '>=1.3.1') s.add_dependency('markaby', '>0.4') s.add_dependency('metaid') s.required_ruby_version = '>= 1.8.2' diff --git a/bin/camping b/bin/camping index ca456b8..6c350dc 100755 --- a/bin/camping +++ b/bin/camping @@ -13,7 +13,6 @@ require 'ostruct' require 'stringio' require 'rubygems' require 'camping' -require 'camping/session' require 'camping/reloader' require 'yaml' @@ -77,27 +76,6 @@ end unless conf.database conf.database = {:adapter => 'sqlite3', :database => conf.db} end -Camping::Models::Base.establish_connection conf.database - -if conf.log - Camping::Models::Base.logger = Logger.new(conf.log == "-" ? STDOUT : conf.log) -end -begin - Camping::Models::Session.create_schema -rescue MissingSourceFile - puts "** #$0 stopped: SQLite3 not found, please install." - puts "** See http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for instructions." - exit -end - -if conf.database[:adapter] == 'sqlite3' - begin - require 'sqlite3_api' - rescue LoadError - puts "!! Your SQLite3 adapter isn't a compiled extension." - abort "!! Please check out http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for tips." - end -end apps = ARGV.inject([]) do |apps, script| if File.directory? script @@ -107,6 +85,8 @@ apps = ARGV.inject([]) do |apps, script| end end +Camping::Reloader.database = conf.database +Camping::Reloader.log = conf.log apps.map! { |script| Camping::Reloader.new(script) } abort("** No apps successfully loaded") unless apps.detect { |app| app.klass } diff --git a/lib/camping.rb b/lib/camping.rb index 034fd70..919ca8c 100644 --- a/lib/camping.rb +++ b/lib/camping.rb @@ -1,4 +1,4 @@ -%w[active_support markaby metaid tempfile uri].map{|l|require l} +%w[active_support/core_ext/hash markaby metaid tempfile uri].map{|l|require l} module Camping;Apps=[];C=self;S=IO.read(__FILE__).sub(/S=I.+$/,'') P="Cam\ping Problem!";module Helpers;def R c,*g;p=/\(.+?\)/;g.inject(c. urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|s.sub p,C.escape((a[ diff --git a/lib/camping/db.rb b/lib/camping/db.rb index 5154e94..0f451c9 100644 --- a/lib/camping/db.rb +++ b/lib/camping/db.rb @@ -1,4 +1,4 @@ -require 'active_record' +%w[active_support active_record].map{|l|require l} module Camping::Models A = ActiveRecord Base = A::Base diff --git a/lib/camping/reloader.rb b/lib/camping/reloader.rb index 81d6c0d..b012147 100644 --- a/lib/camping/reloader.rb +++ b/lib/camping/reloader.rb @@ -63,6 +63,8 @@ def load_app @klass = nil return end + + Reloader.conditional_connect @klass.create if @klass.respond_to? :create @klass end @@ -103,5 +105,46 @@ def run(*a) def view_source File.read(@script) end + + class << self + def database=(db) + @database = db + end + def log=(log) + @log = log + end + def conditional_connect + # If database models are present, `autoload?` will return nil. + unless Camping::Models.autoload? :Base + require 'logger' + require 'camping/session' + Camping::Models::Base.establish_connection @database + + case @log + when Logger + Camping::Models::Base.logger = @log + when String + Camping::Models::Base.logger = Logger.new(@log == "-" ? STDOUT : @log) + end + + begin + Camping::Models::Session.create_schema + rescue MissingSourceFile + puts "** #$0 stopped: SQLite3 not found, please install." + puts "** See http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for instructions." + exit + end + + if @database[:adapter] == 'sqlite3' + begin + require 'sqlite3_api' + rescue LoadError + puts "!! Your SQLite3 adapter isn't a compiled extension." + abort "!! Please check out http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for tips." + end + end + end + end + end end end