forked from github-linguist/linguist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-ids
executable file
·66 lines (55 loc) · 1.53 KB
/
update-ids
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env ruby
require 'bundler/setup'
require 'yaml'
require 'pry'
require 'digest'
def generate_language_id(language)
Digest::SHA256.hexdigest(language).to_i(16) % (2**30 - 1)
end
data = File.read("lib/linguist/languages.yml")
header = data.match(/\A(?:^#[^\n]*\n|^[ \t]*\n)+/m)[0]
languages = YAML.load(data)
# Do a dry-run to report which languages are missing IDs
if ARGV[0] == "--check"
missing = []
languages.each do |name, vals|
missing << name unless vals.has_key?('language_id')
end
list = missing.map {|n| " - #{n}"}.join("\n")
case missing.count
when 0
puts "No languages were found with missing IDs."
exit 0
when 1
puts "1 language was found with a missing ID:"
puts "\n#{list}\n\n"
puts "Run `#{$0}` to generate it."
else
puts "#{missing.count} languages were found with missing IDs:"
puts "\n#{list}\n\n"
puts "Run `#{$0}` to generate them."
end
exit 1
# Generate `language_id' fields for those entries missing them.
else
updated = []
languages.each do |name, vals|
unless vals.has_key?('language_id')
vals.merge!('language_id' => generate_language_id(name))
updated << name
end
end
list = updated.map {|n| " - #{n}"}.join("\n")
case updated.count
when 0
puts "No languages were found with missing IDs."
exit
when 1
puts "Updated 1 language:"
puts "\n#{list}\n\n"
else
puts "Updated #{updated.count} languages:"
puts "\n#{list}\n\n"
end
File.write("lib/linguist/languages.yml", header + YAML.dump(languages))
end