gem_sorter 0.3.1 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bbae0106b44090db037979017493686a388123413febf17d5de70a3770edd1b1
4
- data.tar.gz: 5fa00b9f9509c83ffd9022b8631b669c66e32f00663b2433cfc66d9d1c14a2e4
3
+ metadata.gz: e8a4c3e034c1034831bb4e5c86a019d920709d36307496ba289319d342dc1b25
4
+ data.tar.gz: 05c3b4da42d06f1dd763c3aacda058e44961bdf2c7e51003ebf9cf907b52e549
5
5
  SHA512:
6
- metadata.gz: 1cbf29f10ec73e50adeac3c18a2495c84c25633c6b82bf70ab834185995080534d7faee772910787f503694445838efbb1dd73497eb79ee720ff54b1e66d0e34
7
- data.tar.gz: '0930ef6b73d42328c04d1d6f34740d94186db7cee60c26ada9d7352830d35161aa7037116985f440588d191aeb67f610052f07a5434a2b2e1bc7676b3b45fb18'
6
+ metadata.gz: e39b95e825eee0956bd92f0b02a70acdf4ac9b3bfac1f12b490db6b79c73814ab7dfbb382dd87a611a4236dd6461979015c141569622d0becb66ec992827b13e
7
+ data.tar.gz: 0f0e81fee4d454db0236c0b60babd45793efc6511aec24a37334f075389a3b8a92db00f9f002bc8bef336051529193780c6c9c238196a2d7ee93a53195d56fad
data/README.md CHANGED
@@ -12,6 +12,7 @@ GemSorter is a simple gem to sort the contents of your Gemfile alphabetically wh
12
12
  * Maintains group structure in the Gemfile.
13
13
  * Optionally creates a backup of the original Gemfile.
14
14
  * Update the comments of the gems based on their descriptions.
15
+ * Optionally converts single quotes to double quotes in gem declarations.
15
16
 
16
17
  ## Installation
17
18
  Add the gem to your project's `Gemfile`:
@@ -19,6 +20,11 @@ Add the gem to your project's `Gemfile`:
19
20
  ```ruby
20
21
  gem "gem_sorter"
21
22
  ```
23
+ or install it globally:
24
+
25
+ ```bash
26
+ gem install gem_sorter
27
+ ```
22
28
 
23
29
  ## Usage
24
30
  Once installed, you can use the provided Rake task to sort your Gemfile:
@@ -27,23 +33,31 @@ Once installed, you can use the provided Rake task to sort your Gemfile:
27
33
  rake gemfile:sort
28
34
  ```
29
35
 
36
+ You can also run the gem_sorter globally, without needing to add it to your Gemfile:
37
+
38
+ ```bash
39
+ rake -r gem_sorter gemfile:sort
40
+ ```
41
+
30
42
  ### Options
31
43
  * `backup`: Pass `true` to create a backup of your Gemfile as `Gemfile.old` before sorting.
32
44
  * `update_comments`: Pass `true` to update the comments of the gems based on their descriptions.
33
45
  * `update_versions`: Pass `true` to update the versions of the gems based on the lockfile.
46
+ * `use_double_quotes`: Pass `true` to convert single quotes to double quotes in gem declarations.
34
47
 
35
48
  Example:
36
49
 
37
50
  ```bash
38
- rake gemfile:sort[true,true,true]
51
+ rake gemfile:sort[true,true,true,true]
39
52
  ```
40
- This will sort your Gemfile, create a backup, and update comments and versions.
53
+ This will sort your Gemfile, create a backup, update comments and versions, and convert single quotes to double quotes.
41
54
 
42
55
  ### Options File
43
56
  Create a file in the root of your project called `gem_sorter.yml`
44
57
  * `backup`: Pass `true` to create a backup of your Gemfile as `Gemfile.old` before sorting.
45
58
  * `update_comments`: Pass `true` to update the comments of the gems based on their descriptions.
46
59
  * `update_versions`: Pass `true` to update the versions of the gems based on the lockfile.
60
+ * `use_double_quotes`: Pass `true` to convert single quotes to double quotes in gem declarations.
47
61
  * `ignore_gems`: Pass an array of GEMs you want to ignore versions and comments
48
62
  * `ignore_gem_versions`: Pass an array of GEMs you want to ignore versions
49
63
  * `ignore_gem_comments`: Pass an array of GEMs you want to ignore comments
@@ -54,6 +68,7 @@ Example:
54
68
  backup: true
55
69
  update_comments: true
56
70
  update_versions: false
71
+ use_double_quotes: true
57
72
  ignore_gems:
58
73
  - byebug
59
74
  - discard
@@ -62,7 +77,8 @@ ignore_gem_versions:
62
77
  ignore_gem_comments:
63
78
  - otpor
64
79
  ```
65
- This will sort your Gemfile, create a backup, and update comments and versions.
80
+ This will sort your Gemfile, create a backup, update comments, convert single quotes to double quotes, and ignore specific gems for different operations.
81
+
66
82
  ## Example
67
83
  ### Input Gemfile
