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