Class: Ree::PackagesFacade

Inherits:
Object show all
Includes:
Args
Defined in:
lib/ree/facades/packages_facade.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Args

#check_arg, #check_arg_any, #check_arg_array_of, #check_bool, #not_nil

Constructor Details

#initializePackagesFacade

Returns a new instance of PackagesFacade.



8
9
10
11
# File 'lib/ree/facades/packages_facade.rb', line 8

def initialize
  load_packages_schema
  @package_loader = Ree::PackageLoader.new(@packages_store)
end

Instance Attribute Details

#package_loaderObject (readonly)

Returns the value of attribute package_loader.



6
7
8
# File 'lib/ree/facades/packages_facade.rb', line 6

def package_loader
  @package_loader
end

#packages_storeObject (readonly)

Returns the value of attribute packages_store.



6
7
8
# File 'lib/ree/facades/packages_facade.rb', line 6

def packages_store
  @packages_store
end

Class Method Details

.write_packages_schemaObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ree/facades/packages_facade.rb', line 14

def write_packages_schema
  Ree.logger.debug("write_packages_schema")
  packages_schema = Ree::PackagesSchemaBuilder.new.call

  json = JSON.pretty_generate(packages_schema)

  File.write(
    File.join(Ree.root_dir, Ree::PACKAGES_SCHEMA_FILE),
    json,
    mode: 'w'
  )

  json
end

Instance Method Details

#dump_package_schema(package_name) ⇒ Object

Returns nil.

Parameters:

  • package_name (Symbol)

Returns:

  • nil



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ree/facades/packages_facade.rb', line 72

def dump_package_schema(package_name)
  Ree.logger.debug("dump_package_schema(:#{package_name})")

  read_package_schema_json(package_name)
  package = get_package(package_name)

  if package.dir
    schema_path = Ree::PathHelper.abs_package_schema_path(package)

    if !File.exist?(schema_path)
      raise Ree::Error.new("File does not exist: #{schema_path}", :invalid_path)
    end

    schema = Ree::PackageSchemaBuilder.new.call(package)
    json = JSON.pretty_generate(schema)
    File.write(schema_path, json, mode: 'w')

    json
  end
end

#get_loaded_package(package_name) ⇒ Ree::Package

Parameters:

  • package_name (Symbol)

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ree/facades/packages_facade.rb', line 36

def get_loaded_package(package_name)
  package = get_package(package_name)
  return package if package.schema_loaded?

  if perf_mode?(package)
    read_package_schema_json(package_name)
  else
    load_entire_package(package_name)
  end

  package
end

#get_object(package_name, object_name) ⇒ Ree::Object

Parameters:

  • package_name (Symbol)
  • object_name (Symbol)

Returns:



52
53
54
55
56
57
58
59
60
61
# File 'lib/ree/facades/packages_facade.rb', line 52

def get_object(package_name, object_name)
  package = get_loaded_package(package_name)
  object = package.get_object(object_name)

  if !object && perf_mode?(package)
    raise Ree::Error.new("Ree object :#{object_name} for package :#{package_name} not found")
  end

  object
end

#get_package(package_name, raise_if_missing = true) ⇒ Ree::Package

Parameters:

  • package_name (Symbol)

Returns:



219
220
221
222
223
224
225
226
227
228
# File 'lib/ree/facades/packages_facade.rb', line 219

def get_package(package_name, raise_if_missing = true)
  check_arg(package_name, :package_name, Symbol)
  package = @packages_store.get(package_name)

  if !package && raise_if_missing
    raise Ree::Error.new("Package :#{package_name} is not found in Packages.schema.json. Run `ree gen.packages_json` to update schema.", :package_schema_not_found)
  end

  package
end

#has_object?(package_name, object_name) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
# File 'lib/ree/facades/packages_facade.rb', line 63

def has_object?(package_name, object_name)
  package = get_loaded_package(package_name)
  object = package.get_object(object_name)

  !object.nil?
end

#has_package?(package_name) ⇒ Bool

Parameters:

  • package_name (Symbol)

Returns:

  • (Bool)


212
213
214
215
# File 'lib/ree/facades/packages_facade.rb', line 212

def has_package?(package_name)
  check_arg(package_name, :package_name, Symbol)
  !!@packages_store.get(package_name)
end

#load_entire_package(package_name) ⇒ Ree::Package

Parameters:

  • package_name (Symbol)

