Module: GitStyleBinary::Helpers::NameResolver

Included in:
GitStyleBinary
Defined in:
lib/git-style-binary/helpers/name_resolver.rb

Instance Method Summary collapse

Instance Method Details

#basename(filename = zero) ⇒ Object Also known as: primary_name



5
6
7
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 5

def basename(filename=zero)
  File.basename(filename).match(/(.*?)(\-|$)/).captures.first
end

#binary_directory(filename = zero) ⇒ Object



19
20
21
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 19

def binary_directory(filename=zero)
  File.dirname(filename)
end

#binary_filename_for(name) ⇒ Object

load first from users binary directory. then load built-in commands if available



37
38
39
40
41
42
43
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 37

def binary_filename_for(name)
  user_file = File.join(binary_directory, "#{basename}-#{name}") 
  return user_file if File.exists?(user_file)
  built_in = File.join(built_in_commands_directory, "#{name}.rb") 
  return built_in if File.exists?(built_in)
  user_file
end

#built_in_command_namesObject



27
28
29
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 27

def built_in_command_names
  Dir[built_in_commands_directory + "/*.rb"].collect{|f| File.basename(f.sub(/\.rb$/,''))}
end

#built_in_commands_directoryObject



23
24
25
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 23

def built_in_commands_directory
  File.dirname(__FILE__) + "/../commands"
end

#current_command_name(filename = zero, argv = ARGV) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 45

def current_command_name(filename=zero,argv=ARGV)
  current = File.basename(zero)
  first_arg = ARGV[0]
  return first_arg if valid_subcommand?(first_arg)
  return basename if basename == current
  current.sub(/^#{basename}-/, '')
end

#full_current_command_name(filename = zero, argv = ARGV) ⇒ Object

returns the command name with the prefix if needed



54
55
56
57
58
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 54

def full_current_command_name(filename=zero,argv=ARGV)
  cur = current_command_name(filename, argv)
  subcmd = cur == basename(filename) ? false : true # is this a subcmd?
  "%s%s%s" % [basename(filename), subcmd ? "-" : "", subcmd ? current_command_name(filename, argv) : ""]
end

#list_subcommands(filename = zero) ⇒ Object



31
32
33
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 31

def list_subcommands(filename=zero)
  subcommand_names(filename).join(", ")
end

#pretty_known_subcommands(theme = :long) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 68

def pretty_known_subcommands(theme=:long)
  GitStyleBinary.known_commands.collect do |k,cmd| 
    next if k == basename 
    cmd.process_parser!
    ("%-s%s%-10s" % [basename, '-', k]).colorize(:light_blue) + ("%s       " % [theme == :long ? "\n" : ""]) + ("%s" % [cmd.short_desc]) + "\n"
  end.compact.sort
end

#subcommand_names(filename = zero) ⇒ Object

checks the bin directory for all files starting with basename and returns an array of strings specifying the subcommands



12
13
14
15
16
17
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 12

def subcommand_names(filename=zero)
  subfiles = Dir[File.join(binary_directory, basename + "-*")]
  cmds = subfiles.collect{|file| File.basename(file).sub(/^#{basename}-/, '')}.sort
  cmds += built_in_command_names
  cmds.uniq
end

#valid_subcommand?(name) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 60

def valid_subcommand?(name)
  subcommand_names.include?(name)
end

#zeroObject



64
65
66
# File 'lib/git-style-binary/helpers/name_resolver.rb', line 64

def zero
  $0
end