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.



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

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.



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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.detect_type_by_contents(path) ⇒ Object



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

def self.detect_type_by_contents path
  ASSET_TYPES.keys.detect {|key|
    shell("file '#{path}'")[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:



160
161
162
163
164
165
166
167
168
169
# File 'lib/babushka/asset.rb', line 160

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



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

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
  BuildPrefix
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].map {|i|
      /\.#{i}$/
    }.any? {|dont_descend|
      dir[dont_descend]
    }
  }.first
end

#extract(&block) ⇒ Object



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

def extract &block
  cd(build_prefix, :create => true) { process_extract(&block) }
end

#filenameObject



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

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



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

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_block "Cleaning up" do
        (build_prefix / name).p.rm
      end
    else
      LogHelpers.log "The build artefacts are in #{build_prefix / name / content_subdir}."
    end
  }
end

#supported?Boolean

Returns:

  • (Boolean)


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

def supported?
  !type.nil?
end

#typeObject



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

def type
  self.class.type path
end