lib/kapost/bootstrapper.rb in kapost-bootstrapper-0.4.0 vs lib/kapost/bootstrapper.rb in kapost-bootstrapper-0.5.0

- old
+ new

@@ -2,42 +2,72 @@ module Kapost # Application dependency installer for this Rails application. class Bootstrapper - class CommandFailedError < StandardError - attr_reader :command, :status + class CommandError < StandardError + attr_reader :command + + def initialize(command) + @command = command + end + end + + class CommandNotFoundError < CommandError + def message + "command `%s` not found" % command + end + end + + class CommandVersionMismatchError < CommandError + attr_reader :expected_version, :actual_version + + def initialize(command, expected_version, actual_version) + super(command) + @expected_version, @actual_version = expected_version, actual_version + end + + def message + "command `%s` has incorrect version. I expected %s, but you have %s" % [command, expected_version, actual_version] + end + end + + class CommandFailedError < CommandError + attr_reader :status def initialize(command, status) - @command, @status = command, status + super(command) + @status = status end def message - "Command `#{cmd}` failed with status #{status}" + "Command `#{command}` failed with status #{status}" end end def initialize(cli: Open3, printer: $stdout, platform: RUBY_PLATFORM, shell: Kernel, &block) @cli = cli @printer = printer @platform = platform @shell = shell - run(&block) if block_given? + instance_eval(&block) if block_given? end def default_check(command, version) installed?(command) and (!version or right_version?(command, version)) end def check(command, help = nil, version: nil) say(label(command, version)) do begin block_given? ? yield : default_check(command, version) - rescue CommandFailedError => ex + true + rescue CommandError => ex die help, exception: ex end - end or die(help) + end + true end def check_bundler check "bundler" do sh "gem install bundler --conservative &>/dev/null", verbose: false @@ -50,16 +80,20 @@ end end def installed?(command) _, status = cli.capture2e "bash -c 'type #{command}'" - status.success? + raise CommandNotFoundError, command unless status.success? + true end def right_version?(command, expected_version) version, status = cli.capture2e "#{command} --version" - status.success? && version.include?(expected_version) + unless status.success? && version.include?(expected_version) + raise CommandVersionMismatchError, command, expected_version, version + end + true end def say(message) if block_given? # If we're given a block print a label with a success indicator @@ -74,13 +108,13 @@ end def sh(*cmd) options = (Hash === cmd.last) ? cmd.pop : {} say(cmd.join(" ")) if options[:verbose] - result = system(*cmd) + result = cli.system(*cmd) status = $? - raise CommandFailedError, cmd.join(" "), status.exitstatus unless result + raise CommandFailedError.new(cmd.join(" "), status.exitstatus) unless result result end def die(help, exception: nil) say(exception.message) if exception @@ -99,10 +133,10 @@ def ubuntu(&block) run(&block) if os == :ubuntu end def run(&code) - instance_eval(&code) + instance_eval(&code) or raise CommandError, code end private attr_reader :cli, :printer, :platform, :shell