Skip to content

Commit

Permalink
Move library files to lib/
Browse files Browse the repository at this point in the history
Summary:
This gets the cookbook tree in line with the bookworm-specific github
repo, while still working when run within the cookbook

Instead of using require_relative for libraries, we'll add the bookworm libraries to the LOAD_PATH in the wrapper script created in the prior diff in the stack. This allows us to follow the idiomatic lib/ directory layout seen in most Ruby gems (and used in the
Bookworm github repo).

Differential Revision: D68423773

fbshipit-source-id: 3c9acd5a1ffd971c9f0c55043486639c8b2e032c
  • Loading branch information
dafyddcrosby authored and facebook-github-bot committed Jan 27, 2025
1 parent f499c9a commit 03e935b
Show file tree
Hide file tree
Showing 49 changed files with 44 additions and 18 deletions.
19 changes: 10 additions & 9 deletions cookbooks/fb_bookworm/files/default/bookworm/bin/bookworm
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ end
# We require the libraries *after* the profiler has a chance to start,
# also means faster `bookworm -h` response
require 'set'
require_relative '../exceptions'
require_relative '../keys'
require_relative '../configuration'
require_relative '../crawler'
require_relative '../knowledge_base'
require_relative '../infer_engine'
require_relative '../report_builder'
require 'bookworm/exceptions'
require 'bookworm/keys'
require 'bookworm/configuration'
require 'bookworm/crawler'
require 'bookworm/knowledge_base'
require 'bookworm/infer_engine'
require 'bookworm/report_builder'

module Bookworm
class ClassLoadError < RuntimeError; end
Expand Down Expand Up @@ -189,11 +189,12 @@ module Bookworm
end

def load_src_dirs
@report_src_dirs = ["#{__dir__}/../reports/"]
require 'bookworm/load_hack'
@report_src_dirs = [::Bookworm::BUILTIN_REPORTS_DIR]
if Dir.exist? "#{@config.system_contrib_dir}/reports"
@report_src_dirs.append "#{@config.system_contrib_dir}/reports"
end
@rule_src_dirs = ["#{__dir__}/../rules/"]
@rule_src_dirs = [::Bookworm::BUILTIN_RULES_DIR]
if Dir.exist? "#{@config.system_contrib_dir}/rules/"
@rule_src_dirs.append "#{@config.system_contrib_dir}/rules/"
end
Expand Down
1 change: 1 addition & 0 deletions cookbooks/fb_bookworm/files/default/bookworm/bookworm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
# Wrapper so that we can have ruby load path hackery from the fb_bookworm cookbook
# I'd like to believe this won't be permanent, but these things last for years...

$LOAD_PATH.unshift("#{__dir__}/lib")
load 'bin/bookworm'
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
require_relative 'knowledge_base'
require_relative 'infer_base_classes'
require 'bookworm/knowledge_base'
require 'bookworm/infer_base_classes'

module Bookworm
# The InferEngine class takes a KnowledgeBase object, and then runs the given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 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
#
# http://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.

# TODO: This is gross, and I seem to recall there was some way to get a gem's
# library directory, but this should work for now.
module Bookworm
BUILTIN_REPORTS_DIR = "#{__dir__}/reports/".freeze
BUILTIN_RULES_DIR = "#{__dir__}/rules/".freeze
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
require_relative 'spec_helper'
require_relative '../keys'
require_relative '../knowledge_base'
require 'bookworm/keys'
require 'bookworm/knowledge_base'

describe Bookworm::KnowledgeBase do
it 'holds all yer roles' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:disable Chef/Meta/SpecFilename
# Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
# All rights reserved.
#
Expand All @@ -13,15 +14,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
require 'pathname'
require_relative '../../crawler'
require_relative '../../exceptions'
require_relative '../../infer_engine'
require 'bookworm/crawler'
require 'bookworm/exceptions'
require 'bookworm/infer_engine'

# Load all rule files
files = Dir.glob("#{__dir__}/../../rules/*.rb")
files = Dir.glob("#{__dir__}/../../lib/bookworm/rules/*.rb")
files.each do |f|
name = Pathname(f).basename.to_s.gsub('.rb', '')
::Bookworm.load_rule_class name, :dir => "#{__dir__}/../../rules"
::Bookworm.load_rule_class name, :dir => "#{__dir__}/../../lib/bookworm/rules"
end

require_relative '../spec_helper'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
config.filter_run(:focus)
config.order = :random
end

$LOAD_PATH.unshift("#{__dir__}/../lib")

0 comments on commit 03e935b

Please sign in to comment.