class Object

Constants

REQUIRED_PLUGINS

The plugins you are not allowed to unload or reload. To reload these, you need to restart the application.

Public Instance Methods

application_start() click to toggle source
# File lib/cli.rb, line 63
def application_start
  Dir["#{File.expand_path(File.dirname(__FILE__))}/plugins/*.rb"].sort.each do |file|
      puts "#{debug}Loading application plugin #{File.basename(file, ".rb")}"
    require file
    @plugin_counter += 1
    @plugin_array.push(File.basename(file))
    begin
      @plugin_help.push(plugin_help) unless @plugin_help.include? plugin_help
    rescue NameError => e
      # Do nothing if there's no such method
    end
    begin
      @plugin_commands.merge!(plugin_commands)
    rescue NameError => e
      # Do nothing if theunlessre's no such method
    end
    begin
      @plugin_associations.merge!(plugin_associations)
    rescue NameError => e
      # Do nothing if there's no such method
    end
  end
  begin
    load_history
  rescue NoMethodError => e
    puts "#{error}Unable to load history!"
  end
  puts "#{info}Attempting to load user plugins from #{File.expand_path(File.dirname("~/.ezycli/plugins"))}"
  Dir["#{File.expand_path("~/.ezycli/plugins")}/*.rb"].sort.each do |file|
      puts "#{debug}Loading user plugin #{File.basename(file, ".rb")}"
    require file
    @plugin_counter += 1
    @plugin_array.push(File.basename(file))
    begin
      @plugin_help.push(plugin_help) unless @plugin_help.include? plugin_help
    rescue NameError => e
      # Do nothing if there's no such method
    end
    begin
      @plugin_commands.merge!(plugin_commands)
    rescue NameError => e
      # Do nothing if there's no such method
    end
    begin
      @plugin_associations.merge!(plugin_associations)
    rescue NameError => e
      # Do nothing if there's no such method
    end
  end
  File.open("#{File.expand_path("~/.ezycli/plugs/php")}/hello-world.php", 'w') { |file| file.write("<?php \necho 'Hello, World!';\n") } unless File.file? "#{File.expand_path("~/.ezycli/plugs/php")}/hello-world.php"
  File.open("#{File.expand_path("~/.ezycli/plugins")}/test.rb", 'w') { |file| file.write("def test_function\nputs 'This is a test plugin that was automatically generated when you first ran ezyCLI'\nend\n\n############ THESE FUNCTIONS MUST EXIST ############\n\ndef plugin_commands\n# This is where you tell ezyCLI about the plugins it's to use\nreturn {\"testme\" => \"test_function\"}\nend\n\ndef plugin_associations\n# This is where you tell ezyCLI about which plugins belong to which file. I'll eventually fix that.\nreturn \"test\" => [\"testme\"]\nend") } unless File.file? "#{File.expand_path("~/.ezycli/plugins")}/test.rb"
  end
cli_cmd(cmd) click to toggle source
# File lib/plugins/ezycli.rb, line 24
def cli_cmd(cmd)
  begin
    case
    when (cmd.include?("help"))
      puts "help         - Provides this help text"
      puts "version      - Shows the version of ezyCLI"
      puts "exit         - Exits ezyCLI"
      puts @plugin_help
    when (cmd.include?("exit"))
      @newline = 0
      exit 0
    when (cmd.include?("version"))
      puts "ezyCLI version 0.2.3 DEV"
    when (cmd.empty?)
      # blank command...
    else
      c = cmd.split(" ")
      if c.count == 1
      begin
        send(@plugin_commands["#{cmd}"])
      rescue ArgumentError => e
        puts "#{warning}Wrong number of arguments given"
      rescue TypeError => e
        puts "#{warning}Unknown command: #{cmd}"
      end
      else
        args = Array.new
        begin
        c[1..-1].each do |arg|
          args.push(arg)
        end
        send(@plugin_commands["#{c[0]}"], *args)
      rescue ArgumentError => e
        puts "#{warning}Wrong number of arguments given"
      rescue TypeError => e
        puts "#{warning}Unknown command: #{cmd}"
      end
      end
    end
  rescue NoMethodError => e
    # Nothing doing
  end
