<commit_before>require 'multi_json'
require 'jshintrb'

class ZendeskAppsTools::Package

  LINTER_OPTIONS = {
    # ENFORCING OPTIONS:
    :noarg => true,
    :undef => true,

    # RELAXING OPTIONS:
    :eqnull => true,
    :laxcomma => true,

    # PREDEFINED GLOBALS:
    :predef =>  %w(
                  _ console services helpers alert JSON Base64
                  clearInterval clearTimeout setInterval setTimeout
                )
  }.freeze

  class AppValidationError < StandardError
    class << self
      attr_accessor :key
    end
  end

  attr_reader :manifest_path, :source_path

  def initialize(dir)
    @dir           = dir
    @source_path   = File.join(@dir, 'app.js')
    @manifest_path = File.join(@dir, 'manifest.json')
  end

  def validate!
    errors = ZendeskAppsTools::Validations::Manifest.call(self) +
             ZendeskAppsTools::Validations::Source.call(self)

    raise AppValidationError.new(errors.join("\n")) if errors.any?

    true
  end

  def templates
    @templates ||= begin
      templates_dir = File.join(@dir, 'templates')
      Dir["#{templates_dir}/*.hdbs"].inject({}) do |h, file|
        str = File.read(file)
        str.chomp!
        h[File.basename(file, File.extname(file))] = str
        h
      end
    end
  end

  def translations
    @translations ||= begin
      translation_dir = File.join(@dir, 'translations')
      default_translations = MultiJson.load(File.read("#{translation_dir}/#{self.default_locale}.json"))

      Dir["#{translation_dir}/*.json"].inject({}) do |h, tr|
        locale = File.basename(tr, File.extname(tr))
        locale_translations = if locale == self.default_locale
                                default_translations
                              else
                                default_translations.deep_merge(MultiJson.load(File.read(tr)))
                              end

        h[locale] = locale_translations
        h
      end
    end
  end

  def locales
    translations.keys
  end

  def default_locale
    manifest["default_locale"]
  end

  def translation(en)
    translations[en]
  end

  def name
    manifest["name"]
  end

  def author
    {
      :name  => manifest['author']['name'],
      :email => manifest['author']['email']
    }
  end
<commit_msg>Package: manifests no longer need a name, so provide a default<commit_after>require 'multi_json'
require 'jshintrb'

class ZendeskAppsTools::Package

  LINTER_OPTIONS = {
    # ENFORCING OPTIONS:
    :noarg => true,
    :undef => true,

    # RELAXING OPTIONS:
    :eqnull => true,
    :laxcomma => true,

    # PREDEFINED GLOBALS:
    :predef =>  %w(
                  _ console services helpers alert JSON Base64
                  clearInterval clearTimeout setInterval setTimeout
                )
  }.freeze

  class AppValidationError < StandardError
    class << self
      attr_accessor :key
    end
  end

  attr_reader :manifest_path, :source_path

  def initialize(dir)
    @dir           = dir
    @source_path   = File.join(@dir, 'app.js')
    @manifest_path = File.join(@dir, 'manifest.json')
  end

  def validate!
    errors = ZendeskAppsTools::Validations::Manifest.call(self) +
             ZendeskAppsTools::Validations::Source.call(self)

    raise AppValidationError.new(errors.join("\n")) if errors.any?

    true
  end

  def templates
    @templates ||= begin
      templates_dir = File.join(@dir, 'templates')
      Dir["#{templates_dir}/*.hdbs"].inject({}) do |h, file|
        str = File.read(file)
        str.chomp!
        h[File.basename(file, File.extname(file))] = str
        h
      end
    end
  end

  def translations
    @translations ||= begin
      translation_dir = File.join(@dir, 'translations')
      default_translations = MultiJson.load(File.read("#{translation_dir}/#{self.default_locale}.json"))

      Dir["#{translation_dir}/*.json"].inject({}) do |h, tr|
        locale = File.basename(tr, File.extname(tr))
        locale_translations = if locale == self.default_locale
                                default_translations
                              else
                                default_translations.deep_merge(MultiJson.load(File.read(tr)))
                              end

        h[locale] = locale_translations
        h
      end
    end
  end

  def locales
    translations.keys
  end

  def default_locale
    manifest["default_locale"]
  end

  def translation(en)
    translations[en]
  end

  def name
    manifest["name"] || 'app'
  end

  def author
    {
      :name  => manifest['author']['name'],
      :email => manifest['author']['email']
    }
  end

{'fraction_non_alphanumeric': 0.11236442516268981, 'fraction_numerical': 0.0008676789587852494, 'mean_word_length': 2.2312543798177997, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '5608454', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1388, 'n_words': 295}