<commit_before>require "set"
require "locale"

require "hbc/dsl/appcast"
require "hbc/dsl/base"
require "hbc/dsl/caveats"
require "hbc/dsl/conflicts_with"
require "hbc/dsl/container"
require "hbc/dsl/depends_on"
require "hbc/dsl/gpg"
require "hbc/dsl/installer"
require "hbc/dsl/license"
require "hbc/dsl/postflight"
require "hbc/dsl/preflight"
require "hbc/dsl/stanza_proxy"
require "hbc/dsl/uninstall_postflight"
require "hbc/dsl/uninstall_preflight"
require "hbc/dsl/version"

module Hbc
  class DSL
    ORDINARY_ARTIFACT_TYPES = [
                                :app,
                                :artifact,
                                :audio_unit_plugin,
                                :binary,
                                :colorpicker,
                                :font,
                                :input_method,
                                :internet_plugin,
                                :pkg,
                                :prefpane,
                                :qlplugin,
                                :screen_saver,
                                :service,
                                :stage_only,
                                :suite,
                                :vst_plugin,
                                :vst3_plugin,
                              ].freeze

    ACTIVATABLE_ARTIFACT_TYPES = ([:installer, *ORDINARY_ARTIFACT_TYPES] - [:stage_only]).freeze

    SPECIAL_ARTIFACT_TYPES = [
                               :uninstall,
                               :zap,
                             ].freeze

    ARTIFACT_BLOCK_TYPES = [
                             :preflight,
                             :postflight,
                             :uninstall_preflight,
                             :uninstall_postflight,
                           ].freeze

    DSL_METHODS = Set.new [
                            :accessibility_access,
                            :appcast,
                            :artifacts,
                            :auto_updates,
                            :caskroom_path,
                            :caveats,
                            :conflicts_with,
                            :container,
                            :depends_on,
                            :gpg,
                            :homepage,
                            :language,
                            :license,
                            :name,
                            :sha256,
                            :staged_path,
                            :url,
                            :version,
                            :appdir,
                            *ORDINARY_ARTIFACT_TYPES,
                            *ACTIVATABLE_ARTIFACT_TYPES,
                            *SPECIAL_ARTIFACT_TYPES,
                            *ARTIFACT_BLOCK_TYPES,
                          ].freeze

    attr_reader :token
    def initialize(token)
      @token = token
    end

    def name(*args)
      @name ||= []
      return @name if args.empty?
      @name.concat(args.flatten)
    end

    def assert_only_one_stanza_allowed(stanza, arg_given)
      return unless instance_variable_defined?("@#{stanza}") && arg_given
      raise CaskInvalidError.new(token, "'#{stanza}' stanza may only appear once")
    end

    def homepage(homepage = nil)
      assert_only_one_stanza_allowed :homepage, !homepage.nil?
      @homepage ||= homepage
    end

    def language(*args, default: false, &block)
      if !args.empty? && block_given?
        @language_blocks ||= {}
        @language_blocks[args] = block

        return unless default

        unless @language_blocks.default.nil?
          raise CaskInvalidError.new(token, "Only one default language may be defined")
        end

        @language_blocks.default = block
      else
        language_eval
        @language
      end
    end

    def language_eval
      return if instance_variable_defined?(:@language)

      return unless instance_variable_defined?(:@language_blocks)

      MacOS.languages.map(&Locale.method(:parse)).any? { |locale|
        key = @language_blocks.keys.detect { |strings|
          strings.any? { |string| locale.include?(string) }
        }

        return @language = @language_blocks[key].call unless key.nil?
      }

      @language = @language_blocks.default.call
    end

    def url(*args, &block)
      url_given = !args.empty? || block_given?
      return @url unless url_given
      assert_only_one_stanza_allowed :url, url_given
      @url ||= begin
        URL.from(*args, &block)
      rescue StandardError => e
        raise CaskInvalidError.new(token, "'url' stanza failed with: #{e}")<commit_msg>Style changes in `language_eval`.<commit_after>require "set"
require "locale"

