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