aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/artifact/moved.rb
blob: f5ef790ebb0644cac04c94d4764d27ce8e57ff87 (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
89
require "hbc/artifact/relocated"

module Hbc
  module Artifact
    class Moved < Relocated
      def self.english_description
        "#{english_name}s"
      end

      def install_phase(**options)
        move(**options)
      end

      def uninstall_phase(**options)
        move_back(**options)
      end

      def summarize_installed
        if target.exist?
          "#{printable_target} (#{target.abv})"
        else
          Formatter.error(printable_target, label: "Missing #{self.class.english_name}")
        end
      end

      private

      def move(force: false, command: nil, **options)
        if Utils.path_occupied?(target)
          message = "It seems there is already #{self.class.english_article} #{self.class.english_name} at '#{target}'"
          raise CaskError, "#{message}." unless force
          opoo "#{message}; overwriting."
          delete(target, force: force, command: command, **options)
        end

        unless source.exist?
          raise CaskError, "It seems the #{self.class.english_name} source '#{source}' is not there."
        end

        ohai "Moving #{self.class.english_name} '#{source.basename}' to '#{target}'."
        target.dirname.mkpath

        if target.parent.writable?
          FileUtils.move(source, target)
        else
          command.run("/bin/mv", args: [source, target], sudo: true)
        end

        add_altname_metadata(target, source.basename, command: command)
      end

      def move_back(skip: false, force: false, command: nil, **options)
        if Utils.path_occupied?(source)
          message = "It seems there is already #{self.class.english_article} #{self.class.english_name} at '#{source}'"
          raise CaskError, "#{message}." unless force
          opoo "#{message}; overwriting."
          delete(source, force: force, command: command, **options)
        end

        unless target.exist?
          return if skip
          raise CaskError, "It seems the #{self.class.english_name} source '#{target}' is not there."
        end

        ohai "Moving #{self.class.english_name} '#{target.basename}' back to '#{source}'."
        source.dirname.mkpath

        if source.parent.writable?
          FileUtils.move(target, source)
        else
          command.run("/bin/mv", args: [target, source], sudo: true)
        end
      end

      def delete(target, force: false, command: nil, **_)
        ohai "Removing #{self.class.english_name} '#{target}'."
        raise CaskError, "Cannot remove undeletable #{self.class.english_name}." if MacOS.undeletable?(target)

        return unless Utils.path_occupied?(target)

        if target.parent.writable? && !force
          target.rmtree
        else
          Utils.gain_permissions_remove(target, command: command)
        end
      end
    end
  end
end