Module: LinguistReader

Defined in:
lib/linguist_reader.rb

Overview

Regenerates lib/colors.json from GitHub Linguist's languages.yml. Run with: ruby lib/linguist_reader.rb (or rake colors)

Constant Summary collapse

SOURCE_URL =
'https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml'
OUTPUT_FILE =
File.join(__dir__, 'colors.json')

Class Method Summary collapse

Class Method Details

.build(yaml_content) ⇒ Object



21
22
23
24
25
26
# File 'lib/linguist_reader.rb', line 21

def build(yaml_content)
  YAML.safe_load(yaml_content).each_with_object({}) do |(name, props), colors|
    color = props['color']
    colors[name] = color unless color.nil?
  end.sort.to_h
end

.fetchObject



15
16
17
18
19
# File 'lib/linguist_reader.rb', line 15

def fetch
  URI.parse(SOURCE_URL).open(&:read)
rescue OpenURI::HTTPError, SocketError => e
  abort "Failed to download Linguist languages.yml: #{e.message}"
end

.runObject



32
33
34
35
36
# File 'lib/linguist_reader.rb', line 32

def run
  colors = build(fetch)
  write(colors)
  puts "Wrote #{colors.size} language colors to #{OUTPUT_FILE}"
end

.write(colors) ⇒ Object



28
29
30
# File 'lib/linguist_reader.rb', line 28

def write(colors)
  File.write(OUTPUT_FILE, "#{JSON.pretty_generate(colors)}\n")
end