Module: Gem::License
- Included in:
- Commands::LicenseCommand
- Defined in:
- lib/gem_license.rb
Overview
Container for license fetching utilities
Instance Method Summary collapse
- #download(spdx_id, opt) ⇒ Object
- #fetch_license_list ⇒ Object
- #fetch_license_text(url) ⇒ Object
- #format_license(text) ⇒ Object
- #match_license_id(license_id, id_list) ⇒ Object
- #write_license(license_obj, path = nil, format = false) ⇒ Object
Instance Method Details
#download(spdx_id, opt) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/gem_license.rb', line 80 def download(spdx_id, opt) license_list = fetch_license_list id_list = license_list.collect { |l_obj| l_obj['licenseId'] } id = match_license_id(spdx_id, id_list) license_obj = license_list.find { |l_obj| l_obj['licenseId'] == id } path = opt[:output_path] path = 'LICENSE' + (opt[:markdown] ? '.md' : '') if path.nil? && !opt[:stdout] write_license license_obj, path, opt[:format] say 'Remember to read your license for any fields you might need to fill in.' end |
#fetch_license_list ⇒ Object
7 8 9 10 11 12 |
# File 'lib/gem_license.rb', line 7 def fetch_license_list list_uri = URI('https://spdx.org/licenses/licenses.json') JSON.parse(list_uri.read)['licenses'] rescue OpenURI::HTTPError => e raise Gem::CommandLineError, "Unable to get license list. [#{e.}]" end |
#fetch_license_text(url) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/gem_license.rb', line 14 def fetch_license_text(url) license_uri = URI(url) license_obj = JSON.parse(license_uri.read) license_obj['licenseText'] rescue OpenURI::HTTPError => e raise Gem::CommandLineError, "Unable to get license text. [#{e.}]" end |
#format_license(text) ⇒ Object
31 32 33 34 35 36 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 |
# File 'lib/gem_license.rb', line 31 def format_license(text) data_map = { /<\s*(year|yyyy|dates)\s*>/i => Time.now.year, /<program>/i => File.basename(Dir.pwd) } begin name = `git config --get user.name`.chomp rescue Errno::ENOENT name = nil say 'Can\'t find git for user.name' end if name if name.empty? say '[WARN] No user.name git config value' else = `git config --get user.name`.chomp data_map[ Regexp.union( /<\s*author\s*>/i, /<\s*name of author\s*>/i, /<\s*owner\s*>/i, /<copyright holders?>/i ) ] = end end data_map.each do |pattern, value| text.gsub!(pattern, value.to_s) end text end |
#match_license_id(license_id, id_list) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/gem_license.rb', line 22 def match_license_id(license_id, id_list) perfect_match = id_list.find { |id| id.downcase == license_id.downcase } return perfect_match if perfect_match spell_checker = DidYouMean::SpellChecker.new(dictionary: id_list) id = spell_checker.correct(license_id).first abort 'Unable to find a matching SPDX identifier.' + (id ? " Did you mean `#{id}'?" : '') end |
#write_license(license_obj, path = nil, format = false) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gem_license.rb', line 67 def write_license(license_obj, path = nil, format = false) text = fetch_license_text(license_obj['detailsUrl']) text = format_license(text) if format if path File.open(path, 'w+') do |f| f.write text end else $DEFAULT_OUTPUT.puts text end end |