File: reformat_yaml.rake

package info (click to toggle)
ruby-faker 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,596 kB
  • sloc: ruby: 20,656; sh: 6; makefile: 6
file content (41 lines) | stat: -rw-r--r-- 1,196 bytes parent folder | download
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
# frozen_string_literal: true

require 'yaml'

desc 'Reformat all locales'
task :reformat_locales do
  path = File.absolute_path(File.join(__dir__, '..', 'lib', 'locales', '**', '*.yml'))
  locales = Dir[path]
  failures = []

  locales.each do |locale_path|
    reformat_file(locale_path)
  rescue StandardError => e
    failures << "Error reformatting #{locale_path}: #{e.message}"
  end

  puts failures
end

desc 'Reformat a yaml file into a common format'
task :reformat_yaml, [:filename] do |_, args|
  args.with_defaults(filename: nil)

  raise ArgumentError, 'A filename is required. `bundle exec rake reformat_yaml["lib/path/to/fil"]`' if args[:filename].nil?

  root_dir = File.absolute_path(File.join(__dir__, '..'))
  target_file = File.join(root_dir, args[:filename])
  reformat_file(target_file)
end

def reformat_file(filename)
  puts "Reformatting #{filename}"

  input = YAML.safe_load_file(filename, aliases: true)
  # Psych outputs non-indented hypendated array list items.
  output = input.to_yaml(line_width: -1)
                .gsub(/(^ *- .+$)/, '  \1') # Indent hyphenated list items
                .sub(/^---\n/, '') # Remove header

  File.write(filename, output)
end