<commit_before>require 'pathname'
require 'erubis'
require 'json'

module ZendeskAppsSupport
  class Package

    DEFAULT_LAYOUT = Erubis::Eruby.new( File.read(File.expand_path('../assets/default_template.html.erb', __FILE__)) )
    DEFAULT_SCSS   = File.read(File.expand_path('../assets/default_styles.scss', __FILE__))
    SRC_TEMPLATE = Erubis::Eruby.new( File.read(File.expand_path('../assets/src.js.erb', __FILE__)) )

    attr_reader :root, :warnings

    def initialize(dir)
      @root = Pathname.new(File.expand_path(dir))
      @warnings = []
    end

    def validate
      Validations::Manifest.call(self) +
        Validations::Source.call(self) +
        Validations::Templates.call(self) +
        Validations::Translations.call(self) +
        Validations::Stylesheets.call(self)
    end

    def files
      non_tmp_files
    end

    def template_files
      files.select { |f| f =~ /^templates\/.*\.hdbs$/ }
    end

    def translation_files
      files.select { |f| f =~ /^translations\// }
    end

    def manifest_json
      JSON.parse(File.read(File.join(root, "manifest.json")), :symbolize_names => true)
    end

    def readified_js(app_name, app_id, asset_url_prefix, settings={})
      manifest = manifest_json
      source = File.read(File.join(root, "app.js"))
      name = app_name || manifest[:name] || 'Local App'
      location = manifest[:location]
      app_class_name = "app-#{app_id}"
      author = manifest[:author]
      translations = JSON.parse(File.read(File.join(root, "translations/en.json")))
      framework_version = manifest[:frameworkVersion]
      templates = manifest[:noTemplate] ? {} : compiled_templates(app_id, asset_url_prefix)

      settings["title"] = name

      SRC_TEMPLATE.result(
          :name => name,
          :source => source,
          :location => location,
          :asset_url_prefix => asset_url_prefix,
          :app_class_name => app_class_name,
          :author => author,
          :translations => translations,
          :framework_version => framework_version,
          :templates => templates,
          :settings => settings,
          :app_id => app_id
      )
    end

    def customer_css
      css_file = File.join(root, 'app.css')
      customer_css = File.exist?(css_file) ? File.read(css_file) : ""
    end

    private

    def compiled_templates(app_id, asset_url_prefix)
      compiled_css = ZendeskAppsSupport::StylesheetCompiler.new(DEFAULT_SCSS + customer_css, app_id, asset_url_prefix).compile

      templates = begin
        Dir["#{root.to_s}/templates/*.hdbs"].inject({}) do |h, file|
          str = File.read(file)
          str.chomp!<commit_msg>Use ZendeskAppsSupport::BuildTranslation<commit_after>require 'pathname'
require 'erubis'
require 'json'

module ZendeskAppsSupport
  class Package
    include ZendeskAppsSupport::BuildTranslation

    DEFAULT_LAYOUT = Erubis::Eruby.new( File.read(File.expand_path('../assets/default_template.html.erb', __FILE__)) )
    DEFAULT_SCSS   = File.read(File.expand_path('../assets/default_styles.scss', __FILE__))
    SRC_TEMPLATE = Erubis::Eruby.new( File.read(File.expand_path('../assets/src.js.erb', __FILE__)) )

    attr_reader :root, :warnings

    def initialize(dir)
      @root = Pathname.new(File.expand_path(dir))
      @warnings = []
    end

    def validate
      Validations::Manifest.call(self) +
        Validations::Source.call(self) +
        Validations::Templates.call(self) +
        Validations::Translations.call(self) +
        Validations::Stylesheets.call(self)
    end

    def files
      non_tmp_files
    end

    def template_files
      files.select { |f| f =~ /^templates\/.*\.hdbs$/ }
    end

    def translation_files
      files.select { |f| f =~ /^translations\// }
    end

    def manifest_json
      JSON.parse(File.read(File.join(root, "manifest.json")), :symbolize_names => true)
    end

    def readified_js(app_name, app_id, asset_url_prefix, settings={})
      manifest = manifest_json
      source = File.read(File.join(root, "app.js"))
      name = app_name || manifest[:name] || 'Local App'
      location = manifest[:location]
      app_class_name = "app-#{app_id}"
      author = manifest[:author]
      translations = JSON.parse(File.read(File.join(root, "translations/en.json")))
      framework_version = manifest[:frameworkVersion]
      templates = manifest[:noTemplate] ? {} : compiled_templates(app_id, asset_url_prefix)

      settings["title"] = name

      SRC_TEMPLATE.result(
          :name => name,
          :source => source,
          :location => location,
          :asset_url_prefix => asset_url_prefix,
          :app_class_name => app_class_name,
          :author => author,
          :translations => remove_zendesk_keys(translations),
          :framework_version => framework_version,
          :templates => templates,
          :settings => settings,
          :app_id => app_id
      )
    end

    def customer_css
      css_file = File.join(root, 'app.css')
      customer_css = File.exist?(css_file) ? File.read(css_file) : ""
    end

    private

    def compiled_templates(app_id, asset_url_prefix)
      compiled_css = ZendeskAppsSupport::StylesheetCompiler.new(DEFAULT_SCSS + customer_css, app_id, asset_url_prefix).compile

      templates = begin
        Dir["#{root.to_s}/templates/*.hdbs"].inject({}) do |h, file|
          str = File.read(file)
          str.chomp!
{'fraction_non_alphanumeric': 0.12820986513947905, 'fraction_numerical': 0.0, 'mean_word_length': 2.911849710982659, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '505749', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1698, 'n_words': 294}