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

    return false unless container

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

    true
  end
end