class Fanforce::CLI::Files # CONFIG.RU ####################################################################### def self.create_config_ru(app) file = config_ru_required_lines.join("\n") + "\n\n" if (fanforce_config_lines = fanforce_config_lines(app._id)).size > 0 file += "FanforceApp.config do |config|\n" file += fanforce_config_lines.map {|k,v| " #{k} = #{v}"}.join("\n") + "\n" file += "end\n" end file += "run FanforceApp\n" File.open("#{app.dir}/config.ru", 'w') {|f| f.write file } end def self.update_config_ru(app) return create_config_ru(app) if !File.exists?("#{app.dir}/config.ru") lines = File.open("#{app.dir}/config.ru", 'r') {|f| f.readlines} lines.clone.each {|l| lines.delete(l) if config_ru_required_lines.include?(l.strip) } config_ru_required_lines.reverse.each {|l| lines.unshift(l+"\n") } config_options = extract_fanforce_config_options(lines, 'config.ru') fanforce_config_keys_to_require.each do |k| config_options[k] = fanforce_config_lines(app._id)[k] if config_options[k].blank? end fanforce_config_keys_to_overwrite.each do |k| config_options[k] = fanforce_config_lines(app._id)[k] end lines = replace_fanforce_config_options_in_config_ru(lines, config_options) File.open("#{app.dir}/config.ru", 'w') {|f| f.write(lines.join('')) } end def self.replace_fanforce_config_options_in_config_ru(lines, config_options) if (block = extract_fanforce_config_block(lines, 'config.ru')) new_block = "FanforceApp.config do |config|\n" new_block += config_options.map {|k,v| " #{k} = #{v}"}.join("\n") + "\n" new_block += "end\n" new_block += "run FanforceApp" else block = extract_run_fanforce_line(lines) new_block = "run FanforceApp" end lines.join('').gsub(block[:code], new_block).lines.to_a end def self.config_ru_required_lines [ "require 'bundler'; Bundler.setup", "require 'fanforce/app_factory'" ] end # RAKEFILE ####################################################################### def self.create_rakefile(app) file = rakefile_required_lines.join("\n") + "\n\n" if (fanforce_config_lines = fanforce_config_lines(app._id)).size > 0 file += "FanforceApp.config do |config|\n" file += fanforce_config_lines.map {|k,v| " #{k} = #{v}"}.join("\n") + "\n" file += "end\n" end file += "load 'fanforce/app_factory.rake'\n" File.open("#{app.dir}/Rakefile", 'w') {|f| f.write file } end def self.update_rakefile(app) return create_rakefile(app) if !File.exists?("#{app.dir}/Rakefile") lines = File.open("#{app.dir}/Rakefile", 'r') {|f| f.readlines} lines.clone.each {|l| lines.delete(l) if rakefile_required_lines.include?(l.strip) } rakefile_required_lines.reverse.each {|l| lines.unshift(l+"\n") } config_options = extract_fanforce_config_options(lines, 'Rakefile') fanforce_config_keys_to_require.each do |k| config_options[k] = fanforce_config_lines(app._id)[k] if config_options[k].blank? end fanforce_config_keys_to_overwrite.each do |k| config_options[k] = fanforce_config_lines(app._id)[k] end lines = replace_fanforce_config_options_in_rakefile(lines, config_options) File.open("#{app.dir}/Rakefile", 'w') {|f| f.write(lines.join('')) } end def self.replace_fanforce_config_options_in_rakefile(lines, config_options) if (block = extract_fanforce_config_block(lines, 'Rakefile')) new_block = "FanforceApp.config do |config|\n" new_block += config_options.map {|k,v| " #{k} = #{v}"}.join("\n") + "\n" new_block += "end\n" new_block += "load 'fanforce/app_factory.rake'" else block = extract_load_fanforce_line(lines) new_block = "load 'fanforce/app_factory.rake'" end lines.join('').gsub(block[:code], new_block).lines.to_a end def self.rakefile_required_lines [ "require 'bundler'; Bundler.setup", "require 'fanforce/app_factory'" ] end ######################################################################## def self.extract_load_fanforce_line(lines) code = lines.join('') regex = Regexp.compile('( *load\s+(\'|")fanforce/app_factory.rake(\'|"))', Regexp::MULTILINE) if code !~ regex raise "No valid \"load 'fanforce/app_factory.rake'\" line was found in your Rakefile." end {:code => $1} end def self.extract_run_fanforce_line(lines) code = lines.join('') regex = Regexp.compile('( *run\s+FanforceApp)', Regexp::MULTILINE) if code !~ regex raise "No valid \"run FanforceApp\" line was found in your config.ru." end {:code => $1} end def self.extract_fanforce_config_block(lines, filename) code = lines.join('') 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) if code !~ regex || ($2 == '{' and $5 != '}') || ($2 == 'do' and $5 != 'end') return nil end { :code => $1, :start_keyword => $2, :config_var => $3, :config_code => $4, :end_keyword => $5 } end def self.extract_fanforce_config_options(lines, filename) block = extract_fanforce_config_block(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 def self.fanforce_config_lines(app_id) lines = {} return lines end def self.fanforce_config_keys_to_require [] end def self.fanforce_config_keys_to_overwrite keys = [] return keys end # GEMFILE ####################################################################### def self.create_gemfile(app) file = gemfile_source_lines.join("\n") + "\n" file += gemfile_ruby_version + "\n\n" file += gemfile_factory_line + "\n\n" File.open("#{app.dir}/Gemfile", 'w') {|f| f.write file } end def self.update_gemfile(app) return create_gemfile(app) if !File.exists?("#{app.dir}/Gemfile") lines = File.open("#{app.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-app-factory'") ? gemfile_factory_line : l end lines << gemfile_factory_line if !lines.include?(gemfile_factory_line) File.open("#{app.dir}/Gemfile", 'w') {|f| f.write(lines.join "\n") } end def self.gemfile_source_lines [ "source 'https://rubygems.org'", ] end def self.gemfile_ruby_version "ruby '1.9.3'" end def self.gemfile_factory_line line = "gem 'fanforce-app-factory'" return line if !$Config[:app_factory_gem].is_a?(Hash) line += ", '#{$Config[:app_factory_gem][:version]}'" if $Config[:app_factory_gem][:version].present? line += ", :path => '#{$Config[:app_factory_gem][:path]}'" if $Config[:app_factory_gem][:path].present? line += ", :git => '#{$Config[:app_factory_gem][:git]}'" if $Config[:app_factory_gem][:git].present? line end # .GITIGNORE ####################################################################### def self.create_gitignore(app) file = gitignore_lines.join("\n") + "\n" File.open("#{app.dir}/.gitignore", 'w') {|f| f.write file } end def self.update_gitignore(app) return create_gitignore(app) if !File.exists?("#{app.dir}/.gitignore") lines = File.open("#{app.dir}/.gitignore", 'r') {|f| f.readlines}.map {|l| l.strip } gitignore_lines.each do |line| lines << line if !lines.include?(line) end File.open("#{app.dir}/.gitignore", 'w') {|f| f.write(lines.join "\n") } end def self.gitignore_lines %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 ####################################################################### def self.create_powenv(app) file = powenv_lines.join("\n") + "\n" File.open("#{app.dir}/.powenv", 'w') {|f| f.write file } end def self.update_powenv(app) return create_powenv(app) if !File.exists?("#{app.dir}/.powenv") lines = File.open("#{app.dir}/.powenv", 'r') {|f| f.readlines}.map {|l| l.strip } powenv_lines.each do |line| lines << line if !lines.include?(line) end File.open("#{app.dir}/.powenv", 'w') {|f| f.write(lines.join "\n") } end def self.powenv_lines ['source .appenv'] end end