fanforce-cli 1.7.1 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,205 +0,0 @@
1
- class Fanforce::CLI
2
- require 'fileutils'
3
-
4
- def setup_files(app)
5
- if File.directory?("#{app.dir}/tmp")
6
- puts "#{'Found '.format(:green,:bold)} #{app.dir_name}/tmp/"
7
- else
8
- Dir.mkdir("#{app.dir}/tmp")
9
- puts "#{'Created'.format(:green,:bold)} #{app.dir_name}/tmp/"
10
- end
11
-
12
- if File.exists?("#{app.dir}/config.ru")
13
- app.update_file(:config_ru)
14
- puts "#{'Updated'.format(:green,:bold)} #{app.dir_name}/config.ru"
15
- else
16
- app.create_file(:config_ru)
17
- puts "#{'Created'.format(:green,:bold)} #{app.dir_name}/config.ru"
18
- end
19
-
20
- if File.exists?("#{app.dir}/.gitignore")
21
- app.update_file(:gitignore)
22
- puts "#{'Updated'.format(:green,:bold)} #{app.dir_name}/.gitignore"
23
- else
24
- app.create_file(:gitignore)
25
- puts "#{'Created'.format(:green,:bold)} #{app.dir_name}/.gitignore"
26
- end
27
-
28
- if File.exists?("#{app.dir}/Gemfile")
29
- app.update_file(:gemfile)
30
- puts "#{'Updated'.format(:green,:bold)} #{app.dir_name}/Gemfile"
31
- else
32
- app.create_file(:gemfile)
33
- puts "#{'Created'.format(:green,:bold)} #{app.dir_name}/Gemfile"
34
- end
35
-
36
- if File.exists?("#{app.dir}/Rakefile")
37
- app.update_file(:rakefile)
38
- puts "#{'Updated'.format(:green,:bold)} #{app.dir_name}/Rakefile"
39
- else
40
- app.create_file(:rakefile)
41
- puts "#{'Created'.format(:green,:bold)} #{app.dir_name}/Rakefile"
42
- end
43
-
44
- restart(app, :development)
45
- end
46
-
47
- def setup_envs(app, environment)
48
- environments = environment == :all ? [:development, :staging, :production] : [environment]
49
-
50
- if !File.exists?("#{$HomeDir}/.env/_bind.yml")
51
- return puts "#{'Oops'.format(:bold)}... you must setup .env/_bind.yml before trying to update env variables."
52
- end
53
-
54
- environments.each do |environment|
55
- vars = Env.vars_by_app(environment)[app.dir_name]
56
- has_workers = File.directory?("#{app.dir}/workers") ? true : false
57
-
58
- next puts "#{'Skipped'.format(:bold)} #{environment.to_s.titleize} has 0 env variables" if vars.blank? or vars.size == 0
59
- puts "#{'Updated'.format(:green,:bold)} #{environment.to_s.titleize}#{has_workers ? '... and workers have' : ' has'} #{vars.size} env variables"
60
-
61
- push_env_to(environment, app, vars)
62
- end
63
- end
64
-
65
- def setup_pow(app)
66
- Run.pow_create(app, app.root_domain)
67
- Run.pow_create(app, Fanforce.default_short_domain)
68
- end
69
-
70
- def setup_local_git(app)
71
- if Run.git_init.include?('Reinitialized')
72
- puts "#{'Found '.format(:green,:bold)}" + 'local repository'
73
- else
74
- puts "#{'Initialized '.format(:green,:bold)}" + 'local repository'
75
- Run.git_add
76
- Run.git_first_commit
77
- puts "#{'Created '.format(:green,:bold)}" + 'initial fanforce commit'
78
- end
79
- end
80
-
81
- def setup_bitbucket(app)
82
- bitbucket = BitBucket.new(login: $Config[:bitbucket][:user], password: $Config[:bitbucket][:password])
83
- begin
84
- find_bitbucket_repo(bitbucket, app.dir_name)
85
- "#{'Found '.format(:green,:bold)}" + "#{app.dir_name} repository already exists on bitbucket"
86
- rescue
87
- create_bitbucket_repo(bitbucket, app.dir_name)
88
- puts "#{'Created '.format(:green,:bold)}" + "#{app.dir_name} repository on bitbucket"
89
- end
90
-
91
- if (`git remote`).split(/\r?\n/).include?($Config[:bitbucket][:user])
92
- puts "#{'Updated '.format(:green,:bold)}" + "git remote for #{$Config[:bitbucket][:user]}"
93
- `git remote rm #{$Config[:bitbucket][:user]}`
94
- elsif (`git remote`).split(/\r?\n/).include?('bitbucket')
95
- `git remote rm bitbucket`
96
- puts "#{'Removed '.format(:red,:bold)}" + "git remote for #{$Config[:bitbucket][:user]}"
97
- puts "#{'Created '.format(:green,:bold)}" + "git remote for bitbucket"
98
- else
99
- puts "#{'Created '.format(:green,:bold)}" + "git remote for bitbucket"
100
- end
101
- `git remote add bitbucket git@bitbucket.org:#{$Config[:bitbucket][:user]}/#{app.dir_name}.git`
102
-
103
- puts "#{'Pushing '.format(:green,:bold)}" + "latest commit to #{$Config[:bitbucket][:user]}..."
104
- Run.command("git push bitbucket --all")
105
- end
106
-
107
- def find_bitbucket_repo(bitbucket, repo)
108
- bitbucket.repos.find($Config[:bitbucket][:user], repo)
109
- end
110
-
111
- def create_bitbucket_repo(bitbucket, repo)
112
- bitbucket.repos.create(name: repo, is_private: true)
113
- rescue BitBucket::Error::Unauthorized => e
114
- raise "Bitbucket user #{$Config[:bitbucket][:user]} does not seem to exist: #{e.message}"
115
- raise
116
- end
117
-
118
- def auth_heroku(environment)
119
- @heroku ||= {}
120
- @heroku[environment] ||= Heroku::API.new(:username => $Config[:heroku][environment][:user], :password => $Config[:heroku][environment][:password])
121
- rescue Exception => e
122
- raise "Heroku user #{$Config[:heroku][environment][:user]} does not seem to exist: #{e.message}" if e.response.status == 404
123
- raise
124
- end
125
-
126
- def setup_heroku(app, environment)
127
- return puts "OOPS... #{environment.to_s.upcase}".format(:red,:bold) + ' has not been setup for Heroku' if $Config[:heroku].blank? or $Config[:heroku][environment].blank?
128
-
129
- heroku = auth_heroku(environment)
130
- heroku_app_name = get_heroku_app_name(app, environment)
131
- begin
132
- heroku.get_app(heroku_app_name)
133
- puts "#{'Found '.format(:green,:bold)}" + "#{environment} app on heroku (#{heroku_app_name})"
134
- rescue
135
- heroku.post_app(name: heroku_app_name)
136
- puts "#{'Created '.format(:green,:bold)}" + "#{environment} on heroku (#{heroku_app_name})"
137
- end
138
-
139
- vars = Env.vars_by_app(environment)[app.dir_name]
140
- heroku.put_config_vars(heroku_app_name, vars) if vars
141
-
142
- # Setup standard app domain
143
- domain = "#{app._id}.#{Fanforce::DomainEnvironments.method(environment).call[:apps_base]}"
144
- domain_found = heroku.get_domains(heroku_app_name).body.inject(false) {|result, d| d['domain'] == domain ? (break true) : false }
145
- if domain_found
146
- puts "#{'Found '.format(:green,:bold)}" + "#{domain} domain on #{environment}"
147
- else
148
- heroku.post_domain(heroku_app_name, domain)
149
- puts "#{'Added '.format(:green,:bold)}" + "#{domain} domain to #{environment}"
150
- end
151
-
152
- # Setup default short domain
153
- domain = "#{app._id}.#{Fanforce::DomainEnvironments.method(environment).call[:default_short_domain]}"
154
- domain_found = heroku.get_domains(heroku_app_name).body.inject(false) {|result, d| d['domain'] == domain ? (break true) : false }
155
- if domain_found
156
- puts "#{'Found '.format(:green,:bold)}" + "#{domain} domain on #{environment}"
157
- else
158
- heroku.post_domain(heroku_app_name, domain)
159
- puts "#{'Added '.format(:green,:bold)}" + "#{domain} domain to #{environment}"
160
- end
161
-
162
- remote_name = "#{env(environment)}-heroku"
163
- if (`git remote`).split(/\r?\n/).include?(remote_name)
164
- puts "#{'Updated '.format(:green,:bold)}" + "git remote for #{remote_name}"
165
- `git remote rm #{remote_name}`
166
- else
167
- puts "#{'Created '.format(:green,:bold)}" + "git remote for #{remote_name}"
168
- end
169
- `git remote add #{remote_name} git@#{$Config[:heroku][environment][:git_ssh_domain] || 'heroku.com'}:#{heroku_app_name}.git`
170
-
171
- puts "#{'Pushing '.format(:green,:bold)}" + "latest commit to #{remote_name}..."
172
- Run.command("git push #{remote_name} master")
173
- end
174
-
175
- ######################################################################################################################
176
-
177
- def push_env_to(environment, app, vars, create_worker_env=true)
178
- vars.each {|k,v| puts " - #{k}" }
179
- workers_dir = "#{app.dir}/workers"
180
- workers_env_dir = "#{workers_dir}/.env"
181
- if create_worker_env and File.directory?(workers_dir) and vars['IRON_PROJECT_ID']
182
- FileUtils.mkdir_p(workers_env_dir) unless File.directory?(workers_env_dir)
183
- File.open("#{workers_env_dir}/#{environment}.rb", 'w') {|f| f.write convert_hash_to_ruby_env(vars) }
184
- end
185
- if environment == :development
186
- app.update_file(:powenv)
187
- File.open("#{app.dir}/.appenv", 'w') {|f| f.write convert_hash_to_shell_env(vars) }
188
- else
189
- return if $Config[:heroku][environment].blank?
190
- heroku = auth_heroku(environment)
191
- heroku_app_name = get_heroku_app_name(app, environment)
192
- heroku.put_config_vars(heroku_app_name, vars)
193
- end
194
- restart(app, environment)
195
- end
196
-
197
- def convert_hash_to_shell_env(hash)
198
- hash.inject('') {|script, (k,v)| script += "export #{k}=#{v.to_s.to_json}\n" }
199
- end
200
-
201
- def convert_hash_to_ruby_env(hash)
202
- hash.inject('') {|script, (k,v)| script += "ENV['#{k}']=#{v.to_s.to_json}\n" }
203
- end
204
-
205
- end
@@ -1,41 +0,0 @@
1
- class Fanforce::CLI::Env
2
- require 'singleton'
3
- require 'forwardable'
4
- include Singleton
5
-
6
- def load_vars_by_app(environment)
7
- vars = {}
8
- Fanforce::CLI::Apps.dir_names.each do |dir_name|
9
- vars[dir_name] ||= {}
10
- vars[dir_name]['FANFORCE_APP_ID'] = Fanforce::CLI::App.parse_dir_name(dir_name)[:_id]
11
- end
12
-
13
- raw_yaml = File.read("#{$HomeDir}/.env/_bind.yml")
14
- bindings = (raw_yaml.present?) ? YAML.load(raw_yaml) : {}
15
- bindings.each do |filename, dir_names|
16
- file = YAML.load_file("#{$HomeDir}/.env/#{filename}.yml").symbolize_keys
17
- next if file[environment].blank?
18
-
19
- dir_names = Fanforce::CLI::Apps.dir_names if dir_names.is_a?(String) and dir_names.upcase == 'ALL'
20
- file[environment].each do |k,v|
21
- dir_names.each do |dir_name|
22
- vars[dir_name] ||= {}
23
- vars[dir_name]["#{filename}_#{k}".upcase] = v
24
- end
25
- end
26
- end
27
-
28
- return vars
29
- end
30
-
31
- def vars_by_app(environment)
32
- @vars ||= {}
33
- @vars[environment] ||= load_vars_by_app(environment)
34
- end
35
-
36
- class << self
37
- extend Forwardable
38
- def_delegators :instance, *Fanforce::CLI::Env.instance_methods(false)
39
- end
40
-
41
- end
@@ -1,290 +0,0 @@
1
- class Fanforce::CLI::Files
2
-
3
- # CONFIG.RU #######################################################################
4
-
5
- def self.create_config_ru(app)
6
- file = config_ru_required_lines.join("\n") + "\n\n"
7
- if (fanforce_config_lines = fanforce_config_lines(app._id)).size > 0
8
- file += "FanforceApp.config do |config|\n"
9
- file += fanforce_config_lines.map {|k,v| " #{k} = #{v}"}.join("\n") + "\n"
10
- file += "end\n"
11
- end
12
- file += "run FanforceApp\n"
13
- File.open("#{app.dir}/config.ru", 'w') {|f| f.write file }
14
- end
15
-
16
- def self.update_config_ru(app)
17
- return create_config_ru(app) if !File.exists?("#{app.dir}/config.ru")
18
-
19
- lines = File.open("#{app.dir}/config.ru", 'r') {|f| f.readlines}
20
-
21
- lines.clone.each {|l| lines.delete(l) if config_ru_required_lines.include?(l.strip) }
22
- config_ru_required_lines.reverse.each {|l| lines.unshift(l+"\n") }
23
-
24
- config_options = extract_fanforce_config_options(lines, 'config.ru')
25
-
26
- fanforce_config_keys_to_require.each do |k|
27
- config_options[k] = fanforce_config_lines(app._id)[k] if config_options[k].blank?
28
- end
29
-
30
- fanforce_config_keys_to_overwrite.each do |k|
31
- config_options[k] = fanforce_config_lines(app._id)[k]
32
- end
33
-
34
- lines = replace_fanforce_config_options_in_config_ru(lines, config_options)
35
-
36
- File.open("#{app.dir}/config.ru", 'w') {|f| f.write(lines.join('')) }
37
- end
38
-
39
- def self.replace_fanforce_config_options_in_config_ru(lines, config_options)
40
- if (block = extract_fanforce_config_block(lines, 'config.ru'))
41
- new_block = "FanforceApp.config do |config|\n"
42
- new_block += config_options.map {|k,v| " #{k} = #{v}"}.join("\n") + "\n"
43
- new_block += "end\n"
44
- new_block += "run FanforceApp"
45
- else
46
- block = extract_run_fanforce_line(lines)
47
- new_block = "run FanforceApp"
48
- end
49
- lines.join('').gsub(block[:code], new_block).lines.to_a
50
- end
51
-
52
- def self.config_ru_required_lines
53
- [
54
- "require 'bundler'; Bundler.setup",
55
- "require 'fanforce/app_factory'"
56
- ]
57
- end
58
-
59
- # RAKEFILE #######################################################################
60
-
61
- def self.create_rakefile(app)
62
- file = rakefile_required_lines.join("\n") + "\n\n"
63
- if (fanforce_config_lines = fanforce_config_lines(app._id)).size > 0
64
- file += "FanforceApp.config do |config|\n"
65
- file += fanforce_config_lines.map {|k,v| " #{k} = #{v}"}.join("\n") + "\n"
66
- file += "end\n"
67
- end
68
- file += "load 'fanforce/app_factory.rake'\n"
69
- File.open("#{app.dir}/Rakefile", 'w') {|f| f.write file }
70
- end
71
-
72
- def self.update_rakefile(app)
73
- return create_rakefile(app) if !File.exists?("#{app.dir}/Rakefile")
74
-
75
- lines = File.open("#{app.dir}/Rakefile", 'r') {|f| f.readlines}
76
-
77
- lines.clone.each {|l| lines.delete(l) if rakefile_required_lines.include?(l.strip) }
78
- rakefile_required_lines.reverse.each {|l| lines.unshift(l+"\n") }
79
-
80
- config_options = extract_fanforce_config_options(lines, 'Rakefile')
81
-
82
- fanforce_config_keys_to_require.each do |k|
83
- config_options[k] = fanforce_config_lines(app._id)[k] if config_options[k].blank?
84
- end
85
-
86
- fanforce_config_keys_to_overwrite.each do |k|
87
- config_options[k] = fanforce_config_lines(app._id)[k]
88
- end
89
-
90
- lines = replace_fanforce_config_options_in_rakefile(lines, config_options)
91
-
92
- File.open("#{app.dir}/Rakefile", 'w') {|f| f.write(lines.join('')) }
93
- end
94
-
95
- def self.replace_fanforce_config_options_in_rakefile(lines, config_options)
96
- if (block = extract_fanforce_config_block(lines, 'Rakefile'))
97
- new_block = "FanforceApp.config do |config|\n"
98
- new_block += config_options.map {|k,v| " #{k} = #{v}"}.join("\n") + "\n"
99
- new_block += "end\n"
100
- new_block += "load 'fanforce/app_factory.rake'"
101
- else
102
- block = extract_load_fanforce_line(lines)
103
- new_block = "load 'fanforce/app_factory.rake'"
104
- end
105
- lines.join('').gsub(block[:code], new_block).lines.to_a
106
- end
107
-
108
- def self.rakefile_required_lines
109
- [
110
- "require 'bundler'; Bundler.setup",
111
- "require 'fanforce/app_factory'"
112
- ]
113
- end
114
-
115
- ########################################################################
116
-
117
- def self.extract_load_fanforce_line(lines)
118
- code = lines.join('')
119
- regex = Regexp.compile('( *load\s+(\'|")fanforce/app_factory.rake(\'|"))', Regexp::MULTILINE)
120
- if code !~ regex
121
- raise "No valid \"load 'fanforce/app_factory.rake'\" line was found in your Rakefile."
122
- end
123
- {:code => $1}
124
- end
125
-
126
- def self.extract_run_fanforce_line(lines)
127
- code = lines.join('')
128
- regex = Regexp.compile('( *run\s+FanforceApp)', Regexp::MULTILINE)
129
- if code !~ regex
130
- raise "No valid \"run FanforceApp\" line was found in your config.ru."
131
- end
132
- {:code => $1}
133
- end
134
-
135
- def self.extract_fanforce_config_block(lines, filename)
136
- code = lines.join('')
137
- regex = Regexp.compile('( *FanforceApp\.config\s*(do|{)\s*\|\s*([A-Za-z]+)\s*\|(.*)(end|})\s*(run\s+FanforceApp|load\s+(\'|")fanforce/app_factory.rake(\'|")))', Regexp::MULTILINE)
138
- if code !~ regex || ($2 == '{' and $5 != '}') || ($2 == 'do' and $5 != 'end')
139
- return nil
140
- end
141
- {
142
- :code => $1,
143
- :start_keyword => $2,
144
- :config_var => $3,
145
- :config_code => $4,
146
- :end_keyword => $5
147
- }
148
- end
149
-
150
- def self.extract_fanforce_config_options(lines, filename)
151
- block = extract_fanforce_config_block(lines, filename)
152
- return [] if !block
153
- regex = Regexp.compile(block[:config_var]+'\.([\w]+)\s*=\s*(((?!'+block[:config_var]+'\.).)+)', Regexp::MULTILINE)
154
- block[:config_code].scan(regex).inject({}) {|result, match| result.update "config.#{match[0]}" => match[1].strip }
155
- end
156
-
157
- def self.fanforce_config_lines(app_id)
158
- lines = {}
159
- return lines
160
- end
161
-
162
- def self.fanforce_config_keys_to_require
163
- []
164
- end
165
-
166
- def self.fanforce_config_keys_to_overwrite
167
- keys = []
168
- return keys
169
- end
170
-
171
- # GEMFILE #######################################################################
172
-
173
- def self.create_gemfile(app)
174
- file = gemfile_source_lines.join("\n") + "\n"
175
- file += gemfile_ruby_version + "\n\n"
176
- file += gemfile_factory_line + "\n\n"
177
- File.open("#{app.dir}/Gemfile", 'w') {|f| f.write file }
178
- end
179
-
180
- def self.update_gemfile(app)
181
- return create_gemfile(app) if !File.exists?("#{app.dir}/Gemfile")
182
-
183
- lines = File.open("#{app.dir}/Gemfile", 'r') {|f| f.readlines}.map {|l| l.strip }
184
- gemfile_source_lines.reverse.each {|l| lines.delete(l); lines.unshift(l) }
185
-
186
- lines.clone.each {|l| lines.delete(l) if l =~ /^ruby .+/ }
187
- lines.each_with_index do |l,i|
188
- next if l =~ /^source .+/
189
- lines.insert(i, gemfile_ruby_version) and break
190
- end
191
-
192
- lines = lines.map do |l|
193
- l.include?("gem 'fanforce-app-factory'") ? gemfile_factory_line : l
194
- end
195
- lines << gemfile_factory_line if !lines.include?(gemfile_factory_line)
196
-
197
- File.open("#{app.dir}/Gemfile", 'w') {|f| f.write(lines.join "\n") }
198
- end
199
-
200
- def self.gemfile_source_lines
201
- [
202
- "source 'https://rubygems.org'",
203
- ]
204
- end
205
-
206
- def self.gemfile_ruby_version
207
- "ruby '1.9.3'"
208
- end
209
-
210
- def self.gemfile_factory_line
211
- line = "gem 'fanforce-app-factory'"
212
- return line if !$Config[:app_factory_gem].is_a?(Hash)
213
-
214
- line += ", '#{$Config[:app_factory_gem][:version]}'" if $Config[:app_factory_gem][:version].present?
215
- line += ", :path => '#{$Config[:app_factory_gem][:path]}'" if $Config[:app_factory_gem][:path].present?
216
- line += ", :git => '#{$Config[:app_factory_gem][:git]}'" if $Config[:app_factory_gem][:git].present?
217
- line
218
- end
219
-
220
- # .GITIGNORE #######################################################################
221
-
222
- def self.create_gitignore(app)
223
- file = gitignore_lines.join("\n") + "\n"
224
- File.open("#{app.dir}/.gitignore", 'w') {|f| f.write file }
225
- end
226
-
227
- def self.update_gitignore(app)
228
- return create_gitignore(app) if !File.exists?("#{app.dir}/.gitignore")
229
-
230
- lines = File.open("#{app.dir}/.gitignore", 'r') {|f| f.readlines}.map {|l| l.strip }
231
-
232
- gitignore_lines.each do |line|
233
- lines << line if !lines.include?(line)
234
- end
235
-
236
- File.open("#{app.dir}/.gitignore", 'w') {|f| f.write(lines.join "\n") }
237
- end
238
-
239
- def self.gitignore_lines; [
240
- '*.gem',
241
- '*.rbc',
242
- '.bundle',
243
- '.config',
244
- 'coverage',
245
- 'InstalledFiles',
246
- 'lib/bundler/man',
247
- 'pkg',
248
- 'rdoc',
249
- 'spec/reports',
250
- 'test/tmp',
251
- 'test/version_tmp',
252
- 'tmp',
253
- '.idea/',
254
- '.sass-cache/',
255
- '.DS_STORE',
256
- '.powenv',
257
- '.env/',
258
- '.pluginenv',
259
- '.appenv',
260
- '.widgetenv',
261
- '.*env.rb',
262
- '.yardoc',
263
- '_yardoc',
264
- 'doc/',
265
- ] end
266
-
267
- # .POWENV #######################################################################
268
-
269
- def self.create_powenv(app)
270
- file = powenv_lines.join("\n") + "\n"
271
- File.open("#{app.dir}/.powenv", 'w') {|f| f.write file }
272
- end
273
-
274
- def self.update_powenv(app)
275
- return create_powenv(app) if !File.exists?("#{app.dir}/.powenv")
276
-
277
- lines = File.open("#{app.dir}/.powenv", 'r') {|f| f.readlines}.map {|l| l.strip }
278
-
279
- powenv_lines.each do |line|
280
- lines << line if !lines.include?(line)
281
- end
282
-
283
- File.open("#{app.dir}/.powenv", 'w') {|f| f.write(lines.join "\n") }
284
- end
285
-
286
- def self.powenv_lines
287
- ['source .appenv']
288
- end
289
-
290
- end