bonobot 0.0.10 ā†’ 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5cb9e5c0eaac02524d810bf6f59774cd16437e09a5c0e588996aa80826d4b371
4
- data.tar.gz: dc0b5858278ba66cb75db9839853bfc4a0c25a29667d768761b1ebe46b0b3d01
3
+ metadata.gz: a4f7b3ef84ffd8043657a8fc28a68f0f37bd0e7b7480547106a2ddef35eaf002
4
+ data.tar.gz: ba0ad7a0f8f10fad47a287f79a79798709b9cd3a8e40a90692a088e5215ef6c0
5
5
  SHA512:
6
- metadata.gz: 9805b50c084da6db7e6b593e3e86cfe6af985f5d799355163d0239f70c9c67027a42cf4330c7736d11ebf24e38866075b5426531476f145e5178cd23fe93f4bc
7
- data.tar.gz: aaf8b17b178404a4d78aa7cec759b6136245ac29b3512e01ef5bcf5663a1cd4c21895746ce3b9b8b4b558bf01ce3ef84248b85683a23dfb6ea8cccc5540c8cac
6
+ metadata.gz: 488337ea6b5a647c1563cec5b77864cdb3aa4bef9a50a387df0053635d9b83ccabbc796c637bbcfc02ad89b3029f5803eaf6a058dbd6d415071605570387a52f
7
+ data.tar.gz: d31fc7230c48c77b7de01c56bde1d64631950f64cd757b7bf28084f22e870c221a253495a2b994010dd4fa2dde7aba01ae0c2d092bc31fb44304f420f867db43
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
 
@@ -0,0 +1,18 @@
1
+ ############################################
2
+ # Bonobot configuration file #
3
+ # This file is used to configure Bonobot #
4
+ # https://github.com/armandfardeau/bonobot #
5
+ ############################################
6
+
7
+ ##### Status file configuration ######
8
+ # status_file_path: "" # default to Rails.root
9
+ # status_file_name: "" # default to 'status'
10
+
11
+ ##### Overload matching configuration ######
12
+ # included_dirs: [] # default to ['app']
13
+ # files_pattern: [] # default to ['rb', 'erb']
14
+ # excluded_files: [] # default to []
15
+
16
+ # Fingerprint configuration
17
+ # fingerprint_algorithm: "md5" # default to "md5", available: "md5", "sha1", "sha256"
18
+ # fingerprint_human_readable: false # default to false, available: true, false
@@ -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.1"
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, "config", "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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - armandfardeau
@@ -160,16 +160,22 @@ files:
160
160
  - MIT-LICENSE
161
161
  - README.md
162
162
  - Rakefile
163
+ - config/bonobot_configuration_example.yml
163
164
  - lib/bonobot.rb
164
165
  - lib/bonobot/annotator.rb
166
+ - lib/bonobot/configuration.rb
165
167
  - lib/bonobot/engine_file.rb
166
168
  - lib/bonobot/engines_files_registry.rb
167
169
  - lib/bonobot/files_op.rb
170
+ - lib/bonobot/findable.rb
171
+ - lib/bonobot/fingerprint.rb
168
172
  - lib/bonobot/local_file.rb
169
173
  - lib/bonobot/local_files_registry.rb
174
+ - lib/bonobot/outputable.rb
170
175
  - lib/bonobot/overload.rb
171
176
  - lib/bonobot/overloads_registry.rb
172
177
  - lib/bonobot/railtie.rb
178
+ - lib/bonobot/reloadable.rb
173
179
  - lib/bonobot/status.rb
174
180
  - lib/bonobot/version.rb
175
181
  - lib/tasks/bonobot_tasks.rake