Class: Ankit::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/ankit/runtime.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Runtime

Returns a new instance of Runtime.



120
121
122
# File 'lib/ankit/runtime.rb', line 120

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



73
74
75
# File 'lib/ankit/runtime.rb', line 73

def config
  @config
end

#lineObject



75
# File 'lib/ankit/runtime.rb', line 75

def line; @line ||= HighLine.new; end

#stderrObject



78
# File 'lib/ankit/runtime.rb', line 78

def stderr; @stderr ||= STDERR; end

#stdinObject



76
# File 'lib/ankit/runtime.rb', line 76

def stdin; @stdin ||= STDIN; end

#stdoutObject



77
# File 'lib/ankit/runtime.rb', line 77

def stdout; @stdout ||= STDOUT; end

Class Method Details

.parse_options(args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/ankit/runtime.rb', line 90

def self.parse_options(args)
  options = {}
  OptionParser.new do |spec|
    spec.on("-c", "--config FILE", "Specifies config file") { |file| options[:config] = file }
  end.parse(args)

  options[:noconf]   = options[:config].nil?
  options[:config] ||= Config::DEFAULT_PATH
  options
end

.run(args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ankit/runtime.rb', line 107

def self.run(args)
  splitted = self.split_subcommand(args)
  r = self.setup(splitted[:global])
  if splitted[:subcommand].empty?
    r.dispatch([ChallengeCommand.command_name])
  else
    r.dispatch(splitted[:subcommand])
  end
rescue ExpectedFatalError
  print("Error:", $!.message, "\n")
  exit(1)
end

.setup(args) ⇒ Object



101
102
103
104
105
# File 'lib/ankit/runtime.rb', line 101

def self.setup(args)
  options = self.parse_options(args)
  Config.prepare_default if options[:noconf] and not File.exist?(Config::DEFAULT_PATH)
  r = self.new(Config.open(options[:config]))
end

.split_subcommand(args) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/ankit/runtime.rb', line 80

def self.split_subcommand(args)
  names = Command.by_name.keys
  i = args.find_index { |a| names.include?(a) }
  unless i.nil?
    { global: args[0 ... i], subcommand: args[i .. -1] }
  else
    { global: [], subcommand: [] }
  end
end

Instance Method Details

#clear_screenObject



147
148
149
150
151
152
153
# File 'lib/ankit/runtime.rb', line 147

def clear_screen
  w = HighLine::SystemExtensions.terminal_size[1]
  stdout.print("\033[#{w}D")
  stdout.print("\033[2J")
  h = HighLine::SystemExtensions.terminal_size[0]
  stdout.print("\033[#{h}A")
end

#dispatch(args) ⇒ Object



129
130
131
132
133
# File 'lib/ankit/runtime.rb', line 129

def dispatch(args)
  command = make_command(args)
  command.execute()
  command
end

#dispatch_then(args) ⇒ Object

To encourage one-liner



136
137
138
139
# File 'lib/ankit/runtime.rb', line 136

def dispatch_then(args)
  dispatch(args)
  self
end

#make_command(args) ⇒ Object



124
125
126
127
# File 'lib/ankit/runtime.rb', line 124

def make_command(args)
  name = args.shift
  Command.by_name[name].new(self, args)
end

#supress_ioObject



141
142
143
144
145
# File 'lib/ankit/runtime.rb', line 141

def supress_io
  saved = [@stdin, @stdout, @stderr]
  ["stdin=", "stdout=", "stderr="].each { |m| self.send(m, StringIO.new) }
  saved
end

#unsupress_io(saved) ⇒ Object



155
156
157
# File 'lib/ankit/runtime.rb', line 155

def unsupress_io(saved)
  @stdin, @stdout, @stderr =  saved
end

#with_supressing_io(&block) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/ankit/runtime.rb', line 159

def with_supressing_io(&block)
  saved = supress_io
  begin
    block.call
  ensure
    unsupress_io(saved)
  end
end