Sha256: 6bd0c27b768a3ad6e9db8e64cbccf1e8a1ccb58c4c6b82591f87f9846c380300

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

module Space
  class Action
    autoload :Parser, 'space/action/parser'

    class << self
      def run(app, line)
        new(app, *Parser.new(app.repos.names).parse(line)).run
      end
    end

    attr_reader :app, :repos, :command, :args

    def initialize(app, repos, command, *args)
      @app = app
      @repos = Repos.select(repos) if repos
      @command = normalize(command)
      @args = args
    end

    def run
      if respond_to?(command)
        send(command)
      elsif command
        execute(command)
      end
    end

    def reload
      app.bundle.reset
      run_scoped(true) do |repo|
        repo.reset
      end
    end

    def scope
      app.repos = repos
    end

    def unscope
      app.repos = nil
    end

    def local
      run_scoped do |repo|
        system "bundle config --global local.#{repo.name} #{repo.path}"
      end
    end

    def remote
      run_scoped do |repo|
        system "bundle config --delete local.#{repo.name}"
      end
    end

    def execute(cmd)
      run_scoped do |repo|
        puts
        repo.execute(cmd)
      end
    end

    def run_scoped(reloading = false)
      (repos || app.repos).each { |repo| yield repo }
      confirm && reload unless reloading
    end

    def system(cmd)
      puts cmd
      super
    end

    def confirm
      puts "\n--- hit any key to continue ---\n"
      STDIN.getc
    end

    def normalize(command)
      command = (command || '').strip
      command = 'unscope' if command == '-'
      command = 'scope'   if command.empty?
      command
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
space-0.0.3 lib/space/action.rb
space-0.0.2 lib/space/action.rb