guard-yamlsort 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5b69e36e81d47dea40c33abf82ad0ba2ed112b1f
4
+ data.tar.gz: 61a93e036188631fe64e73f303bd1dd0e208a48f
5
+ SHA512:
6
+ metadata.gz: 8cf1888cda4813de585c6b1b9e2f77530b4b0eeb39d935814177c8ff09a9781d0f44c2ba456376ae933840caee03b2571b47979fe8f4d999c6a6dfe5e6cc0657
7
+ data.tar.gz: 21b4e9ab65c9a14587e8b59b411819ae97d6f2dde86f6a7eee36768cb6723b57ab7be716cfa150ed10dc7f7e4ec936cdda5b8c13af955c013c7cbd4036b9ec8e
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in guard-yamlsort.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,59 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), then you will want to move
18
+ ## the Guardfile to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ guard 'yard', port: "8899", quiet: true do
27
+ watch(%r{app/.+\.rb})
28
+ watch(%r{lib/.+\.rb})
29
+ watch(%r{ext/.+\.c})
30
+ end
31
+
32
+ guard :minitest do
33
+
34
+ # with Minitest::Unit
35
+ watch(%r{^test/(.*)\/?test_(.*)\.rb$})
36
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
37
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
38
+
39
+ # with Minitest::Spec
40
+ # watch(%r{^spec/(.*)_spec\.rb$})
41
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
42
+ # watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
43
+
44
+ # Rails 4
45
+ # watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
46
+ # watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
47
+ # watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
48
+ # watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
49
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
50
+ # watch(%r{^test/.+_test\.rb$})
51
+ # watch(%r{^test/test_helper\.rb$}) { 'test' }
52
+
53
+ # Rails < 4
54
+ # watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
55
+ # watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" }
56
+ # watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
57
+ end
58
+
59
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Thomas Vendetta
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Guard::Yamlsort
2
+ Guard::Yamlsort alphabetically sorts keys in yaml files when a modification is detected.
3
+
4
+ ![Preview of guard-yamlsort](https://raw.githubusercontent.com/thomasv314/guard-yamlsort/master/preview.gif "Preview of Guard yamlsort")
5
+
6
+ ## Requirements
7
+ * The [Guard](https://github.com/guard/guard) rubygem.
8
+ * Ruby 1.9.2 or higher.
9
+
10
+ ## Install
11
+
12
+ Add guard-yamlsort to your Gemfile (inside development group):
13
+
14
+ ```bash
15
+ gem 'guard-yamlsort'
16
+ ```
17
+
18
+ Install or update your bundle:
19
+
20
+ ```bash
21
+ bundle install
22
+ ```
23
+
24
+ Add the default guard-yard definition to your Guardfile:
25
+
26
+ ```bash
27
+ guard init yamlsort
28
+ ```
29
+
30
+ ## Guardfile
31
+
32
+ ```ruby
33
+ guard 'yamlsort' do
34
+ watch(%r{^config/locales/.+\.yml})
35
+ end
36
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'guard/yamlsort/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "guard-yamlsort"
8
+ spec.version = Guard::YamlsortVersion::VERSION
9
+ spec.authors = ["Thomas Vendetta"]
10
+ spec.email = ["thomas@vendetta.io"]
11
+ spec.summary = %q{Sorts YAML files alphabetically by key}
12
+ spec.description = %q{Sorts YAML files alphabetically by key. }
13
+ spec.homepage = "http://vendetta.io"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "yard", "~> 0.8"
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.6"
25
+ spec.add_development_dependency "guard", "~> 2.12"
26
+ spec.add_development_dependency "guard-minitest", "~> 2.4"
27
+ spec.add_development_dependency "guard-yard", "~> 2.1"
28
+ spec.add_dependency 'guard-compat', '~> 1.1'
29
+
30
+ end
@@ -0,0 +1,90 @@
1
+ require 'guard/compat/plugin'
2
+ require 'guard/yamlsort/version'
3
+
4
+ module Guard
5
+ class Yamlsort < Guard::Plugin
6
+
7
+ require 'guard/yamlsort/file_sorter'
8
+
9
+ # Initializes a Guard plugin.
10
+ # Don't do any work here, especially as Guard plugins get initialized even if they are not in an active group!
11
+ #
12
+ # @param [Hash] options the custom Guard plugin options
13
+ # @option options [Array<Guard::Watcher>] watchers the Guard plugin file watchers
14
+ # @option options [Symbol] group the group this Guard plugin belongs to
15
+ # @option options [Boolean] any_return allow any object to be returned from a watcher
16
+ #
17
+ def initialize(options = {})
18
+ super
19
+ end
20
+
21
+ # Called once when Guard starts. Please override initialize method to init stuff.
22
+ #
23
+ # @raise [:task_has_failed] when start has failed
24
+ # @return [Object] the task result
25
+ #
26
+ def start
27
+ Guard::UI.info("Started YAML sorter.")
28
+ end
29
+
30
+ # Called when `stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
31
+ #
32
+ # @raise [:task_has_failed] when stop has failed
33
+ # @return [Object] the task result
34
+ #
35
+ def stop
36
+ true
37
+ end
38
+
39
+ # Called when `reload|r|z + enter` is pressed.
40
+ # This method should be mainly used for "reload" (really!) actions like reloading passenger/spork/bundler/...
41
+ #
42
+ # @raise [:task_has_failed] when reload has failed
43
+ # @return [Object] the task result
44
+ #
45
+ def reload
46
+ true
47
+ end
48
+
49
+ # Called when just `enter` is pressed
50
+ # This method should be principally used for long action like running all specs/tests/...
51
+ #
52
+ # @raise [:task_has_failed] when run_all has failed
53
+ # @return [Object] the task result
54
+ #
55
+ def run_all
56
+ true
57
+ end
58
+
59
+ # Called on file(s) additions that the Guard plugin watches.
60
+ # @param [Array<String>] paths the changes files or paths
61
+ # @raise [:task_has_failed] when run_on_additions has failed
62
+ # @return [Object] the task result
63
+ #
64
+ def run_on_additions(paths)
65
+ true
66
+ end
67
+
68
+ # Called on file(s) modifications that the Guard plugin watches.
69
+ #
70
+ # @param [Array<String>] paths the changes files or paths
71
+ # @raise [:task_has_failed] when run_on_modifications has failed
72
+ # @return [Object] the task result
73
+ #
74
+ def run_on_modifications(paths)
75
+ Guard::UI.info("Running YAML sorter.")
76
+ paths.each { |p| FileSorter.new(p) }
77
+ end
78
+
79
+ # Called on file(s) removals that the Guard plugin watches.
80
+ #
81
+ # @param [Array<String>] paths the changes files or paths
82
+ # @raise [:task_has_failed] when run_on_removals has failed
83
+ # @return [Object] the task result
84
+ #
85
+ def run_on_removals(paths)
86
+ true
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,63 @@
1
+ module Guard
2
+ class Yamlsort
3
+ class FileSorter
4
+
5
+ attr_accessor :yaml
6
+
7
+ # Initialized with the path of the YAML file we want to sort. It automatically
8
+ # loads the file, stores any comments in memory, and then starts sorting
9
+ # unless otherwise specified with the start_on_init parameter.
10
+ # If no parameters are passed a Generic
11
+ # @param path [String] the path on disc this file is located at
12
+ # @param start_on_init [Boolean] whether or not to automatically sort & write on
13
+ # initialization of a FileSorter (for testing purposes)
14
+ def initialize(path, start_on_init = true)
15
+ @path = path
16
+ @comments = []
17
+ load_yaml
18
+ sort_yaml
19
+ write_yaml
20
+ end
21
+
22
+ # Load the YAML file contents into @yaml and store the comments in @comments
23
+ def load_yaml
24
+ self.yaml= File.open(@path, "r") do |f|
25
+ contents = f.read
26
+ contents.split("\n").each do |line|
27
+ @comments.push(line) if line[0] == '#'
28
+ end
29
+ YAML.load(contents)
30
+ end
31
+ end
32
+
33
+ # Recursively sort the contents of @yaml
34
+ def sort_yaml
35
+ self.yaml= self.class.sort(yaml)
36
+ end
37
+
38
+ # Sorts the hash alphabetically by it's keys and then writes the original comments
39
+ # plus the sorted hash to the same path in a valid YAML file.
40
+ def write_yaml
41
+ File.open(@path, "w") do |f|
42
+ if !@comments.empty?
43
+ f.write @comments.join("\n")
44
+ f.write("\n")
45
+ end
46
+ f.write YAML.dump(yaml)
47
+ end
48
+ end
49
+
50
+ # Recursively sorts any hash it's given by key using Array#sort
51
+ def self.sort(obj)
52
+ if obj.is_a? Hash
53
+ sorted = {}
54
+ obj.keys.sort.each { |k| sorted[k] = sort(obj[k]) }
55
+ sorted
56
+ else
57
+ return obj
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ guard 'yamlsort' do
2
+
3
+ watch('config/locales/en.yml')
4
+
5
+ end
@@ -0,0 +1,5 @@
1
+ module Guard
2
+ module YamlsortVersion
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/preview.gif ADDED
Binary file
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class TestFileSorter < MiniTest::Test
4
+
5
+ def setup
6
+ # Create an unsorted hash
7
+ @sample_data = {
8
+ "b" => { "bravo" => true, "zulu" => false, "charlie" => true },
9
+ "c" => { "alpha" => true },
10
+ "a" => { "zulu" => false, "charlie" => true, "bravo" => true },
11
+ }
12
+ end
13
+
14
+ def test_unsorted_file_gets_sorted
15
+ # Write the unsorted hash to a temporary YAML file to test
16
+ test_file = "/tmp/guard-yamlsort-test.yml"
17
+ File.open(test_file, "w") do |f|
18
+ f.write YAML.dump(@sample_data)
19
+ end
20
+
21
+ # Make sure the file was written properly is unsorted
22
+ unsortedYAML = File.open(test_file, "r") do |file|
23
+ YAML.load(file)
24
+ end
25
+
26
+ assert_equal ["b", "c", "a"], unsortedYAML.keys
27
+
28
+ # Create a new FileSorter that loads and writes yaml
29
+ Guard::Yamlsort::FileSorter.new(test_file)
30
+
31
+ # Test sorted YAML
32
+ sortedYAML = File.open(test_file, "r") do |file|
33
+ YAML.load(file)
34
+ end
35
+
36
+ assert_equal ["a", "b", "c"], sortedYAML.keys
37
+ end
38
+
39
+ def test_unsorted_file_gets_sorted_and_retains_header_comments
40
+ # Write some comments and sample data to a temporary YAML file to test
41
+ test_file = "/tmp/guard-yamlsort-test-comments.yml"
42
+ File.open(test_file, "w") do |f|
43
+ f.write "# this is a comment\n"
44
+ f.write "# so is this\n"
45
+ f.write YAML.dump(@sample_data)
46
+ end
47
+
48
+ Guard::Yamlsort::FileSorter.new(test_file)
49
+ fs = File.open(test_file, "r") { |file| file.read }
50
+
51
+ lines = fs.split("\n")
52
+
53
+ assert_equal "#", lines[0][0], "1st comment not written"
54
+ assert_equal "#", lines[1][0], "2nd comment not written"
55
+ end
56
+
57
+ protected
58
+ def test_path(name)
59
+ File.join(Dir.pwd, "test", name)
60
+ end
61
+
62
+ end
@@ -0,0 +1,4 @@
1
+ require 'guard/yamlsort'
2
+ require 'minitest/autorun'
3
+ require 'yaml'
4
+
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-yamlsort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Vendetta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-compat
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.1'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.1'
125
+ description: 'Sorts YAML files alphabetically by key. '
126
+ email:
127
+ - thomas@vendetta.io
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - Gemfile
134
+ - Guardfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - guard-yamlsort.gemspec
139
+ - lib/guard/yamlsort.rb
140
+ - lib/guard/yamlsort/file_sorter.rb
141
+ - lib/guard/yamlsort/templates/Guardfile
142
+ - lib/guard/yamlsort/version.rb
143
+ - preview.gif
144
+ - test/test_file_sorter.rb
145
+ - test/test_helper.rb
146
+ homepage: http://vendetta.io
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.4.5
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Sorts YAML files alphabetically by key
170
+ test_files:
171
+ - test/test_file_sorter.rb
172
+ - test/test_helper.rb
173
+ has_rdoc: