aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/container/base.rb
blob: 52eb5aab173c18fccf295a937eb0ebdfe9d365d0 (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
module Hbc
  class Container
    class Base
      def initialize(cask, path, command, nested: false, verbose: false)
        @cask = cask
        @path = path
        @command = command
        @nested = nested
        @verbose = verbose
      end

      def verbose?
        @verbose
      end

      def extract_nested_inside(dir)
        children = Pathname.new(dir).children

        nested_container = children[0]

        unless children.count == 1 &&
               !nested_container.directory? &&
               @cask.artifacts[:nested_container].empty? &&
               extract_nested_container(nested_container)

          children.each do |src|
            dest = @cask.staged_path.join(src.basename)
            FileUtils.rm_r(dest) if dest.exist?
            FileUtils.mv(src, dest)
          end
        end
      end

      def extract_nested_container(source)
        container = Container.for_path(source, @command)

        return false unless container

        ohai "Extracting nested container #{source.basename}"
        container.new(@cask, source, @command, nested: true, verbose: verbose?).extract

        true
      end
    end
  end
end