Chris@0
|
1 require 'active_support'
|
Chris@0
|
2 require File.join(File.dirname(__FILE__), 'engines/plugin')
|
Chris@0
|
3 require File.join(File.dirname(__FILE__), 'engines/plugin/list')
|
Chris@0
|
4 require File.join(File.dirname(__FILE__), 'engines/plugin/loader')
|
Chris@0
|
5 require File.join(File.dirname(__FILE__), 'engines/plugin/locator')
|
Chris@0
|
6 require File.join(File.dirname(__FILE__), 'engines/assets')
|
Chris@0
|
7 require File.join(File.dirname(__FILE__), 'engines/rails_extensions/rails')
|
Chris@0
|
8
|
Chris@0
|
9 # == Parameters
|
Chris@0
|
10 #
|
Chris@0
|
11 # The Engines module has a number of public configuration parameters:
|
Chris@0
|
12 #
|
Chris@0
|
13 # [+public_directory+] The directory into which plugin assets should be
|
Chris@0
|
14 # mirrored. Defaults to <tt>RAILS_ROOT/public/plugin_assets</tt>.
|
Chris@0
|
15 # [+schema_info_table+] The table to use when storing plugin migration
|
Chris@0
|
16 # version information. Defaults to +plugin_schema_info+.
|
Chris@0
|
17 #
|
Chris@0
|
18 # Additionally, there are a few flags which control the behaviour of
|
Chris@0
|
19 # some of the features the engines plugin adds to Rails:
|
Chris@0
|
20 #
|
Chris@0
|
21 # [+disable_application_view_loading+] A boolean flag determining whether
|
Chris@0
|
22 # or not views should be loaded from
|
Chris@0
|
23 # the main <tt>app/views</tt> directory.
|
Chris@0
|
24 # Defaults to false; probably only
|
Chris@0
|
25 # useful when testing your plugin.
|
Chris@0
|
26 # [+disable_application_code_loading+] A boolean flag determining whether
|
Chris@0
|
27 # or not to load controllers/helpers
|
Chris@0
|
28 # from the main +app+ directory,
|
Chris@0
|
29 # if corresponding code exists within
|
Chris@0
|
30 # a plugin. Defaults to false; again,
|
Chris@0
|
31 # probably only useful when testing
|
Chris@0
|
32 # your plugin.
|
Chris@0
|
33 # [+disable_code_mixing+] A boolean flag indicating whether all plugin
|
Chris@0
|
34 # copies of a particular controller/helper should
|
Chris@0
|
35 # be loaded and allowed to override each other,
|
Chris@0
|
36 # or if the first matching file should be loaded
|
Chris@0
|
37 # instead. Defaults to false.
|
Chris@0
|
38 #
|
Chris@0
|
39 module Engines
|
Chris@0
|
40 # The set of all loaded plugins
|
Chris@0
|
41 mattr_accessor :plugins
|
Chris@0
|
42 self.plugins = Engines::Plugin::List.new
|
Chris@0
|
43
|
Chris@0
|
44 # List of extensions to load, can be changed in init.rb before calling Engines.init
|
Chris@0
|
45 mattr_accessor :rails_extensions
|
Chris@0
|
46 self.rails_extensions = %w(asset_helpers form_tag_helpers migrations dependencies)
|
Chris@0
|
47
|
Chris@0
|
48 # The name of the public directory to mirror public engine assets into.
|
Chris@0
|
49 # Defaults to <tt>RAILS_ROOT/public/plugin_assets</tt>.
|
Chris@0
|
50 mattr_accessor :public_directory
|
Chris@0
|
51 self.public_directory = File.join(RAILS_ROOT, 'public', 'plugin_assets')
|
Chris@0
|
52
|
Chris@0
|
53 # The table in which to store plugin schema information. Defaults to
|
Chris@0
|
54 # "plugin_schema_info".
|
Chris@0
|
55 mattr_accessor :schema_info_table
|
Chris@0
|
56 self.schema_info_table = "plugin_schema_info"
|
Chris@0
|
57
|
Chris@0
|
58 #--
|
Chris@0
|
59 # These attributes control the behaviour of the engines extensions
|
Chris@0
|
60 #++
|
Chris@0
|
61
|
Chris@0
|
62 # Set this to true if views should *only* be loaded from plugins
|
Chris@0
|
63 mattr_accessor :disable_application_view_loading
|
Chris@0
|
64 self.disable_application_view_loading = false
|
Chris@0
|
65
|
Chris@0
|
66 # Set this to true if controller/helper code shouldn't be loaded
|
Chris@0
|
67 # from the application
|
Chris@0
|
68 mattr_accessor :disable_application_code_loading
|
Chris@0
|
69 self.disable_application_code_loading = false
|
Chris@0
|
70
|
Chris@0
|
71 # Set this to true if code should not be mixed (i.e. it will be loaded
|
Chris@0
|
72 # from the first valid path on $LOAD_PATH)
|
Chris@0
|
73 mattr_accessor :disable_code_mixing
|
Chris@0
|
74 self.disable_code_mixing = false
|
Chris@0
|
75
|
Chris@0
|
76 # This is used to determine which files are candidates for the "code
|
Chris@0
|
77 # mixing" feature that the engines plugin provides, where classes from
|
Chris@0
|
78 # plugins can be loaded, and then code from the application loaded
|
Chris@0
|
79 # on top of that code to override certain methods.
|
Chris@0
|
80 mattr_accessor :code_mixing_file_types
|
Chris@0
|
81 self.code_mixing_file_types = %w(controller helper)
|
Chris@0
|
82
|
Chris@0
|
83 class << self
|
Chris@0
|
84 def init(initializer)
|
Chris@0
|
85 load_extensions
|
Chris@0
|
86 Engines::Assets.initialize_base_public_directory
|
Chris@0
|
87 end
|
Chris@0
|
88
|
Chris@0
|
89 def logger
|
Chris@0
|
90 RAILS_DEFAULT_LOGGER
|
Chris@0
|
91 end
|
Chris@0
|
92
|
Chris@0
|
93 def load_extensions
|
Chris@0
|
94 rails_extensions.each { |name| require "engines/rails_extensions/#{name}" }
|
Chris@0
|
95 # load the testing extensions, if we are in the test environment.
|
Chris@0
|
96 require "engines/testing" if RAILS_ENV == "test"
|
Chris@0
|
97 end
|
Chris@0
|
98
|
Chris@0
|
99 def select_existing_paths(paths)
|
Chris@0
|
100 paths.select { |path| File.directory?(path) }
|
Chris@0
|
101 end
|
Chris@0
|
102
|
Chris@0
|
103 # The engines plugin will, by default, mix code from controllers and helpers,
|
Chris@0
|
104 # allowing application code to override specific methods in the corresponding
|
Chris@0
|
105 # controller or helper classes and modules. However, if other file types should
|
Chris@0
|
106 # also be mixed like this, they can be added by calling this method. For example,
|
Chris@0
|
107 # if you want to include "things" within your plugin and override them from
|
Chris@0
|
108 # your applications, you should use the following layout:
|
Chris@0
|
109 #
|
Chris@0
|
110 # app/
|
Chris@0
|
111 # +-- things/
|
Chris@0
|
112 # | +-- one_thing.rb
|
Chris@0
|
113 # | +-- another_thing.rb
|
Chris@0
|
114 # ...
|
Chris@0
|
115 # vendor/
|
Chris@0
|
116 # +-- plugins/
|
Chris@0
|
117 # +-- my_plugin/
|
Chris@0
|
118 # +-- app/
|
Chris@0
|
119 # +-- things/
|
Chris@0
|
120 # +-- one_thing.rb
|
Chris@0
|
121 # +-- another_thing.rb
|
Chris@0
|
122 #
|
Chris@0
|
123 # The important point here is that your "things" are named <whatever>_thing.rb,
|
Chris@0
|
124 # and that they are placed within plugin/app/things (the pluralized form of 'thing').
|
Chris@0
|
125 #
|
Chris@0
|
126 # It's important to note that you'll also want to ensure that the "things" are
|
Chris@0
|
127 # on your load path by including them in Rails load path mechanism, e.g. in init.rb:
|
Chris@0
|
128 #
|
Chris@0
|
129 # ActiveSupport::Dependencies.load_paths << File.join(File.dirname(__FILE__), 'app', 'things'))
|
Chris@0
|
130 #
|
Chris@0
|
131 def mix_code_from(*types)
|
Chris@0
|
132 self.code_mixing_file_types += types.map { |x| x.to_s.singularize }
|
Chris@0
|
133 end
|
Chris@0
|
134
|
Chris@0
|
135 # A general purpose method to mirror a directory (+source+) into a destination
|
Chris@0
|
136 # directory, including all files and subdirectories. Files will not be mirrored
|
Chris@0
|
137 # if they are identical already (checked via FileUtils#identical?).
|
Chris@0
|
138 def mirror_files_from(source, destination)
|
Chris@0
|
139 return unless File.directory?(source)
|
Chris@0
|
140
|
Chris@0
|
141 # TODO: use Rake::FileList#pathmap?
|
Chris@0
|
142 source_files = Dir[source + "/**/*"]
|
Chris@0
|
143 source_dirs = source_files.select { |d| File.directory?(d) }
|
Chris@0
|
144 source_files -= source_dirs
|
Chris@0
|
145
|
Chris@0
|
146 unless source_files.empty?
|
Chris@0
|
147 base_target_dir = File.join(destination, File.dirname(source_files.first).gsub(source, ''))
|
Chris@0
|
148 FileUtils.mkdir_p(base_target_dir)
|
Chris@0
|
149 end
|
Chris@0
|
150
|
Chris@0
|
151 source_dirs.each do |dir|
|
Chris@0
|
152 # strip down these paths so we have simple, relative paths we can
|
Chris@0
|
153 # add to the destination
|
Chris@0
|
154 target_dir = File.join(destination, dir.gsub(source, ''))
|
Chris@0
|
155 begin
|
Chris@0
|
156 FileUtils.mkdir_p(target_dir)
|
Chris@0
|
157 rescue Exception => e
|
Chris@0
|
158 raise "Could not create directory #{target_dir}: \n" + e
|
Chris@0
|
159 end
|
Chris@0
|
160 end
|
Chris@0
|
161
|
Chris@0
|
162 source_files.each do |file|
|
Chris@0
|
163 begin
|
Chris@0
|
164 target = File.join(destination, file.gsub(source, ''))
|
Chris@0
|
165 unless File.exist?(target) && FileUtils.identical?(file, target)
|
Chris@0
|
166 FileUtils.cp(file, target)
|
Chris@0
|
167 end
|
Chris@0
|
168 rescue Exception => e
|
Chris@0
|
169 raise "Could not copy #{file} to #{target}: \n" + e
|
Chris@0
|
170 end
|
Chris@0
|
171 end
|
Chris@0
|
172 end
|
Chris@0
|
173 end
|
Chris@0
|
174 end
|