blob: 8565ab836047ca18767f021cd8aa2f9507642693 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  | 
require "hbc/artifact/relocated"
class Hbc::Artifact::Moved < Hbc::Artifact::Relocated
  def self.english_description
    "#{artifact_english_name}s"
  end
  def install_phase
    each_artifact do |artifact|
      load_specification(artifact)
      next unless preflight_checks
      delete if Hbc::Utils.path_occupied?(target) && force
      move
    end
  end
  def uninstall_phase
    each_artifact do |artifact|
      load_specification(artifact)
      next unless File.exist?(target)
      delete
    end
  end
  private
  def each_artifact
    # the sort is for predictability between Ruby versions
    @cask.artifacts[self.class.artifact_dsl_key].sort.each do |artifact|
      yield artifact
    end
  end
  def move
    ohai "Moving #{self.class.artifact_english_name} '#{source.basename}' to '#{target}'"
    target.dirname.mkpath
    FileUtils.move(source, target)
    add_altname_metadata target, source.basename.to_s
  end
  def preflight_checks
    if Hbc::Utils.path_occupied?(target)
      if force
        ohai(warning_target_exists { |s| s << "overwriting." })
      else
        ohai(warning_target_exists { |s| s << "not moving." })
        return false
      end
    end
    unless source.exist?
      message = "It seems the #{self.class.artifact_english_name} source is not there: '#{source}'"
      raise Hbc::CaskError, message
    end
    true
  end
  def warning_target_exists
    message_parts = [
                      "It seems there is already #{self.class.artifact_english_article} #{self.class.artifact_english_name} at '#{target}'",
                    ]
    yield(message_parts) if block_given?
    message_parts.join("; ")
  end
  def delete
    ohai "Removing #{self.class.artifact_english_name}: '#{target}'"
    if MacOS.undeletable?(target)
      raise Hbc::CaskError, "Cannot remove undeletable #{self.class.artifact_english_name}"
    elsif force
      Hbc::Utils.gain_permissions_remove(target, command: @command)
    else
      target.rmtree
    end
  end
  def summarize_artifact(artifact_spec)
    load_specification artifact_spec
    if target.exist?
      target_abv = " (#{target.abv})"
    else
      warning = "Missing #{self.class.artifact_english_name}"
      warning = "#{Tty.red}#{warning}#{Tty.reset}: "
    end
    "#{warning}#{printable_target}#{target_abv}"
  end
end
  |