<commit_before>require 'fileutils'

module Cany
  class Recipe

    # @api public
    # This method should be call in subclasses to register new recipe instances. Cany ignores any
    # recipe subclasses which does not call register_as. If multiple recipes register on the same
    # name the later one will overwrite the earlier one and therefore used by Cany.
    # @param [Symbol] name A ruby symbol as name for the recipe. It should be short and recognizable
    def self.register_as(name)
      @@recipes ||= {}
      @@recipes[name] = self
    end

    # @api public
    # Looks for the class registered for the given name
    # @param [Symbol] name the name the class is search for
    # @return [Cany::Recipe, nil] Returns the found class or nil if no class is registered on this
    #  name
    def self.from_name(name)
      @@recipes[name]
    end

    # Creates a new instance of this recipe
    # @param [Cany::Specification] spec Specification object
    # @param [Cany::Recipe, nil] inner Inner recipes should should be call between the pre and post
    #  actions of this class. Nil means most inner recipes.
    def initialize(spec, inner)
      @spec = spec
      @inner = inner
    end

    attr_reader :spec, :inner

    # API to use inside the recipe
    ##############################

    # @api public
    # Run a command inside the build directory. In most cases it is not needed to call this method
    # directly. Look at the other helper methods.
    #
    # The method expects as arguments the program name and additional parameters for the program.
    # The arguments can be group with arguments, but must not:
    # @example
    #   exec 'echo', %w(a b)
    #   exec ['echo', 'a', 'b']
    #   exec 'echo', 'a', 'b'
    def exec(*args)
      puts "   #{args.flatten.join(' ')}"
      system *args.flatten
    end

    # @api public
    # Run a ruby task (like gem, bundler, rake ...)
    #
    # The method expects as arguments the program name and additional parameters for the program.
    # See exec for more examples
    def ruby_bin(*args)
      exec 'ruby', '-S', *args
    end

    # @api public
    # Install files or directory from the build directory
    # @param [String] source The relative file name to a filename or directory inside the build
    #   directory that should be installed/copied into the destination package
    # @param [String] destination The diretory name into that the file or directory should be
    #   installed
    def install(src, dest_dir)
      exec 'dh_install', src, dest_dir
    end

    # @api public
    # Install a file. The content is passed as argument. This method is designed to be used by
    # recipes to create files dynamically.
    # @param [String] filename The absolute file name for the file inside the package.
    # @param [String] content The file content
    def install_content(filename, content)
      FileUtils.mkdir_p File.dirname File.join('debian', spec.name, filename)
      File.open File.join('debian', spec.name, filename), 'w' do |f|
        f.write content<commit_msg>recipe: use same ruby intepreter as for cany<commit_after>require 'fileutils'

module Cany
  class Recipe

    # @api public
    # This method should be call in subclasses to register new recipe instances. Cany ignores any
    # recipe subclasses which does not call register_as. If multiple recipes register on the same
    # name the later one will overwrite the earlier one and therefore used by Cany.
    # @param [Symbol] name A ruby symbol as name for the recipe. It should be short and recognizable
    def self.register_as(name)
      @@recipes ||= {}
      @@recipes[name] = self
    end

    # @api public
    # Looks for the class registered for the given name
    # @param [Symbol] name the name the class is search for
    # @return [Cany::Recipe, nil] Returns the found class or nil if no class is registered on this
    #  name
    def self.from_name(name)
      @@recipes[name]
    end

    # Creates a new instance of this recipe
    # @param [Cany::Specification] spec Specification object
    # @param [Cany::Recipe, nil] inner Inner recipes should should be call between the pre and post
    #  actions of this class. Nil means most inner recipes.
    def initialize(spec, inner)
      @spec = spec
      @inner = inner
    end

    attr_reader :spec, :inner

    # API to use inside the recipe
    ##############################

    # @api public
    # Run a command inside the build directory. In most cases it is not needed to call this method
    # directly. Look at the other helper methods.
    #
    # The method expects as arguments the program name and additional parameters for the program.
    # The arguments can be group with arguments, but must not:
    # @example
    #   exec 'echo', %w(a b)
    #   exec ['echo', 'a', 'b']
    #   exec 'echo', 'a', 'b'
    def exec(*args)
      puts "   #{args.flatten.join(' ')}"
      system *args.flatten
    end

    # @api public
    # Run a ruby task (like gem, bundler, rake ...)
    #
    # The method expects as arguments the program name and additional parameters for the program.
    # See exec for more examples
    def ruby_bin(*args)
      exec RbConfig.ruby, '-S', *args
    end

    # @api public
    # Install files or directory from the build directory
    # @param [String] source The relative file name to a filename or directory inside the build
    #   directory that should be installed/copied into the destination package
    # @param [String] destination The diretory name into that the file or directory should be
    #   installed
    def install(src, dest_dir)
      exec 'dh_install', src, dest_dir
    end

    # @api public
    # Install a file. The content is passed as argument. This method is designed to be used by
    # recipes to create files dynamically.
    # @param [String] filename The absolute file name for the file inside the package.
    # @param [String] content The file content
    def install_content(filename, content)
      FileUtils.mkdir_p File.dirname File.join('debian', spec.name, filename)
      File.open File.join('debian', spec.name, filename), 'w' do |f|
        f.write content
{'fraction_non_alphanumeric': 0.08438409311348205, 'fraction_numerical': 0.0, 'mean_word_length': 2.850031113876789, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3664654', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1663, 'n_words': 792}