Class: ZendeskAppsSupport::Package

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
Includes:
BuildTranslation
Defined in:
lib/zendesk_apps_support/package.rb

Constant Summary collapse

MANIFEST_FILENAME =
'manifest.json'
REQUIREMENTS_FILENAME =
'requirements.json'
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__)))
LOCATIONS_WITH_ICONS_PER_PRODUCT =
{
  Product::SUPPORT => %w[top_bar nav_bar system_top_bar ticket_editor].freeze,
  Product::SELL => %w[top_bar].freeze
}.freeze

Constants included from BuildTranslation

BuildTranslation::I18N_KEYS, BuildTranslation::I18N_TITLE_KEY, BuildTranslation::I18N_VALUE_KEY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BuildTranslation

#remove_zendesk_keys, #to_flattened_namespaced_hash

Constructor Details

#initialize(dir, is_cached = true) ⇒ Package



26
27
28
29
30
31
32
# File 'lib/zendesk_apps_support/package.rb', line 26

def initialize(dir, is_cached = true)
  @root     = Pathname.new(File.expand_path(dir))
  @lib_root = Pathname.new(File.join(root, 'lib'))

  @is_cached = is_cached # disabled by ZAT for development
  @warnings = []
end

Instance Attribute Details

#lib_rootObject (readonly)

Returns the value of attribute lib_root.



24
25
26
# File 'lib/zendesk_apps_support/package.rb', line 24

def lib_root
  @lib_root
end

#rootObject (readonly)

Returns the value of attribute root.



24
25
26
# File 'lib/zendesk_apps_support/package.rb', line 24

def root
  @root
end

#warningsObject (readonly)

Returns the value of attribute warnings.



24
25
26
# File 'lib/zendesk_apps_support/package.rb', line 24

def warnings
  @warnings
end

Class Method Details

.has_custom_object_requirements?(requirements_hash) ⇒ Boolean



225
226
227
228
229
230
231
232
233
# File 'lib/zendesk_apps_support/package.rb', line 225

def self.has_custom_object_requirements?(requirements_hash)
  return false if requirements_hash.nil?

  custom_object_requirements = requirements_hash.fetch(AppRequirement::CUSTOM_OBJECTS_KEY, {})
  types = custom_object_requirements.fetch(AppRequirement::CUSTOM_OBJECTS_TYPE_KEY, [])
  relationships = custom_object_requirements.fetch(AppRequirement::CUSTOM_OBJECTS_RELATIONSHIP_TYPE_KEY, [])

  (types | relationships).any?
end

Instance Method Details

#app_cssObject



235
236
237
238
239
# File 'lib/zendesk_apps_support/package.rb', line 235

def app_css
  return File.read(path_to('app.scss')) if has_file?('app.scss')
  return File.read(path_to('app.css')) if has_file?('app.css')
  ''
end

#app_jsObject



241
242
243
244
245
246
247
# File 'lib/zendesk_apps_support/package.rb', line 241

def app_js
  if @is_cached
    @app_js ||= read_file('app.js')
  else
    read_file('app.js')
  end
end

#assetsObject



77
78
79
80
81
# File 'lib/zendesk_apps_support/package.rb', line 77

def assets
  @assets ||= Dir.chdir(root) do
    Dir['assets/**/*'].select { |f| File.file?(f) }
  end
end

#compile(options) ⇒ Object Also known as: compile_js

this is not really compile_js, it compiles the whole app including scss for v1 apps



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/zendesk_apps_support/package.rb', line 135

def compile(options)
  begin
    app_id = options.fetch(:app_id)
    asset_url_prefix = options.fetch(:assets_dir)
    name = options.fetch(:app_name)
  rescue KeyError => e
    raise ArgumentError, e.message
  end

  locale = options.fetch(:locale, 'en')

  source = manifest.iframe_only? ? nil : app_js
  app_class_name = "app-#{app_id}"
  # if no_template is an array, we still need the templates
  templates = manifest.no_template == true ? {} : compiled_templates(app_id, asset_url_prefix)

  SRC_TEMPLATE.result(
    name: name,
    version: manifest.version,
    source: source,
    app_class_properties: manifest.app_class_properties,
    asset_url_prefix: asset_url_prefix,
    logo_asset_hash: generate_logo_hash(manifest.products),
    location_icons: location_icons,
    app_class_name: app_class_name,
    author: manifest.author,
    translations: manifest.iframe_only? ? nil : runtime_translations(translations_for(locale)),
    framework_version: manifest.framework_version,
    templates: templates,
    modules: commonjs_modules,
    iframe_only: manifest.iframe_only?
  )
