<commit_before>require 'json'
require 'yaml'

require 'librarian/manifest'

module Librarian
  module Chef
    module ManifestReader
      extend self

      MANIFESTS = %w(metadata.json metadata.yml metadata.yaml metadata.rb)

      def manifest_path(path)
        MANIFESTS.map{|s| path.join(s)}.find{|s| s.exist?}
      end

      def read_manifest(name, manifest_path)
        case manifest_path.extname
        when ".json" then JSON.parse(manifest_path.read)
        when ".yml", ".yaml" then YAML.load(manifest_path.read)
        when ".rb" then compile_manifest(name, manifest_path.dirname)
        end
      end

      def compile_manifest(name, path)
        # Inefficient, if there are many cookbooks with uncompiled metadata.
        require 'chef/json_compat'
        require 'chef/cookbook/metadata'
        md = ::Chef::Cookbook::Metadata.new
        md.name(name)
        md.from_file(path.join('metadata.rb').to_s)
        JSON.parse(::Chef::JSONCompat.to_json_pretty(md))
      end

    end
  end
end
<commit_msg>No need to roundtrip through JSON.<commit_after>require 'json'
require 'yaml'

require 'librarian/manifest'

module Librarian
  module Chef
    module ManifestReader
      extend self

      MANIFESTS = %w(metadata.json metadata.yml metadata.yaml metadata.rb)

      def manifest_path(path)
        MANIFESTS.map{|s| path.join(s)}.find{|s| s.exist?}
      end

      def read_manifest(name, manifest_path)
        case manifest_path.extname
        when ".json" then JSON.parse(manifest_path.read)
        when ".yml", ".yaml" then YAML.load(manifest_path.read)
        when ".rb" then compile_manifest(name, manifest_path.dirname)
        end
      end

      def compile_manifest(name, path)
        # Inefficient, if there are many cookbooks with uncompiled metadata.
        require 'chef/json_compat'
        require 'chef/cookbook/metadata'
        md = ::Chef::Cookbook::Metadata.new
        md.name(name)
        md.from_file(path.join('metadata.rb').to_s)
        {"name" => md.name, "version" => md.version, "dependencies" => md.dependencies}
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.1069569332702319, 'fraction_numerical': 0.0, 'mean_word_length': 2.721830985915493, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6524115', 'n_tokens_mistral': 702, 'n_tokens_neox': 651, 'n_words': 152}