require "hbc/dsl/appcast"
require "hbc/dsl/base"
require "hbc/dsl/caveats"
require "hbc/dsl/conflicts_with"
require "hbc/dsl/container"
require "hbc/dsl/depends_on"
require "hbc/dsl/gpg"
require "hbc/dsl/installer"
require "hbc/dsl/license"
require "hbc/dsl/postflight"
require "hbc/dsl/preflight"
require "hbc/dsl/stanza_proxy"
require "hbc/dsl/uninstall_postflight"
require "hbc/dsl/uninstall_preflight"
require "hbc/dsl/version"

module Hbc
  class DSL
    ORDINARY_ARTIFACT_TYPES = [
                                :app,
                                :artifact,
                                :audio_unit_plugin,
                                :binary,
                                :colorpicker,
                                :font,
                                :input_method,
                                :internet_plugin,
                                :pkg,
                                :prefpane,
                                :qlplugin,
                                :screen_saver,
                                :service,
                                :stage_only,
                                :suite,
                                :vst_plugin,
                                :vst3_plugin,
                              ].freeze

    ACTIVATABLE_ARTIFACT_TYPES = ([:installer, *ORDINARY_ARTIFACT_TYPES] - [:stage_only]).freeze

    SPECIAL_ARTIFACT_TYPES = [
                               :uninstall,
                               :zap,
                             ].freeze

    ARTIFACT_BLOCK_TYPES = [
                             :preflight,
                             :postflight,
                             :uninstall_preflight,
                             :uninstall_postflight,
                           ].freeze

    DSL_METHODS = Set.new [
                            :accessibility_access,
                            :appcast,
                            :artifacts,
                            :auto_updates,
                            :caskroom_path,
                            :caveats,
                            :conflicts_with,
                            :container,
                            :depends_on,
                            :gpg,
                            :homepage,
                            :language,
                            :license,
                            :name,
                            :sha256,
                            :staged_path,
                            :url,
                            :version,
                            :appdir,
                            *ORDINARY_ARTIFACT_TYPES,
                            *ACTIVATABLE_ARTIFACT_TYPES,
                            *SPECIAL_ARTIFACT_TYPES,
                            *ARTIFACT_BLOCK_TYPES,
                          ].freeze

    attr_reader :token
    def initialize(token)
      @token = token
    end

    def name(*args)
      @name ||= []
      return @name if args.empty?
      @name.concat(args.flatten)
    end

    def assert_only_one_stanza_allowed(stanza, arg_given)
      return unless instance_variable_defined?("@#{stanza}") && arg_given
      raise CaskInvalidError.new(token, "'#{stanza}' stanza may only appear once")
    end

    def homepage(homepage = nil)
      assert_only_one_stanza_allowed :homepage, !homepage.nil?
      @homepage ||= homepage
    end

    def language(*args, default: false, &block)
      if !args.empty? && block_given?
        @language_blocks ||= {}
        @language_blocks[args] = block

        return unless default

        unless @language_blocks.default.nil?
          raise CaskInvalidError.new(token, "Only one default language may be defined")
        end

        @language_blocks.default = block
      else
        language_eval
      end
    end

    def language_eval
      return @language if instance_variable_defined?(:@language)

      if @language_blocks.nil? || @language_blocks.empty?
        return @language = nil
      end

      MacOS.languages.map(&Locale.method(:parse)).each do |locale|
        key = @language_blocks.keys.detect { |strings|
          strings.any? { |string| locale.include?(string) }
        }

        next if key.nil?

        return @language = @language_blocks[key].call
      end

      @language = @language_blocks.default.call
    end

    def url(*args, &block)
      url_given = !args.empty? || block_given?
      return @url unless url_given
      assert_only_one_stanza_allowed :url, url_given
      @url ||= begin
        URL.from(*args, &block)
      rescue StandardError => e
        raise CaskInvalidError.new(token, "'url' stanza failed with: #{e}")
{'fraction_non_alphanumeric': 0.08162612035851473, 'fraction_numerical': 0.0008536064874093043, 'mean_word_length': 1.1721900347624565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6120597', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2503, 'n_words': 453}