Module: My::Runner

Extended by:
Runner
Included in:
Runner
Defined in:
lib/my/runner.rb

Overview

The My::Runner module is called directly whenever the My binary is run. It handles basic tasks like adding and removing script aliases and delegating what gets run.

Instance Method Summary collapse

Instance Method Details

#add(args) ⇒ Object

Registers an alias to a script URL or file path, specified by args[0] as an alias name, and args[1] as a path.

My::Runner.add(['google','http://www.google.com'])


17
18
19
20
21
22
23
24
25
26
# File 'lib/my/runner.rb', line 17

def add(args)
  scripts = YAML::load(File.read(My::Config.scripts))
  unless scripts[args[0]]
    scripts[args[0]] = {:url => args[1]} 
    File.open(My::Config.scripts,'w'){|f|f.write(scripts.to_yaml)}
    puts "The #{args[0]} script has been registered"
  else
    puts "The #{args[0]} script is already registered. Please remove it first."
  end
end

#execute(command = nil, args = []) ⇒ Object

Runs an appropriate task based on the command argument passed. In order, execute will try to:

  • Run My::Runner#command

  • Run a registered script, whose alias is command

  • Run a script located at command

  • Run My::Runner#help

    My::Runner.execute('add',['google','http://www.google.com'])
      # Executes My::Runner.add(['google','http://www.google.com'])
    
    My::Runner.execute('google')
      # Executes a registered script named 'google'
    
    My::Runner.execute('http://www.google.com')
      # Executes a script located at 'http://www.google.com'
    
    My::Runner.execute('flibbitijibbet')
      # Executes My::Runner.help
    


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/my/runner.rb', line 97

def execute(command=nil,args=[])
  if command
    if respond_to?(command)
      begin
        self.send(command,args)
      rescue
        self.send(command)
      end
    elsif (scripts=YAML::load(File.read(My::Config.scripts))).keys.include?(command)
      My::Script.new(open(scripts[command][:url]).read).run
    else
      begin
        script = open(command)
      rescue
        help
      end
      My::Script.new(script.read).run if script
    end 
  else
    help
  end
end

#helpObject

Displays the My version information and basic usage information, along with a list of registered script aliases.

My::Runner.help

produces (for example):

My v.0.3.0
USAGE: my [NAME|URL]

Additional commands:
my add NAME URL   # Add NAME as an alias to a script at URL
my rm NAME        # Remove the NAME alias

Registered scripts:
sinatra
rails
gem


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/my/runner.rb', line 62

def help
  puts <<-END
My v.#{My::Config.version}
USAGE: my [NAME|URL]

Additional commands:
my add NAME URL   # Add NAME as an alias to a script at URL
my rm NAME        # Remove the NAME alias

Registered scripts:
  END

  YAML::load(File.read(My::Config.scripts)).each do |name,params|
    puts "#{name.ljust(30)}#{params[:description]}"
  end
end

#rm(args) ⇒ Object

Removes a registered script alias, specified by args[0] as the alias name.

My::Runner.rm(['google'])


32
33
34
35
36
37
38
39
40
41
# File 'lib/my/runner.rb', line 32

def rm(args)
  scripts = YAML::load(File.read(My::Config.scripts))
  if scripts[args[0]]
    scripts.delete(args[0])
    File.open(My::Config.scripts,'w'){|f|f.write(scripts.to_yaml)}
    puts "The #{args[0]} script has been removed"
  else
    puts "No #{args[0]} script has been registered"
  end
end