bonobot 0.0.10 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5cb9e5c0eaac02524d810bf6f59774cd16437e09a5c0e588996aa80826d4b371
4
- data.tar.gz: dc0b5858278ba66cb75db9839853bfc4a0c25a29667d768761b1ebe46b0b3d01
3
+ metadata.gz: 541fce28b33f9efe5631c3ee2c91b7e5ee61c5cf2b9d33913790d6575fd46388
4
+ data.tar.gz: 306f344425e0f65aa3e19b30fa721bdbf91b561e56f68fd4e07273a861140c46
5
5
  SHA512:
6
- metadata.gz: 9805b50c084da6db7e6b593e3e86cfe6af985f5d799355163d0239f70c9c67027a42cf4330c7736d11ebf24e38866075b5426531476f145e5178cd23fe93f4bc
7
- data.tar.gz: aaf8b17b178404a4d78aa7cec759b6136245ac29b3512e01ef5bcf5663a1cd4c21895746ce3b9b8b4b558bf01ce3ef84248b85683a23dfb6ea8cccc5540c8cac
6
+ metadata.gz: b3a503f11f950f2e8f0e241555a71308024a1a7a18a9d0808887a81cf49d6f3d8a9ec1fab38eff9578d73af6f83b7c6a5811d85f342df8dfe9209517bb0c18af
7
+ data.tar.gz: 68c51c580b004f6de15cfc2568b412312e5195cb317d271061b4717e75b943f09ba2da3cea4df959559357e14c1437ef8f6883d70f84d1ee80c07e8f6e05277a
data/README.md CHANGED
@@ -8,14 +8,14 @@ BonoBot is a Ruby gem that helps with Rails monkey patching.
8
8
  ### Status
9
9
  #### Generate all status:
10
10
  ```bash
11
- bundle exec rake bonobot: status
11
+ bundle exec rake bonobot:status
12
12
  ```
13
13
  #### Generate a specific status:
14
14
  ```bash
15
- bundle exec rake bonobot:status:out_of_date
16
15
  bundle exec rake bonobot:status:up_to_date
17
- bundle exec rake bonobot:update_out_of_date
18
- bundle exec rake bonobot:unused
16
+ bundle exec rake bonobot:status:out_of_date
17
+ bundle exec rake bonobot:status:missing
18
+ bundle exec rake bonobot:status:unused
19
19
  ```
20
20
 
21
21
  ### Add missing
@@ -28,6 +28,11 @@ bundle exec rake bonobot:add_missing
28
28
  bundle exec rake bonobot:update_out_of_date
