gemfile_sorter 0.1.0

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
+ SHA256:
3
+ metadata.gz: b42ddb24655b1c13a05098eaff8e931fad22ea2d4ac46ac50f0f1d53947563ec
4
+ data.tar.gz: 2adb899022c4752e9beb518d03defc791b21b8ce7a43159dadb66be47b29d434
5
+ SHA512:
6
+ metadata.gz: aff892f614c5c9a7c7216be608d04f9dc0f75ddb2123b7188d93ac8189d9dbf026b83651419fc04166658200c22f8923bef6563f8f7d14f7d5cd2dd693a23b1e
7
+ data.tar.gz: e330ce339495765532ef923f825f1700014e70846cb603f9827dc752674b0600049bfb74e0f261f1c64f2b82600f8f8a07174871ada0e27879f265dda3c49c84
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/.standard.yml ADDED
@@ -0,0 +1,4 @@
1
+ ruby_version: 3.1
2
+
3
+ ignore:
4
+ - "spec/unsorted_samples/**/*"
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-11-20
4
+
5
+ - Initial release
@@ -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 noelrap@hey.com. 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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Noel Rappin
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,51 @@
1
+ # GemfileSorter
2
+
3
+ This gem contains an executable that sorts the contents of a `Gemfile`,
4
+ which is to say that it:
5
+
6
+ * sorts gemfiles by name
7
+ * consolidates duplicate group and source blocks
8
+ * pulls inline declarations of group and source
9
+
10
+ This is _not_ a sophisticated parser of Gemfiles, and if you do anything
11
+ particularly complicated in your `Gemfile` this system will likely get
12
+ flummoxed. Specifically..
13
+
14
+ * This system will be confused if you have individual gem declarations that
15
+ span multiple lines.
16
+ * This system will be confused if you do dynamic, conditional, or loops in your
17
+ `Gemfile`.
18
+ * This system will be confused if a gem declaration has both an inline group and
19
+ an inline source (this one will likely be fixed).
20
+
21
+ I'm sure there are other ways this can fail. If you come across such a way,
22
+ please submit your `Gemfile` as an issue and we'll see what we can do.
23
+
24
+ ## Installation and Usage
25
+
26
+ Installing this as `gem install gemfile_sorter`. You don't need this to be part
27
+ of your repo, the command is:
28
+
29
+ `gemfile_sorter <directory>`
30
+
31
+ The existing `Gemfile` is moved to `Gemfile.unsorted` and the new `Gemfile` is
32
+ put in place. Like, I said, this is still early days, so I'd make sure the new
33
+ Gemfile hasn't lost anything before you use it.
34
+
35
+ ## Development
36
+
37
+ 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.
38
+
39
+ 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).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gemfile_sorter. 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/[USERNAME]/gemfile_sorter/blob/master/CODE_OF_CONDUCT.md).
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
49
+ ## Code of Conduct
50
+
51
+ Everyone interacting in the GemfileSorter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/gemfile_sorter/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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 "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/gemfile_sorter"
4
+ GemfileSorter.execute(ARGV)
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/gemfile_sorter/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "gemfile_sorter"
7
+ spec.version = GemfileSorter::VERSION
8
+ spec.authors = ["Noel Rappin"]
9
+ spec.email = ["noelrap@hey.com"]
10
+
11
+ spec.summary = "Sorts gemfiles based on name, group, and source"
12
+ spec.description = "Sorts gemfiles based on name, group, and source"
13
+ spec.homepage = "https://github.com/noelrappin/gemfile_sorter"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.1.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/noelrappin/gemfile_sorter"
19
+ spec.metadata["changelog_uri"] = "https://github.com/noelrappin/gemfile_sorter/CHANGELOG.md"
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(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) ||
26
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "rake"
34
+ spec.add_development_dependency "rspec"
35
+ spec.add_development_dependency "rspec-collection_matchers"
36
+ spec.add_development_dependency "rspec_junit_formatter"
37
+ spec.add_development_dependency "standard"
38
+ spec.add_development_dependency "simplecov"
39
+ spec.add_development_dependency "yard"
40
+ spec.add_development_dependency "zeitwerk"
41
+
42
+ spec.add_dependency "awesome_print"
43
+ end
@@ -0,0 +1,29 @@
1
+ module GemfileSorter
2
+ class BlockMap
3
+ attr_accessor :blocks
4
+
5
+ def initialize
6
+ @blocks = {}
7
+ end
8
+
9
+ def empty? = blocks.empty?
10
+
11
+ def extra_line_unless_empty
12
+ empty? ? "" : "\n"
13
+ end
14
+
15
+ def add_gem(gem)
16
+ add(gem.inline_result, line: gem.inline_line, line_number: gem.line_number)
17
+ end
18
+
19
+ def add(names, line:, line_number:)
20
+ block = block(names, line:, line_number:)
21
+ blocks[block.normalized_name] ||= block
22
+ end
23
+
24
+ def to_s
25
+ extra_line_unless_empty +
26
+ blocks.values.sort.map { _1.to_s }.join.delete_prefix("\n")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ module GemfileSorter
2
+ class BlockOfGems
3
+ attr_accessor :gems, :line, :line_number
4
+
5
+ def initialize(name, line:, line_number:)
6
+ @gems = {}
7
+ @line = line
8
+ @line_number = line_number
9
+ end
10
+
11
+ def container? = false
12
+
13
+ def inside_block_map = nil
14
+
15
+ def add(gem)
16
+ @gems[gem.name] = gem
17
+ end
18
+
19
+ def gem_string
20
+ gems.values.sort.map { _1.to_s }.join.delete_prefix("\n")
21
+ end
22
+
23
+ def leading_spaces
24
+ line.match(/(\A *)/)[1]
25
+ end
26
+
27
+ def to_s
28
+ "\n" +
29
+ line +
30
+ gem_string.delete_suffix(",") +
31
+ (inside_block_map&.to_s || "") +
32
+ "#{leading_spaces}end\n"
33
+ end
34
+
35
+ def empty? = gems.empty?
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ module GemfileSorter
2
+ class CommentGroup
3
+ include Enumerable
4
+
5
+ attr_accessor :comments
6
+
7
+ def initialize(*comments)
8
+ @comments = comments.flatten
9
+ end
10
+
11
+ def to_s
12
+ comments.map { _1.to_s }.join
13
+ end
14
+
15
+ def contents = comments
16
+
17
+ def add(comment)
18
+ self.comments += comment.comments
19
+ end
20
+
21
+ def <<(comment) = add(comment)
22
+
23
+ def empty? = comments.empty?
24
+
25
+ def each = comments.each
26
+
27
+ def extra_line_unless_empty
28
+ empty? ? "" : "\n"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,89 @@
1
+ module GemfileSorter
2
+ class GemExtractor
3
+ attr_reader :gem, :key
4
+ attr_accessor :running_pointer, :result
5
+
6
+ def initialize(gem, key)
7
+ @gem = gem
8
+ @key = key
9
+ @running_pointer = nil
10
+ @result = []
11
+ end
12
+
13
+ def options = gem.options
14
+
15
+ def starting_index = options.find_index { _1.match(/#{key}/) }
16
+
17
+ def match? = !!starting_index
18
+
19
+ def running_option = options[running_pointer]
20
+
21
+ def increment_pointer = self.running_pointer += 1
22
+
23
+ def initial_result
24
+ value = running_option
25
+ if value.match?("=>")
26
+ increment_pointer
27
+ value = running_option
28
+ end
29
+ value
30
+ end
31
+
32
+ def extract_multiple_results
33
+ return unless result.first&.start_with?("[")
34
+ loop do
35
+ increment_pointer
36
+ result << running_option
37
+ break if result.last&.end_with?("]")
38
+ end
39
+ end
40
+
41
+ def extraction_start
42
+ gem.line.index(options[starting_index]) - 2
43
+ end
44
+
45
+ def extraction_end
46
+ size = (result.last&.size || 0)
47
+ size += 1 unless symbol_result?
48
+ gem.line.index(result.last) + (size || 0) - 1
49
+ end
50
+
51
+ def updated_line
52
+ return gem.line if result.empty?
53
+ result = gem.line
54
+ result[extraction_start..extraction_end] = ""
55
+ " #{result}"
56
+ end
57
+
58
+ def result_string
59
+ result.map do |str|
60
+ str.to_s
61
+ .delete_prefix(":")
62
+ .delete_prefix("[:")
63
+ .delete_suffix("]")
64
+ end.sort.join(", ")
65
+ end
66
+
67
+ def extract_initial_result
68
+ self.running_pointer = starting_index + 1
69
+ result << initial_result
70
+ end
71
+
72
+ def extract
73
+ return unless match?
74
+ extract_initial_result
75
+ extract_multiple_results
76
+ result_string
77
+ end
78
+
79
+ def symbol_result? = result.last&.include?(":")
80
+
81
+ def annotated_result
82
+ symbol_result? ? ":#{result_string}" : %("#{result_string}")
83
+ end
84
+
85
+ def resulting_first_line
86
+ "#{key} #{annotated_result} do\n"
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,7 @@
1
+ module GemfileSorter
2
+ class Groups < BlockMap
3
+ def block(names, line:, line_number:)
4
+ Line::Group.new(names, line:, line_number:)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module GemfileSorter
2
+ module Line
3
+ class BlankLine
4
+ def to_s = "\n"
5
+
6
+ def comments = [self]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module GemfileSorter
2
+ module Line
3
+ class Comment
4
+ attr_accessor :line, :line_number
5
+
6
+ def initialize(line: nil, line_number: nil)
7
+ @line = line
8
+ @line_number = line_number
9
+ end
10
+
11
+ def comments = [self]
12
+
13
+ def to_s = line
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,54 @@
1
+ module GemfileSorter
2
+ module Line
3
+ class Gem
4
+ include Comparable
5
+ attr_reader :group_extractor, :source_extractor
6
+ attr_accessor :line, :name, :options, :line_number, :comment_group, :parsed
7
+
8
+ def initialize(name, comment_group = nil, *options, line: "", line_number: nil)
9
+ @line = line.rstrip
10
+ @name = name
11
+ @options = options
12
+ @line_number = line_number
13
+ @comment_group = comment_group || CommentGroup.new
14
+ @group_extractor = GemExtractor.new(self, "group")
15
+ @source_extractor = GemExtractor.new(self, "source")
16
+ @parsed = false
17
+ end
18
+
19
+ def to_s
20
+ (comment_group.empty? ? "" : "\n") + comment_group.to_s + parsed_line + "\n"
21
+ end
22
+
23
+ def parsed_line
24
+ return line unless inline_match?
25
+ inline_match.updated_line
26
+ end
27
+
28
+ def inline_match
29
+ extract unless parsed
30
+ [group_extractor, source_extractor]
31
+ .find { _1.match? }
32
+ end
33
+
34
+ def inline_match? = !!inline_match
35
+
36
+ def extract
37
+ self.parsed = true
38
+ group_extractor.extract || source_extractor.extract
39
+ end
40
+
41
+ def inline_kind = inline_match&.key
42
+
43
+ def inline_result = inline_match&.result_string
44
+
45
+ def inline_line = inline_match.resulting_first_line
46
+
47
+ def <=>(other)
48
+ name <=> other.name
49
+ rescue
50
+ raise InvalidLineComparisonError
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,27 @@
1
+ module GemfileSorter
2
+ module Line
3
+ class Group < BlockOfGems
4
+ attr_accessor :names, :sources
5
+
6
+ def initialize(*names, line:, line_number:)
7
+ @names = names
8
+ @sources = Sources.new
9
+ super(normalized_name, line:, line_number:)
10
+ end
11
+
12
+ def container? = true
13
+
14
+ def inside_block_map = sources
15
+
16
+ def normalized_name
17
+ Array(names).flatten.map { _1.delete_prefix(":") }.sort.join(", ")
18
+ end
19
+
20
+ def <=>(other)
21
+ normalized_name <=> other.normalized_name
22
+ rescue
23
+ raise InvalidLineComparisonError
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module GemfileSorter
2
+ module Line
3
+ class Source < BlockOfGems
4
+ attr_accessor :name, :groups
5
+
6
+ def initialize(name, line:, line_number:)
7
+ @name = name
8
+ @groups = Groups.new
9
+ super(name, line:, line_number:)
10
+ end
11
+
12
+ def container? = true
13
+
14
+ def inside_block_map = groups
15
+
16
+ def normalized_name = name
17
+
18
+ def <=>(other)
19
+ name <=> other.name
20
+ rescue
21
+ raise InvalidLineComparisonError
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,118 @@
1
+ module GemfileSorter
2
+ class Parser
3
+ attr_reader :filename, :groups, :block_stack, :top_level_gems, :sources
4
+ attr_accessor :current_comments, :leading_comments
5
+
6
+ def self.parse(filename)
7
+ parser = new(filename)
8
+ parser.parse
9
+ parser.output
10
+ end
11
+
12
+ def initialize(filename)
13
+ @filename = filename
14
+ @top_level_gems = BlockOfGems.new("top_level", line: "", line_number: nil)
15
+ @leading_comments = CommentGroup.new
16
+ @current_comments = CommentGroup.new
17
+ @block_stack = [@top_level_gems]
18
+ @groups = Groups.new
19
+ @sources = Sources.new
20
+ end
21
+
22
+ def contents
23
+ @contents ||= File.readlines(filename)
24
+ end
25
+
26
+ def parse
27
+ contents.each_with_index do |line, index|
28
+ parse_line(line, index + 1)
29
+ end
30
+ end
31
+
32
+ # handle % delimiters?
33
+ def split_line(line)
34
+ line.strip.split(/\s|\(|\)|,/)
35
+ .map { _1.gsub(/\A"|"\Z|\A'|'\Z/, "") }
36
+ .map { _1.strip }
37
+ .reject { _1.empty? }
38
+ end
39
+
40
+ def output
41
+ leading_comments.to_s +
42
+ top_level_gems.gem_string +
43
+ groups.to_s +
44
+ sources.to_s +
45
+ current_comments.extra_line_unless_empty +
46
+ current_comments.to_s
47
+ end
48
+
49
+ def parse_line(line, line_number)
50
+ case split_line(line)
51
+ in ["gem", name, *options]
52
+ handle_gem(name, *options, line:, line_number:)
53
+ in ["#", "gem", name, *options]
54
+ handle_gem(name, *options, line:, line_number:)
55
+ in ["#", *_rest]
56
+ handle_comment(*_rest, line:, line_number:)
57
+ in ["group", *names, "do"]
58
+ handle_block(:groups, names, line:, line_number:)
59
+ in ["source", name, "do"]
60
+ handle_block(:sources, name, line:, line_number:)
61
+ in ["end"]
62
+ handle_end
63
+ in []
64
+ handle_blank_line
65
+ else
66
+ # treat other lines like comments
67
+ handle_comment(*_rest, line:, line_number:)
68
+ end
69
+ end
70
+
71
+ def current_block_map_container
72
+ block_stack.last.container? ? block_stack.last : self
73
+ end
74
+
75
+ def block_for_gem(gem)
76
+ group = add_inline_gem(gem)
77
+ group || block_stack.last
78
+ end
79
+
80
+ def add_inline_gem(gem)
81
+ return unless gem.inline_match?
82
+ block_map = (gem.inline_kind == "group") ? groups : sources
83
+ block_map.add_gem(gem)
84
+ end
85
+
86
+ def handle_gem(name, *options, line:, line_number:)
87
+ result = Line::Gem.new(name, current_comments, *options, line_number:, line:)
88
+ block_for_gem(result).add(result)
89
+ self.current_comments = CommentGroup.new
90
+ result
91
+ end
92
+
93
+ def handle_block(block_map_name, name, line:, line_number:)
94
+ block = current_block_map_container.send(block_map_name).add(name, line:, line_number:)
95
+ block_stack.push(block)
96
+ end
97
+
98
+ def handle_end
99
+ block_stack.pop
100
+ end
101
+
102
+ def handle_comment(*_rest, line:, line_number:)
103
+ result = Line::Comment.new(line:, line_number:)
104
+ current_comments << result
105
+ result
106
+ end
107
+
108
+ def handle_blank_line
109
+ if top_level_gems.empty?
110
+ leading_comments << current_comments unless current_comments.empty?
111
+ leading_comments << GemfileSorter::Line::BlankLine.new unless leading_comments.empty?
112
+ self.current_comments = CommentGroup.new
113
+ elsif !current_comments.empty?
114
+ current_comments << GemfileSorter::Line::BlankLine.new
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,7 @@
1
+ module GemfileSorter
2
+ class Sources < BlockMap
3
+ def block(name, line:, line_number:)
4
+ Line::Source.new(name, line:, line_number:)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GemfileSorter
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "zeitwerk"
5
+ loader = Zeitwerk::Loader.for_gem
6
+ loader.setup
7
+
8
+ module GemfileSorter
9
+ class InvalidLineComparisonError < StandardError; end
10
+
11
+ def self.sort_gemfile(directory_name = ".")
12
+ Parser.parse("#{directory_name}/Gemfile")
13
+ end
14
+
15
+ def self.execute(directory_name = ".")
16
+ result = Parser.parse("#{directory_name}/Gemfile")
17
+ FileUtils.mv("#{directory_name}/Gemfile", "#{directory_name}/Gemfile.unsorted")
18
+ File.write("#{directory_name}/Gemfile", result)
19
+ end
20
+ end
21
+
22
+ loader.eager_load
@@ -0,0 +1,4 @@
1
+ module GemfileSorter
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,198 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gemfile_sorter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Noel Rappin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rspec
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: rspec-collection_matchers
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
+ - !ruby/object:Gem::Dependency
56
+ name: rspec_junit_formatter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: standard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: zeitwerk
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: awesome_print
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Sorts gemfiles based on name, group, and source
140
+ email:
141
+ - noelrap@hey.com
142
+ executables:
143
+ - gemfile_sorter
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".rspec"
148
+ - ".ruby-version"
149
+ - ".standard.yml"
150
+ - CHANGELOG.md
151
+ - CODE_OF_CONDUCT.md
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - exe/gemfile_sorter
156
+ - gemfile_sorter.gemspec
157
+ - lib/gemfile_sorter.rb
158
+ - lib/gemfile_sorter/block_map.rb
159
+ - lib/gemfile_sorter/block_of_gems.rb
160
+ - lib/gemfile_sorter/comment_group.rb
161
+ - lib/gemfile_sorter/gem_extractor.rb
162
+ - lib/gemfile_sorter/groups.rb
163
+ - lib/gemfile_sorter/line/blank_line.rb
164
+ - lib/gemfile_sorter/line/comment.rb
165
+ - lib/gemfile_sorter/line/gem.rb
166
+ - lib/gemfile_sorter/line/group.rb
167
+ - lib/gemfile_sorter/line/source.rb
168
+ - lib/gemfile_sorter/parser.rb
169
+ - lib/gemfile_sorter/sources.rb
170
+ - lib/gemfile_sorter/version.rb
171
+ - sig/gemfile_sorter.rbs
172
+ homepage: https://github.com/noelrappin/gemfile_sorter
173
+ licenses:
174
+ - MIT
175
+ metadata:
176
+ homepage_uri: https://github.com/noelrappin/gemfile_sorter
177
+ source_code_uri: https://github.com/noelrappin/gemfile_sorter
178
+ changelog_uri: https://github.com/noelrappin/gemfile_sorter/CHANGELOG.md
179
+ post_install_message:
180
+ rdoc_options: []
181
+ require_paths:
182
+ - lib
183
+ required_ruby_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: 3.1.0
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ requirements: []
194
+ rubygems_version: 3.4.19
195
+ signing_key:
196
+ specification_version: 4
197
+ summary: Sorts gemfiles based on name, group, and source
198
+ test_files: []