File indexing completed on 2022-09-27 15:18:58

0001 module Debian
0002   # debian/source representation
0003   class Source
0004     # Represents a dpkg-source format. See manpage.
0005     class Format
0006       attr_reader :version
0007       attr_reader :type
0008 
0009       def initialize(str)
0010         @version = '1'
0011         @type = nil
0012         parse(str) if str
0013       end
0014 
0015       def to_s
0016         return @version unless type
0017         "#{version} (#{type})"
0018       end
0019 
0020       private
0021 
0022       def parse(str)
0023         str = str.read if str.respond_to?(:read)
0024         str = File.read(str) if File.exist?(str)
0025         data = str.strip
0026         match = data.match(/(?<version>[^\s]+)(\s+\((?<type>.*)\))?/)
0027         @version = match[:version]
0028         @type = match[:type].to_sym if match[:type]
0029       end
0030     end
0031 
0032     attr_reader :format
0033 
0034     def initialize(package_path)
0035       @package_path = package_path
0036       raise 'not a package path' unless Dir.exist?("#{package_path}/debian")
0037       parse
0038     end
0039 
0040     private
0041 
0042     def parse
0043       @format = Format.new("#{@package_path}/debian/source/format")
0044     end
0045   end
0046 end