Class: Macinbox::Actions::InstallBox

Inherits:
Object
  • Object
show all
Defined in:
lib/macinbox/actions/install_box.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ InstallBox

Returns a new instance of InstallBox.

Raises:



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/macinbox/actions/install_box.rb', line 14

def initialize(opts)
  @input_box   = opts[:box_path]   or raise ArgumentError.new(":box_path not specified")
  @box_name    = opts[:box_name]   or raise ArgumentError.new(":box_name not specified")
  @box_format  = opts[:box_format] or raise ArgumentError.new(":box_format not specified")
  @boxes_dir   = opts[:boxes_dir]  or raise ArgumentError.new(":boxes_dir not specified")

  @box_version = opts[:macos_version].to_s rescue nil

  raise Macinbox::Error.new("box not found: #{@input_box}") unless File.exist? @input_box
  raise Macinbox::Error.new("boxes directory not found: #{@boxes_dir}") unless File.exist? @boxes_dir
end

Instance Method Details

#runObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/macinbox/actions/install_box.rb', line 30

def run
  Logger.info "Copying box to #{@boxes_dir}..." do
    if !@box_version || File.exist?(target_box_dir)
      @box_version = Dir["#{@boxes_dir}/#{@box_name}/*/*"].map { |o| o.split('/')[-2].to_i }.sort.last.next rescue 0
    end
    raise Macinbox::Error.new("box already exists") if File.exist? target_box_dir
    Task.run %W[ /bin/mkdir -p #{target_box_dir} ]
    Macinbox::copyfiles(from: Dir["#{@input_box}/*"], to: target_box_dir, recursive: true)
    Task.run %W[ /usr/sbin/chown -R #{ENV["SUDO_USER"]} #{@boxes_dir}/#{@box_name} ]
    Logger.info "Installed box: #{@box_name} (#{@box_format}, #{@box_version})"
  end
end

#target_box_dirObject



26
27
28
# File 'lib/macinbox/actions/install_box.rb', line 26

def target_box_dir
  "#{@boxes_dir}/#{@box_name}/#{@box_version}/#{@box_format}"
end