Class: Indexer::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/indexer/command.rb

Overview

Command line interface.

Defined Under Namespace

Modules: CleanBinding Classes: Form

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



17
18
19
20
21
# File 'lib/indexer/command.rb', line 17

def initialize
  @force  = false
  @stdout = false
  @static = false
end

Class Method Details

.run(argv = ARGV) ⇒ Object

Shortcut to new.run(argv).



10
11
12
# File 'lib/indexer/command.rb', line 10

def self.run(argv=ARGV)
  new.run(argv)
end

Instance Method Details

#adding(*sources) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/indexer/command.rb', line 80

def adding(*sources)
  raise Error.exception("no sources given") if sources.empty?
   = Metadata.open
   = Metadata.lock((.sources & sources), :force=>true)
  if @stdout
    puts .to_yaml
  else
    .save!
  end
end

#create_gemspec(file = nil) ⇒ Object (private)

Create a .gemspec file for use with indexer. Or a static one if --static option is used.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/indexer/command.rb', line 205

def create_gemspec(file=nil)
  if file
    if File.extname(file) != '.gemspec'
      warn "gemspec file without .gemspec extension"
    end
  else
    # TODO: look for pre-existent gemspec, but to do that right we should get
    #       the name from the .index file if it eixts.
    file = Dir['{,*,pkg/*}.gemspec'].first || '.gemspec'
  end

  #lib_file = File.join(DIR, "v#{which}", "gemspec.rb")

  if file && File.exist?(file) && !@force && !@stdout
    raise Error.exception("`#{file}' already exists, use -f/--force to overwrite.")
  end

  text = GemspecExporter.source_code + "\nIndexer::GemspecExporter.gemspec"

  if @static
    spec = eval(text, CleanBinding.new, file)
    text = spec.to_yaml
  end

  if @stdout
    puts text
  else
    File.open(file, 'w') do |f|
      f << text
    end
  end
end

#create_ruby(outfile = nil) ⇒ Object (private)



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/indexer/command.rb', line 133

def create_ruby(outfile=nil)
  require 'erb'

  outfile = "Indexfile" unless outfile

  if File.exist?(outfile) && !(@stdout or @force)
    raise Error.exception("#{outfile} file already exists", IOError) 
  end

  template_file = File.join(DATADIR, 'ruby.txt')

  if Metadata.exists?
     = Metadata.open
  else
     = Metadata.new
  end

  # this is a little weak, but...
  if gemspec = Dir['{,pkg/}*.gemspec'].first
    .import_gemspec(gemspec)
  end

  template = ERB.new(File.read(template_file))
  result   = template.result(Form.new().get_binding)

  if @stdout
    puts result
  else
    File.open(outfile, 'w') do |f|
      f << result
    end
  end
end

#create_yaml(outfile) ⇒ Object (private)



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
193
194
195
196
197
198
199
200
# File 'lib/indexer/command.rb', line 168

def create_yaml(outfile)
  require 'erb'

  outfile = "Index.yml" unless outfile

  if File.exist?(outfile) && !(@stdout or @force)
    raise Error.exception("#{outfile} file already exists", IOError) 
  end

  template_file = File.join(DATADIR, 'yaml.txt')

  if Metadata.exists?
     = Metadata.open
  else
     = Metadata.new
  end

  # this is a little weak, but...
  if gemspec = Dir['{,pkg/}*.gemspec'].first
    .import_gemspec(gemspec)
  end

  template = ERB.new(File.read(template_file))
  result   = template.result(Form.new().get_binding)

  if @stdout
    puts result
  else
    File.open(outfile, 'w') do |f|
      f << result
    end
  end
end

#generate(type, outfile = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/indexer/command.rb', line 92

def generate(type, outfile=nil)
  case type.downcase
  when 'gemspec'
    create_gemspec(outfile)
  when 'ruby', 'index.rb', 'indexfile'
    create_ruby(outfile)
  when 'yaml', 'index.yml', 'index.yaml'
    create_yaml(outfile)
  else
    raise Error.exception("unknown file type")
  end
end

#helpObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/indexer/command.rb', line 112

def help
  puts <<-END
    index [command-option] [options...] [arguments...]

    (none) [fields...]              update index and provide information from index
    -u --using <sources...>         create index using given information sources
    -a --adding <sources...>        update index appending additional information sources
    -r --remove <sources...>        update index removing given information sources
    -g --generate <type> [fname]    generate a file (gemspec, indexfile, metadata)
    -w --webserver                  (experimental) edit .index file via web interface
    -h --help                       show this help message

    -o --stdout                     output to console instead of saving to file
    -f --force                      force protected file overwrite if file already exists or is up to date
    -s --static                     keep index as is or generate static format if generator supports it
  END
end

#no_cmd!(cmd) ⇒ Object (private)



239
240
241
# File 'lib/indexer/command.rb', line 239

def no_cmd!(cmd)
  raise Error.exception("more than one command flag") if cmd
end

#run(argv = ARGV) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/indexer/command.rb', line 26

def run(argv=ARGV)
  cmd = nil
  args = cli(argv,
    '-u --using'     => lambda{ no_cmd!(cmd); cmd = :using },
    '-a --adding'    => lambda{ no_cmd!(cmd); cmd = :adding },
    '-g --generate'  => lambda{ no_cmd!(cmd); cmd = :generate },
    '-w --webserver' => lambda{ no_cmd!(cmd); cmd = :webserver },
    '-h --help'      => lambda{ no_cmd!(cmd); cmd = :help },
    '-d --debug'     => lambda{ $DEBUG = true; $VERBOSE = true },
    '-f --force'     => lambda{ @force = true },
    '-o --stdout'    => lambda{ @stdout = true },
    '-s --static'    => lambda{ @static = true }
  )
  send(cmd || :show, *args)
rescue => error
  raise error if $DEBUG
  $stderr.puts "#{File.basename($0)} error: #{error}"
  exit -1
end

#show(*fields) ⇒ Object

Show returns information from the .index file. Before doing so it always ensures the .index file is up to date. To suppress this update use the -S/--static option.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/indexer/command.rb', line 51

def show(*fields)
  if @static
    if Metadata.exists?
       = Metadata.open
      puts .about(*fields)
    else
      raise Error.exception(".index file not found", IOError)
    end
  else
    Metadata.lock!(:force=>@force)
    unless fields.empty?
       = Metadata.open
      puts .about(*fields)
    end
  end
end

#using(*sources) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/indexer/command.rb', line 69

def using(*sources)
  raise Error.exception("no sources given") if sources.empty?
   = Metadata.lock(sources, :force=>true)
  if @stdout
    puts .to_yaml
  else
    .save!
  end
end

#webserverObject



106
107
108
109
# File 'lib/indexer/command.rb', line 106

def webserver
  require 'indexer/webui'
  Indexer::WebUI::Server.start(ARGV)
end