Class: OpalWebpackLoader::LoadPathManager

Inherits:
Object
  • Object
show all
Defined in:
lib/opal-webpack-loader/load_path_manager.rb

Class Method Summary collapse

Class Method Details

.create_load_paths_cache(args) ⇒ Object



33
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
# File 'lib/opal-webpack-loader/load_path_manager.rb', line 33

def self.create_load_paths_cache(args)
  cache_path = args[0]
  filter = args[1..-1]
  load_paths = if File.exist?(File.join('bin', 'rails'))
                 %x{
                     bundle exec rails runner "puts (Rails.configuration.respond_to?(:assets) ? (Rails.configuration.assets.paths + Opal.paths).uniq : Opal.paths)"
                   }
               else
                 %x{
                     bundle exec ruby -e 'if File.exist?("app_loader.rb"); require "./app_loader.rb"; else; require "bundler/setup"; Bundler.require; set :run, false if defined? Sinatra; end; puts Opal.paths'
                   }
               end
  if $? == 0
    load_path_lines = load_paths.split("\n")
    load_path_lines.pop if load_path_lines.last == ''

    load_path_entries = []

    cwd = Dir.pwd

    load_path_lines.each do |path|
      next if path.start_with?(cwd)
      more_path_entries = get_load_path_entries(path, filter)
      load_path_entries.push(*more_path_entries) if more_path_entries.size > 0
    end
    cache_obj = { 'opal_load_paths' => load_path_lines, 'opal_load_path_entries' => load_path_entries }
    File.write(cache_path, Oj.dump(cache_obj, mode: :strict, indent: 2))
    load_path_lines
  else
    raise 'Error getting load paths!'
    exit(2)
  end
end

.get_load_path_entries(path, filter) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/opal-webpack-loader/load_path_manager.rb', line 3

def self.get_load_path_entries(path, filter)
  path_entries = []
  return [] unless Dir.exist?(path)
  dir_entries = Dir.entries(path)
  dir_entries.each do |entry|
    next if entry == '.'
    next if entry == '..'
    next unless entry
    absolute_path = File.join(path, entry)
    if File.directory?(absolute_path)
      more_path_entries = get_load_path_entries(absolute_path, filter)
      path_entries.push(*more_path_entries) if more_path_entries.size > 0
    elsif (absolute_path.end_with?('.rb') || absolute_path.end_with?('.js')) && File.file?(absolute_path)
      push_entry = true
      if filter && filter.size > 0
        filter.each do |filter_entry|
          push_entry = false if absolute_path.end_with?(filter_entry)
        end
      end
      path_entries.push(absolute_path) if push_entry
    end
  end
  path_entries
end

.read_load_paths_cache(json_path) ⇒ Object



28
29
30
31
# File 'lib/opal-webpack-loader/load_path_manager.rb', line 28

def self.read_load_paths_cache(json_path)
  load_paths_cache = Oj.load(File.read(json_path), mode: :strict)
  load_paths_cache['opal_load_paths']
end