Class: Scide::Overmind

Inherits:
Object
  • Object
show all
Defined in:
lib/scide/overmind.rb

Overview

Utility class to run scide in a script.

Instance Method Summary collapse

Constructor Details

#initializeOvermind

Awakens the overmind. Use at your own risk.



9
10
11
12
# File 'lib/scide/overmind.rb', line 9

def initialize
  @cli = Scide::Opts.new
  @config = Scide::Config.new
end

Instance Method Details

#broodObject

Parses command-line arguments and loads the configuration file. Any error will be run through Scide.fail.



16
17
18
19
20
21
22
# File 'lib/scide/overmind.rb', line 16

def brood
  @cli.parse! ARGV
  @config.file = @cli.funnel[:config] if @cli.funnel.key? :config
  @config.load!
  @initialized = true
  self
end

#dominateObject

Runs GNU Screen with the project given as argument. The --dry-run option will cause scide to print the resulting configuration instead of running it.

Errors

  • not_initialized - If #brood was not called.

  • unknown_project - If the given project is not found in the configuration file.

  • screen_not_found - If the GNU Screen binary is not found with which.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/scide/overmind.rb', line 34

def dominate

  Scide.fail :not_initialized, 'ERROR: call #brood to initialize.' unless @initialized

  project_key = ARGV.shift
  
  if project_key.blank?
    available_projects = @config.projects.keys.join(', ')
    Scide.fail :invalid_argument, "You must choose a project. Available projects: #{available_projects}."
  end

  unless @config.projects.key? project_key
    Scide.fail :unknown_project, "ERROR: there is no project '#{project_key}' in configuration #{@config.file}."
  end

  screen = Scide::Screen.new @config.projects[project_key], @config.screen
  screen.check_binary

  if @cli.funnel[:'dry-run']
    puts
    puts Paint['COMMAND', :bold]
    puts "   #{screen.to_command}"
    puts
    puts Paint['SCREEN CONFIGURATION', :bold]
    puts screen.to_s.gsub(/^/, '   ')
    puts
  else
    file = Tempfile.new 'scide'
    file.write screen.to_s
    file.rewind
    file.close
    system screen.to_command(file.path)
    file.unlink
  end
end