gem_sorter 0.4.1 → 0.5.2
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 +4 -4
- data/README.md +36 -4
- data/lib/gem_sorter/version.rb +1 -1
- data/lib/gem_sorter.rb +47 -8
- data/lib/task_config.rb +8 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a14433575929ce85e046a16391ef12ca535aaf889e270311831d6afc84d39c7
|
4
|
+
data.tar.gz: efffe4275cca1eb81d9f824e858945748070137c57d0c6872ec66b985aca7125
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcc7cc1833d9c17b4b04e4a0442fa8e09630a48cce2eae2bf316726f4f267ce77e0d23e87b60783d776c1da7433ebb5ca06f0eaf72f8efb33c520628f74af27d
|
7
|
+
data.tar.gz: 3c5b1e7ae3724f8b3143daac366df453c9196a92b5123e6972a9ea2713d32a9f46a161625a5c817f9a074896a7d2c91a90cb78fb73b370db5e4ed8f43d47f496
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
<img src="https://i.imgur.com/WHOyL9W.png" width="150" alt="GemSorter Logo">
|
3
3
|
</p>
|
4
4
|
|
5
|
+
[](https://badge.fury.io/rb/gem_sorter)
|
6
|
+
|
5
7
|
# GemSorter
|
6
8
|
|
7
9
|
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.
|
@@ -13,6 +15,7 @@ GemSorter is a simple gem to sort the contents of your Gemfile alphabetically wh
|
|
13
15
|
* Optionally creates a backup of the original Gemfile.
|
14
16
|
* Update the comments of the gems based on their descriptions.
|
15
17
|
* Optionally converts single quotes to double quotes in gem declarations.
|
18
|
+
* Optionally removes version constraints from gems while preserving other parameters.
|
16
19
|
|
17
20
|
## Installation
|
18
21
|
Add the gem to your project's `Gemfile`:
|
@@ -44,13 +47,14 @@ rake -r gem_sorter gemfile:sort
|
|
44
47
|
* `update_comments`: Pass `true` to update the comments of the gems based on their descriptions.
|
45
48
|
* `update_versions`: Pass `true` to update the versions of the gems based on the lockfile.
|
46
49
|
* `use_double_quotes`: Pass `true` to convert single quotes to double quotes in gem declarations.
|
50
|
+
* `remove_versions`: Pass `true` to remove version constraints from gems while preserving other parameters like `require` or `platforms`. This option takes precedence over `update_versions` if both are enabled.
|
47
51
|
|
48
52
|
Example:
|
49
53
|
|
50
54
|
```bash
|
51
|
-
rake gemfile:sort[true,true,true,true]
|
55
|
+
rake gemfile:sort[true,true,true,true,true]
|
52
56
|
```
|
53
|
-
This will sort your Gemfile, create a backup, update comments and versions,
|
57
|
+
This will sort your Gemfile, create a backup, update comments and versions, convert single quotes to double quotes, and remove version constraints.
|
54
58
|
|
55
59
|
### Options File
|
56
60
|
Create a file in the root of your project called `gem_sorter.yml`
|
@@ -58,6 +62,7 @@ Create a file in the root of your project called `gem_sorter.yml`
|
|
58
62
|
* `update_comments`: Pass `true` to update the comments of the gems based on their descriptions.
|
59
63
|
* `update_versions`: Pass `true` to update the versions of the gems based on the lockfile.
|
60
64
|
* `use_double_quotes`: Pass `true` to convert single quotes to double quotes in gem declarations.
|
65
|
+
* `remove_versions`: Pass `true` to remove version constraints from gems while preserving other parameters like `require` or `platforms`. This option takes precedence over `update_versions` if both are enabled.
|
61
66
|
* `ignore_gems`: Pass an array of GEMs you want to ignore versions and comments
|
62
67
|
* `ignore_gem_versions`: Pass an array of GEMs you want to ignore versions
|
63
68
|
* `ignore_gem_comments`: Pass an array of GEMs you want to ignore comments
|
@@ -69,6 +74,7 @@ backup: true
|
|
69
74
|
update_comments: true
|
70
75
|
update_versions: false
|
71
76
|
use_double_quotes: true
|
77
|
+
remove_versions: true
|
72
78
|
ignore_gems:
|
73
79
|
- byebug
|
74
80
|
- discard
|
@@ -77,7 +83,7 @@ ignore_gem_versions:
|
|
77
83
|
ignore_gem_comments:
|
78
84
|
- otpor
|
79
85
|
```
|
80
|
-
This will sort your Gemfile, create a backup, update comments, and ignore specific gems for different operations.
|
86
|
+
This will sort your Gemfile, create a backup, update comments, remove version constraints, and ignore specific gems for different operations.
|
81
87
|
|
82
88
|
## Example
|
83
89
|
### Input Gemfile
|
@@ -87,6 +93,9 @@ source "https://rubygems.org"
|
|
87
93
|
# Framework
|
88
94
|
gem "rails"
|
89
95
|
gem "puma", "~> 5.3"
|
96
|
+
gem "thruster", "~> 0.1.13", require: false
|
97
|
+
gem "countries", "~> 7.1", ">= 7.1.1"
|
98
|
+
gem "tzinfo-data", platforms: %i[ windows jruby ]
|
90
99
|
|
91
100
|
group :development do
|
92
101
|
gem "dotenv-rails"
|
@@ -102,10 +111,33 @@ source "https://rubygems.org"
|
|
102
111
|
gem "puma", "~> 5.3"
|
103
112
|
# Full-stack web application framework.
|
104
113
|
gem "rails", "~> 8.0", ">= 8.0.1"
|
114
|
+
gem "thruster", "~> 0.1.13", require: false
|
115
|
+
gem "countries", "~> 7.1", ">= 7.1.1"
|
116
|
+
gem "tzinfo-data", platforms: %i[ windows jruby ]
|
117
|
+
|
118
|
+
group :development do
|
119
|
+
# Autoload dotenv in Rails.
|
120
|
+
gem "dotenv-rails", "~> 3.1", ">= 3.1.7"
|
121
|
+
# A runtime developer console and IRB alternative with powerful introspection capabilities.
|
122
|
+
gem "pry"
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
126
|
+
### Output Gemfile (with remove_versions: true)
|
127
|
+
```ruby
|
128
|
+
source "https://rubygems.org"
|
129
|
+
|
130
|
+
# A Ruby/Rack web server built for parallelism.
|
131
|
+
gem "puma"
|
132
|
+
# Full-stack web application framework.
|
133
|
+
gem "rails"
|
134
|
+
gem "thruster", require: false
|
135
|
+
gem "countries"
|
136
|
+
gem "tzinfo-data", platforms: %i[ windows jruby ]
|
105
137
|
|
106
138
|
group :development do
|
107
139
|
# Autoload dotenv in Rails.
|
108
|
-
gem
|
140
|
+
gem "dotenv-rails"
|
109
141
|
# A runtime developer console and IRB alternative with powerful introspection capabilities.
|
110
142
|
gem "pry"
|
111
143
|
end
|
data/lib/gem_sorter/version.rb
CHANGED
data/lib/gem_sorter.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# lib/gem_sorter.rb
|
2
2
|
require 'net/http'
|
3
3
|
require 'cgi'
|
4
|
+
require 'openssl'
|
4
5
|
|
5
6
|
load File.expand_path('tasks/gem_sorter.rake', __dir__) if defined?(Rake)
|
6
7
|
|
@@ -14,19 +15,25 @@ module GemSorter
|
|
14
15
|
end
|
15
16
|
|
16
17
|
def sort
|
18
|
+
ruby_line = extract_ruby_line(@content)
|
19
|
+
|
17
20
|
parts = @content.split(/^group/)
|
18
21
|
main_section = parts.shift
|
19
22
|
group_sections = parts
|
20
23
|
|
21
|
-
source_line, gems = process_main_section(main_section)
|
24
|
+
source_line, gems, _ = process_main_section(main_section)
|
22
25
|
|
23
26
|
update_gem_summaries(gems) if @config.update_comments
|
24
27
|
update_version_text(gems) if @config.update_versions
|
25
|
-
|
28
|
+
remove_versions(gems) if @config.remove_versions
|
26
29
|
sorted_gems = sort_gem_blocks(gems)
|
27
30
|
|
28
31
|
result = []
|
29
32
|
result << source_line
|
33
|
+
if ruby_line
|
34
|
+
result << ''
|
35
|
+
result << ruby_line
|
36
|
+
end
|
30
37
|
result << ''
|
31
38
|
result.concat(sorted_gems)
|
32
39
|
result << ''
|
@@ -35,6 +42,7 @@ module GemSorter
|
|
35
42
|
group_gems = process_group_section(section)
|
36
43
|
update_gem_summaries(group_gems) if @config.update_comments
|
37
44
|
update_version_text(group_gems) if @config.update_versions
|
45
|
+
remove_versions(group_gems) if @config.remove_versions
|
38
46
|
result << "group#{section.split("\n").first}"
|
39
47
|
result.concat(sort_gem_blocks(group_gems).map { |line| " #{line}" })
|
40
48
|
result << 'end'
|
@@ -48,6 +56,12 @@ module GemSorter
|
|
48
56
|
|
49
57
|
private
|
50
58
|
|
59
|
+
def extract_ruby_line(content)
|
60
|
+
lines = content.split("\n")
|
61
|
+
ruby_line = lines.find { |line| line.strip.start_with?('ruby') }
|
62
|
+
ruby_line&.strip
|
63
|
+
end
|
64
|
+
|
51
65
|
def transform_to_double_quotes(gem_file_content)
|
52
66
|
return gem_file_content unless gem_file_content.is_a?(Array) || gem_file_content.is_a?(String)
|
53
67
|
|
@@ -99,6 +113,21 @@ module GemSorter
|
|
99
113
|
end
|
100
114
|
end
|
101
115
|
|
116
|
+
def remove_versions(gems)
|
117
|
+
gems.each do |gem_block|
|
118
|
+
gem_name = extract_gem_name(gem_block[:gem_line])
|
119
|
+
next if @config.ignore_gems.include?(gem_name) || @config.ignore_gem_versions.include?(gem_name)
|
120
|
+
|
121
|
+
extra_params = extract_params(gem_block[:gem_line])
|
122
|
+
base = gem_block[:gem_line].match(/gem\s+['"][^'"]+['"]/)[0]
|
123
|
+
if extra_params
|
124
|
+
gem_block[:gem_line] = "#{base},#{extra_params}"
|
125
|
+
else
|
126
|
+
gem_block[:gem_line] = base
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
102
131
|
def extract_params(gem_line)
|
103
132
|
return nil unless gem_line =~ /gem\s+['"][^'"]+['"]/
|
104
133
|
|
@@ -131,11 +160,14 @@ module GemSorter
|
|
131
160
|
source_line = lines.shift
|
132
161
|
|
133
162
|
gems = []
|
163
|
+
ruby_line = nil
|
134
164
|
current_comments = []
|
135
165
|
|
136
166
|
lines.each do |line|
|
137
167
|
if line.start_with?('#')
|
138
168
|
current_comments << line
|
169
|
+
elsif line.start_with?('ruby')
|
170
|
+
ruby_line = line
|
139
171
|
elsif line.start_with?('gem')
|
140
172
|
gems << {
|
141
173
|
comments: current_comments,
|
@@ -145,7 +177,7 @@ module GemSorter
|
|
145
177
|
end
|
146
178
|
end
|
147
179
|
|
148
|
-
[source_line, gems]
|
180
|
+
[source_line, gems, ruby_line]
|
149
181
|
end
|
150
182
|
|
151
183
|
def process_group_section(section)
|
@@ -199,7 +231,7 @@ module GemSorter
|
|
199
231
|
return get_summary(gem_name, true) unless remote
|
200
232
|
nil
|
201
233
|
end
|
202
|
-
rescue StandardError
|
234
|
+
rescue StandardError
|
203
235
|
nil
|
204
236
|
end
|
205
237
|
|
@@ -208,11 +240,18 @@ module GemSorter
|
|
208
240
|
url = URI(version ? "#{base_url}/versions/#{version.strip}" : base_url)
|
209
241
|
|
210
242
|
begin
|
211
|
-
|
212
|
-
|
213
|
-
|
243
|
+
http = Net::HTTP.new(url.host, url.port)
|
244
|
+
http.use_ssl = true
|
245
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
246
|
+
|
247
|
+
request = Net::HTTP::Get.new(url)
|
248
|
+
response = http.request(request)
|
249
|
+
|
250
|
+
unless response.is_a?(Net::HTTPSuccess)
|
251
|
+
raise "Error: Could not fetch gem information from RubyGems for #{gem_name} version #{version}. Status: #{response.code}"
|
214
252
|
end
|
215
|
-
|
253
|
+
|
254
|
+
match = response.body.match(/<input[^>]*id=["']gemfile_text["'][^>]*value=["']([^"']+)["']/)
|
216
255
|
|
217
256
|
if match
|
218
257
|
CGI.unescapeHTML(match[1])
|
data/lib/task_config.rb
CHANGED
@@ -9,6 +9,7 @@ module GemSorter
|
|
9
9
|
'update_comments' => false,
|
10
10
|
'update_versions' => false,
|
11
11
|
'use_double_quotes' => false,
|
12
|
+
'remove_versions' => false,
|
12
13
|
'gemfile_path' => 'Gemfile',
|
13
14
|
'ignore_gems' => [],
|
14
15
|
'ignore_gem_versions' => [],
|
@@ -20,6 +21,7 @@ module GemSorter
|
|
20
21
|
@update_comments = nil
|
21
22
|
@update_versions = nil
|
22
23
|
@use_double_quotes = nil
|
24
|
+
@remove_versions = nil
|
23
25
|
@ignore_gems = nil
|
24
26
|
@ignore_gem_versions = nil
|
25
27
|
@ignore_gem_comments = nil
|
@@ -43,6 +45,10 @@ module GemSorter
|
|
43
45
|
@use_double_quotes.nil? ? DEFAULT_CONFIG['use_double_quotes'] : @use_double_quotes
|
44
46
|
end
|
45
47
|
|
48
|
+
def remove_versions
|
49
|
+
@remove_versions.nil? ? DEFAULT_CONFIG['remove_versions'] : @remove_versions
|
50
|
+
end
|
51
|
+
|
46
52
|
def ignore_gems
|
47
53
|
@ignore_gems.nil? ? DEFAULT_CONFIG['ignore_gems'] : @ignore_gems
|
48
54
|
end
|
@@ -75,6 +81,7 @@ module GemSorter
|
|
75
81
|
@update_versions = task_config['update_versions'] if task_config.key?('update_versions')
|
76
82
|
@gemfile_path = task_config['gemfile_path'] if task_config.key?('gemfile_path')
|
77
83
|
@use_double_quotes = task_config['use_double_quotes'] if task_config.key?('use_double_quotes')
|
84
|
+
@remove_versions = task_config['remove_versions'] if task_config.key?('remove_versions')
|
78
85
|
@ignore_gems = task_config['ignore_gems'] if task_config.key?('ignore_gems')
|
79
86
|
@ignore_gem_versions = task_config['ignore_gem_versions'] if task_config.key?('ignore_gem_versions')
|
80
87
|
@ignore_gem_comments = task_config['ignore_gem_comments'] if task_config.key?('ignore_gem_comments')
|
@@ -88,6 +95,7 @@ module GemSorter
|
|
88
95
|
@update_versions = parse_boolean(args[:update_versions]) unless args[:update_versions].nil?
|
89
96
|
@gemfile_path = args[:gemfile_path] unless args[:gemfile_path].nil?
|
90
97
|
@use_double_quotes = parse_boolean(args[:use_double_quotes]) unless args[:use_double_quotes].nil?
|
98
|
+
@remove_versions = parse_boolean(args[:remove_versions]) unless args[:remove_versions].nil?
|
91
99
|
@ignore_gems = args[:ignore_gems] unless args[:ignore_gems].nil?
|
92
100
|
@ignore_gem_versions = args[:ignore_gem_versions] unless args[:ignore_gem_versions].nil?
|
93
101
|
@ignore_gem_comments = args[:ignore_gem_comments] unless args[:ignore_gem_comments].nil?
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gem_sorter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Renan Garcia
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: rake
|
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
70
|
- !ruby/object:Gem::Version
|
71
71
|
version: '0'
|
72
72
|
requirements: []
|
73
|
-
rubygems_version: 3.6.
|
73
|
+
rubygems_version: 3.6.8
|
74
74
|
specification_version: 4
|
75
75
|
summary: Sort gems in the Gemfile alphabetically
|
76
76
|
test_files: []
|