LoginSignup
1
0

More than 5 years have passed since last update.

Ruby gemを使ってYAMLをソートする

Last updated at Posted at 2018-12-30

YAMLをソートするには

以下のgemを使う。
redealumni/i18n_yaml_sorter

使い方

ここではテキトーな作業フォルダを作ってそこでソートしてみる。

Install

bundlerを使う方法(単にgem installでも可)


$ bundle init
---
Gemfileに以下を追加
  gem "i18n_yaml_sorter"
---
$ bundle install --path vendor/bundle

実行

bundle install出来たら、sort_yamlというコマンドが使えるようになっているので、
以下のコマンドでYAMLファイルをソートする。


$ bundle exec sort_yaml < #{入力YAMLファイル} > #{出力先ファイル} 
example:
$ bundle exec sort_yaml < ja.yml > ja_out.yml

実行結果

before

en-US:
  # Note that our comments are important:
  # Don't let your yaml sorter delete them!
  bananas: |
    Bananas are "nice":
      - They are <b> sweet </b>.
      this: not a key  
    That is why everyone like bananas!
  grapes: We eat them.
  apples: >
    Apples are fine,
    just don't eat your 
    iPods!
  grapes: We dont' eat them.

after

en-US:
  # Note that our comments are important:
  # Don't let your yaml sorter delete them!
  apples: >
    Apples are fine,
    just don't eat your
    iPods!
  bananas: |
    Bananas are "nice":
      - They are <b> sweet </b>.
      this: not a key
    That is why everyone like bananas!
  grapes: We dont' eat them.
  grapes: We eat them.
  • コメントはそのままの場所に残るっぽい。
  • キーが重複していると、値でソートするっぽい。

環境情報

意図せずこのバージョンだった。これじゃないと動かないとかではない。


$ rbenv -v
rbenv 1.1.1

$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin18]

$ bundle -v
Bundler version 1.17.1
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0