module RorVsWild::Deployment

def self.author

def self.author
  @author
end

def self.description

def self.description
  @description
end

def self.email

def self.email
  @email
end

def self.load_config(config)

def self.load_config(config)
  read
  if hash = config[:deployment]
    @description = hash[:description]
    @revision = hash[:revision]
    @author = hash[:author]
    @email = hash[:email]
  end
end

def self.normalize_string(string)

def self.normalize_string(string)
  if string
    string = string.strip
    string.empty? ? nil : string
  end
end

def self.rails

def self.rails
  Rails.version if defined?(Rails)
end

def self.read

def self.read
  read_from_heroku || read_from_scalingo || read_from_git || read_from_capistrano
end

def self.read_from_capistrano

def self.read_from_capistrano
  return unless File.readable?("REVISION")
  return unless @revision = File.read("REVISION")
  return unless stdout = shell("git --git-dir ../../repo log --format=%an%n%ae%n%B -n 1 #{@revision}")
  lines = stdout.lines
  @author = normalize_string(lines[0])
  @email = normalize_string(lines[1])
  @description = lines[2..-1] && normalize_string(lines[2..-1].join)
  @revision
end

def self.read_from_git

def self.read_from_git
  return unless @revision = normalize_string(shell("git rev-parse HEAD"))
  return @revision unless log_stdout = shell("git log -1 --pretty=%an%n%ae%n%B")
  lines = log_stdout.lines
  @author = normalize_string(lines[0])
  @email = normalize_string(lines[1])
  @description = lines[2..-1] && normalize_string(lines[2..-1].join)
  @revision
end

def self.read_from_heroku

def self.read_from_heroku
  return unless ENV["HEROKU_SLUG_COMMIT"]
  @revision = ENV["HEROKU_SLUG_COMMIT"]
  @description = ENV["HEROKU_SLUG_DESCRIPTION"]
end

def self.read_from_scalingo

def self.read_from_scalingo
  @revision = ENV["CONTAINER_VERSION"] || ENV["SOURCE_VERSION"]
end

def self.revision

def self.revision
  @revision
end

def self.rorvswild

def self.rorvswild
  RorVsWild::VERSION
end

def self.ruby

def self.ruby
  RUBY_VERSION
end

def self.shell(command)

def self.shell(command)
  stdout, stderr, process = Open3.capture3(command) rescue nil
  stdout if process && process.success?
end

def self.to_h

def self.to_h
  @to_h ||= {revision: revision, description: description, author: author, email: email, ruby: ruby, rails: rails, rorvswild: rorvswild}.compact
end