Class: Reposh::Commands
- Inherits:
-
Object
- Object
- Reposh::Commands
- Defined in:
- lib/reposh.rb
Instance Method Summary collapse
- #add_ext(cmd, ext) ⇒ Object
- #dispatch(cmd, sys) ⇒ Object
-
#execute(cmd) ⇒ Object
require ‘shell’.
-
#initialize(binpath, default_cmd, pathext) ⇒ Commands
constructor
A new instance of Commands.
- #match?(pat, value) ⇒ Boolean
- #register(pattern, systems = nil, &task) ⇒ Object
- #register_builtin_commands ⇒ Object
- #register_custom_commands(commands) ⇒ Object
Constructor Details
#initialize(binpath, default_cmd, pathext) ⇒ Commands
Returns a new instance of Commands.
130 131 132 133 134 |
# File 'lib/reposh.rb', line 130 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
235 236 237 238 |
# File 'lib/reposh.rb', line 235 def add_ext(cmd, ext) exe, *args = cmd.split(' ') "#{exe}#{ext} #{args.join ' '}" end |
#dispatch(cmd, sys) ⇒ Object
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/reposh.rb', line 199 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
require ‘shell’
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/reposh.rb', line 222 def execute(cmd) stat = false ([""] + @pathext).each do |ext| command = add_ext(cmd, ext) puts command if @trace_mode #result = Shell.new.system(command) > $stdout result = system(command) return if result stat = $? end puts "reposh: failed to exec '#{cmd}': status #{stat.exitstatus}" end |
#match?(pat, value) ⇒ Boolean
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/reposh.rb', line 210 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
195 196 197 |
# File 'lib/reposh.rb', line 195 def register(pattern, systems = nil, &task) @commands.unshift [pattern, systems, task] end |
#register_builtin_commands ⇒ Object
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 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/reposh.rb', line 136 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 {|ignore| 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/reposh.rb', line 178 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 |