Class: WebServerConfigGenerator::ProjectDirectory
- Inherits:
-
Pathname
- Object
- Pathname
- Pathname
- WebServerConfigGenerator::ProjectDirectory
show all
- Defined in:
- lib/web_server_config_generator/project_directory.rb
Constant Summary
collapse
- EXAMPLE_TEXT =
"# This YAML file describes a hash of the web server configuration for this project.\n# here's an example:\n#\n# --- \n# :test: \n# :port: 46677\n# :server_names: \n# - app-test.local\n# :development: \n# :port: 43273\n# :relative_root_url: /foo\n# :relative_root_url_root: true\n# :server_names: \n# - sub-uri-app-foo-development.local\n# - sub-uri-apps-development.local\n# :production: \n# :port: 46767\n# :server_names: \n# - app-production.local\n#\n# Visiting app-test.local will load the app in the test environment.\n# Each vost that is configured will listen on all hostnames at the port specified, as well\n# as port 80 on all the hostnames specfied. In the above example accessing\n# \"http://localhost:46767\" will go to the app in production mode, as well as\n# \"http://app-production.local\".\n#\n# You can also see how to setup relative_root_url apps here in the development section.\n# All apps that share a server name and have relative_root_url specified will be setup for relative root access.\n# Say, for example, another app had the following config:\n#\n# --- \n# :development: \n# :port: 44893\n# :relative_root_url: /bar\n# :server_names: \n# - sub-uri-app-bar-development.local\n# - sub-uri-apps-development.local\n#\n# Since these two apps share the server name \"sub-uri-apps-development.local\" and have relative_root_url\n# specified they will be configured so that accessing \"http://sub-uri-apps-development.local/foo\"\n# goes to the first app and accessing \"http://sub-uri-apps-development.local/bar\" goes to the second.\n# In addition, by specifying relative_root_url_root for the foo app you ca visit\n# \"http://sub-uri-apps-development.local/\" and you will access the foo app.\n\n\n"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Pathname
#+, #mkpath, #read, #write
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
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
116
117
118
119
120
|
# File 'lib/web_server_config_generator/project_directory.rb', line 116
def
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
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/web_server_config_generator/project_directory.rb', line 89
def default_webconfig_options
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
81
82
83
|
# File 'lib/web_server_config_generator/project_directory.rb', line 81
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 expand_path
Pathname.new(self).expand_path
end
|
#generate_conf_file_contents(options) ⇒ Object
187
188
189
190
191
192
193
194
195
|
# File 'lib/web_server_config_generator/project_directory.rb', line 187
def generate_conf_file_contents(options)
env = options[:env].to_sym
port = project_webconfig[env][:port]
full_path_to_dir = File.expand_path "#{@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 => options[:env]).contents
end
|
#generate_port_from_env(env) ⇒ Object
146
147
148
149
150
|
# File 'lib/web_server_config_generator/project_directory.rb', line 146
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
137
138
139
140
141
142
143
144
|
# File 'lib/web_server_config_generator/project_directory.rb', line 137
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
85
86
87
|
# File 'lib/web_server_config_generator/project_directory.rb', line 85
def project_name
self.basename.to_s.gsub(/[^[:alnum:]]/, '-').squeeze('-').gsub(/(^-|-$)/, '').downcase
end
|
#project_webconfig ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/web_server_config_generator/project_directory.rb', line 100
def project_webconfig
@project_webconfig ||=
begin
config = default_webconfig_options.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
end
project_webconfig_proc(config)
end
end
|
#project_webconfig_path ⇒ Object
133
134
135
|
# File 'lib/web_server_config_generator/project_directory.rb', line 133
def project_webconfig_path
File.join(self, ".webconfig.yml")
end
|
#project_webconfig_proc(config) ⇒ Object
126
127
128
129
130
131
|
# File 'lib/web_server_config_generator/project_directory.rb', line 126
def project_webconfig_proc(config)
lambda do |env|
config.dup.merge(config[env] || {})
end
end
|
#projects_relative_project_path ⇒ Object
160
161
162
|
# File 'lib/web_server_config_generator/project_directory.rb', line 160
def projects_relative_project_path
File.expand_path(self).sub(File.expand_path(@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
175
176
177
|
# File 'lib/web_server_config_generator/project_directory.rb', line 175
def relative_root_url_for_env(env)
project_webconfig[env.to_sym][:relative_root_url]
end
|
#relative_root_url_root_for_env(env) ⇒ Object
179
180
181
|
# File 'lib/web_server_config_generator/project_directory.rb', line 179
def relative_root_url_root_for_env(env)
project_webconfig[env.to_sym][:relative_root_url_root]
end
|
#save_project_webconfig(config) ⇒ Object
122
123
124
|
# File 'lib/web_server_config_generator/project_directory.rb', line 122
def save_project_webconfig(config)
File.open(project_webconfig_path, "w") { |f| f.write config.to_yaml }
end
|
#server_name_env_pairs ⇒ Object
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/web_server_config_generator/project_directory.rb', line 164
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
152
153
154
|
# File 'lib/web_server_config_generator/project_directory.rb', line 152
def server_name_from_env(env)
"#{self.project_name}-#{env}.local"
end
|
#server_names ⇒ Object
156
157
158
|
# File 'lib/web_server_config_generator/project_directory.rb', line 156
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
183
184
185
|
# File 'lib/web_server_config_generator/project_directory.rb', line 183
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
|