gem_sorter 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: 5b1e869f1a6001e39cb365ed94c07b7ae077002230b6b8310e3931b5021f96a6
4
+ data.tar.gz: 5d1dcf0f273f0c410fc7f4aa1221d0e61a7fa8ec8f86e20f01d46f8c601a06ab
5
+ SHA512:
6
+ metadata.gz: 1bdc7618f04fd23841944fb2daf70255b3e9fbf83a780dfacf9a82f0b0ccc08dafbe5583bfeebffc8d6424374b7e9e9819ade35884b2cefd656d0aa0c8d5698a
7
+ data.tar.gz: 234b92e7d4829520db13b04707f9e6c5fdfebe6bde74a25eac7cd06ea8e435a173567287b32654d82c1c28ee9db4ee41e052f75376b81d603eee67787d17b300
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # GemSorter
2
+
3
+ GemSorter is a simple gem to sort the contents of your Gemfile alphabetically while preserving comments and group structure. It helps maintain a clean and organized Gemfile.
4
+
5
+ ## Features
6
+ * Sorts gems alphabetically.
7
+ * Preserves comments and their association with gems.
8
+ * Maintains group structure in the Gemfile.
9
+ * Optionally creates a backup of the original Gemfile.
10
+
11
+ ## Installation
12
+ Add the gem to your project's `Gemfile`:
13
+
14
+ ```ruby
15
+ gem "gem_sorter"
16
+ ```
17
+
18
+ ## Usage
19
+ Once installed, you can use the provided Rake task to sort your Gemfile:
20
+
21
+ ```bash
22
+ rake gemfile:sort
23
+ ```
24
+
25
+ ### Options
26
+ * `backup`: Pass `true` to create a backup of your Gemfile as `Gemfile.old` before sorting.
27
+
28
+ Example:
29
+
30
+ ```bash
31
+ rake gemfile:sort[true]
32
+ ```
33
+
34
+ This will sort your Gemfile and create a backup.
35
+
36
+ ## Example
37
+ ### Input Gemfile
38
+ ```ruby
39
+ source "https://rubygems.org"
40
+
41
+ # Framework
42
+ gem "rails"
43
+ # Web server
44
+ gem "puma"
45
+
46
+ group :development do
47
+ gem "pry"
48
+ gem "dotenv-rails"
49
+ end
50
+ ```
51
+
52
+ ### Output Gemfile
53
+ ```ruby
54
+ source "https://rubygems.org"
55
+
56
+ # Web server
57
+ gem "puma"
58
+ # Framework
59
+ gem "rails"
60
+
61
+ group :development do
62
+ gem "dotenv-rails"
63
+ gem "pry"
64
+ end
65
+ ```
66
+
67
+ ## Development
68
+ To contribute to this project:
69
+
70
+ 1. Clone the repository:
71
+ ```bash
72
+ git clone https://github.com/renan-garcia/gem_sorter.git
73
+ ```
74
+ 2. Navigate to the project directory:
75
+ ```bash
76
+ cd gem_sorter
77
+ ```
78
+ 3. Install dependencies:
79
+ ```bash
80
+ bundle install
81
+ ```
82
+ 4. Run the tests:
83
+ ```bash
84
+ rspec
85
+ ```
86
+
87
+ ## Contributing
88
+ We welcome contributions! Here's how you can help:
89
+
90
+ 1. Fork the repository.
91
+ 2. Create a feature branch:
92
+ ```bash
93
+ git checkout -b feature/my-new-feature
94
+ ```
95
+ 3. Commit your changes:
96
+ ```bash
97
+ git commit -m "Add a new feature"
98
+ ```
99
+ 4. Push to the branch:
100
+ ```bash
101
+ git push origin feature/my-new-feature
102
+ ```
103
+ 5. Open a pull request.
104
+
105
+ ## Acknowledgments
106
+ Special thanks to the Ruby community for their guidance and support!
@@ -0,0 +1,3 @@
1
+ module GemfileSorter
2
+ VERSION = '0.1.0'
3
+ end
data/lib/gem_sorter.rb ADDED
@@ -0,0 +1,98 @@
1
+ # lib/gem_sorter.rb
2
+
3
+ load File.expand_path('tasks/gem_sorter.rake', __dir__) if defined?(Rake)
4
+
5
+ module GemSorter
6
+ class Sorter
7
+ def initialize(filepath)
8
+ @filepath = filepath
9
+ @content = File.read(filepath)
10
+ end
11
+
12
+ def sort
13
+ parts = @content.split(/^group/)
14
+ main_section = parts.shift
15
+ group_sections = parts
16
+
17
+ source_line, gems = process_main_section(main_section)
18
+
19
+ sorted_gems = sort_gem_blocks(gems)
20
+
21
+ result = []
22
+ result << source_line
23
+ result << ''
24
+ result.concat(sorted_gems)
25
+ result << ''
26
+
27
+ group_sections.each do |section|
28
+ group_gems = process_group_section(section)
29
+ result << "group#{section.split("\n").first}"
30
+ result.concat(sort_gem_blocks(group_gems).map { |line| " #{line}" })
31
+ result << 'end'
32
+ result << ''
33
+ end
34
+
35
+ result.join("\n")
36
+ end
37
+
38
+ private
39
+
40
+ def process_main_section(section)
41
+ lines = section.split("\n").map(&:strip).reject(&:empty?)
42
+ source_line = lines.shift
43
+
44
+ gems = []
45
+ current_comments = []
46
+
47
+ lines.each do |line|
48
+ if line.start_with?('#')
49
+ current_comments << line
50
+ elsif line.start_with?('gem')
51
+ gems << {
52
+ comments: current_comments,
53
+ gem_line: line
54
+ }
55
+ current_comments = []
56
+ end
57
+ end
58
+
59
+ [source_line, gems]
60
+ end
61
+
62
+ def process_group_section(section)
63
+ lines = section.split("\n").map(&:strip).reject(&:empty?)
64
+ lines = lines[1...-1]
65
+
66
+ gems = []
67
+ current_comments = []
68
+
69
+ lines.each do |line|
70
+ if line.start_with?('#')
71
+ current_comments << line
72
+ elsif line.start_with?('gem')
73
+ gems << {
74
+ comments: current_comments,
75
+ gem_line: line
76
+ }
77
+ current_comments = []
78
+ end
79
+ end
80
+
81
+ gems
82
+ end
83
+
84
+ def sort_gem_blocks(gems)
85
+ sorted = gems.sort_by do |gem_block|
86
+ gem_block[:gem_line].match(/gem\s*"([^"]+)"/)[1].downcase
87
+ end
88
+
89
+ result = []
90
+ sorted.each do |gem_block|
91
+ result.concat(gem_block[:comments]) unless gem_block[:comments].empty?
92
+ result << gem_block[:gem_line]
93
+ end
94
+
95
+ result
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,25 @@
1
+ require 'gem_sorter'
2
+
3
+ namespace :gemfile do
4
+ desc 'Sort gems in Gemfile alphabetically'
5
+ task :sort, [:backup] do |_t, args|
6
+ args.with_defaults(backup: 'false')
7
+ gemfile_path = 'Gemfile'
8
+
9
+ if File.exist?(gemfile_path)
10
+ if args.backup.downcase == 'true'
11
+ FileUtils.cp(gemfile_path, "#{gemfile_path}.old")
12
+ puts 'Original Gemfile backed up as Gemfile.old'
13
+ end
14
+
15
+ sorter = Gem::Sorter.new(gemfile_path)
16
+ sorted_content = sorter.sort
17
+
18
+ File.write(gemfile_path, sorted_content)
19
+
20
+ puts 'Gemfile sorted successfully!'
21
+ else
22
+ puts 'Error: Gemfile not found in current directory'
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gem_sorter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Renan Garcia
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rake
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '13.0'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '13.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ description: A simple gem to sort the gems in your Gemfile while preserving comments
41
+ and groups
42
+ email:
43
+ - email@renangarcia.me
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - lib/gem_sorter.rb
50
+ - lib/gem_sorter/version.rb
51
+ - lib/tasks/gem_sorter.rake
52
+ homepage: https://github.com/renan-garcia/gem_sorter
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://github.com/renan-garcia/gem_sorter
57
+ source_code_uri: https://github.com/renan-garcia/gem_sorter
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.6.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.6.2
73
+ specification_version: 4
74
+ summary: Sort gems in the Gemfile alphabetically
75
+ test_files: []