Class: Fanforce::Factory::Files

Inherits:
Object
  • Object
show all
Defined in:
lib/fanforce/factory/files.rb

Class Method Summary collapse

Class Method Details

.config_ru_required_lines(addon_type) ⇒ Object



52
53
54
55
56
57
# File 'lib/fanforce/factory/files.rb', line 52

def self.config_ru_required_lines(addon_type)
  [
      "require 'bundler'; Bundler.setup",
      "require 'fanforce/#{addon_type}_factory'"
  ]
end

.create_config_ru(addon) ⇒ Object

CONFIG.RU #######################################################################



5
6
7
8
9
10
11
12
13
14
# File 'lib/fanforce/factory/files.rb', line 5

def self.create_config_ru(addon)
  file = config_ru_required_lines(addon.type).join("\n") + "\n\n"
  if (fanforce_config_lines = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)).size > 0
    file += "Fanforce#{addon.type.capitalize}.config do |config|\n"
    file += fanforce_config_lines.map {|k,v| "  #{k} = #{v}"}.join("\n") + "\n"
    file += "end\n"
  end
  file += "run Fanforce#{addon.type.capitalize}\n"
  File.open("#{addon.dir}/config.ru", 'w') {|f| f.write file }
end

.create_gemfile(addon) ⇒ Object

GEMFILE #######################################################################



176
177
178
179
180
181
# File 'lib/fanforce/factory/files.rb', line 176

def self.create_gemfile(addon)
  file  = gemfile_source_lines(addon.type).join("\n") + "\n"
  file += gemfile_ruby_version(addon.type) + "\n\n"
  file += gemfile_factory_line(addon.type) + "\n\n"
  File.open("#{addon.dir}/Gemfile", 'w') {|f| f.write file }
end

.create_gitignore(addon) ⇒ Object

.GITIGNORE #######################################################################



225
226
227
228
# File 'lib/fanforce/factory/files.rb', line 225

def self.create_gitignore(addon)
  file = gitignore_lines(addon.type).join("\n") + "\n"
  File.open("#{addon.dir}/.gitignore", 'w') {|f| f.write file }
end

.create_powenv(addon) ⇒ Object

.POWENV #######################################################################



248
249
250
251
# File 'lib/fanforce/factory/files.rb', line 248

def self.create_powenv(addon)
  file = powenv_lines(addon.type).join("\n") + "\n"
  File.open("#{addon.dir}/.powenv", 'w') {|f| f.write file }
end

.create_rakefile(addon) ⇒ Object

RAKEFILE #######################################################################



61
62
63
64
65
66
67
68
69
70
# File 'lib/fanforce/factory/files.rb', line 61

def self.create_rakefile(addon)
  file = rakefile_required_lines(addon.type).join("\n") + "\n\n"
  if (fanforce_config_lines = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)).size > 0
    file += "Fanforce#{addon.type.capitalize}.config do |config|\n"
    file += fanforce_config_lines.map {|k,v| "  #{k} = #{v}"}.join("\n") + "\n"
    file += "end\n"
  end
  file += "load 'fanforce/#{addon.type}_factory.rake'\n"
  File.open("#{addon.dir}/Rakefile", 'w') {|f| f.write file }
end

.extract_fanforce_config_block(addon_type, lines, filename) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fanforce/factory/files.rb', line 135

def self.extract_fanforce_config_block(addon_type, lines, filename)
  code = lines.join('')
  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)
  if code !~ regex || ($2 == '{' and $5 != '}') || ($2 == 'do' and $5 != 'end')
    raise "No valid Fanforce#{addon_type.capitalize}.config block was found in your #{filename}." if addon_type == :plugin
    return nil
  end
  {
      :code          => $1,
      :start_keyword => $2,
      :config_var    => $3,
      :config_code   => $4,
      :end_keyword   => $5
  }
end

.extract_fanforce_config_options(addon_type, lines, filename) ⇒ Object



151
152
153
154
155
156
# File 'lib/fanforce/factory/files.rb', line 151

def self.extract_fanforce_config_options(addon_type, lines, filename)
  block = extract_fanforce_config_block(addon_type, lines, filename)
  return [] if !block
  regex = Regexp.compile(block[:config_var]+'\.([\w]+)\s*=\s*(((?!'+block[:config_var]+'\.).)+)', Regexp::MULTILINE)
  block[:config_code].scan(regex).inject({}) {|result, match| result.update "config.#{match[0]}" => match[1].strip }
end

.extract_load_fanforce_line(addon_type, lines) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/fanforce/factory/files.rb', line 117

def self.extract_load_fanforce_line(addon_type, lines)
  code = lines.join('')
  regex = Regexp.compile('( *load\s+(\'|")fanforce/'+addon_type.to_s+'_factory.rake(\'|"))', Regexp::MULTILINE)
  if code !~ regex
    raise "No valid \"load 'fanforce/#{addon_type}_factory.rake'\" line was found in your Rakefile."
  end
  {:code => $1}
