checky 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 688c096c18ce172e0c5394d4d4d8c7c26f20a640
4
+ data.tar.gz: 4e5819ad3812086712428d1c30bb4b9ac46bc9f2
5
+ SHA512:
6
+ metadata.gz: 72fa236906c0f2fe4420f9c7782078c06824093cccb41321790fd82b9fd6bc4d7489e59882b1c62b3421218e5bee066023490bea303866c09b44375286fcd1e4
7
+ data.tar.gz: e782c78722f77d0c61123843b9ad336b5e323227a96ff711cbab67c9387a5f35e7074b569493fec4e55f14c1b9621c8d0a5e826391971833868e9d00ed921ac7
data/lib/checky.rb ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ require 'ostruct'
3
+
4
+ require 'core_ext'
5
+ require 'checky/version'
6
+ require 'checky/exceptions'
7
+
8
+ validators = Dir[File.expand_path('../checky/validators/*.rb', __FILE__)].map do |file|
9
+ File.basename(file).sub(/\.rb$/, '')
10
+ end
11
+ validators.reject! { |file| file == 'all' }
12
+
13
+ validators.each do |validator|
14
+ require "checky/validators/#{validator}"
15
+ end
16
+ require 'checky/validators/all'
17
+
18
+ require 'checky/checker'
19
+
20
+ module Checky
21
+ attr_accessor :colorize
22
+
23
+ def check(&block)
24
+ Checker.new.check(&block)
25
+ end
26
+
27
+ # :nocov:
28
+ def run(command)
29
+ `#{command}`
30
+ end
31
+ # :nocov:
32
+
33
+ module_function :check, :run, :colorize, :colorize=
34
+ end
35
+
36
+ # :nocov:
37
+ begin
38
+ require 'colorize'
39
+ Checky.colorize = true
40
+ rescue LoadError
41
+ Checky.colorize = false
42
+ end
43
+ # :nocov:
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+ module Checky
3
+ class Checker
4
+ include Checky::Validators::All
5
+
6
+ attr_reader :storage
7
+
8
+ def initialize
9
+ @storage = OpenStruct.new(checky_blocks: {}, checky_results: {})
10
+ @self_before_instance_eval = false
11
+ end
12
+
13
+ def check(&block)
14
+ @self_before_instance_eval = eval 'self', block.binding
15
+ instance_eval(&block)
16
+ with_hooks { check_result }
17
+ @storage.checky_final_result
18
+ end
19
+
20
+ # :nocov:
21
+ # :reek:BooleanParameter
22
+ def respond_to_missing?(method, _include_private = false)
23
+ methods.include?("run_#{method}".to_sym)
24
+ end
25
+ # :nocov:
26
+
27
+ # rubocop:disable Style/MethodMissing
28
+ def method_missing(method, *args, &block)
29
+ raise Checky::Exception unless methods.include?("populate_#{method}".to_sym)
30
+ @storage.send("#{method}=", send("populate_#{method}", *args, &block))
31
+ @storage.checky_blocks[method.to_sym] = block if block_given?
32
+ rescue Checky::Exception
33
+ @self_before_instance_eval.send method, *args, &block if @self_before_instance_eval.present?
34
+ end
35
+ # rubocop:enable Style/MethodMissing
36
+
37
+ private
38
+
39
+ def check_result
40
+ @storage.checky_final_result = active_validators.all? do |validator_name|
41
+ block = @storage.checky_blocks[validator_name]
42
+ @storage.checky_results[validator_name] = if block.present?
43
+ instance_eval(&block)
44
+ else
45
+ send("check_#{validator_name}")
46
+ end
47
+ end
48
+ end
49
+
50
+ def active_validators
51
+ @storage.to_h.keys.reject { |key| key.to_s.start_with?('checky_') }
52
+ end
53
+
54
+ def with_hooks
55
+ fire_hook :before
56
+ yield if block_given?
57
+ fire_hook :after
58
+ end
59
+
60
+ def fire_hook(hook_name)
61
+ active_validators.each { |validator_name| send("#{hook_name}_#{validator_name}") }
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ module Checky
3
+ class Exception < RuntimeError
4
+ end
5
+
6
+ class ValidationError < RuntimeError
7
+ end
8
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+ module Checky
3
+ module Validators
4
+ module All
5
+ end
6
+ end
7
+ end
8
+
9
+ Checky::Validators.constants.each do |child|
10
+ next if child == :All
11
+ mod = Checky::Validators.const_get(child)
12
+
13
+ next unless mod.is_a?(Module)
14
+ Checky::Validators::All.include(mod)
15
+
16
+ mod.send(:attr_accessor, :storage)
17
+ mod.send(:module_function, :storage, :storage=)
18
+ %w(populate check message before after).each do |method|
19
+ mod.send(:module_function, method) if mod.method_defined?(method)
20
+ end
21
+
22
+ Checky::Validators::All.send(:define_method, "populate_#{child.to_s.underscore}") do |*args, &block|
23
+ validator = Object.const_get("Checky::Validators::#{child}")
24
+ data = validator.respond_to?(:populate) ? validator.populate(*args, &block) : true
25
+ @storage.send("#{child.to_s.underscore}=", data)
26
+ end
27
+
28
+ %w(check message before after).each do |helper_method|
29
+ Checky::Validators::All.send(:define_method, "#{helper_method}_#{child.to_s.underscore}") do |*args, &block|
30
+ validator = Object.const_get("Checky::Validators::#{child}")
31
+ validator.storage = @storage
32
+ validator.respond_to?(helper_method) ? validator.send(helper_method, *args, &block) : true
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ module Checky
3
+ module Validators
4
+ module Binary
5
+ # :reek:UtilityFunction
6
+ def populate(command)
7
+ command.which.to_s
8
+ end
9
+
10
+ def check
11
+ !storage.binary.empty?
12
+ end
13
+
14
+ # :reek:UtilityFunction
15
+ def message(value)
16
+ "Checking for #{File.basename(value)}"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ module Checky
3
+ module Validators
4
+ module FailHard
5
+ def after
6
+ raise Checky::ValidationError if storage.fail_hard && !storage.checky_final_result
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+ module Checky
3
+ module Validators
4
+ module Verbose
5
+ # :reek:ManualDispatch
6
+ def after
7
+ storage.to_h.keys.each do |key|
8
+ next if key.to_s.start_with?('checky_')
9
+
10
+ mod = Object.const_get("Checky::Validators::#{String(key).classify}")
11
+ next unless mod.respond_to?(:check)
12
+
13
+ display_message(mod, key)
14
+ end
15
+ end
16
+
17
+ def display_message(mod, key)
18
+ result = storage.checky_results[key]
19
+ stream = result ? $stdout : $stderr
20
+
21
+ stream.puts("#{build_message(mod, key)} #{result_message(key)}")
22
+ end
23
+
24
+ def build_message(mod, key)
25
+ value = storage.send(key)
26
+
27
+ # :nocov:
28
+ message = begin
29
+ mod.message(value)
30
+ rescue
31
+ "Checking #{key} (#{value})"
32
+ end
33
+ # :nocov:
34
+ format_message(message, key)
35
+ end
36
+
37
+ def format_message(message, key)
38
+ key_size = key.to_s.size
39
+
40
+ "#{message[0..(65 - key_size)].ljust(67 - key_size, '.')}[#{key.capitalize}]"
41
+ end
42
+
43
+ def result_message(key)
44
+ result = storage.checky_results[key]
45
+
46
+ if Checky.colorize
47
+ result ? 'OK'.green : 'FAIL'.red
48
+ else
49
+ result ? 'OK' : 'FAIL'
50
+ end
51
+ end
52
+
53
+ module_function :display_message, :build_message, :format_message, :result_message
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+ module Checky
3
+ module Validators
4
+ module Version
5
+ def populate(requirement_string)
6
+ requirement_string
7
+ end
8
+
9
+ def check
10
+ version = Gem::Version.new(version_string)
11
+ requirement = Gem::Requirement.new(storage.version)
12
+ requirement.satisfied_by?(version)
13
+ end
14
+
15
+ def version_string
16
+ command_path = storage.binary
17
+ command_output = Checky.run("#{command_path} --version").presence || Checky.run("#{command_path} -v").presence
18
+ command_output[/[0-9]+(?:\.[0-9]+)+/]
19
+ end
20
+
21
+ def message(value)
22
+ "Checking #{File.basename(storage.binary)} version against #{value}"
23
+ end
24
+
25
+ module_function :version_string
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Checky
3
+ VERSION = '0.1.0'
4
+ end
data/lib/core_ext.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+ module CoreExt
3
+ end
4
+
5
+ class Object
6
+ # :reek:ManualDispatch
7
+ def blank?
8
+ respond_to?(:empty?) ? empty? : !self
9
+ end
10
+
11
+ def present?
12
+ !blank?
13
+ end
14
+
15
+ def presence
16
+ self if present?
17
+ end
18
+ end
19
+
20
+ class String
21
+ def which
22
+ bin_path = ENV['PATH'].split(File::PATH_SEPARATOR).find do |path|
23
+ file = File.join(path, self)
24
+ File.exist?(file) && File.executable?(file)
25
+ end
26
+ bin_path ? File.join(bin_path, self) : nil
27
+ end
28
+
29
+ # :reek:TooManyStatements
30
+ def underscore
31
+ return self unless self =~ /[A-Z-]|::/
32
+ word = to_s.gsub('::', '/')
33
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
34
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
35
+ word.tr('-', '_').downcase
36
+ end
37
+
38
+ def classify
39
+ sub(/^[a-z\d]*/, &:capitalize).gsub(%r{(?:_|(\/))([a-z\d]*)}i) do
40
+ "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}"
41
+ end.gsub('/', '::')
42
+ end
43
+
44
+ def satisfies_requirement?(requirement_string)
45
+ version = Gem::Version.new(self)
46
+ requirement = Gem::Requirement.new(requirement_string)
47
+ requirement.satisfied_by?(version)
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: checky
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Rzegocki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Dependencies checker for CLI tools
14
+ email:
15
+ - igor@rzegocki.pl
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/checky.rb
21
+ - lib/checky/checker.rb
22
+ - lib/checky/exceptions.rb
23
+ - lib/checky/validators/all.rb
24
+ - lib/checky/validators/binary.rb
25
+ - lib/checky/validators/fail_hard.rb
26
+ - lib/checky/validators/verbose.rb
27
+ - lib/checky/validators/version.rb
28
+ - lib/checky/version.rb
29
+ - lib/core_ext.rb
30
+ homepage: https://github.com/ajgon/checky
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.5.1
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Dependencies checker for CLI tools
54
+ test_files: []