Class: Vanagon::Component::Source::Local
- Inherits:
-
Object
- Object
- Vanagon::Component::Source::Local
- Defined in:
- lib/vanagon/component/source/local.rb
Direct Known Subclasses
Constant Summary collapse
- ARCHIVE_EXTENSIONS =
Extensions for files we intend to unpack during the build
{ "7z" => %w[.7z], "bzip2" => %w[.bz2 .bz], "cpio" => %w[.cpio], "gzip" => %w[.gz .z], "rar" => %w[.rar], "tar" => %w[.tar], "tbz2" => %w[.tar.bz2 .tbz2 .tbz], "tgz" => %w[.tar.gz .tgz], "txz" => %w[.tar.xz .txz], "xz" => %w[.xz], "zip" => %w[.zip], }.freeze
Instance Attribute Summary collapse
-
#cleanup ⇒ String
Return the correct incantation to cleanup the source archive and source directory for a given source.
-
#extension ⇒ Object
rubocop:disable Lint/DuplicateMethods.
-
#file ⇒ Object
rubocop:disable Lint/DuplicateMethods.
-
#url ⇒ Object
Returns the value of attribute url.
-
#workdir ⇒ Object
Returns the value of attribute workdir.
Class Method Summary collapse
- .archive_extensions ⇒ Object
-
.mangle(path) ⇒ Object
If a scheme is specified as “file://”, this will return strip off the scheme and delimiters – we need to do this because once upon a time we allowed specifying files with no strong specifications for where they should be located.
- .valid_file?(target_file) ⇒ Boolean
Instance Method Summary collapse
- #archive? ⇒ Boolean
- #archive_extensions ⇒ Object
-
#copy ⇒ Object
(also: #fetch)
Moves file from source to workdir.
- #decompressor ⇒ Object
-
#dirname ⇒ String
The dirname to reference when building from the source.
-
#extname ⇒ String
Returns the extension for @file.
-
#extract(tar = "tar") ⇒ String?
Gets the command to extract the archive given if needed (uses @extension).
-
#initialize(path, workdir:, **options) ⇒ Local
constructor
Constructor for the File source type.
-
#mangle(path) ⇒ Object
Wrapper around the class method ‘.mangle’.
-
#verify ⇒ Object
Local files need no checksum so this is a noop.
Constructor Details
#initialize(path, workdir:, **options) ⇒ Local
Constructor for the File source type
47 48 49 50 |
# File 'lib/vanagon/component/source/local.rb', line 47 def initialize(path, workdir:, **) @url = ::Pathname.new(mangle(path)) @workdir = ::Pathname.new(workdir) end |
Instance Attribute Details
#cleanup ⇒ String
Return the correct incantation to cleanup the source archive and source directory for a given source
119 120 121 |
# File 'lib/vanagon/component/source/local.rb', line 119 def cleanup @cleanup end |
#extension ⇒ Object
rubocop:disable Lint/DuplicateMethods
71 72 73 |
# File 'lib/vanagon/component/source/local.rb', line 71 def extension @extension end |
#file ⇒ Object
rubocop:disable Lint/DuplicateMethods
67 68 69 |
# File 'lib/vanagon/component/source/local.rb', line 67 def file @file end |
#url ⇒ Object
Returns the value of attribute url.
8 9 10 |
# File 'lib/vanagon/component/source/local.rb', line 8 def url @url end |
#workdir ⇒ Object
Returns the value of attribute workdir.
8 9 10 |
# File 'lib/vanagon/component/source/local.rb', line 8 def workdir @workdir end |
Class Method Details
.archive_extensions ⇒ Object
38 39 40 |
# File 'lib/vanagon/component/source/local.rb', line 38 def archive_extensions ARCHIVE_EXTENSIONS.values.flatten end |
.mangle(path) ⇒ Object
If a scheme is specified as “file://”, this will return strip off the scheme and delimiters – we need to do this because once upon a time we allowed specifying files with no strong specifications for where they should be located.
34 35 36 |
# File 'lib/vanagon/component/source/local.rb', line 34 def mangle(path) path.gsub(%r{^file://}, '') end |
.valid_file?(target_file) ⇒ Boolean
26 27 28 |
# File 'lib/vanagon/component/source/local.rb', line 26 def valid_file?(target_file) File.exist?(mangle(target_file.to_s)) end |
Instance Method Details
#archive? ⇒ Boolean
136 137 138 |
# File 'lib/vanagon/component/source/local.rb', line 136 def archive? archive_extensions.include?(extension) end |
#archive_extensions ⇒ Object
132 133 134 |
# File 'lib/vanagon/component/source/local.rb', line 132 def archive_extensions self.class.archive_extensions end |
#copy ⇒ Object Also known as: fetch
Moves file from source to workdir
60 61 62 63 64 |
# File 'lib/vanagon/component/source/local.rb', line 60 def copy VanagonLogger.info "Copying file '#{url.basename}' to workdir" FileUtils.cp_r(url, file) end |
#decompressor ⇒ Object
140 141 142 |
# File 'lib/vanagon/component/source/local.rb', line 140 def decompressor @decompressor ||= ARCHIVE_EXTENSIONS.select { |k, v| v.include? extension }.keys.first end |
#dirname ⇒ String
The dirname to reference when building from the source
148 149 150 151 152 153 154 155 156 |
# File 'lib/vanagon/component/source/local.rb', line 148 def dirname # We are not treating file as a Pathname since other sources can inherit from this class # which could cause file to be a URI instead of a string. if archive? || File.directory?(file) File.basename(file, extension) else './' end end |
#extname ⇒ String
Returns the extension for @file
126 127 128 129 130 |
# File 'lib/vanagon/component/source/local.rb', line 126 def extname extension_match = file.to_s.match %r{#{Regexp.union(archive_extensions)}\Z} return extension_match.to_s if extension_match File.extname(file) end |
#extract(tar = "tar") ⇒ String?
Gets the command to extract the archive given if needed (uses @extension)
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 105 106 107 108 109 110 111 112 113 |
# File 'lib/vanagon/component/source/local.rb', line 80 def extract(tar = "tar") # rubocop:disable Metrics/AbcSize # Extension does not appear to be an archive, so "extract" is a no-op return ': nothing to extract' unless archive_extensions.include?(extension) case decompressor when "7z" %(7z x "#{file}") when "bzip2" %(bunzip2 "#{file}") when "cpio" %( mkdir "#{file.basename}" && pushd "#{file.basename}" 2>&1 > /dev/null && cpio -idv < "#{file}" && popd 2>&1 > /dev/null ).undent when "gzip" %(gunzip "#{file}") when "rar" %(unrar x "#{file}") when "tar", "txz" %(#{tar} xf "#{file}") when "tbz2" %(bunzip2 -c "#{file}" | #{tar} xf -) when "tgz" %(gunzip -c "#{file}" | #{tar} xf -) when "xz" %(unxz "#{file}") when "zip" "unzip -d '#{File.basename(file, '.zip')}' '#{file}' || 7za x -r -tzip -o'#{File.basename(file, '.zip')}' '#{file}'" else raise Vanagon::Error, "Don't know how to decompress #{extension} archives" end end |
#mangle(path) ⇒ Object
Wrapper around the class method ‘.mangle’
159 160 161 |
# File 'lib/vanagon/component/source/local.rb', line 159 def mangle(path) self.class.mangle(path) end |
#verify ⇒ Object
Local files need no checksum so this is a noop
53 54 55 |
# File 'lib/vanagon/component/source/local.rb', line 53 def verify # nothing to do here, so just return end |