end

.extract_run_fanforce_line(addon_type, lines) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/fanforce/factory/files.rb', line 126

def self.extract_run_fanforce_line(addon_type, lines)
  code = lines.join('')
  regex = Regexp.compile('( *run\s+Fanforce'+addon_type.to_s.capitalize+')', Regexp::MULTILINE)
  if code !~ regex
    raise "No valid \"run Fanforce#{addon_type.to_s.capitalize}\" line was found in your config.ru."
  end
  {:code => $1}
end

.fanforce_config_keys_to_overwrite(addon_type, plugin_type) ⇒ Object



168
169
170
171
172
# File 'lib/fanforce/factory/files.rb', line 168

def self.fanforce_config_keys_to_overwrite(addon_type, plugin_type)
  keys = []
  keys << 'config.type' if !plugin_type.nil?
  return keys
end

.fanforce_config_keys_to_require(addon_type, plugin_type) ⇒ Object



164
165
166
# File 'lib/fanforce/factory/files.rb', line 164

def self.fanforce_config_keys_to_require(addon_type, plugin_type)
  []
end

.fanforce_config_lines(addon_id, addon_type, plugin_type) ⇒ Object



158
159
160
161
162
# File 'lib/fanforce/factory/files.rb', line 158

def self.fanforce_config_lines(addon_id, addon_type, plugin_type)
  lines = {}
  lines['config.type'] = ":#{plugin_type}" if !plugin_type.nil?
  return lines
end

.gemfile_factory_line(addon_type) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/fanforce/factory/files.rb', line 213

def self.gemfile_factory_line(addon_type)
  line = "gem 'fanforce-#{addon_type}-factory'"
  return line if !$Config[:factory_gems].is_a?(Hash) or !$Config[:factory_gems][addon_type.to_sym].is_a?(Hash)

  line += ", '#{$Config[:factory_gems][addon_type][:version]}'" if $Config[:factory_gems][addon_type][:version].present?
  line += ", :path => '#{$Config[:factory_gems][addon_type][:path]}'" if $Config[:factory_gems][addon_type][:path].present?
  line += ", :git => '#{$Config[:factory_gems][addon_type][:git]}'" if $Config[:factory_gems][addon_type][:git].present?
  line
end

.gemfile_ruby_version(addon_type = nil) ⇒ Object



209
210
211
# File 'lib/fanforce/factory/files.rb', line 209

def self.gemfile_ruby_version(addon_type=nil)
  "ruby '1.9.3'"
end

.gemfile_source_lines(addon_type = nil) ⇒ Object



203
204
205
206
207
# File 'lib/fanforce/factory/files.rb', line 203

def self.gemfile_source_lines(addon_type=nil)
  [
    "source 'https://rubygems.org'",
  ]
end

.gitignore_lines(addon_type = nil) ⇒ Object



242
243
244
# File 'lib/fanforce/factory/files.rb', line 242

def self.gitignore_lines(addon_type=nil)
  %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 .appenv .widgetenv .*env.rb .yardoc _yardoc doc/)
end

.powenv_lines(addon_type) ⇒ Object



265
266
267
# File 'lib/fanforce/factory/files.rb', line 265

def self.powenv_lines(addon_type)
  ["source .#{addon_type}env"]
end

.rakefile_required_lines(addon_type) ⇒ Object



108
109
110
111
112
113
# File 'lib/fanforce/factory/files.rb', line 108

def self.rakefile_required_lines(addon_type)
  [
      "require 'bundler'; Bundler.setup",
      "require 'fanforce/#{addon_type}_factory'"
  ]
end

