Class: SvelteOnRails::Lib::Utils
- Inherits:
-
Object
- Object
- SvelteOnRails::Lib::Utils
- Defined in:
- lib/svelte_on_rails/lib/utils.rb
Class Method Summary collapse
- .asset_path(file_path) ⇒ Object
- .backtrace_until_sor(exception) ⇒ Object
- .component_paths_uncached(component, current_controller_path) ⇒ Object
- .error_log(title, messages) ⇒ Object
- .file_exist_case_sensitive?(containing_dir, filename) ⇒ Boolean
- .gem_app_dir ⇒ Object
- .manifest_file_mtime ⇒ Object
-
.puts_error(text) ⇒ Object
Defining methods to log errors and warnings using Rails logger.
-
.puts_warning(text) ⇒ Object
Defining method to log warnings using Rails logger.
- .vite_source_code_dir ⇒ Object
Class Method Details
.asset_path(file_path) ⇒ Object
9 10 11 12 13 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 9 def self.asset_path(file_path) config = SvelteOnRails::Configuration.instance manifest = config.ssr_manifest manifest[file_path]['file'] end |
.backtrace_until_sor(exception) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 119 def self.backtrace_until_sor(exception) trace = Array(exception.backtrace) last_match_index = trace.rindex do |line| line.include?("svelte-on-rails") || line.include?("svelte_on_rails") || line.include?(Rails.root.to_s) end return trace if last_match_index.nil? trace[0..last_match_index] end |
.component_paths_uncached(component, current_controller_path) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 45 def self.component_paths_uncached(component, current_controller_path) file_basename = File.basename(component, ".svelte") conf = SvelteOnRails::Configuration.instance cnf = conf.configs aliases = cnf[:aliases] && cnf[:aliases].stringify_keys || {} ali = component[/^([^\/]*)\//, 1] # everything before the first "/" dir = component.match(/^.*(?=\/[^\/]*$)/).to_s path = if aliases[ali] File.join(aliases[ali], component.sub(/^#{ali}\//, '')) + '.svelte' elsif cnf[:default_components_root].to_s.match(/^app\/views/) attempts = [ File.join('app', 'views', dir, "_#{file_basename}" + '.svelte'), File.join('app', 'views', dir, file_basename + '.svelte'), File.join('app', 'views', current_controller_path, dir, "_#{file_basename}" + '.svelte'), File.join('app', 'views', current_controller_path, dir, file_basename + '.svelte'), ] attempts.find { |f| File.exist? Rails.root.join(f) } else cmp_root = File.join(cnf[:vite_source_dir], cnf[:components_subdir]) File.join(cmp_root, dir, file_basename + '.svelte') end vite_path = if path.to_s.match(/^#{cnf[:vite_source_dir]}/) "/#{path.sub(/^#{cnf[:vite_source_dir]}[\/]/, '')}" elsif path # we need the relative path from vite-source-dir # for matching with the frontend javascript source_dir = Pathname.new(cnf[:vite_source_dir]) Pathname.new(path).relative_path_from(source_dir).to_s end { path: path, component: [(dir.present? ? dir : nil), file_basename].compact.join('/'), file_basename: file_basename, name: file_basename.camelcase, vite_path: vite_path } end |
.error_log(title, messages) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 97 def self.error_log(title, ) sep_char = '=' sep_line = sep_char * 100 inner_sep = '-' * 100 puts sep_line puts "ERROR: #{title}" puts sep_line puts .join("\n#{inner_sep}\n") puts sep_line puts "END #{title.downcase}" puts sep_line $stdout.flush end |
.file_exist_case_sensitive?(containing_dir, filename) ⇒ Boolean
15 16 17 18 19 20 21 22 23 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 15 def self.file_exist_case_sensitive?(containing_dir, filename) # Combine the directory path and filename full_path = File.join(containing_dir, filename) # Check if the file exists and the path matches case-sensitively File.exist?(full_path) && Dir[File.join(containing_dir, "**/*")].any? do |f| f == full_path end end |
.gem_app_dir ⇒ Object
5 6 7 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 5 def self.gem_app_dir File.('../../svelte_on_rails', __dir__) + '/' end |
.manifest_file_mtime ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 88 def self.manifest_file_mtime cnf = SvelteOnRails::Configuration.instance if File.exist?(cnf.manifest_json_path) File.mtime(cnf.manifest_json_path).to_f.round(3) else 0.0 end end |
.puts_error(text) ⇒ Object
Defining methods to log errors and warnings using Rails logger
26 27 28 29 30 31 32 33 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 26 def self.puts_error(text) # Using Rails logger to log error with custom formatting caller_info = caller[1] =~ /`([^']*)'/ ? $1 : "unknown" Rails.logger.error(" => [SOR] #{caller_info} ERROR") text.split("\n").each do |line| Rails.logger.error(" => #{line}") end end |
.puts_warning(text) ⇒ Object
Defining method to log warnings using Rails logger
36 37 38 39 40 41 42 43 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 36 def self.puts_warning(text) # Using Rails logger to log warning with custom formatting caller_info = caller[1] =~ /`([^']*)'/ ? $1 : "unknown" Rails.logger.warn(" => [SOR] #{caller_info} WARNING \e[0m") text.split("\n").each do |line| Rails.logger.warn(" => #{line}") end end |
.vite_source_code_dir ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 111 def self.vite_source_code_dir vite_config = Rails.root.join('config', 'vite.json') if vite_config.exist? vc = JSON.parse(File.read(vite_config)) vc[Rails.env]['sourceCodeDir'] || vc['all']['sourceCodeDir'] end end |