Class: Ree::CLI::Indexing::IndexFile

Inherits:
Object
  • Object
show all
Includes:
Ree::CLI::Indexing
Defined in:
lib/ree/cli/indexing/index_file.rb

Class Method Summary collapse

Methods included from Ree::CLI::Indexing

included

Class Method Details

.find_package(dir) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ree/cli/indexing/index_file.rb', line 67

def find_package(dir)
  package_schema = File.join(dir, Ree::PACKAGE_SCHEMA_FILE)
  
  if File.exist?(package_schema)
    return package_schema
  end
  
  if dir == '/'
    return nil
  end
  
  find_package(File.expand_path('..', dir))
end

.run(file_path:, project_path:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ree/cli/indexing/index_file.rb', line 10

def run(file_path:, project_path:)
  ENV['REE_SKIP_ENV_VARS_CHECK'] = 'true'
  
  path = Ree.locate_packages_schema(project_path)
  dir = Pathname.new(path).dirname.to_s
  
  Ree.init(dir)
  
  file_path = File.join(dir, file_path)
  
  current_package_schema = self.find_package(File.dirname(file_path))
  
  return {} unless current_package_schema
  
  package_schema = JSON.load_file(current_package_schema)
  current_package_name = package_schema["name"].to_sym
  
  facade = Ree.container.packages_facade
  Ree.load_package(current_package_name)
  
  package = facade.get_package(current_package_name)
  
  files = Dir[
    File.join(
      Ree::PathHelper.abs_package_module_dir(package), '**/*.rb'
    )
  ]
  
  return {} if !files.include?(file_path)
  
  objects_class_names = package.objects.map(&:class_name)
  file_name_const_string = Ree::StringUtils.camelize(file_path.split('/')[-1].split('.rb')[0])
  const_string_with_module = "#{package.module}::#{file_name_const_string}"
  
  return {} if objects_class_names.include?(const_string_with_module) # skip objects
  
  klass = Object.const_get(const_string_with_module)
  
  methods = klass
    .public_instance_methods(false)
    .reject { _1.match?(/original/) } # remove aliases defined by contracts
    .map {
      {
        name: _1,
        location: klass.public_instance_method(_1).source_location&.last,
      }
    }
  
  hsh = {
    path: file_path,
    package: current_package_name,
    methods: methods
  }
  
  JSON.pretty_generate({ file_name_const_string => hsh })
end