18F/identity-idp

View on GitHub
scripts/yaml_characters

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'pathname'
require_relative '../config/environment'

excluded_locales = []
excluded_gem_paths = []
OptionParser.new do |opts|
  opts.banner = <<~TXT
    Usage
    =======================

      #{$PROGRAM_NAME} [OPTIONS]

      Prints all unique characters from Rails loaded I18n data, which can be used in combination
      with font subsetting to optimize font sizes.

  TXT

  opts.on('--exclude-locale=LOCALE', 'Disregard characters from the given locale') do |locale|
    excluded_locales << locale.to_sym
  end

  opts.on('--exclude-gem-path=GEM', 'Disregard characters loaded by the given gem') do |gem|
    excluded_gem_paths << Gem.loaded_specs[gem].full_gem_path
  end

  opts.on('-h', '--help', 'Prints this help') do
    STDOUT.puts opts
    exit
  end
end.parse!(ARGV)

def sanitize(string)
  string.gsub(/<.+?>/, '').gsub(/%{.+?}/, '').gsub(/[\n\r\t]/, '')
end

def hash_values(hash)
  hash.values.flat_map do |value|
    case value
    when Hash
      hash_values(value)
    when Array
      value
    else
      [value]
    end
  end
end

I18n.load_path.reject! do |load_path|
  excluded_gem_paths.any? { |gem_path| load_path.start_with?(gem_path) }
end

I18n.backend.eager_load!

data = I18n.backend.translations.slice(*I18n.available_locales - excluded_locales)
strings = hash_values(data)
joined_string = strings.join('')
sanitized_string = sanitize(joined_string)
characters = sanitized_string.chars.to_a.uniq.sort.join('')

STDOUT.puts characters