yaml-sort 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +26 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +23 -0
- data/exe/yaml-sort +7 -0
- data/lib/yaml/sort/cli.rb +87 -0
- data/lib/yaml/sort/dictionary.rb +47 -0
- data/lib/yaml/sort/list.rb +37 -0
- data/lib/yaml/sort/parser.rb +363 -0
- data/lib/yaml/sort/scalar.rb +27 -0
- data/lib/yaml/sort/tasks/puppet.rb +13 -0
- data/lib/yaml/sort/tasks/rails.rb +13 -0
- data/lib/yaml/sort/value.rb +19 -0
- data/lib/yaml/sort/version.rb +7 -0
- data/lib/yaml/sort.rb +15 -0
- data/sig/yaml/sort.rbs +6 -0
- data/yaml-sort.gemspec +36 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b17a9fdbfc9612b6b2bb1a75583e9a31c269fbba87c088afc0fc6ffe16e44aa1
|
4
|
+
data.tar.gz: edde89f395f92d321c95e4899c6744bba2b005b93aa4399c00d090c4511a27ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a3a763b77216fad635aec8707d388d1bc1c8afb1ccd2217922b17c6179bc59d6c1998459c3e131375d1fed6e743d94d2d39dc2f080bd4adf45b42a73aff56b94
|
7
|
+
data.tar.gz: 11573f243771dcd0ec7c52748a9927c358472860e0a52e287f8764c960e539aa76b6df6ee6f2fff724eb9f5e780c2b62d7921b65b1d0f9aaf696d388e618b6f4
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
TargetRubyVersion: 2.7
|
4
|
+
Exclude:
|
5
|
+
- lib/yaml/sort/parser.rb
|
6
|
+
- vendor/**/*
|
7
|
+
|
8
|
+
Style/Documentation:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Style/StringLiterals:
|
12
|
+
Enabled: true
|
13
|
+
EnforcedStyle: double_quotes
|
14
|
+
|
15
|
+
Style/StringLiteralsInInterpolation:
|
16
|
+
Enabled: true
|
17
|
+
EnforcedStyle: double_quotes
|
18
|
+
|
19
|
+
Layout/LineLength:
|
20
|
+
Max: 140
|
21
|
+
|
22
|
+
Metrics/BlockLength:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/TrailingCommaInHashLiteral:
|
26
|
+
EnforcedStyleForMultiline: consistent_comma
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at romain@blogreen.org. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in yaml-sort.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem "rubocop", "~> 1.21"
|
13
|
+
gem "rubocop-rake"
|
14
|
+
gem "rubocop-rspec"
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Romain Tartière
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# yaml-sort
|
2
|
+
|
3
|
+
An utility to manage YAML files and ensure their lines is in a predictable order.
|
4
|
+
|
5
|
+
This is mainly indended to manage [Hiera files](https://puppet.com/docs/puppet/7/hiera_intro.html) for [Puppet](https://puppet.com/) and translation files in [Ruby on Rails](https://rubyonrails.org/) applications.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add yaml-sort
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
$ gem install yaml-sort
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```
|
20
|
+
Usage: yaml-sort [options] [filename]
|
21
|
+
-i, --in-place Update files in-place
|
22
|
+
-l, --lint Ensure files content is sorted as expected
|
23
|
+
```
|
24
|
+
|
25
|
+
## Hiera Integration for Puppet
|
26
|
+
|
27
|
+
Add this to your Rakefile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require "yaml/sort/tasks/puppet"
|
31
|
+
|
32
|
+
Rake::Task[:lint].enhance ["lint:hiera_data_ordering"]
|
33
|
+
```
|
34
|
+
|
35
|
+
This provide two new targets:
|
36
|
+
|
37
|
+
```
|
38
|
+
rake lint:hiera_data_ordering # Check Hiera data content ordering
|
39
|
+
rake reorder_hiera_data # Reorder Hiera data in-place
|
40
|
+
```
|
41
|
+
|
42
|
+
## Ruby on Rails Integration
|
43
|
+
|
44
|
+
Add this to your Rakefile:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
require "yaml/sort/tasks/rails"
|
48
|
+
|
49
|
+
Rake::Task[:test].enhance ["test:translations_ordering"]
|
50
|
+
```
|
51
|
+
|
52
|
+
This provide two new targets:
|
53
|
+
|
54
|
+
```
|
55
|
+
rake reorder_translations # Reorder translations in-place
|
56
|
+
rake test:translations_ordering # Check translations content ordering
|
57
|
+
```
|
58
|
+
|
59
|
+
## Development
|
60
|
+
|
61
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
62
|
+
|
63
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/smortex/yaml-sort. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/smortex/yaml-sort/blob/main/CODE_OF_CONDUCT.md).
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
72
|
+
|
73
|
+
## Code of Conduct
|
74
|
+
|
75
|
+
Everyone interacting in the yaml-sort project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/smortex/yaml-sort/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
require "rubocop/rake_task"
|
9
|
+
|
10
|
+
RuboCop::RakeTask.new
|
11
|
+
|
12
|
+
require "cucumber/rake/task"
|
13
|
+
|
14
|
+
Cucumber::Rake::Task.new
|
15
|
+
|
16
|
+
task default: %i[rubocop spec cucumber]
|
17
|
+
|
18
|
+
file "lib/yaml/sort/parser.rb": ["lib/yaml/sort/parser.ra"] do
|
19
|
+
sh "racc --output-status --output-file=lib/yaml/sort/parser.rb lib/yaml/sort/parser.ra"
|
20
|
+
end
|
21
|
+
|
22
|
+
task spec: ["lib/yaml/sort/parser.rb"]
|
23
|
+
task cucumber: ["lib/yaml/sort/parser.rb"]
|
data/exe/yaml-sort
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
|
5
|
+
require "cri"
|
6
|
+
|
7
|
+
module Yaml
|
8
|
+
module Sort
|
9
|
+
class Cli
|
10
|
+
def initialize
|
11
|
+
@parser = Yaml::Sort::Parser.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def execute(argv) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
15
|
+
options = {
|
16
|
+
in_place: false,
|
17
|
+
lint: false,
|
18
|
+
}
|
19
|
+
OptionParser.new do |opts|
|
20
|
+
opts.banner = "Usage: yaml-sort [options] [filename]"
|
21
|
+
|
22
|
+
opts.on("-i", "--in-place", "Update files in-place") do
|
23
|
+
options[:in_place] = true
|
24
|
+
end
|
25
|
+
opts.on("-l", "--lint", "Ensure files content is sorted as expected") do
|
26
|
+
options[:lint] = true
|
27
|
+
end
|
28
|
+
end.parse!
|
29
|
+
|
30
|
+
if !options[:in_place] && !options[:lint] && argv.count > 1
|
31
|
+
warn "Sorting multiple YAML document to stdout does not make sense"
|
32
|
+
return 1
|
33
|
+
end
|
34
|
+
|
35
|
+
if options[:in_place] && argv.count < 1
|
36
|
+
warn "Cannot sort in-place when reading from stdin"
|
37
|
+
return 1
|
38
|
+
end
|
39
|
+
|
40
|
+
@exit_code = 0
|
41
|
+
|
42
|
+
if argv.empty?
|
43
|
+
process_document(nil, options)
|
44
|
+
else
|
45
|
+
argv.each do |filename|
|
46
|
+
process_document(filename, options)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
@exit_code
|
51
|
+
end
|
52
|
+
|
53
|
+
def process_document(filename, options)
|
54
|
+
yaml = read_document(filename)
|
55
|
+
sorted_yaml = sort_yaml(yaml)
|
56
|
+
write_output(yaml, sorted_yaml, filename, options)
|
57
|
+
end
|
58
|
+
|
59
|
+
def read_document(filename)
|
60
|
+
if filename
|
61
|
+
File.read(filename)
|
62
|
+
else
|
63
|
+
$stdin.read
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def sort_yaml(yaml)
|
68
|
+
document = @parser.parse(yaml)
|
69
|
+
document = document.sort
|
70
|
+
"---\n#{document}\n"
|
71
|
+
end
|
72
|
+
|
73
|
+
def write_output(yaml, sorted_yaml, filename, options)
|
74
|
+
if options[:in_place]
|
75
|
+
File.write(filename, sorted_yaml)
|
76
|
+
elsif options[:lint]
|
77
|
+
if yaml != sorted_yaml
|
78
|
+
warn "#{filename || "<stdin>"} is not sorted as expected"
|
79
|
+
@exit_code = 1
|
80
|
+
end
|
81
|
+
else
|
82
|
+
puts sorted_yaml
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yaml
|
4
|
+
module Sort
|
5
|
+
class Dictionary < Value
|
6
|
+
attr_reader :items
|
7
|
+
|
8
|
+
def initialize(items = [])
|
9
|
+
super()
|
10
|
+
@items = items
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_item(key, value)
|
14
|
+
@items << [key, value]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.create(key, value)
|
18
|
+
dict = Dictionary.new
|
19
|
+
dict.add_item(key, value)
|
20
|
+
dict
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
super + items.map do |k, v|
|
25
|
+
case v
|
26
|
+
when List, Dictionary
|
27
|
+
"#{k}\n#{v}"
|
28
|
+
when Scalar
|
29
|
+
"#{k} #{v}"
|
30
|
+
end
|
31
|
+
end.join("\n")
|
32
|
+
end
|
33
|
+
|
34
|
+
def sort
|
35
|
+
Dictionary.new(items.map { |a| [a[0], a[1].sort] }.sort do |a, b|
|
36
|
+
if a[0].value == "lookup_options:"
|
37
|
+
-1
|
38
|
+
elsif b[0].value == "lookup_options:"
|
39
|
+
1
|
40
|
+
else
|
41
|
+
a[0] <=> b[0]
|
42
|
+
end
|
43
|
+
end)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yaml
|
4
|
+
module Sort
|
5
|
+
class List < Value
|
6
|
+
attr_reader :items
|
7
|
+
|
8
|
+
def initialize(items = [])
|
9
|
+
super()
|
10
|
+
@items = items
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_item(dash, value)
|
14
|
+
@items << [dash, value]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.create(dash, value)
|
18
|
+
list = List.new
|
19
|
+
list.add_item(dash, value)
|
20
|
+
list
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
super + items.map do |item|
|
25
|
+
"#{item[0]}#{item[1]}"
|
26
|
+
end.join("\n")
|
27
|
+
end
|
28
|
+
|
29
|
+
def sort
|
30
|
+
List.new(items.sort { |a, b| a[1] <=> b[1] })
|
31
|
+
rescue ArgumentError
|
32
|
+
# Non-comparable items
|
33
|
+
self
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,363 @@
|
|
1
|
+
#
|
2
|
+
# DO NOT MODIFY!!!!
|
3
|
+
# This file is automatically generated by Racc 1.6.0
|
4
|
+
# from Racc grammar file "".
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'racc/parser.rb'
|
8
|
+
|
9
|
+
|
10
|
+
require 'strscan'
|
11
|
+
|
12
|
+
module Yaml
|
13
|
+
module Sort
|
14
|
+
class Parser < Racc::Parser
|
15
|
+
|
16
|
+
module_eval(<<'...end parser.ra/module_eval...', 'parser.ra', 34)
|
17
|
+
|
18
|
+
def scan(text)
|
19
|
+
scan_value = false
|
20
|
+
|
21
|
+
@lines = text.lines
|
22
|
+
s = StringScanner.new(text)
|
23
|
+
@tokens = []
|
24
|
+
@lineno = 1
|
25
|
+
@fakelineno = 0
|
26
|
+
@position = 0
|
27
|
+
@indent_stack = []
|
28
|
+
|
29
|
+
until s.eos?
|
30
|
+
if scan_value
|
31
|
+
unless s.match?(/[[:space:]]*\n/)
|
32
|
+
@position += s.matched_size if s.scan(/\s*/)
|
33
|
+
case
|
34
|
+
when s.scan(/[|>][-+]?(?=\n)/)
|
35
|
+
match = s.matched
|
36
|
+
|
37
|
+
while s.match?("\n" + last_indent_value + " ")
|
38
|
+
match += s.scan(/\n[^\n]+(?=\n)/)
|
39
|
+
end
|
40
|
+
emit(:VALUE, match)
|
41
|
+
when s.scan(/"/)
|
42
|
+
match = s.matched
|
43
|
+
loop do
|
44
|
+
match += s.scan_until(/"|\\/)
|
45
|
+
if match[-1] == "\\"
|
46
|
+
match += s.scan(/.|\n/)
|
47
|
+
else
|
48
|
+
break
|
49
|
+
end
|
50
|
+
end
|
51
|
+
emit(:VALUE, match)
|
52
|
+
when s.scan(/'/)
|
53
|
+
match = s.matched
|
54
|
+
loop do
|
55
|
+
match += s.scan_until(/'/)
|
56
|
+
break unless s.match?(/'/)
|
57
|
+
match += s.scan(/'/)
|
58
|
+
end
|
59
|
+
emit(:VALUE, match)
|
60
|
+
when s.scan(/\S+/)
|
61
|
+
match = s.matched
|
62
|
+
until s.match?(/[\n]/) || s.eos?
|
63
|
+
match += s.scan(/[^\n]+/)
|
64
|
+
end
|
65
|
+
emit(:VALUE, match)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
scan_value = false
|
69
|
+
else
|
70
|
+
case
|
71
|
+
when s.scan(/---/) then emit(:START_OF_DOCUMENT, s.matched)
|
72
|
+
when s.scan(/(\n\s*)+(?=\n)/) then # Ignore empty lines
|
73
|
+
when s.scan(/\n?\s*#.*/) then emit(:COMMENT, s.matched)
|
74
|
+
when s.scan(/\n?(\s*-) */) then emit("-", s.matched, indent: s.captures[0])
|
75
|
+
when s.scan(/\n?\s*\.\.\./) then emit(:END_OF_DOCUMENT, s.matched)
|
76
|
+
when s.scan(/\n?(\s*)([^[[:space:]]\n]+):(?=[ \n])/)
|
77
|
+
indent = s.captures[0]
|
78
|
+
indent = last_indent_value + indent + " " unless s.matched.start_with?("\n")
|
79
|
+
emit(:KEY, s.matched, indent: indent)
|
80
|
+
|
81
|
+
when s.scan(/\n\z/)
|
82
|
+
# Done
|
83
|
+
when s.match?(/./)
|
84
|
+
scan_value = true
|
85
|
+
else
|
86
|
+
raise "LoST: #{s.rest.inspect} #{s.eos?}"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
unindent
|
92
|
+
|
93
|
+
comment = []
|
94
|
+
@tokens.each do |token|
|
95
|
+
if token[0] == :COMMENT
|
96
|
+
comment << token[1][:value] + "\n"
|
97
|
+
elsif comment.any?
|
98
|
+
token[1][:comment] = comment
|
99
|
+
comment = []
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
@tokens = @tokens.delete_if { |itm| itm[0] == :COMMENT }
|
104
|
+
|
105
|
+
@tokens
|
106
|
+
end
|
107
|
+
|
108
|
+
def emit(token, value, length: nil, indent: nil)
|
109
|
+
indent.gsub!("-", " ") if indent
|
110
|
+
if token && length.nil?
|
111
|
+
raise "length must be explicitly passed when value is not a String (#{value.class.name})" unless value.is_a?(String)
|
112
|
+
length = value.length
|
113
|
+
end
|
114
|
+
|
115
|
+
if value.start_with?("\n")
|
116
|
+
@lineno += 1
|
117
|
+
value = value[1..-1]
|
118
|
+
length -= 1
|
119
|
+
@position = 0
|
120
|
+
end
|
121
|
+
|
122
|
+
if indent
|
123
|
+
unindent(indent)
|
124
|
+
@indent_stack.push(indent) unless @indent_stack.last == indent
|
125
|
+
end
|
126
|
+
|
127
|
+
exvalue = {
|
128
|
+
value: value,
|
129
|
+
lineno: @lineno,
|
130
|
+
position: @position,
|
131
|
+
filename: @filename,
|
132
|
+
length: length,
|
133
|
+
indent: indent,
|
134
|
+
}
|
135
|
+
@tokens << [token, exvalue]
|
136
|
+
|
137
|
+
@lineno += value.count("\n")
|
138
|
+
|
139
|
+
@position += length
|
140
|
+
end
|
141
|
+
|
142
|
+
def last_indent_value
|
143
|
+
@indent_stack.last || ""
|
144
|
+
end
|
145
|
+
|
146
|
+
def unindent(new_indent = nil)
|
147
|
+
while @indent_stack.count > 0 && (new_indent.nil? || @indent_stack.last.length > new_indent.length)
|
148
|
+
value = @indent_stack.pop
|
149
|
+
@tokens << [:UNINDENT, { value: value, lineno: @lineno, position: 0, filename: @filename, length: value.length, indent: nil }]
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def next_token
|
154
|
+
@current_token = @tokens.shift
|
155
|
+
end
|
156
|
+
|
157
|
+
def parse(text)
|
158
|
+
scan(text)
|
159
|
+
do_parse
|
160
|
+
end
|
161
|
+
|
162
|
+
def on_error(error_token_id, error_value, value_stack)
|
163
|
+
puts "unexpected #{@current_token[0]} on #{@current_token[1][:filename] || "<stdin>"}:#{@current_token[1][:lineno]}"
|
164
|
+
puts @lines[@current_token[1][:lineno]]
|
165
|
+
puts " " * @current_token[1][:position] + "^" + "~" * (@current_token[1][:length] - 1)
|
166
|
+
|
167
|
+
raise "Error"
|
168
|
+
end
|
169
|
+
...end parser.ra/module_eval...
|
170
|
+
##### State transition tables begin ###
|
171
|
+
|
172
|
+
racc_action_table = [
|
173
|
+
2, 11, 4, 8, 19, 10, 4, 8, 20, 10,
|
174
|
+
4, 8, nil, 10, 4, 8, nil, 10, 8, 13,
|
175
|
+
15, 10 ]
|
176
|
+
|
177
|
+
racc_action_check = [
|
178
|
+
0, 1, 0, 0, 11, 0, 2, 2, 12, 2,
|
179
|
+
8, 8, nil, 8, 10, 10, nil, 10, 5, 5,
|
180
|
+
6, 6 ]
|
181
|
+
|
182
|
+
racc_action_pointer = [
|
183
|
+
-2, 1, 2, nil, nil, 13, 14, nil, 6, nil,
|
184
|
+
10, 4, 5, nil, nil, nil, nil, nil, nil, nil,
|
185
|
+
nil ]
|
186
|
+
|
187
|
+
racc_action_default = [
|
188
|
+
-13, -13, -13, -3, -4, -13, -13, -8, -13, -11,
|
189
|
+
-13, -13, -2, -5, -7, -6, -10, -9, -12, 21,
|
190
|
+
-1 ]
|
191
|
+
|
192
|
+
racc_goto_table = [
|
193
|
+
3, 1, 12, 14, 16, nil, nil, nil, 17, nil,
|
194
|
+
18 ]
|
195
|
+
|
196
|
+
racc_goto_check = [
|
197
|
+
2, 1, 2, 5, 6, nil, nil, nil, 2, nil,
|
198
|
+
2 ]
|
199
|
+
|
200
|
+
racc_goto_pointer = [
|
201
|
+
nil, 1, 0, nil, nil, -2, -2 ]
|
202
|
+
|
203
|
+
racc_goto_default = [
|
204
|
+
nil, nil, nil, 5, 6, 7, 9 ]
|
205
|
+
|
206
|
+
racc_reduce_table = [
|
207
|
+
0, 0, :racc_error,
|
208
|
+
3, 9, :_reduce_1,
|
209
|
+
2, 9, :_reduce_2,
|
210
|
+
1, 9, :_reduce_none,
|
211
|
+
1, 10, :_reduce_4,
|
212
|
+
2, 10, :_reduce_5,
|
213
|
+
2, 10, :_reduce_6,
|
214
|
+
2, 11, :_reduce_7,
|
215
|
+
1, 11, :_reduce_8,
|
216
|
+
2, 13, :_reduce_9,
|
217
|
+
2, 12, :_reduce_10,
|
218
|
+
1, 12, :_reduce_11,
|
219
|
+
2, 14, :_reduce_12 ]
|
220
|
+
|
221
|
+
racc_reduce_n = 13
|
222
|
+
|
223
|
+
racc_shift_n = 21
|
224
|
+
|
225
|
+
racc_token_table = {
|
226
|
+
false => 0,
|
227
|
+
:error => 1,
|
228
|
+
:START_OF_DOCUMENT => 2,
|
229
|
+
:END_OF_DOCUMENT => 3,
|
230
|
+
:VALUE => 4,
|
231
|
+
:KEY => 5,
|
232
|
+
:UNINDENT => 6,
|
233
|
+
"-" => 7 }
|
234
|
+
|
235
|
+
racc_nt_base = 8
|
236
|
+
|
237
|
+
racc_use_result_var = true
|
238
|
+
|
239
|
+
Racc_arg = [
|
240
|
+
racc_action_table,
|
241
|
+
racc_action_check,
|
242
|
+
racc_action_default,
|
243
|
+
racc_action_pointer,
|
244
|
+
racc_goto_table,
|
245
|
+
racc_goto_check,
|
246
|
+
racc_goto_default,
|
247
|
+
racc_goto_pointer,
|
248
|
+
racc_nt_base,
|
249
|
+
racc_reduce_table,
|
250
|
+
racc_token_table,
|
251
|
+
racc_shift_n,
|
252
|
+
racc_reduce_n,
|
253
|
+
racc_use_result_var ]
|
254
|
+
|
255
|
+
Racc_token_to_s_table = [
|
256
|
+
"$end",
|
257
|
+
"error",
|
258
|
+
"START_OF_DOCUMENT",
|
259
|
+
"END_OF_DOCUMENT",
|
260
|
+
"VALUE",
|
261
|
+
"KEY",
|
262
|
+
"UNINDENT",
|
263
|
+
"\"-\"",
|
264
|
+
"$start",
|
265
|
+
"document",
|
266
|
+
"value",
|
267
|
+
"dictionary",
|
268
|
+
"list",
|
269
|
+
"dictionary_item",
|
270
|
+
"list_item" ]
|
271
|
+
|
272
|
+
Racc_debug_parser = false
|
273
|
+
|
274
|
+
##### State transition tables end #####
|
275
|
+
|
276
|
+
# reduce 0 omitted
|
277
|
+
|
278
|
+
module_eval(<<'.,.,', 'parser.ra', 9)
|
279
|
+
def _reduce_1(val, _values, result)
|
280
|
+
result = val[1]
|
281
|
+
result
|
282
|
+
end
|
283
|
+
.,.,
|
284
|
+
|
285
|
+
module_eval(<<'.,.,', 'parser.ra', 10)
|
286
|
+
def _reduce_2(val, _values, result)
|
287
|
+
result = val[1]
|
288
|
+
result
|
289
|
+
end
|
290
|
+
.,.,
|
291
|
+
|
292
|
+
# reduce 3 omitted
|
293
|
+
|
294
|
+
module_eval(<<'.,.,', 'parser.ra', 13)
|
295
|
+
def _reduce_4(val, _values, result)
|
296
|
+
result = Scalar.new(val[0])
|
297
|
+
result
|
298
|
+
end
|
299
|
+
.,.,
|
300
|
+
|
301
|
+
module_eval(<<'.,.,', 'parser.ra', 14)
|
302
|
+
def _reduce_5(val, _values, result)
|
303
|
+
result = val[0]
|
304
|
+
result
|
305
|
+
end
|
306
|
+
.,.,
|
307
|
+
|
308
|
+
module_eval(<<'.,.,', 'parser.ra', 15)
|
309
|
+
def _reduce_6(val, _values, result)
|
310
|
+
result = val[0]
|
311
|
+
result
|
312
|
+
end
|
313
|
+
.,.,
|
314
|
+
|
315
|
+
module_eval(<<'.,.,', 'parser.ra', 17)
|
316
|
+
def _reduce_7(val, _values, result)
|
317
|
+
val[0].add_item(*val[1]); result = val[0]
|
318
|
+
result
|
319
|
+
end
|
320
|
+
.,.,
|
321
|
+
|
322
|
+
module_eval(<<'.,.,', 'parser.ra', 18)
|
323
|
+
def _reduce_8(val, _values, result)
|
324
|
+
result = Dictionary.create(*val[0])
|
325
|
+
result
|
326
|
+
end
|
327
|
+
.,.,
|
328
|
+
|
329
|
+
module_eval(<<'.,.,', 'parser.ra', 20)
|
330
|
+
def _reduce_9(val, _values, result)
|
331
|
+
result = [Scalar.new(val[0]), val[1]]
|
332
|
+
result
|
333
|
+
end
|
334
|
+
.,.,
|
335
|
+
|
336
|
+
module_eval(<<'.,.,', 'parser.ra', 22)
|
337
|
+
def _reduce_10(val, _values, result)
|
338
|
+
val[0].add_item(*val[1]); result = val[0]
|
339
|
+
result
|
340
|
+
end
|
341
|
+
.,.,
|
342
|
+
|
343
|
+
module_eval(<<'.,.,', 'parser.ra', 23)
|
344
|
+
def _reduce_11(val, _values, result)
|
345
|
+
result = List.create(*val[0])
|
346
|
+
result
|
347
|
+
end
|
348
|
+
.,.,
|
349
|
+
|
350
|
+
module_eval(<<'.,.,', 'parser.ra', 25)
|
351
|
+
def _reduce_12(val, _values, result)
|
352
|
+
result = [Scalar.new(val[0]), val[1]]
|
353
|
+
result
|
354
|
+
end
|
355
|
+
.,.,
|
356
|
+
|
357
|
+
def _reduce_none(val, _values, result)
|
358
|
+
val[0]
|
359
|
+
end
|
360
|
+
|
361
|
+
end # class Parser
|
362
|
+
end # module Sort
|
363
|
+
end # module Yaml
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yaml
|
4
|
+
module Sort
|
5
|
+
class Scalar < Value
|
6
|
+
attr_reader :value
|
7
|
+
|
8
|
+
def initialize(value)
|
9
|
+
super()
|
10
|
+
@comment = value[:comment] || []
|
11
|
+
@value = value[:value]
|
12
|
+
end
|
13
|
+
|
14
|
+
def <=>(other)
|
15
|
+
if other.is_a?(Scalar)
|
16
|
+
value <=> other.value
|
17
|
+
else
|
18
|
+
0
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
super + value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
namespace :lint do
|
4
|
+
desc "Check Hiera data content ordering"
|
5
|
+
task :hiera_data_ordering do
|
6
|
+
sh "yaml-sort", "--lint", *Dir.glob("data/**/*.yaml")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "Reorder Hiera data in-place"
|
11
|
+
task :reorder_hiera_data do
|
12
|
+
sh "yaml-sort", "--in-place", *Dir.glob("data/**/*.yaml")
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
namespace :test do
|
4
|
+
desc "Check translations content ordering"
|
5
|
+
task :translations_ordering do
|
6
|
+
sh "yaml-sort", "--lint", *Dir.glob("config/locales/**/*.yml")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "Reorder translations in-place"
|
11
|
+
task :reorder_translations do
|
12
|
+
sh "yaml-sort", "--in-place", *Dir.glob("config/locales/**/*.yml")
|
13
|
+
end
|
data/lib/yaml/sort.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "sort/parser"
|
4
|
+
require_relative "sort/value"
|
5
|
+
require_relative "sort/dictionary"
|
6
|
+
require_relative "sort/list"
|
7
|
+
require_relative "sort/scalar"
|
8
|
+
require_relative "sort/version"
|
9
|
+
|
10
|
+
module Yaml
|
11
|
+
module Sort
|
12
|
+
class Error < StandardError; end
|
13
|
+
# Your code goes here...
|
14
|
+
end
|
15
|
+
end
|
data/sig/yaml/sort.rbs
ADDED
data/yaml-sort.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/yaml/sort/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "yaml-sort"
|
7
|
+
spec.version = Yaml::Sort::VERSION
|
8
|
+
spec.authors = ["Romain Tartière"]
|
9
|
+
spec.email = ["romain@blogreen.org"]
|
10
|
+
|
11
|
+
spec.summary = "Sort lines in YAML files in a predictable order"
|
12
|
+
spec.homepage = "https://github.com/smortex/yaml-sort"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = ">= 2.7.0"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/smortex/yaml-sort/blob/master/CHANGELOG.md"
|
19
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
26
|
+
end - ["lib/yaml/sort/parser.ra"] + ["lib/yaml/sort/parser.rb"]
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_dependency "cri"
|
33
|
+
|
34
|
+
spec.add_development_dependency "aruba"
|
35
|
+
spec.add_development_dependency "racc"
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml-sort
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Romain Tartière
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-04-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: cri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aruba
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: racc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- romain@blogreen.org
|
58
|
+
executables:
|
59
|
+
- yaml-sort
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".rspec"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- CHANGELOG.md
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- exe/yaml-sort
|
72
|
+
- lib/yaml/sort.rb
|
73
|
+
- lib/yaml/sort/cli.rb
|
74
|
+
- lib/yaml/sort/dictionary.rb
|
75
|
+
- lib/yaml/sort/list.rb
|
76
|
+
- lib/yaml/sort/parser.rb
|
77
|
+
- lib/yaml/sort/scalar.rb
|
78
|
+
- lib/yaml/sort/tasks/puppet.rb
|
79
|
+
- lib/yaml/sort/tasks/rails.rb
|
80
|
+
- lib/yaml/sort/value.rb
|
81
|
+
- lib/yaml/sort/version.rb
|
82
|
+
- sig/yaml/sort.rbs
|
83
|
+
- yaml-sort.gemspec
|
84
|
+
homepage: https://github.com/smortex/yaml-sort
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata:
|
88
|
+
homepage_uri: https://github.com/smortex/yaml-sort
|
89
|
+
source_code_uri: https://github.com/smortex/yaml-sort
|
90
|
+
changelog_uri: https://github.com/smortex/yaml-sort/blob/master/CHANGELOG.md
|
91
|
+
rubygems_mfa_required: 'true'
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 2.7.0
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubygems_version: 3.2.5
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Sort lines in YAML files in a predictable order
|
111
|
+
test_files: []
|