dorian-sort-yaml 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/sort-yaml +40 -2
  3. metadata +5 -6
  4. data/lib/dorian/sort-yaml.rb +0 -44
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30228cc27871ce993b8716ef26d6332d72df2bd2f46df221b1856862cf4aba8e
4
- data.tar.gz: d8f6c63e66d9e3a47da7eaec954a3bf508b9d9553267cc2c87733e349804ec54
3
+ metadata.gz: 3a8e2db05d6c40e7ac8ca91793699c22b6fba871b3223d96ca379f8a2f065d3a
4
+ data.tar.gz: 8fbc2e4adfed3e7ffb0cd53ad6247174869fa0a543d7000efc55d101513c0145
5
5
  SHA512:
6
- metadata.gz: ff1d3380502172e79a460cdec69760daeb97bf7a20918c0dd41cda1df42e95e445975e73636f4d13c1b02cbf54ef3d892be722c77122f99f578cdab9779a90da
7
- data.tar.gz: c106803271e8308aed73babd1a66a164c841ebf81bf75a7a90cf25bce19111adcb04f838ebbd8691ff20f01c461987bde9c6c42f9b4951893b3b0cb7f678d875
6
+ metadata.gz: 6e57da1e308e4b7149908eb1b332c80281cb5a2407c13a7b470a7382db3f3d4a011ab099da699e56e0124e9645dd21570f72b5aa6ac56c971911be791aede564
7
+ data.tar.gz: 78802f796358ef285f4efbac9287d9b9e1aab1e7e1ab6dfadccc881851bdc032cfc69275a67fa7f9f5af695c14ecec983512f09aa81554bfed796a6824a761a1
data/bin/sort-yaml CHANGED
@@ -1,4 +1,42 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require 'dorian/sort-yaml'
4
- Dorian::SortYaml.run
4
+ require "yaml"
5
+
6
+ def sort_yaml(data)
7
+ if data.is_a?(Array)
8
+ data.map { |element| sort_yaml(element) }
9
+ elsif data.is_a?(Hash)
10
+ data
11
+ .sort_by { |key, _value| key }
12
+ .to_h
13
+ .transform_values { |value| sort_yaml(value) }
14
+ else
15
+ data
16
+ end
17
+ end
18
+
19
+ if ARGV[0] == "--help" || ARGV[0] == "-h"
20
+ puts "USAGE: sort-yaml FILES..."
21
+ puts "USAGE: ... | sort-yaml"
22
+ exit
23
+ end
24
+
25
+ inputs = ARGV
26
+
27
+ if inputs.empty?
28
+ inputs = $stdin.each_line.to_a
29
+
30
+ inputs =
31
+ if File.exist?(inputs.first.strip)
32
+ inputs.map(&:strip)
33
+ else
34
+ [inputs.join]
35
+ end
36
+ end
37
+
38
+ inputs.each do |input|
39
+ content = File.exist?(input) ? File.read(input) : input
40
+ yaml = sort_yaml(YAML.safe_load(content)).to_yaml
41
+ File.exist?(input) ? File.write(input, yaml) : puts(yaml)
42
+ end
metadata CHANGED
@@ -1,28 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-sort-yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-21 00:00:00.000000000 Z
11
+ date: 2024-03-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  Sorts keys of hashes of YAML files
15
15
 
16
16
  e.g. `sort-yaml config/locales/*`
17
- email: dorian@dorianmarie.fr
17
+ email: dorian@dorianmarie.com
18
18
  executables:
19
19
  - sort-yaml
20
20
  extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
23
  - bin/sort-yaml
24
- - lib/dorian/sort-yaml.rb
25
- homepage: https://github.com/dorianmariefr/sort-yaml
24
+ homepage: https://github.com/dorianmariecom/sort-yaml
26
25
  licenses:
27
26
  - MIT
28
27
  metadata:
@@ -42,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
41
  - !ruby/object:Gem::Version
43
42
  version: '0'
44
43
  requirements: []
45
- rubygems_version: 3.2.22
44
+ rubygems_version: 3.5.3
46
45
  signing_key:
47
46
  specification_version: 4
48
47
  summary: Sorts keys of hashes of YAML files
@@ -1,44 +0,0 @@
1
- require "yaml"
2
-
3
- module Dorian
4
- class SortYaml
5
- def self.run
6
- if ARGV[0] == "--help" || ARGV[0] == "-h"
7
- puts "USAGE: sort-yaml FILES..."
8
- puts "USAGE: ... | sort-yaml"
9
- exit
10
- end
11
-
12
- inputs = ARGV
13
-
14
- if inputs.size.zero?
15
- inputs = STDIN.each_line.to_a
16
-
17
- if File.exist?(inputs.first.strip)
18
- inputs = inputs.map(&:strip)
19
- else
20
- inputs = [inputs.join]
21
- end
22
- end
23
-
24
- inputs.each do |input|
25
- content = File.exist?(input) ? File.read(input) : input
26
- yaml = sort_yaml(YAML.safe_load(content)).to_yaml
27
- File.exist?(input) ? File.write(input, yaml) : puts(yaml)
28
- end
29
- end
30
-
31
- def self.sort_yaml(data)
32
- if data.is_a?(Array)
33
- data.map { |element| sort_yaml(element) }
34
- elsif data.is_a?(Hash)
35
- data
36
- .sort_by { |key, _value| key }
37
- .to_h
38
- .transform_values { |value| sort_yaml(value) }
39
- else
40
- data
41
- end
42
- end
43
- end
44
- end