Class: WebServerConfigGenerator::ProjectDirectory
- Defined in:
- lib/web_server_config_generator/project_directory.rb
Constant Summary collapse
- EXAMPLE_TEXT =
<<-EXAMPLE # This YAML file describes a hash of the web server configuration for this project. # here's an example: # # --- # :test: # :port: 46677 # :server_names: # - app-test.local # :development: # :port: 43273 # :relative_root_url: /foo # :relative_root_url_root: true # :server_names: # - sub-uri-app-foo-development.local # - sub-uri-apps-development.local # :production: # :port: 46767 # :server_names: # - app-production.local # # Visiting app-test.local will load the app in the test environment. # Each vost that is configured will listen on all hostnames at the port specified, as well # as port 80 on all the hostnames specfied. In the above example accessing # "http://localhost:46767" will go to the app in production mode, as well as # "http://app-production.local". # # You can also see how to setup relative_root_url apps here in the development section. # All apps that share a server name and have relative_root_url specified will be setup for relative root access. # Say, for example, another app had the following config: # # --- # :development: # :port: 44893 # :relative_root_url: /bar # :server_names: # - sub-uri-app-bar-development.local # - sub-uri-apps-development.local # # Since these two apps share the server name "sub-uri-apps-development.local" and have relative_root_url # specified they will be configured so that accessing "http://sub-uri-apps-development.local/foo" # goes to the first app and accessing "http://sub-uri-apps-development.local/bar" goes to the second. # In addition, by specifying relative_root_url_root for the foo app you ca visit # "http://sub-uri-apps-development.local/" and you will access the foo app. EXAMPLE
Instance Attribute Summary collapse
-
#original_realpath ⇒ Object
readonly
Returns the value of attribute original_realpath.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #add_example_comment_to_config ⇒ Object
- #basename ⇒ Object
- #default_webconfig_options ⇒ Object
- #environments ⇒ Object
- #eql?(obj) ⇒ Boolean (also: #==)
- #expand_path ⇒ Object
- #generate_conf_file_contents(options) ⇒ Object
- #generate_port_from_env(env) ⇒ Object
-
#initialize(dir, web_config_generator) ⇒ ProjectDirectory
constructor
A new instance of ProjectDirectory.
- #path ⇒ Object
- #project_config_files_contents ⇒ Object
- #project_name ⇒ Object
- #project_webconfig ⇒ Object
- #project_webconfig_path ⇒ Object
- #project_webconfig_proc(config) ⇒ Object
- #projects_relative_project_path ⇒ Object
- #realpath ⇒ Object
- #relative_root_url_for_env(env) ⇒ Object
- #relative_root_url_root_for_env(env) ⇒ Object
- #save_project_webconfig(config) ⇒ Object
- #server_name_env_pairs ⇒ Object
- #server_name_from_env(env) ⇒ Object
- #server_names ⇒ Object
- #server_names_without_sub_uri_apps_for_env(env) ⇒ Object
Methods inherited from Pathname
Constructor Details
#initialize(dir, web_config_generator) ⇒ ProjectDirectory
Returns a new instance of ProjectDirectory.
53 54 55 56 57 |
# File 'lib/web_server_config_generator/project_directory.rb', line 53 def initialize(dir, web_config_generator) @web_config_generator = web_config_generator super(dir) @original_realpath = self.realpath end |
Instance Attribute Details
#original_realpath ⇒ Object (readonly)
Returns the value of attribute original_realpath.
51 52 53 |
# File 'lib/web_server_config_generator/project_directory.rb', line 51 def original_realpath @original_realpath end |
Instance Method Details
#+(other) ⇒ Object
81 82 83 84 |
# File 'lib/web_server_config_generator/project_directory.rb', line 81 def +(other) other = self.class.new(other) unless self.class === other self.class.new(plus(@path, other.to_s)) end |
#add_example_comment_to_config ⇒ Object
121 122 123 124 125 |
# File 'lib/web_server_config_generator/project_directory.rb', line 121 def add_example_comment_to_config old_contents = File.read(project_webconfig_path) new_contents = EXAMPLE_TEXT + old_contents File.open(project_webconfig_path, "w") { |f| f.write new_contents } end |
#basename ⇒ Object
67 68 69 |
# File 'lib/web_server_config_generator/project_directory.rb', line 67 def basename Pathname.new(self).basename end |
#default_webconfig_options ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/web_server_config_generator/project_directory.rb', line 94 def opts = {} environments.each do |env| opts[env.to_sym] = { :port => self.generate_port_from_env(env), :server_names => [self.server_name_from_env(env)], } end opts end |
#environments ⇒ Object
86 87 88 |
# File 'lib/web_server_config_generator/project_directory.rb', line 86 def environments Dir[path.to_s + "/config/environments/*.rb"].map { |p| File.basename(p).gsub(/\.rb/, '') } end |
#eql?(obj) ⇒ Boolean Also known as: ==
75 76 77 78 |
# File 'lib/web_server_config_generator/project_directory.rb', line 75 def eql?(obj) return false unless ProjectDirectory === obj self.original_realpath == obj.original_realpath end |
#expand_path ⇒ Object
71 72 73 |
# File 'lib/web_server_config_generator/project_directory.rb', line 71 def Pathname.new(self). end |
#generate_conf_file_contents(options) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/web_server_config_generator/project_directory.rb', line 192 def generate_conf_file_contents() env = [:env].to_sym port = project_webconfig[env][:port] full_path_to_dir = File. "#{@web_config_generator.web_server_links_dir}/#{env}/#{projects_relative_project_path}" root = "#{full_path_to_dir}/public" NginxConf.new(:port => port, :server_names => server_names_without_sub_uri_apps_for_env(env), :root => root, :environment => [:env]).contents end |
#generate_port_from_env(env) ⇒ Object
151 152 153 154 155 |
# File 'lib/web_server_config_generator/project_directory.rb', line 151 def generate_port_from_env(env) config = self.project_config_files_contents pseudo_random_number = Digest::SHA1.hexdigest(config + env.to_s).hex WebServerConfigGenerator::STARTING_PORT + (pseudo_random_number % WebServerConfigGenerator::PORT_POOL_SIZE) end |
#path ⇒ Object
59 60 61 |
# File 'lib/web_server_config_generator/project_directory.rb', line 59 def path self end |
#project_config_files_contents ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/web_server_config_generator/project_directory.rb', line 142 def project_config_files_contents config_contents = "" config_dir = File.join(self, "config") Find.find(config_dir) do |path| config_contents << File.open(path) { |f| f.read } if File.file? path end config_contents end |
#project_name ⇒ Object
90 91 92 |
# File 'lib/web_server_config_generator/project_directory.rb', line 90 def project_name self.basename.to_s.gsub(/[^[:alnum:]]/, '-').squeeze('-').gsub(/(^-|-$)/, '').downcase end |
#project_webconfig ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/web_server_config_generator/project_directory.rb', line 105 def project_webconfig @project_webconfig ||= begin config = .merge(File.exist?(self.project_webconfig_path) ? YAML.load_file(self.project_webconfig_path) : {}) unless File.exist?(self.project_webconfig_path) save_project_webconfig config add_example_comment_to_config end project_webconfig_proc(config) end end |
#project_webconfig_path ⇒ Object
138 139 140 |
# File 'lib/web_server_config_generator/project_directory.rb', line 138 def project_webconfig_path File.join(self, ".webconfig.yml") end |
#project_webconfig_proc(config) ⇒ Object
131 132 133 134 135 136 |
# File 'lib/web_server_config_generator/project_directory.rb', line 131 def project_webconfig_proc(config) lambda do |env| # lets us have top level defaults in the config, and merge those under env specific options config.dup.merge(config[env] || {}) end end |
#projects_relative_project_path ⇒ Object
165 166 167 |
# File 'lib/web_server_config_generator/project_directory.rb', line 165 def projects_relative_project_path File.(self).sub(File.(@web_config_generator.projects_dir), '') end |
#realpath ⇒ Object
63 64 65 |
# File 'lib/web_server_config_generator/project_directory.rb', line 63 def realpath Pathname.new(self).realpath end |
#relative_root_url_for_env(env) ⇒ Object
180 181 182 |
# File 'lib/web_server_config_generator/project_directory.rb', line 180 def relative_root_url_for_env(env) project_webconfig[env.to_sym][:relative_root_url] end |
#relative_root_url_root_for_env(env) ⇒ Object
184 185 186 |
# File 'lib/web_server_config_generator/project_directory.rb', line 184 def relative_root_url_root_for_env(env) project_webconfig[env.to_sym][:relative_root_url_root] end |
#save_project_webconfig(config) ⇒ Object
127 128 129 |
# File 'lib/web_server_config_generator/project_directory.rb', line 127 def save_project_webconfig(config) File.open(project_webconfig_path, "w") { |f| f.write config.to_yaml } end |
#server_name_env_pairs ⇒ Object
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/web_server_config_generator/project_directory.rb', line 169 def server_name_env_pairs pairs = [] environments.each do |env| env = env.to_sym project_webconfig[env][:server_names].each do |h| pairs << [h, env] end end pairs end |
#server_name_from_env(env) ⇒ Object
157 158 159 |
# File 'lib/web_server_config_generator/project_directory.rb', line 157 def server_name_from_env(env) "#{self.project_name}-#{env}.local" end |
#server_names ⇒ Object
161 162 163 |
# File 'lib/web_server_config_generator/project_directory.rb', line 161 def server_names environments.map { |env| project_webconfig[env.to_sym][:server_names] }.flatten end |
#server_names_without_sub_uri_apps_for_env(env) ⇒ Object
188 189 190 |
# File 'lib/web_server_config_generator/project_directory.rb', line 188 def server_names_without_sub_uri_apps_for_env(env) project_webconfig[env][:server_names].reject { |n| @web_config_generator.sub_uri_projects.map { |p| p.server_names }.flatten.include? n } end |