end
cli_is_ready() click to toggle source

What? you thought that the CLI also wouldn't be a plugin?

# File lib/plugins/ezycli.rb, line 4
def cli_is_ready
  puts "#{info}Loaded #{@plugin_counter} plugin(s)!"
  puts "#{info}Welcome to the ezyCLI! _stuck? try 'help'"
end
cli_loop() click to toggle source
# File lib/plugins/ezycli.rb, line 9
def cli_loop
  loop {
      begin
        input = Readline.readline("ezyCLI> ", true)
        if input.instance_of? String
          cli_cmd(input)
        else
          exit 0
        end
        rescue SystemExit, Interrupt => e
          exit 0
      end
  }
end
colorize(text, color = "default", bgColor = "default") click to toggle source

COLORS!

# File lib/cli.rb, line 12
def colorize(text, color = "default", bgColor = "default")
    colors = {"default" => "38","black" => "30","red" => "31","green" => "32","brown" => "33", "blue" => "34", "purple" => "35",
     "cyan" => "36", "gray" => "37", "dark gray" => "1;30", "light red" => "1;31", "light green" => "1;32", "yellow" => "1;33",
      "light blue" => "1;34", "light purple" => "1;35", "light cyan" => "1;36", "white" => "1;37"}
    bgColors = {"default" => "0", "black" => "40", "red" => "41", "green" => "42", "brown" => "43", "blue" => "44",
     "purple" => "45", "cyan" => "46", "gray" => "47", "dark gray" => "100", "light red" => "101", "light green" => "102",
     "yellow" => "103", "light blue" => "104", "light purple" => "105", "light cyan" => "106", "white" => "107"}
    color_code = colors[color]
    bgColor_code = bgColors[bgColor]
    return "\033[#{bgColor_code};#{color_code}m#{text}\033[0m"
end
debug() click to toggle source
# File lib/cli.rb, line 34
def debug
  return colorize("[DEBUG] ", "blue")
end
error() click to toggle source
# File lib/cli.rb, line 31
def error
  return colorize("[ERROR] ", "red")
end
exit_handler() click to toggle source
# File lib/cli.rb, line 38
def exit_handler
  if !@newline
    puts ""
  end
  begin
    save_history
  rescue NoMethodError => e
    puts "#{error}Unable to save history!"
  end
  puts "#{debug}Exiting ezyCLI"
  puts "#{info}Have a great day!"
end
info() click to toggle source

Color mappings

# File lib/cli.rb, line 25
def info
  return colorize("[INFO] ", "green")
end
load_history() click to toggle source
# File lib/plugins/history.rb, line 8
def load_history
  if File.file?("#{File.expand_path("~/.ezycli")}/.ezycli_history")
    conf = YAML.load_file "#{File.expand_path("~/.ezycli")}/.ezycli_history"
    conf.each do |line|
      if line.present? and !line.include? "exit"
        Readline::HISTORY.push(line)
      end
    end
  else
    puts "#{info}Initialized empty history"
  end
end
load_plugin(plugin_name) click to toggle source

Yes, the ability to load plugins is itself a plugin. You may also not unload the plugin loader. This is a hardcoded dep for the application to run correctly

# File lib/plugins/load.rb, line 3
def load_plugin(plugin_name)
  if @plugin_associations["plugin_name"]
    puts "#{error}Cowardly refusing to load a plugin that's already loaded"
  else
    begin
      silence_warnings {load "#{File.expand_path("~/.ezycli/plugins")}/"+plugin_name+".rb"}
      begin
        @plugin_help.push(plugin_help) unless @plugin_help.include? plugin_help
      rescue NameError => e
        # Do nothing if there's no such method
      end
      begin
        @plugin_commands.merge!(plugin_commands)
      rescue NameError => e
        # Do nothing if there's no such method
      end
      begin
        @plugin_associations.merge!(plugin_associations)
      rescue NameError => e
        # Do nothing if there's no such method
      end
      puts "#{info}Loaded plugin #{plugin_name}"
    rescue Exception => e
      puts "#{warning}Failed to load plugin #{plugin_name}"
    end
  end