.replace_fanforce_config_options_in_config_ru(addon_type, lines, config_options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fanforce/factory/files.rb', line 39

def self.replace_fanforce_config_options_in_config_ru(addon_type, lines, config_options)
  if (block = extract_fanforce_config_block(addon_type, lines, 'config.ru'))
    new_block  = "Fanforce#{addon_type.capitalize}.config do |config|\n"
    new_block += config_options.map {|k,v| "  #{k} = #{v}"}.join("\n") + "\n"
    new_block += "end\n"
    new_block += "run Fanforce#{addon_type.capitalize}"
  else
    block = extract_run_fanforce_line(addon_type, lines)
    new_block = "run Fanforce#{addon_type.capitalize}"
  end
  lines.join('').gsub(block[:code], new_block).lines.to_a
end

.replace_fanforce_config_options_in_rakefile(addon_type, lines, config_options) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fanforce/factory/files.rb', line 95

def self.replace_fanforce_config_options_in_rakefile(addon_type, lines, config_options)
  if (block = extract_fanforce_config_block(addon_type, lines, 'Rakefile'))
    new_block  = "Fanforce#{addon_type.capitalize}.config do |config|\n"
    new_block += config_options.map {|k,v| "  #{k} = #{v}"}.join("\n") + "\n"
    new_block += "end\n"
    new_block += "load 'fanforce/#{addon_type}_factory.rake'"
  else
    block = extract_load_fanforce_line(addon_type, lines)
    new_block = "load 'fanforce/#{addon_type}_factory.rake'"
  end
  lines.join('').gsub(block[:code], new_block).lines.to_a
end

.update_config_ru(addon) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fanforce/factory/files.rb', line 16

def self.update_config_ru(addon)
  return create_config_ru(addon) if !File.exists?("#{addon.dir}/config.ru")

  lines = File.open("#{addon.dir}/config.ru", 'r') {|f| f.readlines}

  lines.clone.each {|l| lines.delete(l) if config_ru_required_lines(addon.type).include?(l.strip) }
  config_ru_required_lines(addon.type).reverse.each {|l| lines.unshift(l+"\n") }

  config_options = extract_fanforce_config_options(addon.type, lines, 'config.ru')

  fanforce_config_keys_to_require(addon.type, addon.plugin_type).each do |k|
    config_options[k] = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)[k] if config_options[k].blank?
  end

  fanforce_config_keys_to_overwrite(addon.type, addon.plugin_type).each do |k|
    config_options[k] = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)[k]
  end

  lines = replace_fanforce_config_options_in_config_ru(addon.type, lines, config_options)

  File.open("#{addon.dir}/config.ru", 'w') {|f| f.write(lines.join('')) }
end

.update_gemfile(addon) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/fanforce/factory/files.rb', line 183

def self.update_gemfile(addon)
  return create_gemfile(addon) if !File.exists?("#{addon.dir}/Gemfile")

  lines = File.open("#{addon.dir}/Gemfile", 'r') {|f| f.readlines}.map {|l| l.strip }
  gemfile_source_lines.reverse.each {|l| lines.delete(l); lines.unshift(l) }

  lines.clone.each {|l| lines.delete(l) if l =~ /^ruby .+/ }
  lines.each_with_index do |l,i|
    next if l =~ /^source .+/
    lines.insert(i, gemfile_ruby_version) and break
  end

  lines = lines.map do |l|
    l.include?("gem 'fanforce-#{addon.type}-factory'") ? gemfile_factory_line(addon.type) : l
  end
  lines << gemfile_factory_line(addon.type) if !lines.include?(gemfile_factory_line(addon.type))

  File.open("#{addon.dir}/Gemfile", 'w') {|f| f.write(lines.join "\n") }
end

.update_gitignore(addon) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/fanforce/factory/files.rb', line 230

def self.update_gitignore(addon)
  return create_gitignore(addon) if !File.exists?("#{addon.dir}/.gitignore")

  lines = File.open("#{addon.dir}/.gitignore", 'r') {|f| f.readlines}.map {|l| l.strip }

  gitignore_lines.each do |line|
    lines << line if !lines.include?(line)
  end

  File.open("#{addon.dir}/.gitignore", 'w') {|f| f.write(lines.join "\n") }
end

.update_powenv(addon) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/fanforce/factory/files.rb', line 253

def self.update_powenv(addon)
  return create_powenv(addon) if !File.exists?("#{addon.dir}/.powenv")

  lines = File.open("#{addon.dir}/.powenv", 'r') {|f| f.readlines}.map {|l| l.strip }

  powenv_lines(addon.type).each do |line|
    lines << line if !lines.include?(line)
  end

  File.open("#{addon.dir}/.powenv", 'w') {|f| f.write(lines.join "\n") }
end

.update_rakefile(addon) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fanforce/factory/files.rb', line 72

def self.update_rakefile(addon)
  return create_rakefile(addon) if !File.exists?("#{addon.dir}/Rakefile")

  lines = File.open("#{addon.dir}/Rakefile", 'r') {|f| f.readlines}

  lines.clone.each {|l| lines.delete(l) if rakefile_required_lines(addon.type).include?(l.strip) }
  rakefile_required_lines(addon.type).reverse.each {|l| lines.unshift(l+"\n") }

  config_options = extract_fanforce_config_options(addon.type, lines, 'Rakefile')

  fanforce_config_keys_to_require(addon.type, addon.plugin_type).each do |k|
    config_options[k] = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)[k] if config_options[k].blank?
  end

  fanforce_config_keys_to_overwrite(addon.type, addon.plugin_type).each do |k|
    config_options[k] = fanforce_config_lines(addon._id, addon.type, addon.plugin_type)[k]
  end

  lines = replace_fanforce_config_options_in_rakefile(addon.type, lines, config_options)

  File.open("#{addon.dir}/Rakefile", 'w') {|f| f.write(lines.join('')) }
end