Sha256: a33af69ed094611332471134f45e5491dee2f89fb7cae810340f0b84b998d7fb

Contents?: true

Size: 1.77 KB

Versions: 21

Compression:

Stored size: 1.77 KB

Contents

require 'toml'

module Bibliothecary
  module Parsers
    class Cargo
      PLATFORM_NAME = 'cargo'

      def self.parse(filename, file_contents)
        toml = TOML.parse(file_contents)
        if filename.match(/^Cargo\.toml$/)
          parse_manifest(toml)
        elsif filename.match(/^Cargo\.lock$/)
          parse_lockfile(toml)
        else
          []
        end
      end

      def self.analyse(folder_path, file_list)
        [analyse_cargo_toml(folder_path, file_list),
          analyse_cargo_lock(folder_path, file_list)]
      end

      def self.analyse_cargo_toml(folder_path, file_list)
        path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Cargo\.toml$/) }
        return unless path

        manifest = TOML.load_file(path)

        {
          platform: PLATFORM_NAME,
          path: path,
          dependencies: parse_manifest(manifest)
        }
      end

      def self.parse_manifest(manifest)
        manifest.fetch('dependencies', []).map do |name, requirement|
          {
            name: name,
            requirement: requirement,
            type: 'runtime'
          }
        end
      end

      def self.analyse_cargo_lock(folder_path, file_list)
        path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Cargo\.lock$/) }
        return unless path

        manifest = TOML.load_file(path)

        {
          platform: PLATFORM_NAME,
          path: path,
          dependencies: parse_lockfile(manifest)
        }
      end

      def self.parse_lockfile(manifest)
        manifest.fetch('package',[]).map do |dependency|
          {
            name: dependency['name'],
            requirement: dependency['version'],
            type: 'runtime'
          }
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
bibliothecary-0.16.1 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.16.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.15.2 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.15.1 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.15.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.14.1 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.14.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.13.1 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.13.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.12.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.11.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.10.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.9.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.8.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.7.1 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.7.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.6.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.5.0 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.4.1 lib/bibliothecary/parsers/cargo.rb
bibliothecary-0.4.0 lib/bibliothecary/parsers/cargo.rb