forked from rubygems/rubygems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_spdx_license_list.rb
51 lines (43 loc) · 1.05 KB
/
generate_spdx_license_list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
require 'json'
require 'net/http'
json = Net::HTTP.get('spdx.org', '/licenses/licenses.json')
licenses = JSON.parse(json)['licenses'].map do |licenseObject|
licenseObject['licenseId']
end
open 'lib/rubygems/util/licenses.rb', 'w' do |io|
io.write <<-RUBY
# frozen_string_literal: true
require 'rubygems/text'
class Gem::Licenses
extend Gem::Text
NONSTANDARD = 'Nonstandard'.freeze
# Software Package Data Exchange (SPDX) standard open-source software
# license identifiers
IDENTIFIERS = %w(
#{licenses.sort.join "\n "}
).freeze
REGEXP = %r{
\\A
(
\#{Regexp.union(IDENTIFIERS)}
\\+?
(\\s WITH \\s .+)?
| \#{NONSTANDARD}
)
\\Z
}ox.freeze
def self.match?(license)
!REGEXP.match(license).nil?
end
def self.suggestions(license)
by_distance = IDENTIFIERS.group_by do |identifier|
levenshtein_distance(identifier, license)
end
lowest = by_distance.keys.min
return unless lowest < license.size
by_distance[lowest]
end
end
RUBY
end