68
84
  ```ruby
@@ -1,3 +1,3 @@
1
1
  module GemSorter
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
data/lib/gem_sorter.rb CHANGED
@@ -41,11 +41,49 @@ module GemSorter
41
41
  result << ''
42
42
  end
43
43
 
44
+ result = transform_to_double_quotes(result) if @config.use_double_quotes
45
+
44
46
  result.join("\n")
45
47
  end
46
48
 
47
49
  private
48
50
 
51
+ def transform_to_double_quotes(gem_file_content)
52
+ return gem_file_content unless gem_file_content.is_a?(Array) || gem_file_content.is_a?(String)
53
+
54
+ content = gem_file_content.is_a?(Array) ? gem_file_content : gem_file_content.split("\n")
55
+
56
+ transformed_content = content.map do |line|
57
+ next line if line.nil? || line.strip.empty?
58
+
59
+ if line.strip.start_with?('#')
60
+ line
61
+ elsif line.include?('gem') && line =~ /gem\s+[']/
62
+ begin
63
+ parts = line.split('#', 2)
64
+ main_part = parts[0]
65
+ comment_part = parts[1]
66
+ processed_main = main_part.gsub(/gem\s+'([^']+)'/) { |m| %Q{gem "#{$1}"} }
67
+
68
+ if comment_part
69
+ "#{processed_main}##{comment_part}"
70
+ else
71
+ processed_main
72
+ end
73
+ rescue => e
74
+ puts "Error transforming to double quotes: #{e.message}"
75
+ line
76
+ end
77
+ elsif line.include?('source')
78
+ line.gsub("'", '"')
79
+ else
80
+ line
81
+ end
82
+ end
83
+
84
+ gem_file_content.is_a?(Array) ? transformed_content : transformed_content.join("\n")
85
+ end
86
+
49
87
  def update_version_text(gems)
50
88
  @versions ||= fetch_versions_from_lockfile("#{@filepath}.lock")
51
89
  gems.each do |gem_block|
data/lib/task_config.rb CHANGED
@@ -1,94 +1,104 @@
1
1
  require 'yaml'
2
2
 
3
- class GemSorter::TaskConfig
4
- attr_reader :backup, :update_comments, :update_versions, :ignore_gems, :gemfile_path
5
-
6
- DEFAULT_CONFIG = {
7
- 'backup' => false,
8
- 'update_comments' => false,
9
- 'update_versions' => false,
10
- 'gemfile_path' => 'Gemfile',
11
- 'ignore_gems' => [],
12
- 'ignore_gem_versions' => [],
13
- 'ignore_gem_comments' => [],
14
- }.freeze
15
-
16
- def initialize(args = nil)
17
- @backup = nil
18
- @update_comments = nil
19
- @update_versions = nil
20
- @ignore_gems = nil
21
- @ignore_gem_versions = nil
22
- @ignore_gem_comments = nil
23
- @gemfile_path = nil
24
- load_config(args)
25
- end
3
+ module GemSorter
4
+ class TaskConfig
5
+ attr_reader :backup, :update_comments, :update_versions, :ignore_gems, :gemfile_path
6
+
7
+ DEFAULT_CONFIG = {
8
+ 'backup' => false,
9
+ 'update_comments' => false,
10
+ 'update_versions' => false,
11
+ 'use_double_quotes' => false,
12
+ 'gemfile_path' => 'Gemfile',
13
+ 'ignore_gems' => [],
14
+ 'ignore_gem_versions' => [],
15
+ 'ignore_gem_comments' => []
16
+ }.freeze
17
+
18
+ def initialize(args = nil)
19
+ @backup = nil
20
+ @update_comments = nil
21
+ @update_versions = nil
22
+ @use_double_quotes = nil
23
+ @ignore_gems = nil
24
+ @ignore_gem_versions = nil
25
+ @ignore_gem_comments = nil
26
+ @gemfile_path = nil
27
+ load_config(args)
28
+ end
26
29
 
27
- def backup
28
- @backup.nil? ? DEFAULT_CONFIG['backup'] : @backup
29
- end
30
+ def backup
31
+ @backup.nil? ? DEFAULT_CONFIG['backup'] : @backup
32
+ end
30
33
 
31
- def update_comments
32
- @update_comments.nil? ? DEFAULT_CONFIG['update_comments'] : @update_comments
33
- end
34
+ def update_comments
35
+ @update_comments.nil? ? DEFAULT_CONFIG['update_comments'] : @update_comments
36
+ end
34
37
 
35
- def update_versions
36
- @update_versions.nil? ? DEFAULT_CONFIG['update_versions'] : @update_versions
37
- end
38
+ def update_versions
39
+ @update_versions.nil? ? DEFAULT_CONFIG['update_versions'] : @update_versions
40
+ end
38
41
 
39
- def ignore_gems
40
- @ignore_gems.nil? ? DEFAULT_CONFIG['ignore_gems'] : @ignore_gems
41
- end
42
+ def use_double_quotes
43
+ @use_double_quotes.nil? ? DEFAULT_CONFIG['use_double_quotes'] : @use_double_quotes
44
+ end
42
45
 
43
- def ignore_gem_versions
44
- @ignore_gem_versions.nil? ? DEFAULT_CONFIG['ignore_gem_versions'] : @ignore_gem_versions
45
- end
46
+ def ignore_gems
47
+ @ignore_gems.nil? ? DEFAULT_CONFIG['ignore_gems'] : @ignore_gems
48
+ end
46
49
 
47
- def ignore_gem_comments
48
- @ignore_gem_comments.nil? ? DEFAULT_CONFIG['ignore_gem_comments'] : @ignore_gem_comments
49
- end
50
+ def ignore_gem_versions
51
+ @ignore_gem_versions.nil? ? DEFAULT_CONFIG['ignore_gem_versions'] : @ignore_gem_versions
52
+ end
50
53
 
51
- def gemfile_path
52
- @gemfile_path.nil? ? DEFAULT_CONFIG['gemfile_path'] : @gemfile_path
53
- end
54
+ def ignore_gem_comments
55
+ @ignore_gem_comments.nil? ? DEFAULT_CONFIG['ignore_gem_comments'] : @ignore_gem_comments
56
+ end
54
57
 
55
- private
58
+ def gemfile_path
59
+ @gemfile_path.nil? ? DEFAULT_CONFIG['gemfile_path'] : @gemfile_path
60
+ end
56
61
 
57
- def load_config(args)
58
- load_config_from_file
59
- load_config_from_args(args)
60
- end
62
+ private
63
+
64
+ def load_config(args)
65
+ load_config_from_file
66
+ load_config_from_args(args)
67
+ end
68
+
69
+ def load_config_from_file
70
+ return unless File.exist?(gem_sorter_config_file_path)
61
71
 
62
- def load_config_from_file
63
- if File.exist?(gem_sorter_config_file_path)
64
72
  task_config = YAML.load_file(gem_sorter_config_file_path)
65
73
  @backup = task_config['backup'] if task_config.key?('backup')
66
74
  @update_comments = task_config['update_comments'] if task_config.key?('update_comments')
67
75
  @update_versions = task_config['update_versions'] if task_config.key?('update_versions')
68
76
  @gemfile_path = task_config['gemfile_path'] if task_config.key?('gemfile_path')
77
+ @use_double_quotes = task_config['use_double_quotes'] if task_config.key?('use_double_quotes')
69
78
  @ignore_gems = task_config['ignore_gems'] if task_config.key?('ignore_gems')
70
79
  @ignore_gem_versions = task_config['ignore_gem_versions'] if task_config.key?('ignore_gem_versions')
71
80
  @ignore_gem_comments = task_config['ignore_gem_comments'] if task_config.key?('ignore_gem_comments')
72
81
  end
73
- end
74
-
75
- def load_config_from_args(args)
76
- return unless args
77
82
 
78
- @backup = parse_boolean(args[:backup]) unless args[:backup].nil?
79
- @update_comments = parse_boolean(args[:update_comments]) unless args[:update_comments].nil?
80
- @update_versions = parse_boolean(args[:update_versions]) unless args[:update_versions].nil?
81
- @gemfile_path = args[:gemfile_path] unless args[:gemfile_path].nil?
82
- @ignore_gems = args[:ignore_gems] unless args[:ignore_gems].nil?
83
- @ignore_gem_versions = args[:ignore_gem_versions] unless args[:ignore_gem_versions].nil?
84
- @ignore_gem_comments = args[:ignore_gem_comments] unless args[:ignore_gem_comments].nil?
85
- end
83
+ def load_config_from_args(args)
84
+ return unless args
85
+
86
+ @backup = parse_boolean(args[:backup]) unless args[:backup].nil?
87
+ @update_comments = parse_boolean(args[:update_comments]) unless args[:update_comments].nil?
88
+ @update_versions = parse_boolean(args[:update_versions]) unless args[:update_versions].nil?
89
+ @gemfile_path = args[:gemfile_path] unless args[:gemfile_path].nil?
90
+ @use_double_quotes = parse_boolean(args[:use_double_quotes]) unless args[:use_double_quotes].nil?
91
+ @ignore_gems = args[:ignore_gems] unless args[:ignore_gems].nil?
92
+ @ignore_gem_versions = args[:ignore_gem_versions] unless args[:ignore_gem_versions].nil?
93
+ @ignore_gem_comments = args[:ignore_gem_comments] unless args[:ignore_gem_comments].nil?
94
+ end
86
95
 
87
- def parse_boolean(value)
88
- value.to_s.downcase == 'true'
89
- end
96
+ def parse_boolean(value)
97
+ value.to_s.downcase == 'true'
98
+ end
90
99
 
91
- def gem_sorter_config_file_path
92
- 'gem_sorter.yml'
100
+ def gem_sorter_config_file_path
101
+ 'gem_sorter.yml'
102
+ end
93
103
  end
94
- end
104
+ end
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.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renan Garcia
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-09 00:00:00.000000000 Z
10
+ date: 2025-03-24 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rake