Class: Mamiya::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/mamiya/package.rb

Defined Under Namespace

Classes: InternalError, NotExists

Constant Summary collapse

PATH_SUFFIXES =
/\.(?:tar\.gz|json)\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Package

Returns a new instance of Package.



13
14
15
16
17
18
# File 'lib/mamiya/package.rb', line 13

def initialize(path)
  @path_without_ext = Pathname.new(path.sub(PATH_SUFFIXES, ''))
  @meta_loaded_from_file = false
  @loaded_meta = nil
  meta # load
end

Instance Attribute Details

#metaObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mamiya/package.rb', line 35

def meta
  if !@meta_loaded_from_file && meta_path.exist?
    @meta_loaded_from_file = true
    loaded_meta = load_meta()
    if @loaded_meta == @meta
      @loaded_meta = loaded_meta
      @meta = load_meta()
    end
  end
  @meta ||= {}
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/mamiya/package.rb', line 20

def path
  @path
end

Instance Method Details

#applicationObject



47
48
49
# File 'lib/mamiya/package.rb', line 47

def application
  meta['application'] || meta[:application]
end

#build!(build_dir, exclude_from_package: [], dereference_symlinks: false, package_under: nil, logger: Mamiya::Logger.new) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mamiya/package.rb', line 67

def build!(build_dir, exclude_from_package: [], dereference_symlinks: false, package_under: nil, logger: Mamiya::Logger.new)
  logger = logger['Package']

  exclude_from_package.push('.svn', '.git').uniq!

  build_dir = Pathname.new(build_dir)
  build_dir += package_under if package_under
  meta_in_build = build_dir.join('.mamiya.meta.json')

  meta['name'] = self.name
  File.write meta_in_build, self.meta.to_json

  Dir.chdir(build_dir) do
    excludes = exclude_from_package.flat_map { |exclude| ['--exclude', exclude] }
    dereference = dereference_symlinks ? ['-h'] : []

    cmd = ["tar", "czf", self.path.to_s,
           *dereference,
           *excludes,
           "."]

    logger.debug "$ #{cmd.join(' ')}"
    result = system(*cmd)
    raise InternalError, "failed to run: #{cmd.inspect}" unless result
  end

  checksum = self.checksum()
  raise InternalError, 'checksum should not be nil after package built' unless checksum
  meta['checksum'] = checksum

  File.write meta_path, self.meta.to_json
  nil
ensure
  if meta_in_build && meta_in_build.exist?
    meta_in_build.delete()
  end
end

#checksumObject



51
52
53
54
# File 'lib/mamiya/package.rb', line 51

def checksum
  return nil unless exist?
  Digest::SHA2.file(path).hexdigest
end

#exists?Boolean Also known as: exist?

Returns:

  • (Boolean)


62
63
64
# File 'lib/mamiya/package.rb', line 62

def exists?
  path.exist?
end

#extract_onto!(destination) ⇒ Object

Raises:



105
106
107
108
109
110
111
112
113
114
# File 'lib/mamiya/package.rb', line 105

def extract_onto!(destination)
  raise NotExists unless exist?
  Dir.mkdir(destination) unless File.directory?(destination)

  cmd = ["tar", "xf", path.to_s, "-C", destination.to_s]
  result = system(*cmd)
  raise InternalError, "Failed to run: #{cmd.inspect}" unless result

  nil
end

#meta_pathObject



31
32
33
# File 'lib/mamiya/package.rb', line 31

def meta_path
  Pathname.new(@path_without_ext.to_s + '.json')
end

#nameObject



23
24
25
# File 'lib/mamiya/package.rb', line 23

def name
  meta['name'] || @path_without_ext.basename.to_s
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:



56
57
58
59
60
# File 'lib/mamiya/package.rb', line 56

def valid?
  raise NotExists, 'package not exist' unless exist?
  raise NotExists, 'meta not exist' unless meta_path.exist?
  !meta['checksum'] || checksum == meta['checksum']
end