Class: ZendeskAppsSupport::Package

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

Constant Summary collapse

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__)))
LEGACY_URI_STUB =
'_legacy'

Constants included from BuildTranslation

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

Instance Attribute 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

Returns a new instance of Package.



19
20
21
22
23
24
25
# File 'lib/zendesk_apps_support/package.rb', line 19

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.



17
18
19
# File 'lib/zendesk_apps_support/package.rb', line 17

def lib_root
  @lib_root
end

#rootObject (readonly)

Returns the value of attribute root.



17
18
19
# File 'lib/zendesk_apps_support/package.rb', line 17

def root
  @root
end

#warningsObject (readonly)

Returns the value of attribute warnings.



17
18
19
# File 'lib/zendesk_apps_support/package.rb', line 17

def warnings
  @warnings
end

Instance Method Details

#app_cssObject



203
204
205
206
207
# File 'lib/zendesk_apps_support/package.rb', line 203

def app_css
  css_file = path_to('app.css')
  scss_file = path_to('app.scss')
  File.exist?(scss_file) ? File.read(scss_file) : ( File.exist?(css_file) ? File.read(css_file) : '' )
end

#assetsObject



63
64
65
66
67
# File 'lib/zendesk_apps_support/package.rb', line 63

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

#compile_js(options) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/zendesk_apps_support/package.rb', line 108

def compile_js(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 = iframe_only? ? nil : app_js
  version = manifest_json['version']
  app_class_name = "app-#{app_id}"
  author = manifest_json['author']
  framework_version = manifest_json['frameworkVersion']
  single_install = manifest_json['singleInstall'] || false
  signed_urls = manifest_json['signedUrls'] || false
  templates = is_no_template ? {} : compiled_templates(app_id, asset_url_prefix)

  app_settings = {
    location: locations,
    noTemplate: no_template_locations,
    singleInstall: single_install,
    signedUrls: signed_urls
  }.select { |_k, v| !v.nil? }

  SRC_TEMPLATE.result(
    name: name,
    version: version,
    source: source,
    app_settings: app_settings,
    asset_url_prefix: asset_url_prefix,
    app_class_name: app_class_name,
    author: author,
    translations: translations_for(locale),
    framework_version: framework_version,
    templates: templates,
    modules: commonjs_modules,
    iframe_only: iframe_only?
  )
end

#compiled_templates(app_id, asset_url_prefix) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/zendesk_apps_support/package.rb', line 176

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

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

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

#filesObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/zendesk_apps_support/package.rb', line 81

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

#has_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/zendesk_apps_support/package.rb', line 199

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

#has_location?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/zendesk_apps_support/package.rb', line 195

def has_location?
  manifest_json['location']
end

#iframe_only?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/zendesk_apps_support/package.rb', line 223

def iframe_only?
  !legacy_non_iframe_app?
end

#is_no_templateObject



160
161
162
163
164
165
166
# File 'lib/zendesk_apps_support/package.rb', line 160

def is_no_template
  if manifest_json['noTemplate'].is_a?(Array)
    false
  else
    !!manifest_json['noTemplate']
  end
end

#js_filesObject



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

def js_files
  @js_files ||= files.select { |f| f.to_s == 'app.js' || ( f.to_s.start_with?('lib/') && f.to_s.end_with?('.js') ) }
end

#lib_filesObject



96
97
98
# File 'lib/zendesk_apps_support/package.rb', line 96

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

#localesObject



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

def locales
  translations.keys
end

#locationsObject



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/zendesk_apps_support/package.rb', line 209

def locations
  locations = manifest_json['location']
  case locations
  when Hash
    locations
  when Array
    { 'zendesk' => Hash[locations.map { |location| [ location, LEGACY_URI_STUB ] }] }
  when String
    { 'zendesk' => { locations => LEGACY_URI_STUB } }
  else # NilClass
    { 'zendesk' => {} }
  end
end

#manifest_jsonObject



151
152
153
# File 'lib/zendesk_apps_support/package.rb', line 151

def manifest_json
  @manifest ||= read_json('manifest.json')
end

#market_translations!(locale) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/zendesk_apps_support/package.rb', line 186

def market_translations!(locale)
  result = translations[locale].fetch('app', {})
  result.delete('name')
  result.delete('description')
  result.delete('long_description')
  result.delete('installation_instructions')
  result
end

#no_template_locationsObject



168
169
170
171
172
173
174
# File 'lib/zendesk_apps_support/package.rb', line 168

def no_template_locations
  if manifest_json['noTemplate'].is_a?(Array)
    manifest_json['noTemplate']
  else
    !!manifest_json['noTemplate']
  end
end

#path_to(file) ⇒ Object



69
70
71
# File 'lib/zendesk_apps_support/package.rb', line 69

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

#requirements_jsonObject



155
156
157
158
# File 'lib/zendesk_apps_support/package.rb', line 155

def requirements_json
  return nil unless has_requirements?
  @requirements ||= read_json('requirements.json')
end

#requirements_pathObject



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

def requirements_path
  path_to(REQUIREMENTS_FILENAME)
end

#template_filesObject



100
101
102
# File 'lib/zendesk_apps_support/package.rb', line 100

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

#translation_filesObject



104
105
106
# File 'lib/zendesk_apps_support/package.rb', line 104

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

#validate(marketplace: true) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zendesk_apps_support/package.rb', line 27

def validate(marketplace: true)
  [].tap do |errors|
    errors << Validations::Marketplace.call(self) if marketplace

    errors << Validations::Manifest.call(self)

    if has_manifest?
      errors << Validations::Source.call(self)
      errors << Validations::Translations.call(self)

      unless manifest_json['requirementsOnly']
        errors << Validations::Templates.call(self)
        errors << Validations::Stylesheets.call(self)
      end

      if has_requirements?
        errors << Validations::Requirements.call(self)
      end
    end

    if has_banner?
      errors << Validations::Banner.call(self)
    end

    errors.flatten!.compact!
  end
end

#validate!(marketplace: true) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/zendesk_apps_support/package.rb', line 55

def validate!(marketplace: true)
  errors = validate(marketplace: marketplace)
  if errors.any?
    raise errors.first
  end
  true
end