end
plug_exec(language, plugin_name) click to toggle source

WARNING: DO NOT MODIFY THIS FILE! I CANNOT BE MORE EXPLICIT! WARNING: THIS FILE LOADS THE RIGHT PLUG FOR THE LANGUAGE. *DO NOT TOUCH ME*!

# File lib/plugins/plugs.rb, line 3
def plug_exec(language, plugin_name)
  io = IO.popen("#{language} #{File.expand_path("~/.ezycli/plugs")}/php/#{plugin_name}.php")
  trap("INT") {
    puts "#{info}Ctrl-C received, cleaning up"
    Process.kill("INT", io.pid)
  }
  io.each do |line|
    if line.chomp.include? "Could not open input file"
      puts "#{error}Failed to run #{language} plug #{plugin_name}"
    else
      puts line.chomp
    end
  end
  trap('INT', 'DEFAULT')
end
plug_loader(language, plugin_name) click to toggle source
# File lib/plugins/plugs.rb, line 19
def plug_loader(language, plugin_name)
  case
  when (language == "php")
    puts "#{warning}Plugs are limited to output only at this time"
    plug_exec("php", plugin_name)
  else
    puts "#{error}Couldn't find suitable plug language for #{language}"
  end
end
plugin_associations() click to toggle source
# File lib/plugins/history.rb, line 28
def plugin_associations
  # see the above comment.
  return "history" => ["save", "reset"]
end
plugin_commands() click to toggle source

THESE FUNCTIONS MUST EXIST ############

# File lib/plugins/history.rb, line 23
def plugin_commands
  # I know, I know. I'll clean it up later. shhhhh
  return {"save" => "save_history", "reset" => "load_history"}
end
plugin_help() click to toggle source

THESE FUNCTIONS MUST EXIST ############

# File lib/plugins/load.rb, line 33
def plugin_help
  return ["load         - Loads a plugin"]
end
reload_plugin(plugin_name) click to toggle source
# File lib/plugins/reload.rb, line 1
def reload_plugin(plugin_name)
  unload_plugin plugin_name
  load_plugin plugin_name
end
save_history() click to toggle source
# File lib/plugins/history.rb, line 3
def save_history
  File.open("#{File.expand_path("~/.ezycli")}/.ezycli_history", 'w') { |f| f.write(YAML.dump(Readline::HISTORY.to_a))}
  puts "#{info}Wrote history to #{File.expand_path("~/.ezycli")}/.ezycli_history"
end
silence_warnings() { || ... } click to toggle source
# File lib/plugins/silence.rb, line 1
def silence_warnings
    original_verbosity = $VERBOSE
    $VERBOSE = nil
    result = yield
    $VERBOSE = original_verbosity
    return result
end
unload_plugin(plugin_name) click to toggle source

Yes, the ability to unload plugins is itself a plugin. You may also not unload the plugin unloader. This is a hardcoded dep for the application to run correctly

# File lib/plugins/unload.rb, line 3
def unload_plugin(plugin_name)
  if REQUIRED_PLUGINS.include? plugin_name
    puts "#{error}Cowardly refusing to unload a core plugin"
  else
    begin
      @plugin_associations["#{plugin_name}"].each do |ext|
        @plugin_commands.delete("#{ext}")
        @plugin_help.reject! {|e| e == ext}
      end
      @plugin_associations.delete("#{plugin_name}")
      puts "#{info}Unloaded plugin #{plugin_name}"
    rescue Exception => e
      puts "#{warning}Failed to unload plugin #{plugin_name}"
    end
  end
end
warning() click to toggle source
# File lib/cli.rb, line 28
def warning
  return colorize("[WARNING] ", "yellow")
end