@@ -1,132 +1,128 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2014 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | module Redmine |
|
18 | module Redmine | |
19 | module Configuration |
|
19 | module Configuration | |
20 |
|
20 | |||
21 | # Configuration default values |
|
21 | # Configuration default values | |
22 | @defaults = { |
|
22 | @defaults = { | |
23 | 'email_delivery' => nil, |
|
23 | 'email_delivery' => nil, | |
24 | 'max_concurrent_ajax_uploads' => 2 |
|
24 | 'max_concurrent_ajax_uploads' => 2 | |
25 | } |
|
25 | } | |
26 |
|
26 | |||
27 | @config = nil |
|
27 | @config = nil | |
28 |
|
28 | |||
29 | class << self |
|
29 | class << self | |
30 | # Loads the Redmine configuration file |
|
30 | # Loads the Redmine configuration file | |
31 | # Valid options: |
|
31 | # Valid options: | |
32 | # * <tt>:file</tt>: the configuration file to load (default: config/configuration.yml) |
|
32 | # * <tt>:file</tt>: the configuration file to load (default: config/configuration.yml) | |
33 | # * <tt>:env</tt>: the environment to load the configuration for (default: Rails.env) |
|
33 | # * <tt>:env</tt>: the environment to load the configuration for (default: Rails.env) | |
34 | def load(options={}) |
|
34 | def load(options={}) | |
35 | filename = options[:file] || File.join(Rails.root, 'config', 'configuration.yml') |
|
35 | filename = options[:file] || File.join(Rails.root, 'config', 'configuration.yml') | |
36 | env = options[:env] || Rails.env |
|
36 | env = options[:env] || Rails.env | |
37 |
|
37 | |||
38 | @config = @defaults.dup |
|
38 | @config = @defaults.dup | |
39 |
|
39 | |||
40 | load_deprecated_email_configuration(env) |
|
40 | load_deprecated_email_configuration(env) | |
41 | if File.file?(filename) |
|
41 | if File.file?(filename) | |
42 | @config.merge!(load_from_yaml(filename, env)) |
|
42 | @config.merge!(load_from_yaml(filename, env)) | |
43 | end |
|
43 | end | |
44 |
|
44 | |||
45 | # Compatibility mode for those who copy email.yml over configuration.yml |
|
45 | # Compatibility mode for those who copy email.yml over configuration.yml | |
46 | %w(delivery_method smtp_settings sendmail_settings).each do |key| |
|
46 | %w(delivery_method smtp_settings sendmail_settings).each do |key| | |
47 | if value = @config.delete(key) |
|
47 | if value = @config.delete(key) | |
48 | @config['email_delivery'] ||= {} |
|
48 | @config['email_delivery'] ||= {} | |
49 | @config['email_delivery'][key] = value |
|
49 | @config['email_delivery'][key] = value | |
50 | end |
|
50 | end | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | if @config['email_delivery'] |
|
53 | if @config['email_delivery'] | |
54 | ActionMailer::Base.perform_deliveries = true |
|
54 | ActionMailer::Base.perform_deliveries = true | |
55 | @config['email_delivery'].each do |k, v| |
|
55 | @config['email_delivery'].each do |k, v| | |
56 | v.symbolize_keys! if v.respond_to?(:symbolize_keys!) |
|
56 | v.symbolize_keys! if v.respond_to?(:symbolize_keys!) | |
57 | ActionMailer::Base.send("#{k}=", v) |
|
57 | ActionMailer::Base.send("#{k}=", v) | |
58 | end |
|
58 | end | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | check_regular_expressions |
|
61 | check_regular_expressions | |
62 | @config |
|
62 | @config | |
63 | end |
|
63 | end | |
64 |
|
64 | |||
65 | # Returns a configuration setting |
|
65 | # Returns a configuration setting | |
66 | def [](name) |
|
66 | def [](name) | |
67 | load unless @config |
|
67 | load unless @config | |
68 | @config[name] |
|
68 | @config[name] | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | # Yields a block with the specified hash configuration settings |
|
71 | # Yields a block with the specified hash configuration settings | |
72 | def with(settings) |
|
72 | def with(settings) | |
73 | settings.stringify_keys! |
|
73 | settings.stringify_keys! | |
74 | load unless @config |
|
74 | load unless @config | |
75 | was = settings.keys.inject({}) {|h,v| h[v] = @config[v]; h} |
|
75 | was = settings.keys.inject({}) {|h,v| h[v] = @config[v]; h} | |
76 | @config.merge! settings |
|
76 | @config.merge! settings | |
77 | yield if block_given? |
|
77 | yield if block_given? | |
78 | @config.merge! was |
|
78 | @config.merge! was | |
79 | end |
|
79 | end | |
80 |
|
80 | |||
81 | private |
|
81 | private | |
82 |
|
82 | |||
83 | def load_from_yaml(filename, env) |
|
83 | def load_from_yaml(filename, env) | |
84 | yaml = nil |
|
84 | yaml = nil | |
85 | begin |
|
85 | begin | |
86 | yaml = YAML::load(ERB.new(File.read(filename)).result) |
|
86 | yaml = YAML::load(ERB.new(File.read(filename)).result) | |
87 | rescue ArgumentError |
|
87 | rescue ArgumentError | |
88 |
|
|
88 | abort "Your Redmine configuration file located at #{filename} is not a valid YAML file and could not be loaded." | |
89 | exit 1 |
|
|||
90 | rescue SyntaxError => e |
|
89 | rescue SyntaxError => e | |
91 |
|
|
90 | abort "A syntax error occurred when parsing your Redmine configuration file located at #{filename} with ERB:\n#{e.message}" | |
92 | exit 1 |
|
|||
93 | end |
|
91 | end | |
94 | conf = {} |
|
92 | conf = {} | |
95 | if yaml.is_a?(Hash) |
|
93 | if yaml.is_a?(Hash) | |
96 | if yaml['default'] |
|
94 | if yaml['default'] | |
97 | conf.merge!(yaml['default']) |
|
95 | conf.merge!(yaml['default']) | |
98 | end |
|
96 | end | |
99 | if yaml[env] |
|
97 | if yaml[env] | |
100 | conf.merge!(yaml[env]) |
|
98 | conf.merge!(yaml[env]) | |
101 | end |
|
99 | end | |
102 | else |
|
100 | else | |
103 |
|
|
101 | abort "Your Redmine configuration file located at #{filename} is not a valid Redmine configuration file." | |
104 | exit 1 |
|
|||
105 | end |
|
102 | end | |
106 | conf |
|
103 | conf | |
107 | end |
|
104 | end | |
108 |
|
105 | |||
109 | def load_deprecated_email_configuration(env) |
|
106 | def load_deprecated_email_configuration(env) | |
110 | deprecated_email_conf = File.join(Rails.root, 'config', 'email.yml') |
|
107 | deprecated_email_conf = File.join(Rails.root, 'config', 'email.yml') | |
111 | if File.file?(deprecated_email_conf) |
|
108 | if File.file?(deprecated_email_conf) | |
112 | warn "Storing outgoing emails configuration in config/email.yml is deprecated. You should now store it in config/configuration.yml using the email_delivery setting." |
|
109 | warn "Storing outgoing emails configuration in config/email.yml is deprecated. You should now store it in config/configuration.yml using the email_delivery setting." | |
113 | @config.merge!({'email_delivery' => load_from_yaml(deprecated_email_conf, env)}) |
|
110 | @config.merge!({'email_delivery' => load_from_yaml(deprecated_email_conf, env)}) | |
114 | end |
|
111 | end | |
115 | end |
|
112 | end | |
116 |
|
113 | |||
117 | # Checks the validness of regular expressions set for repository paths at startup |
|
114 | # Checks the validness of regular expressions set for repository paths at startup | |
118 | def check_regular_expressions |
|
115 | def check_regular_expressions | |
119 | @config.each do |name, value| |
|
116 | @config.each do |name, value| | |
120 | if value.present? && name =~ /^scm_.+_path_regexp$/ |
|
117 | if value.present? && name =~ /^scm_.+_path_regexp$/ | |
121 | begin |
|
118 | begin | |
122 | Regexp.new value.to_s.strip |
|
119 | Regexp.new value.to_s.strip | |
123 | rescue => e |
|
120 | rescue => e | |
124 |
|
|
121 | abort "Invalid regular expression set as #{name} setting in your Redmine configuration file:\n#{e.message}" | |
125 | exit 1 |
|
|||
126 | end |
|
122 | end | |
127 | end |
|
123 | end | |
128 | end |
|
124 | end | |
129 | end |
|
125 | end | |
130 | end |
|
126 | end | |
131 | end |
|
127 | end | |
132 | end |
|
128 | end |
General Comments 0
You need to be logged in to leave comments.
Login now