end

#compiled_templates(app_id, asset_url_prefix) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/zendesk_apps_support/package.rb', line 196

def compiled_templates(app_id, asset_url_prefix)
  compiler = ZendeskAppsSupport::StylesheetCompiler.new(DEFAULT_SCSS + app_css, app_id, asset_url_prefix)
  compiled_css = compiler.compile(sassc: manifest.enabled_experiments.include?('newCssCompiler'))

  layout = templates['layout'] || DEFAULT_LAYOUT.result

  templates.tap do |templates|
    templates['layout'] = "<style>\n#{compiled_css}</style>\n#{layout}"
  end
end

#filesObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/zendesk_apps_support/package.rb', line 95

def files
  files = []
  Dir[root.join('**/**')].each do |f|
    next unless File.file?(f)
    relative_file_name = f.sub(%r{#{root}/?}, '')
    next if relative_file_name =~ %r{^tmp/}
    files << AppFile.new(self, relative_file_name)
  end
  files
end

#has_file?(path) ⇒ Boolean



213
214
215
# File 'lib/zendesk_apps_support/package.rb', line 213

def has_file?(path)
  File.file?(path_to(path))
end

#has_requirements?Boolean



221
222
223
# File 'lib/zendesk_apps_support/package.rb', line 221

def has_requirements?
  has_file?(REQUIREMENTS_FILENAME)
end

#has_svgs?Boolean



217
218
219
# File 'lib/zendesk_apps_support/package.rb', line 217

def has_svgs?
  svg_files.any?
end

#html_filesObject



114
115
116
# File 'lib/zendesk_apps_support/package.rb', line 114

def html_files
  @html_files ||= files.select { |f| f =~ %r{.*\.html?$} }
end

#iframe_only?Boolean



249
250
251
# File 'lib/zendesk_apps_support/package.rb', line 249

def iframe_only?
  manifest.iframe_only?
end

#is_no_templateObject



186
187
188
# File 'lib/zendesk_apps_support/package.rb', line 186

def is_no_template
  manifest.no_template?
end

#js_filesObject



110
111
112
# File 'lib/zendesk_apps_support/package.rb', line 110

def js_files
  @js_files ||= files.select { |f| f =~ %r{^.*\.jsx?$} }
end

#lib_filesObject



118
119
120
# File 'lib/zendesk_apps_support/package.rb', line 118

def lib_files
  @lib_files ||= js_files.select { |f| f =~ %r{^lib/} }
end

#localesObject



91
92
93
# File 'lib/zendesk_apps_support/package.rb', line 91

def locales
  translations.keys
end

#location_iconsObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/zendesk_apps_support/package.rb', line 288

def location_icons
  Hash.new { |h, k| h[k] = {} }.tap do |location_icons|
    manifest.location_options.each do |location_options|
      # no location information in the manifest
      next unless location_options.location

      product = location_options.location.product
      location_name = location_options.location.name
      # the location on the product does not support icons
      next unless LOCATIONS_WITH_ICONS_PER_PRODUCT.fetch(product, []).include?(location_name)

      host = location_options.location.product.name
      product_directory = manifest.products.count > 1 ? "#{host}/" : ''
      location_icons[host][location_name] = build_location_icons_hash(location_name, product_directory)
    end
  end
end

#manifestObject



177
178
179
# File 'lib/zendesk_apps_support/package.rb', line 177

def manifest
  @manifest ||= Manifest.new(read_file(MANIFEST_FILENAME))
end

#manifest_jsonObject



172
173
174
# File 'lib/zendesk_apps_support/package.rb', line 172

def manifest_json
  @manifest_json ||= read_json(MANIFEST_FILENAME)
end

#no_template_locationsObject



191
192
193
# File 'lib/zendesk_apps_support/package.rb', line 191

def no_template_locations
  manifest.no_template_locations
end

#path_to(file) ⇒ Object



83
84
85
# File 'lib/zendesk_apps_support/package.rb', line 83

def path_to(file)
  File.join(root, file)
end

#requirements_jsonObject



181
182
183
184
# File 'lib/zendesk_apps_support/package.rb', line 181

def requirements_json
  return nil unless has_requirements?
  @requirements ||= read_json(REQUIREMENTS_FILENAME, object_class: Manifest::NoOverrideHash)
end

#requirements_pathObject



87
88
89
# File 'lib/zendesk_apps_support/package.rb', line 87

def requirements_path
  path_to(REQUIREMENTS_FILENAME)
end

#svg_filesObject



122
123
124
# File 'lib/zendesk_apps_support/package.rb', line 122

def svg_files
  @svg_files ||= files.select { |f| f =~ %r{^assets/.*\.svg$} }
end

#template_filesObject



126
127
128
# File 'lib/zendesk_apps_support/package.rb', line 126

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

#templatesObject



254
255
256
257
258
259
260
261
262
# File 'lib/zendesk_apps_support/package.rb', line 254

def templates
  templates_dir = path_to('templates')
  Dir["#{templates_dir}/*.hdbs"].each_with_object({}) do |file, memo|
    str = File.read(file)
    str.chomp!
    memo[File.basename(file, File.extname(file))] = str
    memo
  end
end

#text_filesObject



106
107
108
# File 'lib/zendesk_apps_support/package.rb', line 106

def text_files
  @text_files ||= files.select { |f| f =~ %r{.*(html?|xml|js|json?)$} }
end

#translation_filesObject



130
131
132
# File 'lib/zendesk_apps_support/package.rb', line 130

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

#translationsObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/zendesk_apps_support/package.rb', line 264

def translations
  return @translations if @is_cached && @translations

  @translations = begin
    translation_dir = path_to('translations')
    return {} unless File.directory?(translation_dir)

    locale_path = "#{translation_dir}/#{manifest.default_locale}.json"
    default_translations = process_translations(locale_path, default_locale: true)

    Dir["#{translation_dir}/*.json"].each_with_object({}) do |path, memo|
      locale = File.basename(path, File.extname(path))

      locale_translations = if locale == manifest.default_locale
                              default_translations
                            else
                              deep_merge_hash(default_translations, process_translations(path))
                            end

      memo[locale] = locale_translations
    end
  end
end

#translations_for(locale) ⇒ Object



207
208
209
210
211
# File 'lib/zendesk_apps_support/package.rb', line 207

def translations_for(locale)
  trans = translations
  return trans[locale] if trans[locale]
  trans[manifest.default_locale]
end

#validate(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/zendesk_apps_support/package.rb', line 34

def validate(options = {})
  marketplace = options.fetch(:marketplace, true)
  skip_marketplace_translations = options.fetch(:skip_marketplace_translations, false)
  error_on_password_parameter = options.fetch(:error_on_password_parameter, false)
  validate_custom_objects_v2 = options.fetch(:validate_custom_objects_v2, false)
  validate_scopes_for_secure_parameter = options.fetch(:validate_scopes_for_secure_parameter, false)

  errors = []
  errors << Validations::Manifest.call(self, error_on_password_parameter: error_on_password_parameter, validate_scopes_for_secure_parameter: validate_scopes_for_secure_parameter)

  if has_valid_manifest?(errors)
    errors << Validations::Marketplace.call(self) if marketplace
    errors << Validations::Source.call(self)
    errors << Validations::Translations.call(self, skip_marketplace_translations: skip_marketplace_translations)
    errors << Validations::Requirements.call(self, validate_custom_objects_v2:)

    # only adds warnings
    Validations::SecureSettings.call(self)
    Validations::Requests.call(self)

    unless manifest.requirements_only? || manifest.marketing_only? || manifest.iframe_only?
      errors << Validations::Templates.call(self)
      errors << Validations::Stylesheets.call(self)
    end
  end

  errors << Validations::Banner.call(self) if has_banner?
  errors << Validations::Svg.call(self) if has_svgs?
  errors << Validations::Mime.call(self)

  # only adds warnings
  Validations::Secrets.call(self)

  errors.flatten.compact
end

#validate!(opts = {}) ⇒ Object



70
71
72
73
74
75
# File 'lib/zendesk_apps_support/package.rb', line 70

def validate!(opts = {})
  errors = validate(opts)
  raise errors.first if errors.any?

  true
end