class Agen::Builder

Public Class Methods

new(commands = [], rcfile = Runner::DEFAULT_RCFILE) click to toggle source
# File lib/agen/builder.rb, line 5
def initialize(commands = [], rcfile = Runner::DEFAULT_RCFILE)
  @commands = commands
  @rcfile = rcfile
end

Public Instance Methods

aliases() click to toggle source
# File lib/agen/builder.rb, line 10
def aliases
  aliases = []
  @commands.map do |cmd|
    aliaz = cmd.scan(/\b\w/).join

    # Is is possibly we could overwrite a command here still? Sure.
    # I will live with it for now.
    if command_already_exists?(aliaz) || aliases.include?(aliaz)
      # We could improve to look more like the original command, but again, works for now.
      aliaz += aliaz[-1]
    end

    candidate = construct_alias(aliaz, cmd)
    if alias_does_not_exist?(candidate)
      aliases << aliaz
      {alias: aliaz, full_alias: candidate, command: cmd}
    end
  end.compact
end
construct_alias(aliaz, cmd) click to toggle source
# File lib/agen/builder.rb, line 30
def construct_alias(aliaz, cmd)
  "alias #{aliaz}=\"#{cmd}\""
end

Private Instance Methods

alias_does_not_exist?(aliaz) click to toggle source
# File lib/agen/builder.rb, line 36
def alias_does_not_exist?(aliaz)
  File.readlines(@rcfile).none? { |line| line.include?(aliaz) }
end
command_already_exists?(cmd) click to toggle source

Shoutout to Stack Overflow: stackoverflow.com/a/5471032

# File lib/agen/builder.rb, line 41
def command_already_exists?(cmd)
  exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]
  ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return true if File.executable?(exe) && !File.directory?(exe)
    end
  end

  false
end