Class: Reposh::Commands

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

Instance Method Summary collapse

Constructor Details

#initialize(binpath, default_cmd, pathext) ⇒ Commands

Returns a new instance of Commands.



119
120
121
122
123
# File 'lib/reposh.rb', line 119

def initialize(binpath, default_cmd, pathext)
  @binpath, @default_cmd, @pathext = binpath, default_cmd, pathext
  @commands = []
  register_builtin_commands
end

Instance Method Details

#add_ext(cmd, ext) ⇒ Object



222
223
224
225
# File 'lib/reposh.rb', line 222

def add_ext(cmd, ext)
  exe, *args = cmd.split(' ')
  "#{exe}#{ext} #{args.join ' '}"
end

#dispatch(cmd, sys) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/reposh.rb', line 188

def dispatch(cmd, sys)
  @commands.each do |pattern, systems, task|
    next if systems && !systems.include?(sys)

    if (match = match?(pattern, cmd))
      return task.call(match)
    end
  end
  raise "must not happen"
end

#execute(cmd) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/reposh.rb', line 210

def execute(cmd)
  stat = false
  ([""] + @pathext).each do |ext|
    command = add_ext(cmd, ext)
    puts command if @trace_mode
    result = system(command)
    return if result
    stat = $?
  end
  puts "reposh: failed to exec '#{cmd}': status #{stat.exitstatus}"
end

#match?(pat, value) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
202
203
204
205
206
207
208
# File 'lib/reposh.rb', line 199

def match?(pat, value)
  case pat
  when Regexp
    pat.match(value)
  when nil
    value == nil
  else
    pat.strip == value
  end
end

#register(pattern, systems = nil, &task) ⇒ Object



184
185
186
# File 'lib/reposh.rb', line 184

def register(pattern, systems = nil, &task)
  @commands.unshift [pattern, systems, task]
end

#register_builtin_commandsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/reposh.rb', line 125

def register_builtin_commands
  # default command
  register(/.*/){|match|
    cmd = (match[0] == "") ? @default_cmd : match[0]
    execute "#{@binpath} #{cmd}"
  }

  # system commands
  register("%reload"){ 
    load __FILE__ 
  }
  register("%env"){
    require 'pp'
    pp ENV
  }
  register("%version"){
    puts VERSION 
  }
  register(/\A%ruby (.*)/){|match|
    puts "reposh: result is " + eval(match[1]).inspect
  }
  @trace_mode = false
  register("%trace"){
    @trace_mode = (not @trace_mode)
    puts "set trace_mode to #{@trace_mode}"
  }

  # exit commands
  exit_task = lambda{
    puts ""
    exit
  }
  register(nil,    &exit_task)
  register("exit", &exit_task)
  register("quit", &exit_task)

  # shell execution command
  register(/^:(.*)/){|match|
    execute match[1]
  }
end

#register_custom_commands(commands) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/reposh.rb', line 167

def register_custom_commands(commands)
  commands.each do |hash|
    if hash["for"] 
      systems = hash["for"].split(/,/).map{|s| s.strip}
    else
      systems = nil
    end
    register(Regexp.new(hash["pattern"]), systems){|match|
      cmd = hash["rule"].
              gsub(/\{system\}/, @binpath).
              gsub(/\{\$(\d+)\}/){ match[$1.to_i] }
      puts cmd
      execute cmd
    }
  end
end