Class: Gitenv::Copy

Inherits:
Object
  • Object
show all
Defined in:
lib/gitenv/actions/copy.rb

Defined Under Namespace

Classes: Action

Instance Method Summary collapse

Constructor Details

#initialize(context, file, options = {}) ⇒ Copy

Returns a new instance of Copy.



26
27
28
29
30
31
32
33
34
# 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

Instance Method Details

#applyObject



36
37
38
39
40
41
42
43
# 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

#originObject



64
65
66
# File 'lib/gitenv/actions/copy.rb', line 64

def origin
  @origin ||= File.join(*[@context.from, @file].compact)
end

#statusObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

#targetObject



68
69
70
# File 'lib/gitenv/actions/copy.rb', line 68

def target
  @target ||= File.join(*[@context.to, target_name].compact)
end

#target_backupObject



72
73
74
# File 'lib/gitenv/actions/copy.rb', line 72

def target_backup
  @target_backup ||= "#{target}.orig"
end

#to_sObject



45
46
47
# File 'lib/gitenv/actions/copy.rb', line 45

def to_s
  "#{Paint[target, :cyan]} #{Paint['<', :bold]} #{origin}"
end