Returns:



174
175
176
# File 'lib/ree/facades/packages_facade.rb', line 174

def load_entire_package(package_name)
  @package_loader.call(package_name)
end

#load_file(path, package_name) ⇒ nil

Parameters:

  • path (String)
  • package_name (Symbol)

Returns:

  • (nil)


239
240
241
# File 'lib/ree/facades/packages_facade.rb', line 239

def load_file(path, package_name)
  @package_loader.load_file(path, package_name)
end

#load_package_entry(package_name) ⇒ Object

Returns nil.

Parameters:

  • package_name (Symbol)

Returns:

  • nil



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ree/facades/packages_facade.rb', line 118

def load_package_entry(package_name)
  package = @packages_store.get(package_name)

  if package.nil?
    raise Ree::Error.new("package :#{package_name} not found in Packages.schema.json")
  end

  return if package.loaded?

  Ree.logger.debug("load_package_entry(:#{package_name})")

  load_file(
    Ree::PathHelper.abs_package_entry_path(package),
    package_name
  )
end

#load_package_object(package_name, object_name) ⇒ Ree::Object

Parameters:

  • package_name (Symbol)
  • object_name (Symbol)

Returns:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ree/facades/packages_facade.rb', line 138

def load_package_object(package_name, object_name)
  package = get_loaded_package(package_name)
  load_package_entry(package_name)

  object = get_object(package_name, object_name)
  return object if object && object.loaded?

  if !object && !perf_mode?(package)
    Dir[
      File.join(
        Ree::PathHelper.abs_package_module_dir(package),
        "**/#{object_name}.rb"
      )
    ].each do |path|
      load_file(path, package_name)
    end

    object = get_object(package_name, object_name)
  end

  if !object
    raise Ree::Error.new("object :#{object_name} from :#{package_name} was not found")
  end

  Ree.logger.debug("load_package_object(:#{package_name}, :#{object_name})")

  if object.rpath
    object_path = Ree::PathHelper.abs_object_path(object)
    load_file(object_path, package_name)
  end

  object
end

#load_packages_schemaRee::PackagesStore

Returns:



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/ree/facades/packages_facade.rb', line 197

def load_packages_schema
  Ree.logger.debug("load_packages_schema: #{self.object_id}")
  @packages_store = Ree::PackagesSchemaLoader.new.call(Ree.packages_schema_path, @packages_store)

  Ree.gems.each do |gem|
    Ree::PackagesSchemaLoader.new.call(
      gem.packages_schema_path, @packages_store, gem.name
    )
  end

  @packages_store
end

#perf_mode?(package) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/ree/facades/packages_facade.rb', line 30

def perf_mode?(package)
  package.gem? ? true : Ree.performance_mode?
end

#read_package_schema_json(package_name) ⇒ Ree::Package

Parameters:

  • package_name (Symbol)

Returns:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/ree/facades/packages_facade.rb', line 180

def read_package_schema_json(package_name)
  @loaded_schemas ||= {}
  return @loaded_schemas[package_name] if @loaded_schemas[package_name]

  Ree.logger.debug("read_package_schema_json(:#{package_name})")
  package = get_package(package_name)

  if !package.dir
    package.set_schema_loaded
    return package
  end

  schema_path = Ree::PathHelper.abs_package_schema_path(package)
  @loaded_schemas[package_name] = Ree::PackageSchemaLoader.new.call(schema_path, package)
end

#store_package(package) ⇒ Ree::Package

Parameters:

Returns:



232
233
234
# File 'lib/ree/facades/packages_facade.rb', line 232

def store_package(package)
  @packages_store.add_package(package)
end

#write_package_schema(package_name) ⇒ Object

Returns nil.

Parameters:

  • package_name (Symbol)

Returns:

  • nil



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ree/facades/packages_facade.rb', line 95

def write_package_schema(package_name)
  Ree.logger.debug("write_package_schema(:#{package_name})")

  load_entire_package(package_name)
  package = get_package(package_name)

  if package.dir
    schema_path = Ree::PathHelper.abs_package_schema_path(package)

    if !File.exist?(schema_path)
      raise Ree::Error.new("File does not exist: #{schema_path}", :invalid_path)
    end

    schema = Ree::PackageSchemaBuilder.new.call(package)
    json = JSON.pretty_generate(schema)
    File.write(schema_path, json, mode: 'w')

    json
  end
end