29
29
  ```
30
30
 
31
+ ### Customization
32
+ ```bash
33
+ bundle exec rake bonobot:install
34
+ ```
35
+
31
36
  ## Installation
32
37
  Add this line to your application's Gemfile:
33
38
 
@@ -21,7 +21,7 @@ module Bonobot
21
21
  if f.first == "# frozen_string_literal: true"
22
22
  f.insert(1, "\n#{annotation}")
23
23
  else
24
- f.insert(0, "\n#{annotation}")
24
+ f.insert(0, annotation)
25
25
  end
26
26
 
27
27
  File.write(@path, "#{f.join("\n")}\n")
@@ -39,7 +39,7 @@ module Bonobot
39
39
 
40
40
  def annotation
41
41
  if @path.to_s.end_with?(".erb")
42
- "<%# bonobot_fingerprint: #{@fingerprint} %>"
42
+ "<%# bonobot_fingerprint: #{@fingerprint} %>\n"
43
43
  else
44
44
  "# bonobot_fingerprint: #{@fingerprint}"
45
45
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bonobot::Configuration
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def configure
14
+ yield(configuration)
15
+ end
16
+ end
17
+
18
+ class Configuration
19
+ attr_accessor :included_dirs, :files_pattern, :excluded_files, :fingerprint_algorithm, :fingerprint_human_readable
20
+
21
+ def initialize
22
+ @status_file_path = config_value_for("status_file_path", Rails.root)
23
+ @status_file_name = "#{config_value_for("status_file_name", "status")}.json"
24
+ @included_dirs = "{#{config_value_for("included_dirs", ["app"]).join(",")}}"
25
+ @files_pattern = "{#{config_value_for("files_pattern", %w(rb erb)).join(",")}}"
26
+ @excluded_files = config_value_for("excluded_files", [])
27
+ @fingerprint_algorithm = config_value_for("fingerprint_algorithm", "md5")
28
+ @fingerprint_human_readable = config_value_for("fingerprint_human_readable", false)
29
+ end
30
+
31
+ def status_file
32
+ File.join(@status_file_path, @status_file_name)
33
+ end
34
+
35
+ def self.config_file
36
+ @config_file ||= File.exist?(Rails.root.join(".bonobot.yml")) ? YAML.load_file(Rails.root.join(".bonobot.yml")) : {}
37
+ end
38
+
39
+ def config_value_for(key, default_value)
40
+ self.class.config_file.fetch(key, default_value)
41
+ end
42
+ end
43
+ end
@@ -2,17 +2,14 @@
2
2
 
3
3
  module Bonobot
4
4
  class EngineFile
5
- attr_reader :path, :engine_name, :short_path, :root_path
5
+ attr_reader :path, :engine_name, :short_path, :root_path, :fingerprint
6
6
 
7
7
  def initialize(path, engine)
8
8
  @path = path
9
9
  @root_path = engine.instance.root
10
10
  @engine_name = engine_to_name(engine)
11
11
  @short_path = path.sub("#{@root_path}/", "")
12
- end
13
-
14
- def fingerprint
15
- Digest::MD5.hexdigest(File.read(@path))
12
+ @fingerprint = Bonobot::Fingerprint.calculate(path)
16
13
  end
17
14
 
18
15
  def to_hash
@@ -2,13 +2,18 @@
2
2
 
3
3
  module Bonobot
4
4
  class EnginesFilesRegistry
5
+ include Bonobot::Configuration
6
+ include Bonobot::Findable
7
+ include Bonobot::Outputable
8
+ include Bonobot::Reloadable
9
+
5
10
  def self.all
6
- @all ||= deduplicate(generate)
11
+ @all ||= deduplicate(generate).reject { |engine_file| configuration.excluded_files.include?(engine_file.short_path) }
7
12
  end
8
13
 
9
14
  def self.generate
10
15
  Parallel.flat_map(::Rails::Engine.subclasses) do |klass|
11
- Dir.glob(root(klass.instance.root).join("**", "*.{erb,rb}")).map do |path|
16
+ Dir.glob(root(klass.instance.root).join("**", "*.#{file_pattern}")).map do |path|
12
17
  EngineFile.new(path, klass)
13
18
  end
14
19
  end
@@ -18,20 +23,12 @@ module Bonobot
18
23
  engine_files.group_by(&:path).map { |_, files| files.min_by(&:engine_name) }
19
24
  end
20
25
 
21
- def self.find_by(attributes)
22
- all.select do |local_file|
23
- attributes.all? do |key, value|
24
- local_file.try(key) == value
25
- end
26
- end
27
- end
28
-
29
- def self.output
30
- all.map(&:as_json)
26
+ def self.root(path)
27
+ Pathname.new(path).join(configuration.included_dirs)
31
28
  end
32
29
 
33
- def self.root(path)
34
- Pathname.new(path).join("app")
30
+ def self.file_pattern
31
+ configuration.files_pattern
35
32
  end
36
33
  end
37
34
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bonobot/overloads_registry"
4
- require "bonobot/annotator"
5
-
6
3
  module Bonobot
7
4
  class FilesOp
8
5
  def self.missing
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bonobot::Findable
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def find_by(attributes)
10
+ all.select do |local_file|
11
+ attributes.all? do |key, value|
12
+ local_file.try(key) == value
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "digest/bubblebabble"
5
+
6
+ module Bonobot::Fingerprint
7
+ include Bonobot::Configuration
8
+ ALGORITHM = { "md5" => Digest::MD5, "sha1" => Digest::SHA1, "sha256" => Digest::SHA256 }.freeze
9
+
10
+ def self.calculate(path)
11
+ algorithm.send(method, File.read(path))
12
+ end
13
+
14
+ def self.method
15
+ if configuration.fingerprint_human_readable
16
+ :bubblebabble
17
+ else
18
+ :hexdigest
19
+ end
20
+ end
21
+
22
+ def self.algorithm
23
+ ALGORITHM.fetch(configuration.fingerprint_algorithm, Digest::MD5)
24
+ end
25
+ end
@@ -1,25 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "parallel"
4
-
5
3
  module Bonobot
6
4
  class LocalFilesRegistry
5
+ include Bonobot::Configuration
6
+ include Bonobot::Outputable
7
+ include Bonobot::Reloadable
8
+
7
9
  def self.all
8
- @all ||= Parallel.map(Dir.glob(root.join("**", "*.{erb,rb}"))) do |path|
9
- LocalFile.new(path, rails_root)
10
- end
10
+ @all ||= Parallel.map(Dir.glob(root.join("**", "*.#{file_pattern}"))) { |path| LocalFile.new(path, ::Rails.root) }
11
+ .reject { |local_file| configuration.excluded_files.include?(local_file.path) }
11
12
  end
12
13
 
13
14
  def self.root
14
- rails_root.join("app")
15
- end
16
-
17
- def self.rails_root
18
- ::Rails.root
15
+ ::Rails.root.join(configuration.included_dirs)
19
16
  end
20
17
 
21
- def self.output
22
- all.map(&:as_json)
18
+ def self.file_pattern
19
+ configuration.files_pattern
23
20
  end
24
21
  end
25
22
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bonobot::Outputable
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def output
10
+ all.map(&:as_json)
11
+ end
12
+ end
13
+ end
@@ -2,12 +2,17 @@
2
2
 
3
3
  module Bonobot
4
4
  module OverloadsRegistry
5
+ include Bonobot::Findable
6
+ include Bonobot::Outputable
7
+ include Bonobot::Reloadable
8
+
5
9
  def self.all
6
10
  @all ||= LocalFilesRegistry.all.flat_map do |local_file|
7
- next if local_file.nil? || local_file.annotation.nil?
11
+ next if local_file.nil?
8
12
 
9
13
  engines_files = EnginesFilesRegistry.find_by(short_path: local_file.path)
10
- if engines_files.empty?
14
+
15
+ if engines_files.empty? && local_file.annotation.present?
11
16
  Overload.new(local_file, nil)
12
17
  else
13
18
  engines_files.map do |engine_file|
@@ -16,22 +21,5 @@ module Bonobot
16
21
  end
17
22
  end.compact
18
23
  end
19
-
20
- # TODO: Extract to module
21
- def self.find_by(attributes)
22
- all.select do |item|
23
- attributes.all? do |key, value|
24
- item.try(key) == value
25
- end
26
- end
27
- end
28
-
29
- def self.output
30
- all.map(&:as_json)
31
- end
32
-
33
- def self.reload
34
- @all = nil
35
- end
36
24
  end
37
25
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bonobot::Reloadable
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def reload
10
+ @all = nil
11
+ end
12
+ end
13
+ end
@@ -4,8 +4,9 @@ require "json"
4
4
 
5
5
  module Bonobot
6
6
  class Status
7
+ include Bonobot::Configuration
8
+
7
9
  STATUS = { up_to_date: "🄳", out_of_date: "😱", unused: "šŸ˜…", missing: "🤬" }.freeze
8
- STATUS_FILE_PATH = "status.json"
9
10
 
10
11
  def self.generate(status = nil)
11
12
  new(status).generate
@@ -51,7 +52,7 @@ module Bonobot
51
52
  end
52
53
 
53
54
  def generate_status_file
54
- File.write(STATUS_FILE_PATH, status_json)
55
+ File.write(self.class.configuration.status_file, status_json)
55
56
  end
56
57
 
57
58
  def display_banner
@@ -59,7 +60,7 @@ module Bonobot
59
60
  end
60
61
 
61
62
  def display_intro
62
- "-----\nšŸ™ˆ šŸ™‰ šŸ™Š Bonobot šŸ™ˆ šŸ™‰ šŸ™Š\n-----\n\nšŸ›  Generating status\n#{File.expand_path("status.json")}\n-----\n\n"
63
+ "-----\nšŸ™ˆ šŸ™‰ šŸ™Š Bonobot šŸ™ˆ šŸ™‰ šŸ™Š\n-----\n\nšŸ›  Generating status\n#{File.expand_path(self.class.configuration.status_file)}\n-----\n\n"
63
64
  end
64
65
 
65
66
  def display_status
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bonobot
4
- VERSION = "0.0.10"
4
+ VERSION = "0.1.0"
5
5
  end
data/lib/bonobot.rb CHANGED
@@ -12,4 +12,9 @@ module Bonobot
12
12
  autoload :Overload, "bonobot/overload"
13
13
  autoload :FilesOp, "bonobot/files_op"
14
14
  autoload :Annotator, "bonobot/annotator"
15
+ autoload :Configuration, "bonobot/configuration"
16
+ autoload :Findable, "bonobot/findable"
17
+ autoload :Outputable, "bonobot/outputable"
18
+ autoload :Reloadable, "bonobot/reloadable"
19
+ autoload :Fingerprint, "bonobot/fingerprint"
15
20
  end
@@ -45,4 +45,11 @@ namespace :bonobot do
45
45
 
46
46
  task update_outdated: :update_out_of_date
47
47
  task update: :update_out_of_date
48
+
49
+ desc "Install bonobot"
50
+ task install: :environment do
51
+ dir = Gem::Specification.find_by_name("bonobot").gem_dir
52
+ FileUtils.cp_r File.join(dir, "bonobot_configuration_example.yml"), ".bonobot.yml"
53
+ puts "Bonobot configuration installed at .bonobot.yml"
54
+ end
48
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bonobot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - armandfardeau
@@ -162,14 +162,19 @@ files:
162
162
  - Rakefile
163
163
  - lib/bonobot.rb
164
164
  - lib/bonobot/annotator.rb
165
+ - lib/bonobot/configuration.rb
165
166
  - lib/bonobot/engine_file.rb
166
167
  - lib/bonobot/engines_files_registry.rb
167
168
  - lib/bonobot/files_op.rb
169
+ - lib/bonobot/findable.rb
170
+ - lib/bonobot/fingerprint.rb
168
171
  - lib/bonobot/local_file.rb
169
172
  - lib/bonobot/local_files_registry.rb
173
+ - lib/bonobot/outputable.rb
170
174
  - lib/bonobot/overload.rb
171
175
  - lib/bonobot/overloads_registry.rb
172
176
  - lib/bonobot/railtie.rb
177
+ - lib/bonobot/reloadable.rb
173
178
  - lib/bonobot/status.rb
174
179
  - lib/bonobot/version.rb
175
180
  - lib/tasks/bonobot_tasks.rake