rebundler 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b4fcf003a478f6954d9b00ceb4be97ad3a80676ca7590107f645614784662402
4
+ data.tar.gz: 923aef754252bfedb100489f1a3d574fb81c955857503e149d328443b4cf8682
5
+ SHA512:
6
+ metadata.gz: 68cc4c80cb9858c9ff1918d241dcf61b3fb2eb85530ce3b592fff002992f3e28800d8752bffbfe6ea2ebcbbdb941b8ccf5c730752c3a56f4b5fb7396a4c017b3
7
+ data.tar.gz: bf8bcebd8b0b5bfb92833576968914cbbcb66205bcb7eb5d992cb8feeaa02c13eb2a33642b1322dc921bdb2d218023559b94c826015b9702aa7d6b2c1e35d80f
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ plugins:
2
+ - rubocop-minitest
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.3
7
+ NewCops: enable
8
+
9
+ Style/StringLiterals:
10
+ EnforcedStyle: double_quotes
11
+
12
+ Style/StringLiteralsInInterpolation:
13
+ EnforcedStyle: double_quotes
14
+
15
+ Metrics:
16
+ Enabled: false
17
+
18
+ Style/Documentation:
19
+ Enabled: false
20
+
21
+ Style/StringConcatenation:
22
+ Enabled: false
23
+
24
+ Layout/LineLength:
25
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-11-17
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dennis Paagman
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,72 @@
1
+ # Rebundler
2
+
3
+ Rebundler automatically reorders and annotes your Gemfile.
4
+
5
+ ## Why would you want that?
6
+
7
+ - **No more manual ordering of gems.** Let's admit that you usually just put them somewhere vaguely adjacent. Eventually your Gemfile will be a mess.
8
+ - **No bike shedding about the structure of your Gemfile.** Rebundler will take care of it.
9
+ - **More context on what gems do.** Especially with all the funky gem names in our community (which is fun!) it's not entirely clear from most names alone what it does. Rebundler will add a comment with the gem's description.
10
+
11
+ ## Example
12
+
13
+ This is a real life example from my own project. That looks a lot better, doesn't it?
14
+
15
+ | Before | After |
16
+ | ----------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
17
+ | ![image](https://github.com/user-attachments/assets/42a76744-111b-4f73-bc62-8723637e6655) | ![image](https://github.com/user-attachments/assets/3ea6c70e-2239-4511-9040-c4db58203ec4) |
18
+
19
+ ## Known limitations
20
+
21
+ - **Existing comments will be lost.** At this moment Rebundler does not persist existing comments.
22
+ - **Probably does not work with all possible Gemfile configurations.** It is designed to work with the most common setups right now. If you encounter an issue, please open an issue on GitHub. I strive to support each sensible configuration.
23
+
24
+ ## Installation
25
+
26
+ There are two ways to install Rebundler.
27
+
28
+ ### 1. Automatic mode
29
+
30
+ > [!NOTE]
31
+ > This does not seem to work properly yet, I need to dig into why later.
32
+
33
+ Simply add them gem to your Gemfile. the location does not matter as it will be resorted immediately.
34
+
35
+ ```ruby
36
+ gem "rebundler"
37
+ ```
38
+
39
+ This installs a Bundler plugin that automatically runs after installing gems. You should see a message in your terminal after running `bundle`:
40
+
41
+ ```sh
42
+ $ bundle
43
+
44
+ ...
45
+
46
+ Reordering and annotating Gemfile...
47
+ Bundle complete! 10 Gemfile dependencies, 45 gems now installed.
48
+ ```
49
+
50
+ ### 2. Manual mode
51
+
52
+ ```ruby
53
+ gem "rebundler", require: false
54
+ ```
55
+
56
+ This does **not** load the plugin and means you have to run rebundler yourself by running the `rebundle` command:
57
+
58
+ ```sh
59
+ $ bundle exec rebundle
60
+ ```
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dennispaagman/rebundler.
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
data/exe/rebundle ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/rebundler"
5
+
6
+ file = ARGV[0] || (Dir.pwd + "/Gemfile")
7
+
8
+ puts "Reordering and annotating Gemfile..."
9
+
10
+ begin
11
+ # Load all gems in the Gemfile so we can use them to grab the summary instead of
12
+ # having to hit rubygems.org
13
+ Bundler.setup
14
+ parser = Rebundler::Parser.new(file)
15
+ File.write(file, parser.write!)
16
+ rescue StandardError => e
17
+ puts "Error parsing Gemfile: #{e.message}"
18
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler"
4
+
5
+ module Bundler
6
+ class Rebundle < Bundler::Plugin::API
7
+ command "rebundle"
8
+
9
+ hook "after-install-all" do
10
+ parse_and_write_gemfile!
11
+ end
12
+
13
+ def exec(command, args)
14
+ send command, args
15
+ end
16
+
17
+ def self.parse_and_write_gemfile!
18
+ Bundler.ui.info "Reordering and annotating Gemfile..."
19
+
20
+ file = Bundler.root.to_s + "/Gemfile"
21
+
22
+ begin
23
+ # Load all gems in the Gemfile so we can use them to grab the summary instead of
24
+ # having to hit rubygems.org
25
+ Bundler.setup
26
+
27
+ parser = ::Rebundler::Parser.new(file)
28
+ File.write(file, parser.write!)
29
+ rescue StandardError => e
30
+ Bundler.ui.error "Error parsing Gemfile: #{e.message}"
31
+ raise Bundler::PluginError, e
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rebundler"
4
+
5
+ path = File.expand_path(__dir__)
6
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| import f }
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ module Rebundler
6
+ class Parser
7
+ SORTABLE_NODES = %i[plugin gem].freeze
8
+ BLOCK_NODES = %i[gemspec git group path platforms ruby source].freeze
9
+
10
+ attr_reader :file, :before, :sets, :frozen_string_literal
11
+
12
+ def initialize(file)
13
+ @file = file
14
+ @frozen_string_literal = false
15
+ @before = []
16
+ @sets = [
17
+ { node: nil, plugin: [], gem: [] } # all gems outside a specific block (group, source, etc) will end up here
18
+ ]
19
+ end
20
+
21
+ def write!
22
+ parse!
23
+
24
+ buffer = []
25
+
26
+ buffer << "# frozen_string_literal: true" if frozen_string_literal
27
+
28
+ before.each do |node|
29
+ buffer << node_to_s(node)
30
+ end
31
+
32
+ sets.sort_by { node_to_s(_1[:node]) || "" }.each do |set|
33
+ set_node = set[:node]
34
+ set_buffer = []
35
+
36
+ set_buffer << [node_to_s(set_node), set_node.block.opening].compact.join(" ") if set_node
37
+
38
+ set_buffer << SORTABLE_NODES.map do |node_type|
39
+ nodes = set[node_type].sort_by { _1[:name] }
40
+
41
+ nodes.map do |gem|
42
+ line = +""
43
+ line << " " if set_node
44
+ line << node_to_s(gem[:node])
45
+ line << " # #{gem[:summary]}" if gem[:summary]
46
+
47
+ line
48
+ end.join("\n")
49
+ end.reject(&:empty?).join("\n\n")
50
+
51
+ set_buffer << set_node.block.closing if set_node&.block
52
+
53
+ buffer << set_buffer.join("\n")
54
+ end
55
+
56
+ buffer.reject(&:empty?).join("\n\n") + "\n"
57
+ end
58
+
59
+ private
60
+
61
+ def parse!
62
+ parsed = Prism.parse(File.read(file))
63
+
64
+ @frozen_string_literal = parsed.magic_comments.map(&:key).include?("frozen_string_literal")
65
+
66
+ parsed.value.compact_child_nodes[0].compact_child_nodes.each do |node|
67
+ case node.type
68
+ when :call_node
69
+ case node.name
70
+ when *SORTABLE_NODES
71
+ @sets[0][node.name] << parse_gem(node)
72
+ when *BLOCK_NODES
73
+ if node.block
74
+ children = node.block.body.compact_child_nodes
75
+
76
+ set = { node:, plugin: [], gem: [] }
77
+
78
+ children.each do |child_node|
79
+ parsed_node = parse_gem(child_node)
80
+
81
+ set[child_node.name] << parsed_node if parsed_node
82
+ end
83
+
84
+ @sets << set
85
+ else
86
+ @before << node
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ def find_gem(name)
94
+ summary = find_loaded_gem_summary(name) || find_external_gem_summary(name)
95
+
96
+ { name:, summary: }
97
+ end
98
+
99
+ def find_loaded_gem_summary(name)
100
+ gem = Gem::Specification.find_by_name(name)
101
+
102
+ gem.summary
103
+ rescue Gem::MissingSpecError
104
+ nil
105
+ end
106
+
107
+ def find_external_gem_summary(name)
108
+ spec = Gem::SpecFetcher.fetcher.spec_for_dependency Gem::Dependency.new(name)
109
+
110
+ return unless spec
111
+ return if spec == [[], []]
112
+
113
+ spec.first.first.first.summary
114
+ end
115
+
116
+ def node_to_s(node)
117
+ return if node.nil?
118
+
119
+ case node.type
120
+ when :call_node
121
+ [node.name, node_to_s(node.arguments)].compact.join(" ")
122
+ when :keyword_hash_node
123
+ node.elements.map do |element|
124
+ key = node_to_s(element.key)
125
+ value = node_to_s(element.value)
126
+
127
+ "#{key} #{value}"
128
+ end
129
+ when :symbol_node
130
+ "#{node.opening}#{node.value}#{node.closing}"
131
+ when :string_node
132
+ "#{node.opening}#{node.content}#{node.closing}"
133
+ when :array_node
134
+ separator = node.opening == "[" ? ", " : " "
135
+
136
+ node.opening + node.elements.map do |element|
137
+ node_to_s(element)
138
+ end.join(separator) + node.closing
139
+ when :true_node
140
+ "true"
141
+ when :false_node
142
+ "false"
143
+ when :arguments_node
144
+ return if node.child_nodes.empty?
145
+
146
+ node.child_nodes.map { node_to_s(_1) }.join(", ")
147
+ else
148
+ raise NotImplementedError, "Unknown node type: #{node.type}"
149
+ end
150
+ end
151
+
152
+ def parse_gem(node)
153
+ return unless node.type == :call_node && SORTABLE_NODES.include?(node.name)
154
+
155
+ gem_name = node.arguments.child_nodes[0].content
156
+
157
+ find_gem(gem_name).merge(node:)
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rebundler
4
+ VERSION = "0.1.0"
5
+ end
data/lib/rebundler.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rebundler/version"
4
+ require_relative "rebundler/parser"
5
+
6
+ require_relative "bundler/rebundle"
7
+
8
+ module Rebundler
9
+ class Error < StandardError; end
10
+ # Your code goes here...
11
+ end
data/plugins.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lib/rebundler"
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rebundler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dennis Paagman
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ email:
13
+ - dennis@paagman.dev
14
+ executables:
15
+ - rebundle
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".rubocop.yml"
20
+ - CHANGELOG.md
21
+ - LICENSE.txt
22
+ - README.md
23
+ - Rakefile
24
+ - exe/rebundle
25
+ - lib/bundler/rebundle.rb
26
+ - lib/rebundler.rb
27
+ - lib/rebundler/Rakefile
28
+ - lib/rebundler/parser.rb
29
+ - lib/rebundler/version.rb
30
+ - plugins.rb
31
+ homepage: https://github.com/dennispaagman/rebundler
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/dennispaagman/rebundler
36
+ source_code_uri: https://github.com/dennispaagman/rebundler
37
+ changelog_uri: https://github.com/dennispaagman/rebundler/blob/main/CHANGELOG.md
38
+ rubygems_mfa_required: 'true'
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.3.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.6.7
54
+ specification_version: 4
55
+ summary: Rebundler makes your Gemfile look good.
56
+ test_files: []