Module: Bookworm

Defined in:
lib/bookworm/keys.rb,
lib/bookworm/crawler.rb,
lib/bookworm/load_hack.rb,
lib/bookworm/exceptions.rb,
lib/bookworm/infer_engine.rb,
lib/bookworm/configuration.rb,
lib/bookworm/knowledge_base.rb,
lib/bookworm/report_builder.rb,
lib/bookworm/infer_base_classes.rb

Overview

Copyright © 2022-present, Meta Platforms, Inc. and affiliates All rights reserved.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: InferRules, KnowledgeBaseBackends, Reports Classes: BaseReport, ClassLoadError, Configuration, Crawler, InferEngine, InferRule, KnowledgeBase, ReportBuilder

Constant Summary collapse

BOOKWORM_KEYS =

These keys are how we generalize and generate a lot of code within Bookworm

VALUES metakey: A metakey isn’t crawled, but is useful as a place to store information plural: The pluralized form of the key - typically used in method creation source_dirs: Specify which array of source directories to crawl glob_pattern: Specify the glob pattern for which files to crawl in source directories dont_init_kb_key: Don’t initialize the keys on knowledge base creation determine_cookbook_name: Determines the cookbook name from the given path path_name_regex: A regex with a capture to determine the prettified name of the file

{
  'cookbook' => {
    'metakey' => true,
    'dont_init_kb_key' => true,
  },
  'role' => {
    'source_dirs' => 'role_dirs',
    'glob_pattern' => '*.rb',
    'path_name_regex' => '([\w-]+)\.rb',
  },
  'metadatarb' => {
    'glob_pattern' => '*/metadata.rb',
    'determine_cookbook_name' => true,
    'path_name_regex' => '(metadata\.rb)',
  },
  'recipe' => {
    'determine_cookbook_name' => true,
    'path_name_regex' => 'recipes/(.*)\.rb',
  },
  'attribute' => {
    'determine_cookbook_name' => true,
    'path_name_regex' => 'attributes/(.*)\.rb',
  },
  'library' => {
    'plural' => 'libraries', # <-- the troublemaker that prompted keys first ;-)
    'determine_cookbook_name' => true,
    'path_name_regex' => 'libraries\/(.*)\.rb',
  },
  'resource' => {
    'determine_cookbook_name' => true,
    'path_name_regex' => 'resources/(.*)\.rb',
  },
  'provider' => {
    'determine_cookbook_name' => true,
    'path_name_regex' => 'providers/(.*)\.rb',
  },
}.freeze
BUILTIN_REPORTS_DIR =
"#{__dir__}/reports/".freeze
BUILTIN_RULES_DIR =
"#{__dir__}/rules/".freeze

Class Method Summary collapse

Class Method Details

.get_report_rules(report_name) ⇒ Object



68
69
70
71
# File 'lib/bookworm/report_builder.rb', line 68

def self.get_report_rules(report_name)
  klass = Module.const_get("Bookworm::Reports::#{report_name}")
  klass.needs_rules
end

.load_report_class(name, dir: '') ⇒ Object



73
74
75
76
77
78
79
# File 'lib/bookworm/report_builder.rb', line 73

def self.load_report_class(name, dir: '')
  f = File.read "#{dir}/#{name.to_sym}.rb"
  ::Bookworm::Reports.const_set(name.to_sym, ::Class.new(::Bookworm::BaseReport))
  ::Bookworm::Reports.const_get(name.to_sym).class_eval(f)
rescue StandardError
  raise Bookworm::ClassLoadError
end

.load_reports_dir(dir) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bookworm/report_builder.rb', line 81

def self.load_reports_dir(dir)
  files = Dir.glob("#{dir}/*.rb")
  files.each do |f|
    name = Pathname(f).basename.to_s.gsub('.rb', '')
    begin
      Bookworm.load_report_class name, :dir => dir
    rescue Bookworm::ClassLoadError
      puts "Unable to load report #{f}"
      exit(false)
    end
  end
end

.load_rule_class(name, dir: '') ⇒ Object



60
61
62
63
64
65
66
# File 'lib/bookworm/infer_base_classes.rb', line 60

def self.load_rule_class(name, dir: '')
  f = File.read "#{dir}/#{name.to_sym}.rb"
  ::Bookworm::InferRules.const_set(name.to_sym, ::Class.new(::Bookworm::InferRule))
  ::Bookworm::InferRules.const_get(name.to_sym).class_eval(f)
rescue StandardError
  raise Bookworm::ClassLoadError
end

.load_rules_dir(dir) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bookworm/infer_base_classes.rb', line 68

def self.load_rules_dir(dir)
  files = Dir.glob("#{dir}/*.rb")
  files.each do |f|
    name = Pathname(f).basename.to_s.gsub('.rb', '')
    begin
      Bookworm.load_rule_class name, :dir => dir
    rescue Bookworm::ClassLoadError
      puts "Unable to load rule #{f}"
      exit(false)
    end
  end
end