Class: ZendeskAppsSupport::Package
- Inherits:
-
Object
- Object
- ZendeskAppsSupport::Package
- 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.('../assets/default_template.html.erb', __FILE__)))
- DEFAULT_SCSS =
File.read(File.('../assets/default_styles.scss', __FILE__))
- SRC_TEMPLATE =
Erubis::Eruby.new(File.read(File.('../assets/src.js.erb', __FILE__)))
- LOCATIONS_WITH_ICONS =
%w(top_bar nav_bar system_top_bar ticket_editor).freeze
Constants included from BuildTranslation
BuildTranslation::I18N_KEYS, BuildTranslation::I18N_TITLE_KEY, BuildTranslation::I18N_VALUE_KEY
Instance Attribute Summary collapse
-
#lib_root ⇒ Object
readonly
Returns the value of attribute lib_root.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
- #app_css ⇒ Object
- #app_js ⇒ Object
- #assets ⇒ Object
-
#compile(options) ⇒ Object
(also: #compile_js)
this is not really compile_js, it compiles the whole app including scss for v1 apps.
- #compiled_templates(app_id, asset_url_prefix) ⇒ Object
- #files ⇒ Object
- #has_file?(path) ⇒ Boolean
- #has_requirements? ⇒ Boolean
- #iframe_only? ⇒ Boolean
-
#initialize(dir, is_cached = true) ⇒ Package
constructor
A new instance of Package.
- #is_no_template ⇒ Object
- #js_files ⇒ Object
- #lib_files ⇒ Object
- #locales ⇒ Object
- #manifest ⇒ Object
- #manifest_json ⇒ Object
- #no_template_locations ⇒ Object
- #path_to(file) ⇒ Object
- #requirements_json ⇒ Object
- #requirements_path ⇒ Object
- #template_files ⇒ Object
- #templates ⇒ Object
- #translation_files ⇒ Object
- #translations ⇒ Object
- #translations_for(locale) ⇒ Object
- #validate(marketplace: true) ⇒ Object
- #validate!(marketplace: true) ⇒ Object
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.
22 23 24 25 26 27 28 |
# File 'lib/zendesk_apps_support/package.rb', line 22 def initialize(dir, is_cached = true) @root = Pathname.new(File.(dir)) @lib_root = Pathname.new(File.join(root, 'lib')) @is_cached = is_cached # disabled by ZAT for development @warnings = [] end |
Instance Attribute Details
#lib_root ⇒ Object (readonly)
Returns the value of attribute lib_root.
20 21 22 |
# File 'lib/zendesk_apps_support/package.rb', line 20 def lib_root @lib_root end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
20 21 22 |
# File 'lib/zendesk_apps_support/package.rb', line 20 def root @root end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
20 21 22 |
# File 'lib/zendesk_apps_support/package.rb', line 20 def warnings @warnings end |
Instance Method Details
#app_css ⇒ Object
188 189 190 191 192 |
# File 'lib/zendesk_apps_support/package.rb', line 188 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_js ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/zendesk_apps_support/package.rb', line 194 def app_js if @is_cached @app_js ||= read_file('app.js') else read_file('app.js') end end |
#assets ⇒ Object
57 58 59 60 61 |
# File 'lib/zendesk_apps_support/package.rb', line 57 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
103 104 105 106 107 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 |
# File 'lib/zendesk_apps_support/package.rb', line 103 def compile() begin app_id = .fetch(:app_id) asset_url_prefix = .fetch(:assets_dir) name = .fetch(:app_name) rescue KeyError => e raise ArgumentError, e. end locale = .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, location_icons: location_icons, app_class_name: app_class_name, author: manifest., translations: 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
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/zendesk_apps_support/package.rb', line 163 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 |
#files ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/zendesk_apps_support/package.rb', line 75 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
180 181 182 |
# File 'lib/zendesk_apps_support/package.rb', line 180 def has_file?(path) File.file?(path_to(path)) end |
#has_requirements? ⇒ Boolean
184 185 186 |
# File 'lib/zendesk_apps_support/package.rb', line 184 def has_requirements? has_file?(REQUIREMENTS_FILENAME) end |
#iframe_only? ⇒ Boolean
202 203 204 |
# File 'lib/zendesk_apps_support/package.rb', line 202 def iframe_only? manifest.iframe_only? end |
#is_no_template ⇒ Object
153 154 155 |
# File 'lib/zendesk_apps_support/package.rb', line 153 def is_no_template manifest.no_template? end |
#js_files ⇒ Object
86 87 88 |
# File 'lib/zendesk_apps_support/package.rb', line 86 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_files ⇒ Object
90 91 92 |
# File 'lib/zendesk_apps_support/package.rb', line 90 def lib_files @lib_files ||= js_files.select { |f| f =~ %r{^lib/} } end |
#locales ⇒ Object
71 72 73 |
# File 'lib/zendesk_apps_support/package.rb', line 71 def locales translations.keys end |
#manifest ⇒ Object
144 145 146 |
# File 'lib/zendesk_apps_support/package.rb', line 144 def manifest @manifest ||= Manifest.new(read_file(MANIFEST_FILENAME)) end |
#manifest_json ⇒ Object
139 140 141 |
# File 'lib/zendesk_apps_support/package.rb', line 139 def manifest_json @manifest_json ||= read_json(MANIFEST_FILENAME) end |
#no_template_locations ⇒ Object
158 159 160 |
# File 'lib/zendesk_apps_support/package.rb', line 158 def no_template_locations manifest.no_template_locations end |
#path_to(file) ⇒ Object
63 64 65 |
# File 'lib/zendesk_apps_support/package.rb', line 63 def path_to(file) File.join(root, file) end |
#requirements_json ⇒ Object
148 149 150 151 |
# File 'lib/zendesk_apps_support/package.rb', line 148 def requirements_json return nil unless has_requirements? @requirements ||= read_json(REQUIREMENTS_FILENAME, object_class: Manifest::NoOverrideHash) end |
#requirements_path ⇒ Object
67 68 69 |
# File 'lib/zendesk_apps_support/package.rb', line 67 def requirements_path path_to(REQUIREMENTS_FILENAME) end |
#template_files ⇒ Object
94 95 96 |
# File 'lib/zendesk_apps_support/package.rb', line 94 def template_files files.select { |f| f =~ %r{^templates/.*\.hdbs$} } end |
#templates ⇒ Object
207 208 209 210 211 212 213 214 215 |
# File 'lib/zendesk_apps_support/package.rb', line 207 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 |
#translation_files ⇒ Object
98 99 100 |
# File 'lib/zendesk_apps_support/package.rb', line 98 def translation_files files.select { |f| f =~ %r{^translations/} } end |
#translations ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/zendesk_apps_support/package.rb', line 217 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) 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
174 175 176 177 178 |
# File 'lib/zendesk_apps_support/package.rb', line 174 def translations_for(locale) trans = translations return trans[locale] if trans[locale] trans[manifest.default_locale] end |
#validate(marketplace: true) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/zendesk_apps_support/package.rb', line 30 def validate(marketplace: true) [].tap do |errors| errors << Validations::Manifest.call(self) if has_manifest? errors << Validations::Marketplace.call(self) if marketplace errors << Validations::Source.call(self) errors << Validations::Translations.call(self) errors << Validations::Requirements.call(self) if !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 errors.flatten!.compact! end end |
#validate!(marketplace: true) ⇒ Object
51 52 53 54 55 |
# File 'lib/zendesk_apps_support/package.rb', line 51 def validate!(marketplace: true) errors = validate(marketplace: marketplace) raise errors.first if errors.any? true end |