Class: Babushka::Asset

Inherits:
Object show all
Extended by:
ShellHelpers
Includes:
PathHelpers, ShellHelpers
Defined in:
lib/babushka/asset.rb,
lib/babushka/asset.rb

Direct Known Subclasses

DmgAsset, FileAsset, TarAsset, ZipAsset

Constant Summary collapse

ASSET_TYPES =
{
  :deb =>   {:exts => %w[deb],          :class => FileAsset, :file_match => 'Debian binary package'},
  :pkg =>   {:exts => %w[pkg],          :class => FileAsset, :file_match => 'xar archive'},
  :tar =>   {:exts => %w[tar],          :class => TarAsset,  :file_match => 'tar archive'},
  :gzip =>  {:exts => %w[tgz tar.gz],   :class => TarAsset,  :file_match => 'gzip compressed data'},
  :bzip2 => {:exts => %w[tbz2 tar.bz2], :class => TarAsset,  :file_match => 'bzip2 compressed data'},
  :zip =>   {:exts => %w[zip],          :class => ZipAsset,  :file_match => 'Zip archive data'},
  :dmg =>   {:exts => %w[dmg],          :class => DmgAsset,  :file_match => 'VAX COFF executable not stripped'}
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellHelpers

cmd_dir, current_username, log_shell, login_shell, raw_shell, shell, shell!, shell?, shell_cmd, sudo, which

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Methods included from PathHelpers

cd, in_build_dir, in_download_dir

Constructor Details

#initialize(path, opts = {}) ⇒ Asset

Returns a new instance of Asset.


31
32
33
34
# File 'lib/babushka/asset.rb', line 31

def initialize path, opts = {}
  @path = path.p
  @name = ASSET_TYPES[type][:exts].inject(filename) {|fn,t| fn.gsub(/\.#{t}$/, '') }
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


29
30
31
# File 'lib/babushka/asset.rb', line 29

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path


29
30
31
# File 'lib/babushka/asset.rb', line 29

def path
  @path
end

Class Method Details

.detect_type_by_contents(path) ⇒ Object


18
19
20
21
22
23
# File 'lib/babushka/asset.rb', line 18

def self.detect_type_by_contents path
  file_output = shell("file '#{path}'")
  ASSET_TYPES.keys.detect {|key|
    file_output[ASSET_TYPES[key][:file_match]]
  }
end

.detect_type_by_extension(path) ⇒ Object


10
11
12
13
14
15
16
# File 'lib/babushka/asset.rb', line 10

def self.detect_type_by_extension path
  ASSET_TYPES.keys.detect {|key|
    ASSET_TYPES[key][:exts].any? {|extension|
      path.has_extension? extension
    }
  }
end

.for(path, opts = {}) ⇒ Object

Raises:


172
173
174
175
176
177
178
179
180
181
# File 'lib/babushka/asset.rb', line 172

def self.for path, opts = {}
  path = path.p
  filename = path.basename.to_s
  raise AssetError, "The archive #{filename} does not exist." unless path.exists?
  if (asset_type = ASSET_TYPES[type(path)]).nil?
    raise AssetError, "Don't know how to extract #{filename}."
  else
    asset_type[:class].new(path, opts)
  end
end

.type(path) ⇒ Object


25
26
27
# File 'lib/babushka/asset.rb', line 25

def self.type path
  detect_type_by_extension(path) || detect_type_by_contents(path)
end

Instance Method Details

#build_prefixObject


93
94
95
# File 'lib/babushka/asset.rb', line 93

def build_prefix
  BUILD_PREFIX
end

#content_subdirObject


72
73
74
75
76
77
78
79
80
# File 'lib/babushka/asset.rb', line 72

def content_subdir
  identity_dirs.reject {|dir|
    %w[app pkg bundle tmbundle prefPane lbaction].map {|i|
      /\.#{i}$/
    }.any? {|dont_descend|
      dir[dont_descend]
    }
  }.first
end

#extract(&block) ⇒ Object


48
49
50
51
# File 'lib/babushka/asset.rb', line 48

def extract &block
  rm_build_dir # A previous build might be in a bad state.
  cd(build_prefix, :create => true) { process_extract(&block) }
end

#filenameObject


36
37
38
# File 'lib/babushka/asset.rb', line 36

def filename
  path.basename.to_s
end

#identity_dirsObject


82
83
84
85
86
87
88
89
90
91
# File 'lib/babushka/asset.rb', line 82

def identity_dirs
  everything = Dir.glob('*')
  if everything.length == 1 && File.directory?(everything.first)
    everything
  else
    Dir.glob('*/').map {|dir| dir.chomp('/') }.select {|dir|
      dir.downcase.gsub(/[ \-_\.]/, '') == name.downcase.gsub(/[ \-_\.]/, '')
    }
  end
end

#process_extract(&block) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/babushka/asset.rb', line 53

def process_extract &block
  shell("mkdir -p '#{name}'") and
  cd(name) {
    unless ShellHelpers.log_shell("Extracting #{filename}", extract_command)
      LogHelpers.log_error "Couldn't extract #{path} - probably a bad download."
    else
      cd(content_subdir) {
        block.nil? or block.call(self)
      }
    end
  }.tap {|result|
    if !result
      LogHelpers.log "The build artefacts are in #{build_prefix / name / content_subdir}."
    elsif !block.nil?
      cleanup!
    end
  }
end

#supported?Boolean

Returns:

  • (Boolean)

44
45
46
# File 'lib/babushka/asset.rb', line 44

def supported?
  !type.nil?
end

#typeObject


40
41
42
# File 'lib/babushka/asset.rb', line 40

def type
  self.class.type path
end