<commit_before>require 'json'
require 'yaml'

require 'librarian/manifest'

module Librarian
  module Chef
    class Manifest < Manifest

      module Helpers

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

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

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

      end

      include Helpers
      extend Helpers

    end
  end
end<commit_msg>Compile manifests to json if there only rb manifests.<commit_after>require 'json'
require 'yaml'

require 'librarian/manifest'

module Librarian
  module Chef
    class Manifest < Manifest

      module Helpers

        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(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(manifest_path.dirname) ; read_manifest(manifest_path(manifest_path.dirname))
          end
        end

        def compile_manifest(path)
          # Inefficient, if there are many cookbooks with uncompiled metadata.
          pid = fork do
            require 'chef/cookbook/metadata'
            md = ::Chef::Cookbook::Metadata.new
            md.name(path.basename.to_s)
            md.from_file(path.join('metadata.rb'))
            json_file = path.join('metadata.json')
            json_file.open('wb') { |f| f.write(::Chef::JSONCompat.to_json_pretty(md)) }
          end
          Process.wait(pid)
        end

      end

      include Helpers
      extend Helpers

    end
  end
end
{'fraction_non_alphanumeric': 0.09444444444444444, 'fraction_numerical': 0.0, 'mean_word_length': 2.2475409836065574, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4601350', 'n_tokens_mistral': 638, 'n_tokens_neox': 582, 'n_words': 137}