Sha256: e5cb40e62b26f71692c69db8da280b57d7050bbae4ba25417c3390fa0d3a162a

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

require 'ansi/code'

module Space
  module Shell
    class Command
      class << self
        def execute(command)
          `#{command}`
        end
      end

      attr_reader :context, :command, :result

      def initialize(context, command)
        @context = context
        @command = command
      end

      def run
        Watcher.suspend do
          App.logger.debug "RUNNING #{command} [#{context.path}]"
          notifying do
            @result = chain.call
          end
        end
      end

      private

        def notifying(&block)
          old = result
          yield.tap do |new|
            notify unless old == new
          end
        end

        def notify
          context.notify(command, result)
        end

        def chain
          runner = -> { clean(self.class.execute(command)) }
          filters.reverse.inject(runner) { |runner, filter| -> { filter.call(&runner) } }
        end

        def filters
          [method(:chdir), ::Bundler.method(:with_clean_env) ]
        end

        def chdir(&block)
          Dir.chdir(context.path) { |path| block.call }
        end

        def clean(string)
          strip_ansi(string.chomp)
        end

        def strip_ansi(string)
          string.gsub(ANSI::Code::PATTERN, '')
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
space-0.0.5 lib/space/shell/command.rb
space-0.0.4 lib/space/shell/command.rb