Class: Mamiya::Package
- Inherits:
-
Object
- Object
- Mamiya::Package
- 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
- #meta ⇒ Object
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #application ⇒ Object
- #build!(build_dir, exclude_from_package: [], dereference_symlinks: false, package_under: nil, logger: Mamiya::Logger.new) ⇒ Object
- #checksum ⇒ Object
- #exists? ⇒ Boolean (also: #exist?)
- #extract_onto!(destination) ⇒ Object
-
#initialize(path) ⇒ Package
constructor
A new instance of Package.
- #meta_path ⇒ Object
- #name ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(path) ⇒ Package
Returns a new instance of Package.
14 15 16 17 18 19 |
# File 'lib/mamiya/package.rb', line 14 def initialize(path) @path_without_ext = Pathname.new(path.sub(PATH_SUFFIXES, '')) @meta_loaded_from_file = false @loaded_meta = nil # load end |
Instance Attribute Details
#meta ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mamiya/package.rb', line 36 def if !@meta_loaded_from_file && .exist? @meta_loaded_from_file = true = () if @loaded_meta == @meta @loaded_meta = @meta = () end end @meta ||= {} end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
21 22 23 |
# File 'lib/mamiya/package.rb', line 21 def path @path end |
Instance Method Details
#application ⇒ Object
48 49 50 |
# File 'lib/mamiya/package.rb', line 48 def application ['application'] || [:application] end |
#build!(build_dir, exclude_from_package: [], dereference_symlinks: false, package_under: nil, logger: Mamiya::Logger.new) ⇒ Object
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 104 |
# File 'lib/mamiya/package.rb', line 68 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 = build_dir.join('.mamiya.meta.json') ['name'] = self.name File.write , self..to_json Mamiya.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 ['checksum'] = checksum File.write , self..to_json nil ensure if && .exist? .delete() end end |
#checksum ⇒ Object
52 53 54 55 |
# File 'lib/mamiya/package.rb', line 52 def checksum return nil unless exist? Digest::SHA2.file(path).hexdigest end |
#exists? ⇒ Boolean Also known as: exist?
63 64 65 |
# File 'lib/mamiya/package.rb', line 63 def exists? path.exist? end |
#extract_onto!(destination) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mamiya/package.rb', line 106 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_path ⇒ Object
32 33 34 |
# File 'lib/mamiya/package.rb', line 32 def Pathname.new(@path_without_ext.to_s + '.json') end |
#name ⇒ Object
24 25 26 |
# File 'lib/mamiya/package.rb', line 24 def name ['name'] || @path_without_ext.basename.to_s end |