class Gitenv::Copy

Public Class Methods

new(context, file, options = {}) click to toggle source
# File lib/gitenv/actions/copy.rb, line 26
def initialize(context, file, options = {})
  @context = context
  @file = file
  @as = options[:as]
  @overwrite = options[:overwrite]
  @backup = options[:backup]
  @mkdir = options.fetch :mkdir, true
  @backup = true if @overwrite && !options.key?(:backup)
end

Public Instance Methods

apply() click to toggle source
# File lib/gitenv/actions/copy.rb, line 36
def apply
  # TODO: test with mkdir option set to false
  FileUtils.mkdir_p File.dirname(target) if @mkdir
  backup_exists = File.exist? target_backup
  FileUtils.mv target, target_backup if @backup && File.exist?(target) && !backup_exists
  FileUtils.rm target if @overwrite && File.exist?(target) && !backup_exists
  FileUtils.cp origin, target unless File.exist?(target)
end
origin() click to toggle source
# File lib/gitenv/actions/copy.rb, line 64
def origin
  @origin ||= File.join(*[@context.from, @file].compact)
end
status() click to toggle source
# File lib/gitenv/actions/copy.rb, line 49
def status
  if !File.exist?(target)
    Status.missing 'is not set up; apply will create the copy'
  elsif @overwrite == false || digest(origin) == digest(target)
    Status.ok 'ok'
  elsif !@overwrite
    Status.error 'already exists; enable overwrite if you want to replace it'
  elsif @backup && File.exist?(target_backup)
    Status.error 'already exists with backup copy'
  else
    backup_notice = (' backup the file and' if @backup)
    Status.warning "already exists; apply will#{backup_notice} overwrite"
  end
end
target() click to toggle source
# File lib/gitenv/actions/copy.rb, line 68
def target
  @target ||= File.join(*[@context.to, target_name].compact)
end
target_backup() click to toggle source
# File lib/gitenv/actions/copy.rb, line 72
def target_backup
  @target_backup ||= "#{target}.orig"
end
to_s() click to toggle source
# File lib/gitenv/actions/copy.rb, line 45
def to_s
  "#{Paint[target, :cyan]} #{Paint['<', :bold]} #{origin}"
end

Private Instance Methods

digest(file) click to toggle source
# File lib/gitenv/actions/copy.rb, line 82
def digest(file)
  Digest::SHA1.new.tap do |dig|
    File.open(file, 'rb') { |io| dig.update io.readpartial(4096) until io.eof }
  end
end
target_name() click to toggle source
# File lib/gitenv/actions/copy.rb, line 78
def target_name
  @target_name ||= @as || @file
end