Permalink
Browse files

Added support for single files

  • Loading branch information...
1 parent d3b3835 commit 0f6923ad1568af71a0496fe9fa8c267c28822e1a @solso committed Jun 19, 2012
Showing with 41 additions and 11 deletions.
  1. +40 −10 bin/source2swagger
  2. +1 −1 lib/swagger_reader.rb
View
@@ -21,16 +21,21 @@ opt_input = nil
opt_output = nil
opt_comment = nil
opt_extension = nil
+opt_filename = nil
parser = OptionParser.new do |parser|
- parser.on('-i','--input PATH', 'Directory of the input source code') do |value|
+ parser.on('-i','--input PATH', 'Directory of the annotated source code') do |value|
opt_input = value
end
parser.on('-e','--ext ("rb"|"c"|"js"|"py")', 'File extension of the source code') do |value|
opt_extension = value
end
+ parser.on('-f','--file FILENAME', 'Overwrites options: -i and -e. Takes a single annotated source code file') do |value|
+ opt_filename = value
+ end
+
parser.on('-c','--comment ("##~"|"//~")','Comment tag used to write docs') do |value|
opt_comment = value
end
@@ -39,14 +44,23 @@ parser = OptionParser.new do |parser|
opt_output = value
end
- parser.parse!
+ begin
+ parser.parse!
+ rescue Exception => e
+ puts e.message
+ puts parser
+ abort
+ end
end
-unless opt_extension and opt_input and opt_comment
+
+puts opt_filename
+
+unless ((opt_extension and opt_input and opt_comment) || (opt_filename and opt_comment))
puts parser
abort
end
-
+
def save(results, output_path)
results.each do |k,v|
puts " Saving API #{k} to #{output_path}/#{k}.json"
@@ -63,22 +77,38 @@ def print(results)
end
end
-def run(input, extension, comment, output)
+def run(input, extension, comment, output, filename)
reader = SwaggerReader.new
$_swaggerhash = Hash.new
- code = reader.analyze_all_files(input,extension,comment)
- results = reader.process_code(code) unless code.nil? || code.empty?
-
- puts "Swagger API in #{ARGV[0]}/**/*.#{ARGV[1]}: #{results.size} API\n"
+ if filename.nil?
+ code = reader.analyze_all_files(input,extension,comment)
+ else
+ code = reader.analyze_file(filename, comment)
+ end
+
+ begin
+ results = reader.process_code(code) unless code.nil? || code.empty?
+ rescue SwaggerReaderException => e
+ puts "There is a problem with your swagger annotation. See below...\n\n"
+ puts e.inspect
+ end
+
+ if filename.nil?
+ puts "Swagger API in #{input}/**/*.#{extension}: #{results.size} API\n"
+ else
+ puts "Swagger API in #{filename}: #{results.size} API\n"
+ end
+
if output.nil?
print(results)
else
save(results,output)
end
+
puts "Done!"
end
-run(opt_input, opt_extension, opt_comment, opt_output)
+run(opt_input, opt_extension, opt_comment, opt_output, opt_filename)
View
@@ -47,7 +47,7 @@ def sort_for_vars_declaration(code)
cont = 0
code[:code].each do |code_line|
code_line.strip!
- if code_line[0]=='@'
+ if code_line[0]=="@"[0]
tmp_vars[:code] << code_line.gsub('@',' ')
tmp_vars[:line_number] << code[:line_number][cont]
tmp_vars[:file] << code[:file][cont]

0 comments on commit 0f6923a

Please sign in to comment.