lib/switches.rb in switches-0.1.6 vs lib/switches.rb in switches-0.1.7

- old
+ new

@@ -1,75 +1,93 @@ require 'yaml' require 'pp' require 'fileutils' require 'active_support' +require 'active_support/core_ext/module/attribute_accessors' -# TODO not agnostic, expects RAILS_ROOT - module Switches - CONFIG_DIR = File.join RAILS_ROOT, 'config', 'switches' - RAKE_PATH = File.join RAILS_ROOT, 'lib', 'tasks', 'switches.rake' - CAPISTRANO_PATH = File.join CONFIG_DIR, 'capistrano_tasks.rb' - CAPISTRANO_LOAD_PATH = CAPISTRANO_PATH.gsub "#{RAILS_ROOT}/", '' # => 'config/switches/capistrano_tasks.rb' - CAPFILE_PATH = File.join RAILS_ROOT, 'Capfile' - CURRENT_PATH = File.join CONFIG_DIR, 'current.yml' - DEFAULT_PATH = File.join CONFIG_DIR, 'default.yml' - BACKUP_PATH = File.join CONFIG_DIR, 'backup.yml' - TRANSACTION_PID_PATH = File.join CONFIG_DIR, 'transaction.pid' - + mattr_accessor :root_path class << self + def config_dir + @_config_dir ||= File.join root_path, 'config', 'switches' + end + def rake_path + @_rake_path ||= File.join root_path, 'lib', 'tasks', 'switches.rake' + end + def capistrano_path + @_capistrano_path ||= File.join config_dir, 'capistrano_tasks.rb' + end + def capistrano_load_path + @_capistrano_load_path ||= capistrano_path.gsub "#{root_path}/", '' # => 'config/switches/capistrano_tasks.rb' + end + def capfile_path + @_capfile_path ||= File.join root_path, 'Capfile' + end + def current_path + @_current_path ||= File.join config_dir, 'current.yml' + end + def default_path + @_default_path ||= File.join config_dir, 'default.yml' + end + def backup_path + @_backup_path ||= File.join config_dir, 'backup.yml' + end + def transaction_pid_path + @_transaction_pid_path ||= File.join config_dir, 'transaction.pid' + end + def dump(method) if ENV['SWITCHES_XML'] == 'true' puts send(method).to_xml else pp send(method) end end def say(str) - $stderr.puts "[SWITCHES GEM] #{str.gsub "#{RAILS_ROOT}/", ''}" + $stderr.puts "[SWITCHES GEM] #{str.gsub "#{root_path}/", ''}" end def setup - say "Making #{CONFIG_DIR}." - FileUtils.mkdir_p CONFIG_DIR + say "Making #{config_dir}." + FileUtils.mkdir_p config_dir - if File.exists? DEFAULT_PATH - say "Not putting an example default.yml into #{DEFAULT_PATH} because you already have one." + if File.exists? default_path + say "Not putting an example default.yml into #{default_path} because you already have one." else - say "Putting an example default.yml into #{DEFAULT_PATH}." - File.open(DEFAULT_PATH, 'w') { |f| f.write({ 'quick_brown' => true, 'fox_jumps' => false }.to_yaml) } + say "Putting an example default.yml into #{default_path}." + File.open(default_path, 'w') { |f| f.write({ 'quick_brown' => true, 'fox_jumps' => false }.to_yaml) } end - say "Refreshing gem-related Rake tasks at #{RAKE_PATH}." - FileUtils.cp File.join(File.dirname(__FILE__), 'tasks', 'switches.rake'), RAKE_PATH + say "Refreshing gem-related Rake tasks at #{rake_path}." + FileUtils.cp File.join(File.dirname(__FILE__), 'tasks', 'switches.rake'), rake_path - say "Refreshing gem-related Capistrano tasks at #{CAPISTRANO_PATH}." - FileUtils.cp File.join(File.dirname(__FILE__), 'tasks', 'capistrano_tasks.rb'), CAPISTRANO_PATH + say "Refreshing gem-related Capistrano tasks at #{capistrano_path}." + FileUtils.cp File.join(File.dirname(__FILE__), 'tasks', 'capistrano_tasks.rb'), capistrano_path needs_append = false - if not File.exists?(CAPFILE_PATH) + if not File.exists?(capfile_path) say "Creating a Capfile and including our tasks in it." needs_append = true - FileUtils.touch CAPFILE_PATH - elsif old_capfile = IO.read(CAPFILE_PATH) and old_capfile.include?(CAPISTRANO_LOAD_PATH) + FileUtils.touch capfile_path + elsif old_capfile = IO.read(capfile_path) and old_capfile.include?(capistrano_load_path) say "Found a Capfile that already includes our tasks. Great!" else say "I'm going to add a line to your existing Capfile. Sorry if I break anything!" needs_append = true end - File.open(CAPFILE_PATH, 'a') do |f| + File.open(capfile_path, 'a') do |f| say "Appending a line that loads our Capistrano tasks to your Capfile." - f.write "\n# Added by switches gem #{Time.now}\nload '#{CAPISTRANO_LOAD_PATH}'\n" + f.write "\n# Added by switches gem #{Time.now}\nload '#{capistrano_load_path}'\n" end if needs_append say "Don't forget to:" - say "* git add #{DEFAULT_PATH}" - say "* git add #{RAKE_PATH}" - say "* git ignore #{CAPISTRANO_PATH}" - say "* git ignore #{CURRENT_PATH}" + say "* git add #{default_path}" + say "* git add #{rake_path}" + say "* git ignore #{capistrano_path}" + say "* git ignore #{current_path}" say "You can refresh the gem tasks with Switches.setup. It won't touch anything else." end # taken from ActiveSupport::StringInquirer def method_missing(method_name, *args) @@ -90,26 +108,26 @@ end end def default return @_default unless @_default.nil? - # say "file system activity #{DEFAULT_PATH}" + # say "file system activity #{default_path}" resolve_transaction! - @_default = YAML.load(IO.read(DEFAULT_PATH)) + @_default = YAML.load(IO.read(default_path)) @_default.stringify_keys! rescue Errno::ENOENT - say "Couldn't read defaults from #{DEFAULT_PATH}." + say "Couldn't read defaults from #{default_path}." say "You probably want to run \"./script/runner 'Switches.setup'\"." raise $! end def current return @_current unless @_current.nil? resolve_transaction! - if File.exist?(CURRENT_PATH) - # say "file system activity #{CURRENT_PATH}" - @_current = YAML.load(IO.read(CURRENT_PATH)) + if File.exist?(current_path) + # say "file system activity #{current_path}" + @_current = YAML.load(IO.read(current_path)) @_current.stringify_keys! else @_current = default.dup end @_current @@ -149,39 +167,39 @@ current.delete name write_current end def reset - FileUtils.rm_f CURRENT_PATH + FileUtils.rm_f current_path @_current = nil end def backup write_current start_transaction! - # say "file system activity #{BACKUP_PATH}" - FileUtils.cp CURRENT_PATH, BACKUP_PATH + # say "file system activity #{backup_path}" + FileUtils.cp current_path, backup_path end def restore - if File.exist?(BACKUP_PATH) - FileUtils.mv BACKUP_PATH, CURRENT_PATH + if File.exist?(backup_path) + FileUtils.mv backup_path, current_path else - raise ArgumentError, "#{BACKUP_PATH} doesn't exist." + raise ArgumentError, "#{backup_path} doesn't exist." end end_transaction! @_current = nil end def write_current current # load it first! - File.open(CURRENT_PATH, 'w') { |f| f.write current.stringify_keys.to_yaml } + File.open(current_path, 'w') { |f| f.write current.stringify_keys.to_yaml } end def transaction_pid - # say "file system activity #{TRANSACTION_PID_PATH}" - IO.readlines(TRANSACTION_PID_PATH).first.chomp.to_i if File.exists?(TRANSACTION_PID_PATH) + # say "file system activity #{transaction_pid_path}" + IO.readlines(transaction_pid_path).first.chomp.to_i if File.exists?(transaction_pid_path) end def resolve_transaction! if transaction_pid.present? and transaction_pid != Process.pid say "Resolving... calling restore" @@ -190,14 +208,14 @@ end def start_transaction! resolve_transaction! say "Starting transaction" - File.open(TRANSACTION_PID_PATH, 'w') { |f| f.write Process.pid } + File.open(transaction_pid_path, 'w') { |f| f.write Process.pid } end def end_transaction! say "Finishing transaction" - FileUtils.rm_f TRANSACTION_PID_PATH + FileUtils.rm_f transaction_pid_path end end end