fanforce-factory 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,180 @@
1
+ class Fanforce::Factory
2
+
3
+ def get_heroku_app_name(addon, environment)
4
+ heroku_app_name = "#{environment==:production ? 'prd' : 'qa'}-#{addon.dir_name}"
5
+ heroku_app_name.length > 30 ? heroku_app_name.gsub!(/(a|e|i|o|u)/, '') : heroku_app_name
6
+ end
7
+
8
+ def setup_files(addon)
9
+ if File.directory?("#{addon.dir}/tmp")
10
+ puts "#{'Found '.format(:green,:bold)} #{addon.dir_name}/tmp/"
11
+ else
12
+ Dir.mkdir("#{addon.dir}/tmp")
13
+ puts "#{'Created'.format(:green,:bold)} #{addon.dir_name}/tmp/"
14
+ end
15
+
16
+ if File.exists?("#{addon.dir}/config.ru")
17
+ addon.update_file(:config_ru)
18
+ puts "#{'Updated'.format(:green,:bold)} #{addon.dir_name}/config.ru"
19
+ else
20
+ addon.create_file(:config_ru)
21
+ puts "#{'Created'.format(:green,:bold)} #{addon.dir_name}/config.ru"
22
+ end
23
+
24
+ if File.exists?("#{addon.dir}/.gitignore")
25
+ addon.update_file(:gitignore)
26
+ puts "#{'Updated'.format(:green,:bold)} #{addon.dir_name}/.gitignore"
27
+ else
28
+ addon.create_file(:gitignore)
29
+ puts "#{'Created'.format(:green,:bold)} #{addon.dir_name}/.gitignore"
30
+ end
31
+
32
+ if File.exists?("#{addon.dir}/Gemfile")
33
+ addon.update_file(:gemfile)
34
+ puts "#{'Updated'.format(:green,:bold)} #{addon.dir_name}/Gemfile"
35
+ else
36
+ addon.create_file(:gemfile)
37
+ puts "#{'Created'.format(:green,:bold)} #{addon.dir_name}/Gemfile"
38
+ end
39
+
40
+ if File.exists?("#{addon.dir}/Rakefile")
41
+ addon.update_file(:rakefile)
42
+ puts "#{'Updated'.format(:green,:bold)} #{addon.dir_name}/Rakefile"
43
+ else
44
+ addon.create_file(:rakefile)
45
+ puts "#{'Created'.format(:green,:bold)} #{addon.dir_name}/Rakefile"
46
+ end
47
+
48
+ restart(addon, :development)
49
+ end
50
+
51
+ def setup_envs(addon, environment)
52
+ environments = environment == :all ? [:development, :qa, :production] : [environment]
53
+
54
+ if !File.exists?("#{$HomeDir}/.env/_bind.yml")
55
+ return puts "#{'Oops'.format(:bold)}... you must setup .env/_bind.yml before trying to update env variables."
56
+ end
57
+
58
+ environments.each do |environment|
59
+ vars = Env.vars_by_addon(environment)[addon.dir_name]
60
+ has_workers = File.directory?("#{addon.dir}/workers") ? true : false
61
+
62
+ next puts "#{'Skipped'.format(:bold)} #{environment.to_s.titleize} has 0 env variables" if vars.blank? or vars.size == 0
63
+ puts "#{'Updated'.format(:green,:bold)} #{environment.to_s.titleize}#{has_workers ? '... and workers have' : ' has'} #{vars.size} env variables"
64
+
65
+ push_env_to(environment, addon, vars)
66
+ end
67
+ end
68
+
69
+ def setup_pow(addon)
70
+ Run.pow_create(addon, addon.root_domain)
71
+ Run.pow_create(addon, $SMARTURL_DOMAIN) if addon.plugin_type == :behavior
72
+ end
73
+
74
+ def setup_bitbucket(addon)
75
+ bitbucket = BitBucket.new(login: $Config[:bitbucket][:user], password: $Config[:bitbucket][:password])
76
+ begin
77
+ bitbucket.repos.find($Config[:bitbucket][:user], addon.dir_name)
78
+ "#{'Found '.format(:green,:bold)}" + "#{addon.dir_name} repository already exists on bitbucket"
79
+ rescue
80
+ bitbucket.repos.create(name: addon.dir_name, is_private: true)
81
+ puts "#{'Created '.format(:green,:bold)}" + "#{addon.dir_name} repository on bitbucket"
82
+ end
83
+
84
+ if (`git remote`).split(/\r?\n/).include?($Config[:bitbucket][:user])
85
+ puts "#{'Updated '.format(:green,:bold)}" + "git remote for #{$Config[:bitbucket][:user]}"
86
+ `git remote rm #{$Config[:bitbucket][:user]}`
87
+ else
88
+ puts "#{'Created '.format(:green,:bold)}" + "git remote for #{$Config[:bitbucket][:user]}"
89
+ end
90
+ `git remote add #{$Config[:bitbucket][:user]} https://#{$Config[:bitbucket][:user]}@bitbucket.org/#{$Config[:bitbucket][:user]}/#{addon.dir_name}.git`
91
+
92
+ puts "#{'Pushing '.format(:green,:bold)}" + "latest commit to #{$Config[:bitbucket][:user]}..."
93
+ Run.command("git push #{$Config[:bitbucket][:user]} --all")
94
+ end
95
+
96
+ def auth_heroku(environment)
97
+ @heroku ||= {}
98
+ @heroku[environment] ||= Heroku::API.new(:username => $Config[:heroku][environment][:user], :password => $Config[:heroku][environment][:password])
99
+ end
100
+
101
+ def setup_heroku(addon, environment)
102
+ 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?
103
+
104
+ heroku = auth_heroku(environment)
105
+ heroku_app_name = get_heroku_app_name(addon, environment)
106
+ begin
107
+ heroku.get_app(heroku_app_name)
108
+ puts "#{'Found '.format(:green,:bold)}" + "#{environment} app on heroku (#{heroku_app_name})"
109
+ rescue
110
+ heroku.post_app(name: heroku_app_name)
111
+ puts "#{'Created '.format(:green,:bold)}" + "#{environment} on heroku (#{heroku_app_name})"
112
+ end
113
+
114
+ vars = Env.vars_by_addon(environment)[addon.dir_name]
115
+ heroku.put_config_vars(heroku_app_name, vars)
116
+
117
+ # Setup standard plugin domain
118
+ if $Config[:heroku][environment][:"#{addon.type}_domain"]
119
+ domain = "#{addon._id}.#{$Config[:heroku][environment][:"#{addon.type}_domain"]}"
120
+ domain_found = heroku.get_domains(heroku_app_name).body.inject(false) {|result, d| d['domain'] == domain ? (break true) : false }
121
+ if domain_found
122
+ puts "#{'Found '.format(:green,:bold)}" + "#{domain} domain on #{environment}"
123
+ else
124
+ heroku.post_domain(heroku_app_name, domain)
125
+ puts "#{'Added '.format(:green,:bold)}" + "#{domain} domain to #{environment}"
126
+ end
127
+ end
128
+
129
+ # Setup standard plugin domain
130
+ if addon.type == :plugin and addon.plugin_type == :behavior and $Config[:heroku][environment][:smarturl_domain]
131
+ domain = "#{addon._id}.#{$Config[:heroku][environment][:smarturl_domain]}"
132
+ domain_found = heroku.get_domains(heroku_app_name).body.inject(false) {|result, d| d['domain'] == domain ? (break true) : false }
133
+ if domain_found
134
+ puts "#{'Found '.format(:green,:bold)}" + "#{domain} domain on #{environment}"
135
+ else
136
+ heroku.post_domain(heroku_app_name, domain)
137
+ puts "#{'Added '.format(:green,:bold)}" + "#{domain} domain to #{environment}"
138
+ end
139
+ end
140
+
141
+ remote_name = "#{environment==:qa ? 'qa' : 'prd'}-heroku"
142
+ if (`git remote`).split(/\r?\n/).include?(remote_name)
143
+ puts "#{'Updated '.format(:green,:bold)}" + "git remote for #{remote_name}"
144
+ `git remote rm #{remote_name}`
145
+ else
146
+ puts "#{'Created '.format(:green,:bold)}" + "git remote for #{remote_name}"
147
+ end
148
+ `git remote add #{remote_name} git@#{$Config[:heroku][environment][:git_ssh_domain] || 'heroku.com'}:#{heroku_app_name}.git`
149
+
150
+ puts "#{'Pushing '.format(:green,:bold)}" + "latest commit to #{remote_name}..."
151
+ Run.command("git push #{remote_name} master")
152
+ end
153
+
154
+ ######################################################################################################################
155
+
156
+ def push_env_to(environment, addon, vars, create_worker_env=true)
157
+ vars.each {|k,v| puts " - #{k}" }
158
+ if environment == :development
159
+ addon.update_file(:powenv)
160
+ File.open("#{addon.dir}/.#{addon.type}env", 'w') {|f| f.write convert_hash_to_shell_env(vars) }
161
+ File.open("#{addon.dir}/workers/.#{addon.type}env.rb", 'w') {|f| f.write convert_hash_to_ruby_env(vars) } if create_worker_env and File.directory?("#{addon.dir}/workers") and vars['iron_project_id']
162
+ else
163
+ File.open("#{addon.dir}/workers/.#{addon.type}env.rb", 'w') {|f| f.write convert_hash_to_ruby_env(vars) } if create_worker_env and File.directory?("#{addon.dir}/workers") and vars['iron_project_id']
164
+ return if $Config[:heroku][environment].blank?
165
+ heroku = auth_heroku(environment)
166
+ heroku_app_name = get_heroku_app_name(addon, environment)
167
+ heroku.put_config_vars(heroku_app_name, vars)
168
+ end
169
+ restart(addon, environment)
170
+ end
171
+
172
+ def convert_hash_to_shell_env(hash)
173
+ hash.inject('') {|script, (k,v)| script += "export #{k}=#{v.to_s.to_json}\n" }
174
+ end
175
+
176
+ def convert_hash_to_ruby_env(hash)
177
+ hash.inject('') {|script, (k,v)| script += "ENV['#{k}']=#{v.to_s.to_json}\n" }
178
+ end
179
+
180
+ end
@@ -0,0 +1,112 @@
1
+ module Fanforce::Factory::DeveloperConfig
2
+ ########################################################################
3
+ def self.app(addon)
4
+ response = <<-eos
5
+ "public_urls": {
6
+ "marketplace_img": "http://${PRIMARY_DOMAIN}/assets/marketplace.png",
7
+ "icon_16": "http://${PRIMARY_DOMAIN}/assets/icon-16.png",
8
+ "icon_32": "http://${PRIMARY_DOMAIN}/assets/icon-32.png",
9
+ "icon_42": "http://${PRIMARY_DOMAIN}/assets/icon-42.png"
10
+ },
11
+ "staffer_ui_urls": {
12
+ "dashboard": "http://${PRIMARY_DOMAIN}/dashboard.html"
13
+ },
14
+ "callback_urls": {
15
+ "install": "http://${PRIMARY_DOMAIN}/install",
16
+ "uninstall": "http://${PRIMARY_DOMAIN}/uninstall"
17
+ },
18
+ "plugin_dependencies": []
19
+ eos
20
+ strip(response)
21
+ end
22
+
23
+ ########################################################################
24
+ def self.plugin(addon)
25
+ plugin_type = addon.plugin_type
26
+
27
+ response = " \"type\": \"#{plugin_type}\",\n"
28
+ response += " \"#{plugin_type}_config\": {\n"
29
+ if plugin_type == :data_connector
30
+ response += <<-eos
31
+ "category_id": "bulk_messaging"
32
+ eos
33
+ elsif plugin_type == :data_processor
34
+ elsif plugin_type == :broadcaster
35
+ response += <<-eos
36
+ "new_button_label": "#{addon._id.humanize.titleize}"
37
+ eos
38
+ elsif plugin_type == :identifier
39
+ elsif plugin_type == :behavior
40
+ response += <<-eos
41
+ "nouns": {
42
+ "common": "action"
43
+ },
44
+ "verbs": {
45
+ "simple": "do",
46
+ "progressive": "doing",
47
+ "perfect": "did"
48
+ },
49
+ "default_total_steps": 1,
50
+ "valuerank_formula": [],
51
+ "engagement_js_url": "http://${PRIMARY_DOMAIN}/assets/engage.js"
52
+ eos
53
+ end
54
+ response += " },\n"
55
+
56
+ response += <<-eos
57
+ "public_urls": {
58
+ "icon_16": "http://${PRIMARY_DOMAIN}/assets/icon-16.png",
59
+ "icon_32": "http://${PRIMARY_DOMAIN}/assets/icon-32.png",
60
+ "icon_42": "http://${PRIMARY_DOMAIN}/assets/icon-42.png"
61
+ },
62
+ eos
63
+
64
+ response += " \"staffer_ui_urls\": {\n"
65
+ if plugin_type == :data_connector
66
+ response += <<-eos
67
+ "add_source": "http://${PRIMARY_DOMAIN}/add_source.html",
68
+ "source_details": "http://${PRIMARY_DOMAIN}/source_details.html"
69
+ eos
70
+ elsif plugin_type == :data_processor
71
+ elsif plugin_type == :broadcaster
72
+ response += <<-eos
73
+ "new_message": "http://${PRIMARY_DOMAIN}/new_message.html"
74
+ eos
75
+ elsif plugin_type == :identifier
76
+ elsif plugin_type == :behavior
77
+ response += <<-eos
78
+ "add_initiative": "http://${PRIMARY_DOMAIN}/add_initiative.html",
79
+ "edit_initiative": "http://${PRIMARY_DOMAIN}/edit_initiative.html",
80
+ "new_message": "http://${PRIMARY_DOMAIN}/new_message.html"
81
+ eos
82
+ end
83
+ response += " },\n"
84
+
85
+ response += " \"staffer_js_urls\": {\n"
86
+ if plugin_type == :data_connector
87
+ response += <<-eos
88
+ "add_source": "http://${PRIMARY_DOMAIN}/assets/add_source_popup.js"
89
+ eos
90
+ elsif plugin_type == :data_processor
91
+ elsif plugin_type == :broadcaster
92
+ elsif plugin_type == :identifier
93
+ elsif plugin_type == :behavior
94
+ end
95
+ response += " },\n"
96
+
97
+ response += <<-eos
98
+ "callback_urls": {
99
+ "install": "http://${PRIMARY_DOMAIN}/install",
100
+ "uninstall": "http://${PRIMARY_DOMAIN}/uninstall"
101
+ },
102
+ eos
103
+ response += ' "widget_dependencies": []' if plugin_type == :behavior
104
+
105
+ strip response.gsub!(/,\s*\z/, '')
106
+ end
107
+
108
+ def self.strip(data)
109
+ data.gsub(/^ /, '')
110
+ end
111
+
112
+ end
@@ -0,0 +1,37 @@
1
+ class Fanforce::Factory::Env
2
+ require 'singleton'
3
+ require 'forwardable'
4
+ include Singleton
5
+
6
+ def load_vars_by_addon(environment)
7
+ bindings = YAML.load_file("#{$HomeDir}/.env/_bind.yml")
8
+ result = bindings.inject({}) do |result, (filename, dir_names)|
9
+ file = YAML.load_file("#{$HomeDir}/.env/#{filename}.yml").symbolize_keys
10
+ if file[environment].blank?
11
+ next result
12
+ end
13
+
14
+ dir_names = Fanforce::Factory::Addons.dir_names if dir_names.is_a?(String) and dir_names.upcase == 'ALL'
15
+ file[environment].each do |k,v|
16
+ dir_names.each do |d|
17
+ result[d] ||= {}
18
+ result[d]["#{filename}_#{k}".upcase] = v
19
+ d_parts = Fanforce::Factory::Addon.parse_dir_name(d)
20
+ result[d]["fanforce_#{d_parts[:type]}_id".upcase] = d_parts[:_id] if d_parts
21
+ end
22
+ end
23
+ result
24
+ end
25
+ end
26
+
27
+ def vars_by_addon(environment)
28
+ @vars ||= {}
29
+ @vars[environment] ||= load_vars_by_addon(environment)
30
+ end
31
+
32
+ class << self
33
+ extend Forwardable
34
+ def_delegators :instance, *Fanforce::Factory::Env.instance_methods(false)
35
+ end
36
+
37
+ end
@@ -0,0 +1,269 @@
1
+ class Fanforce::Factory::Files
2
+
3
+ # CONFIG.RU #######################################################################
4
+
5
+ def self.create_config_ru(addon)
6
+ file = config_ru_required_lines(addon.type).join("\n") + "\n\n"
7
+ if (fanforce_config_lines = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)).size > 0
8
+ file += "Fanforce#{addon.type.capitalize}.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 Fanforce#{addon.type.capitalize}\n"
13
+ File.open("#{addon.dir}/config.ru", 'w') {|f| f.write file }
14
+ end
15
+
16
+ def self.update_config_ru(addon)
17
+ return create_config_ru(addon) if !File.exists?("#{addon.dir}/config.ru")
18
+
19
+ lines = File.open("#{addon.dir}/config.ru", 'r') {|f| f.readlines}
20
+
21
+ lines.clone.each {|l| lines.delete(l) if config_ru_required_lines(addon.type).include?(l.strip) }
22
+ config_ru_required_lines(addon.type).reverse.each {|l| lines.unshift(l+"\n") }
23
+
24
+ config_options = extract_fanforce_config_options(addon.type, lines, 'config.ru')
25
+
26
+ fanforce_config_keys_to_require(addon.type, addon.plugin_type).each do |k|
27
+ config_options[k] = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)[k] if config_options[k].blank?
28
+ end
29
+
30
+ fanforce_config_keys_to_overwrite(addon.type, addon.plugin_type).each do |k|
31
+ config_options[k] = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)[k]
32
+ end
33
+
34
+ lines = replace_fanforce_config_options_in_config_ru(addon.type, lines, config_options)
35
+
36
+ File.open("#{addon.dir}/config.ru", 'w') {|f| f.write(lines.join('')) }
37
+ end
38
+
39
+ def self.replace_fanforce_config_options_in_config_ru(addon_type, lines, config_options)
40
+ if (block = extract_fanforce_config_block(addon_type, lines, 'config.ru'))
41
+ new_block = "Fanforce#{addon_type.capitalize}.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 Fanforce#{addon_type.capitalize}"
45
+ else
46
+ block = extract_run_fanforce_line(addon_type, lines)
47
+ new_block = "run Fanforce#{addon_type.capitalize}"
48
+ end
49
+ lines.join('').gsub(block[:code], new_block).lines.to_a
50
+ end
51
+
52
+ def self.config_ru_required_lines(addon_type)
53
+ [
54
+ "require 'bundler'; Bundler.setup",
55
+ "require 'fanforce/#{addon_type}_factory'"
56
+ ]
57
+ end
58
+
59
+ # RAKEFILE #######################################################################
60
+
61
+ def self.create_rakefile(addon)
62
+ file = rakefile_required_lines(addon.type).join("\n") + "\n\n"
63
+ if (fanforce_config_lines = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)).size > 0
64
+ file += "Fanforce#{addon.type.capitalize}.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/#{addon.type}_factory.rake'\n"
69
+ File.open("#{addon.dir}/Rakefile", 'w') {|f| f.write file }
70
+ end
71
+
72
+ def self.update_rakefile(addon)
73
+ return create_rakefile(addon) if !File.exists?("#{addon.dir}/Rakefile")
74
+
75
+ lines = File.open("#{addon.dir}/Rakefile", 'r') {|f| f.readlines}
76
+
77
+ lines.clone.each {|l| lines.delete(l) if rakefile_required_lines(addon.type).include?(l.strip) }
78
+ rakefile_required_lines(addon.type).reverse.each {|l| lines.unshift(l+"\n") }
79
+
80
+ config_options = extract_fanforce_config_options(addon.type, lines, 'Rakefile')
81
+
82
+ fanforce_config_keys_to_require(addon.type, addon.plugin_type).each do |k|
83
+ config_options[k] = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)[k] if config_options[k].blank?
84
+ end
85
+
86
+ fanforce_config_keys_to_overwrite(addon.type, addon.plugin_type).each do |k|
87
+ config_options[k] = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)[k]
88
+ end
89
+
90
+ lines = replace_fanforce_config_options_in_rakefile(addon.type, lines, config_options)
91
+
92
+ File.open("#{addon.dir}/Rakefile", 'w') {|f| f.write(lines.join('')) }
93
+ end
94
+
95
+ def self.replace_fanforce_config_options_in_rakefile(addon_type, lines, config_options)
96
+ if (block = extract_fanforce_config_block(addon_type, lines, 'Rakefile'))
97
+ new_block = "Fanforce#{addon_type.capitalize}.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/#{addon_type}_factory.rake'"
101
+ else
102
+ block = extract_load_fanforce_line(addon_type, lines)
103
+ new_block = "load 'fanforce/#{addon_type}_factory.rake'"
104
+ end
105
+ lines.join('').gsub(block[:code], new_block).lines.to_a
106
+ end
107
+
108
+ def self.rakefile_required_lines(addon_type)
109
+ [
110
+ "require 'bundler'; Bundler.setup",
111
+ "require 'fanforce/#{addon_type}_factory'"
112
+ ]
113
+ end
114
+
115
+ ########################################################################
116
+
117
+ def self.extract_load_fanforce_line(addon_type, lines)
118
+ code = lines.join('')
119
+ regex = Regexp.compile('( *load\s+(\'|")fanforce/'+addon_type.to_s+'_factory.rake(\'|"))', Regexp::MULTILINE)
120
+ if code !~ regex
121
+ raise "No valid \"load 'fanforce/#{addon_type}_factory.rake'\" line was found in your Rakefile."
122
+ end
123
+ {:code => $1}
124
+ end
125
+
126
+ def self.extract_run_fanforce_line(addon_type, lines)
127
+ code = lines.join('')
128
+ regex = Regexp.compile('( *run\s+Fanforce'+addon_type.to_s.capitalize+')', Regexp::MULTILINE)
129
+ if code !~ regex
130
+ raise "No valid \"run Fanforce#{addon_type.to_s.capitalize}\" line was found in your config.ru."
131
+ end
132
+ {:code => $1}
133
+ end
134
+
135
+ def self.extract_fanforce_config_block(addon_type, lines, filename)
136
+ code = lines.join('')
137
+ regex = Regexp.compile('( *Fanforce'+addon_type.to_s.capitalize+'\.config\s*(do|{)\s*\|\s*([A-Za-z]+)\s*\|(.*)(end|})\s*(run\s+Fanforce'+addon_type.to_s.capitalize+'|load\s+(\'|")fanforce/'+addon_type.to_s+'_factory.rake(\'|")))', Regexp::MULTILINE)
138
+ if code !~ regex || ($2 == '{' and $5 != '}') || ($2 == 'do' and $5 != 'end')
139
+ raise "No valid Fanforce#{addon_type.capitalize}.config block was found in your #{filename}." if addon_type == :plugin
140
+ return nil
141
+ end
142
+ {
143
+ :code => $1,
144
+ :start_keyword => $2,
145
+ :config_var => $3,
146
+ :config_code => $4,
147
+ :end_keyword => $5
148
+ }
149
+ end
150
+
151
+ def self.extract_fanforce_config_options(addon_type, lines, filename)
152
+ block = extract_fanforce_config_block(addon_type, lines, filename)
153
+ return [] if !block
154
+ regex = Regexp.compile(block[:config_var]+'\.([\w]+)\s*=\s*(((?!'+block[:config_var]+'\.).)+)', Regexp::MULTILINE)
155
+ block[:config_code].scan(regex).inject({}) {|result, match| result.update "config.#{match[0]}" => match[1].strip }
156
+ end
157
+
158
+ def self.fanforce_config_lines(addon_id, addon_type, plugin_type)
159
+ lines = {}
160
+ lines['config.type'] = ":#{plugin_type}" if !plugin_type.nil?
161
+ return lines
162
+ end
163
+
164
+ def self.fanforce_config_keys_to_require(addon_type, plugin_type)
165
+ []
166
+ end
167
+
168
+ def self.fanforce_config_keys_to_overwrite(addon_type, plugin_type)
169
+ keys = []
170
+ keys << 'config.type' if !plugin_type.nil?
171
+ return keys
172
+ end
173
+
174
+ # GEMFILE #######################################################################
175
+
176
+ def self.create_gemfile(addon)
177
+ file = gemfile_source_lines(addon.type).join("\n") + "\n"
178
+ file += gemfile_ruby_version(addon.type) + "\n\n"
179
+ file += gemfile_factory_line(addon.type) + "\n\n"
180
+ File.open("#{addon.dir}/Gemfile", 'w') {|f| f.write file }
181
+ end
182
+
183
+ def self.update_gemfile(addon)
184
+ return create_gemfile(addon) if !File.exists?("#{addon.dir}/Gemfile")
185
+
186
+ lines = File.open("#{addon.dir}/Gemfile", 'r') {|f| f.readlines}.map {|l| l.strip }
187
+ gemfile_source_lines.reverse.each {|l| lines.delete(l); lines.unshift(l) }
188
+
189
+ lines.clone.each {|l| lines.delete(l) if l =~ /^ruby .+/ }
190
+ lines.each_with_index do |l,i|
191
+ next if l =~ /^source .+/
192
+ lines.insert(i, gemfile_ruby_version) and break
193
+ end
194
+
195
+ lines = lines.map do |l|
196
+ l.include?("gem 'fanforce-#{addon.type}-factory'") ? gemfile_factory_line(addon.type) : l
197
+ end
198
+ lines << gemfile_factory_line(addon.type) if !lines.include?(gemfile_factory_line(addon.type))
199
+
200
+ File.open("#{addon.dir}/Gemfile", 'w') {|f| f.write(lines.join "\n") }
201
+ end
202
+
203
+ def self.gemfile_source_lines(addon_type=nil)
204
+ [
205
+ "source 'https://rubygems.org'",
206
+ ]
207
+ end
208
+
209
+ def self.gemfile_ruby_version(addon_type=nil)
210
+ "ruby '1.9.3'"
211
+ end
212
+
213
+ def self.gemfile_factory_line(addon_type)
214
+ line = "gem 'fanforce-#{addon_type}-factory'"
215
+ return line if !$Config[:factory_gems].is_a?(Hash) or !$Config[:factory_gems][addon_type.to_sym].is_a?(Hash)
216
+
217
+ line += ", '#{$Config[:factory_gems][addon_type][:version]}'" if $Config[:factory_gems][addon_type][:version].present?
218
+ line += ", :path => '#{$Config[:factory_gems][addon_type][:path]}'" if $Config[:factory_gems][addon_type][:path].present?
219
+ line += ", :git => '#{$Config[:factory_gems][addon_type][:git]}'" if $Config[:factory_gems][addon_type][:git].present?
220
+ line
221
+ end
222
+
223
+ # .GITIGNORE #######################################################################
224
+
225
+ def self.create_gitignore(addon)
226
+ file = gitignore_lines(addon.type).join("\n") + "\n"
227
+ File.open("#{addon.dir}/.gitignore", 'w') {|f| f.write file }
228
+ end
229
+
230
+ def self.update_gitignore(addon)
231
+ return create_gitignore(addon) if !File.exists?("#{addon.dir}/.gitignore")
232
+
233
+ lines = File.open("#{addon.dir}/.gitignore", 'r') {|f| f.readlines}.map {|l| l.strip }
234
+
235
+ gitignore_lines.each do |line|
236
+ lines << line if !lines.include?(line)
237
+ end
238
+
239
+ File.open("#{addon.dir}/.gitignore", 'w') {|f| f.write(lines.join "\n") }
240
+ end
241
+
242
+ def self.gitignore_lines(addon_type=nil)
243
+ %w(*.gem *.rbc .bundle .config coverage InstalledFiles lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp .idea/ .sass-cache/ .DS_STORE .powenv .pluginenv .pluginenv.rb .appenv.rb .appenv .widgetenv .widgetenv.rb .yardoc _yardoc doc/)
244
+ end
245
+
246
+ # .POWENV #######################################################################
247
+
248
+ def self.create_powenv(addon)
249
+ file = powenv_lines(addon.type).join("\n") + "\n"
250
+ File.open("#{addon.dir}/.powenv", 'w') {|f| f.write file }
251
+ end
252
+
253
+ def self.update_powenv(addon)
254
+ return create_powenv(addon) if !File.exists?("#{addon.dir}/.powenv")
255
+
256
+ lines = File.open("#{addon.dir}/.powenv", 'r') {|f| f.readlines}.map {|l| l.strip }
257
+
258
+ powenv_lines(addon.type).each do |line|
259
+ lines << line if !lines.include?(line)
260
+ end
261
+
262
+ File.open("#{addon.dir}/.powenv", 'w') {|f| f.write(lines.join "\n") }
263
+ end
264
+
265
+ def self.powenv_lines(addon_type)
266
+ ["source .#{addon_type}env"]
267
+ end
268
+
269
+ end