Skip to content

Commit

Permalink
CLI support and JSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
copyhacker committed May 21, 2014
1 parent 0894cb9 commit b53555c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ Comb your gems for licensing info and collate it nicely for the lawyers.
# TODO

- [x] Command line or rake task
- [ ] Run and parse `bundle list`
- [ ] Hit rubygems and/or GitHub for info on each gem
- [ ] Pull all that into a hash: name, version, owner, license type, link to license text
- [x] Run and parse `bundle list`
- [x] Hit rubygems and/or GitHub for info on each gem
- [x] Pull all that into a hash: name, version, owner, license type
- [x] Link to license text
- [ ] Output options
- [ ] JSON
- [x] JSON
- [ ] HTML
- [ ] CSV
- [x] CSV
13 changes: 12 additions & 1 deletion lib/diligent/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ def legalese
end

desc 'list', 'List of gem information. JSON by default, or pass format option.'
option :format
option :outfile
def list
puts Diligent::List.load
list = Diligent::List.new

case (options[:format] || 'json')
when 'csv'
list.as_csv(options[:outfile] || './diligent.csv')
when 'json'
puts list.as_json(options[:outfile] || nil)
else
raise 'Only CSV and JSON are currently supported.'
end
end
end
end
26 changes: 17 additions & 9 deletions lib/diligent/list.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'csv'
require 'json'

module Diligent
class List
Expand All @@ -14,13 +15,6 @@ def load
def initialize
# TODO Optionally point at a different path?
@specs = Bundler.load.specs



binding.pry



end

def as_hash
Expand All @@ -38,6 +32,13 @@ def as_hash
end
end

def as_json(filename = nil)
json = as_hash.to_json
# File.open(filename, 'w') { |f| f.write(json) } if filename
# json
write_to_file filename, json
end

def as_csv(filename = nil)
as_array = as_hash.inject([]) do |arr, gem_info|
row = []
Expand All @@ -53,9 +54,16 @@ def as_csv(filename = nil)
as_array.each { |row| csv << row }
end

File.open(filename, 'w') { |f| f.write(csv) } if filename
# File.open(filename, 'w') { |f| f.write(csv) } if filename
write_to_file filename, csv
# csv
end

protected

csv
def write_to_file(filename, content)
File.open(filename, 'w') { |f| f.write(content) } if filename
content
end
end
end
2 changes: 1 addition & 1 deletion spec/diligent/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@

# FIXME: Brittle
def check_csv_content(csv_rows)
expect(csv_rows.first).to eq(%w{ Gem Version Author Summary Description Homepage })
expect(csv_rows.first).to eq(%w{ Gem Version Author Summary Description License Homepage })
expect(csv_rows[1].first).to eq ('awesome_print')
end

0 comments on commit b53555c

Please sign in to comment.