Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / lib / generators / instance / templates / .svn / text-base / instance_boot.rb.svn-base
bloba9e7f820bbe8b1eb481b2a2bc596cc6279ced14c
1 # Don't change this file!
2 # Configure your app in config/environment.rb and config/environments/*.rb
4 RAILS_ROOT = File.expand_path("#{File.dirname(__FILE__)}/..") unless defined?(RAILS_ROOT)
6 module Radiant
7   class << self
8     def boot!
9       unless booted?
10         pick_boot.run
11       end
12     end
14     def booted?
15       defined? Radiant::Initializer
16     end
18     def pick_boot
19       case
20       when app?
21         AppBoot.new
22       when vendor?
23         VendorBoot.new
24       else
25         GemBoot.new
26       end
27     end
29     def vendor?
30       File.exist?("#{RAILS_ROOT}/vendor/radiant")
31     end
32     
33     def app?
34       File.exist?("#{RAILS_ROOT}/lib/radiant.rb")
35     end
36     
37     def loaded_via_gem?
38       pick_boot.is_a? GemBoot
39     end
40   end
42   class Boot
43     def run
44       load_initializer
45       Radiant::Initializer.run(:set_load_path)
46     end
47     
48     def load_initializer
49       begin
50         require 'radiant'
51         require 'radiant/initializer'
52       rescue LoadError => e
53         $stderr.puts %(Radiant could not be initialized. #{load_error_message})
54         exit 1
55       end
56     end
57   end
59   class VendorBoot < Boot
60     def load_initializer
61       $LOAD_PATH.unshift "#{RAILS_ROOT}/vendor/radiant/lib" 
62       super
63     end
64     
65     def load_error_message
66       "Please verify that vendor/radiant contains a complete copy of the Radiant sources."
67     end
68   end
70   class AppBoot < Boot
71     def load_initializer
72       $LOAD_PATH.unshift "#{RAILS_ROOT}/lib" 
73       super
74     end
75     
76     def load_error_message
77       "Please verify that you have a complete copy of the Radiant sources."
78     end
79   end
81   class GemBoot < Boot
82     def load_initializer
83       self.class.load_rubygems
84       load_radiant_gem
85       super
86     end
88     def load_error_message
89       "Please reinstall the Radiant gem with the command 'gem install radiant'."
90     end
92     def load_radiant_gem
93       if version = self.class.gem_version
94         gem 'radiant', version
95       else
96         gem 'radiant'
97       end
98     rescue Gem::LoadError => load_error
99       $stderr.puts %(Missing the Radiant #{version} gem. Please `gem install -v=#{version} radiant`, update your RADIANT_GEM_VERSION setting in config/environment.rb for the Radiant version you do have installed, or comment out RADIANT_GEM_VERSION to use the latest version installed.)
100       exit 1
101     end
103     class << self
104       def rubygems_version
105         Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
106       end
108       def gem_version
109         if defined? RADIANT_GEM_VERSION
110           RADIANT_GEM_VERSION
111         elsif ENV.include?('RADIANT_GEM_VERSION')
112           ENV['RADIANT_GEM_VERSION']
113         else
114           parse_gem_version(read_environment_rb)
115         end
116       end
118       def load_rubygems
119         require 'rubygems'
121         unless rubygems_version >= '0.9.4'
122           $stderr.puts %(Radiant requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)
123           exit 1
124         end
126       rescue LoadError
127         $stderr.puts %(Radiant requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)
128         exit 1
129       end
131       def parse_gem_version(text)
132         $1 if text =~ /^[^#]*RADIANT_GEM_VERSION\s*=\s*'([!~<>=]*\s*[\d.]+)'/
133       end
135       private
136         def read_environment_rb
137           File.read("#{RAILS_ROOT}/config/environment.rb")
138         end
139     end
140   end
143 # All that for this:
144 Radiant.boot!