Class: BitClust::Runner

Inherits:
Object show all
Defined in:
lib/bitclust/runner.rb

Overview

Body of bin/bitclust.

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.


25
26
# File 'lib/bitclust/runner.rb', line 25

def initialize
end

Instance Method Details

#_run(argv) ⇒ Object


109
110
111
112
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/bitclust/runner.rb', line 109

def _run(argv)
  begin
    @parser.order!(argv)
    if argv.empty?
      $stderr.puts 'no sub-command given'
      $stderr.puts @parser.help
      exit 1
    end
    name = argv.shift
    cmd = @subcommands[name] or error "no such sub-command: #{name}"
  rescue OptionParser::ParseError => err
    $stderr.puts err.message
    $stderr.puts @parser.help
    exit 1
  end
  begin
    cmd.parse(argv)
  rescue OptionParser::ParseError => err
    $stderr.puts err.message
    $stderr.puts cmd.help
    exit 1
  end
  config = load_config()
  if config
    @version ||= config[:default_version]
    @prefix ||= "#{config[:database_prefix]}-#{@version}"
  end
  options = {
    :prefix => @prefix,
    :capi   => @capi
  }
  cmd.exec(argv, options)
rescue BitClust::WriterError => err
  raise if $DEBUG
  error err.message
end

#error(message) ⇒ Object


156
157
158
159
# File 'lib/bitclust/runner.rb', line 156

def error(message)
  $stderr.puts "#{File.basename($0, '.*')}: error: #{message}"
  exit 1
end

#load_configObject


146
147
148
149
150
151
152
153
154
# File 'lib/bitclust/runner.rb', line 146

def load_config
  home_directory = Pathname(ENV['HOME'])
  config_path = home_directory + ".bitclust/config"
  if config_path.exist?
    YAML.load_file(config_path)
  else
    nil
  end
end

#prepareObject


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bitclust/runner.rb', line 37

def prepare
  @prefix = nil
  @version = nil
  @capi = false
  @parser = OptionParser.new
  @parser.banner = <<-EndBanner
Usage: #{File.basename($0, '.*')} [global options] <subcommand> [options] [args]

Subcommands(for users):
init        Initialize database.
update      Update database.
setup       Initialize and update database with default options.
list        List libraries/classes/methods in database.
lookup      Lookup a library/class/method from database.
search      Search classes/methods from database.

Subcommands(for developers):
ancestors   Compare class/module's ancestors between Ruby and DB.
htmlfile    Generate a static HTML file for test.
query       Dispatch arbitrary query.
property    Handle database properties.
preproc     Preprocess source file.
extract     Extract method entries from source file.
classes     Display defined classes for all ruby.
methods     Display defined methods for all ruby.

Subcommands(for packagers):
statichtml  Generate static HTML files.
epub        Generate EPUB file.
chm         Generate static HTML files for CHM.

Global Options:
  EndBanner
  @parser.on('-d', '--database=PATH', 'Database prefix.') {|path|
    @prefix = path
  }
  @parser.on('-t', '--target=VERSION', 'Specify Ruby version.') {|v|
    @version = v
  }
  @parser.on('--capi', 'Process C API database.') {
    @capi = true
  }
  @parser.on('--version', 'Print version and quit.') {
    puts BitClust::VERSION
    exit 0
  }
  @parser.on('--help', 'Prints this message and quit.') {
    puts @parser.help
    exit 0
  }
  @subcommands = {
    'init'        => BitClust::Subcommands::InitCommand.new,
    'list'        => BitClust::Subcommands::ListCommand.new,
    'lookup'      => BitClust::Subcommands::LookupCommand.new,
    'search'      => BitClust::Searcher.new,
    'query'       => BitClust::Subcommands::QueryCommand.new,
    'update'      => BitClust::Subcommands::UpdateCommand.new,
    'property'    => BitClust::Subcommands::PropertyCommand.new,
    'setup'       => BitClust::Subcommands::SetupCommand.new,
    'server'      => BitClust::Subcommands::ServerCommand.new,
    'statichtml'  => BitClust::Subcommands::StatichtmlCommand.new,
    'htmlfile'    => BitClust::Subcommands::HtmlfileCommand.new,
    'chm'         => BitClust::Subcommands::ChmCommand.new,
    'epub'        => BitClust::Subcommands::EPUBCommand.new,
    'ancestors'   => BitClust::Subcommands::AncestorsCommand.new,
    'preproc'     => BitClust::Subcommands::PreprocCommand.new,
    'extract'     => BitClust::Subcommands::ExtractCommand.new,
    'classes'     => BitClust::Subcommands::ClassesCommand.new,
    'methods'     => BitClust::Subcommands::MethodsCommand.new,
  }
end

#run(argv) ⇒ Object


28
29
30
31
32
33
34
35
# File 'lib/bitclust/runner.rb', line 28

def run(argv)
  Signal.trap(:PIPE, 'IGNORE') rescue nil   # Win32 does not have SIGPIPE
  Signal.trap(:INT) { exit 3 }
  prepare
  _run(argv)
rescue Errno::EPIPE
  exit 0
end