Class: Mexico::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/mexico/cmd.rb

Overview

This class serves as the interface for the MExiCo executable. All subcommands of the MExiCo executable are bound to class methods of the Cmd class.

Constant Summary collapse

SUBCOMMANDS =

Array of names of currently supported subcommands.

%w(help info init status)
SUBCOMMAND_DESCRIPTIONS =

Hash of descriptions for the currently supported subcommands.

{}
DEFAULT_BANNER =

the default banner to be displayed on the console when asked for how to use the executable.

"Usage: mexico [subcommand] [options]"
OPTION_PARSERS =

Hash of option parser objects for the currently supported subcommand.s

{}
@@options =
{}

Class Method Summary collapse

Class Method Details

.debug(message) ⇒ void

This method returns an undefined value.

Helper method that writes debugging information (typically in critical and error cases) to the console if the debug parameter was given on the command line.

Parameters:

  • message (String)

    A debugging message to be sent to stdout.


90
91
92
# File 'lib/mexico/cmd.rb', line 90

def self.debug(message)
  puts "DEBUG:    #{message}".magenta if @@options[:debug]
end

.help(options) ⇒ Object

This method handles the help subcommand. It does nothing but output usage information to the console and quit.

Parameters:

  • options (Hash)

    a hash of raw options from the command line.

Returns:

  • nil


150
151
152
153
154
# File 'lib/mexico/cmd.rb', line 150

def self.help(options)
  puts "Help".magenta
  puts OPTION_PARSERS['help']
  exit(0)
end

.humanize(number) ⇒ String

helper method that creates a human-readable file size representation (suffixed with “KB”, “MB”, “GB”, etc.).

Parameters:

  • number (Number)

    the number to be formatted

Returns:

  • (String)

    a human-readable file size representation of that number


98
99
100
101
102
103
104
105
106
107
# File 'lib/mexico/cmd.rb', line 98

def self.humanize(number)
  prefs = %w(T G M K)
  display_number = number.to_f
  unit = ""
  while(display_number>1000 && !prefs.empty?)
    display_number = display_number / 1024.0
    unit = prefs.pop()
  end
  return "%.1f %sB" % [display_number, unit]
end

.info(options) ⇒ Object

This method handles the info subcommand. It prints out basic information about an existing corpus.

Parameters:

  • options (Hash)

    a hash of raw options from the command line.

Returns:

  • nil


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mexico/cmd.rb', line 160

def self.info(options)
  # check if current folder is corpus folder
  current_folder = File.dirname(__FILE__)
  if options.has_key?(:path)
    current_folder = options[:path]
  end
  corpus_manifest = File.join(current_folder,"Corpus.xml")
  if File.exists?(corpus_manifest)
    self.verbose "Current Directory '#{current_folder}' is a corpus folder, opening."
    # open corpus manifest, analyse
    corpus = Mexico::FileSystem::Corpus.open(current_folder)
    puts ""
    puts "-"*72
    puts "%15s  :  %-50s" % ["Identifier", corpus.identifier]
    puts "%15s  :  %-50s" % ["Name", corpus.name]
    displ_description = ""
    displ_description = corpus.description.split("\n").first unless corpus.description.blank?
    displ_description = displ_description.slice(0,47)+" ..." unless displ_description.length<47
    puts "%15s  :  %-50s" % ["Description", displ_description]
    puts "-"*72
    puts "%15s  :  %-50s" % ["Designs", corpus.designs.size]
    puts "%15s  :  %-50s" % ["Trials", corpus.trials.size]
    puts "%15s  :  %-50s" % ["Resources", corpus.resources.size]
    puts "%15s  :  %-50s" % ["Local Files", corpus.resources.collect{|i| i.local_files}.flatten.size]
    puts "%15s  :  %-50s" % ["Used Disk Space", humanize(corpus.complete_file_size)]
    puts "-"*72
    puts ""
  else
    puts "Error: No corpus manifest folder found in '#{current_folder}'.".red
    puts "Seems like no corpus has been initialized here.".red
    exit(1)
  end
end

.init(options) ⇒ Object

This method handles the init subcommand. It can create the necessary structures and files for a new corpus folder.

Parameters:

  • options (Hash)

    a hash of raw options from the command line.

Returns:

  • nil


198
199
200
201
# File 'lib/mexico/cmd.rb', line 198

def self.init(options)
  puts "The 'init' subcommand is not yet implemented."
  exit(0)
end

.run!(arx) ⇒ void

This method returns an undefined value.

The core method that is called when the executable runs.

Parameters:

  • arx (Collection)

    a list of the command line parameters.


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mexico/cmd.rb', line 113

def self.run!(arx)
  argsize = ARGV.size
  if argsize>0
    subcommand = ARGV.shift

    if SUBCOMMANDS.include?(subcommand)
      # ok, command is known
      if argsize>1
        OPTION_PARSERS[subcommand].parse!
      else
        # no more commands.
      end
      self.verbose "Verbose mode. MExiCo displays more info on what it does in cyan."
      self.debug   "Debug mode. MExiCo displays lots of internal information in magenta."
      self.verbose "MExiCo #{subcommand}"
      Mexico::Cmd.send(subcommand, @@options)
    else
      # break: command not known
      OPTION_PARSERS['help'].parse!
      self.help(@@options)
    end
    self.debug("Detected arguments:  %s" % @@options)
    unless ARGV.empty?
      self.debug("Leftover arguments:  %s" % ARGV)
    end
    self.debug("Subcommand:          %s" % subcommand)
  else
    puts option_parser
    exit(1)
  end
  exit(0)
end

.status(options) ⇒ Object

This method handles the status subcommand. It outputs various pieces of information about the corpus, its linkage to a remote repository, untracked files, etc.


205
206
207
208
# File 'lib/mexico/cmd.rb', line 205

def self.status(options)
  puts "The 'status' subcommand is not yet implemented."
  exit(0)
end

.verbose(message) ⇒ void

This method returns an undefined value.

Helper method that writes an informative verbose message to the console if the verbose parameter was given on the command line.

Parameters:

  • message (String)

    A verbose message to be sent to stdout.


81
82
83
# File 'lib/mexico/cmd.rb', line 81

def self.verbose(message)
  puts "VERBOSE:  #{message}".cyan if @@options[:verbose]
end