gem_sorter 0.2.0 → 0.3.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: 9a7e010153615999089999a370d116a6404dbdaf855bcb6257c801279baec90b
4
- data.tar.gz: 23c131930d94049ad3c90f556fd2e8fc3a7b2c74b3a7eadbed026fb4ef08b333
3
+ metadata.gz: 91f751af186dcb78cb90b9acbf22c086e895dc6b74ad6b202783fe3f6d799a3d
4
+ data.tar.gz: dc18ef774b7be132570b526e510c95022b0da490b45fd4483d6ec6ae39a0b1aa
5
5
  SHA512:
6
- metadata.gz: e0f45801b449beff02390ae7227ef679148214cbf79a96068ef17fa7d668eff14bc7fa64d78309fbeb02e32e9677813adcb96a3371ca2a2d54690b91a26c9022
7
- data.tar.gz: 29529cd23267c390d2bafe645a10d612d6f22aca97706157501091952f5266ba3ec735e52f47bdab733318ce41ada18cb28e81230efe091e18702d1f9e8bb59e
6
+ metadata.gz: 3c527d936237f42547330dc39c740aaca01788a20a3c6e73024ebcddd11dbbce80d74bdeab5d7c39391d7beca6b23264a20fd25e79a1f44c58bb588164ae7238
7
+ data.tar.gz: 9a640f1ee3d35cd1aa019cea2b9e0eb7898c3d39d0635334660f527073faeea0d5cf860ce5dfc7c90478d8c5f2361b61dbb083621805f95ae153a7900ef85d26
data/README.md CHANGED
@@ -1,3 +1,7 @@
1
+ <p align="center">
2
+ <img src="https://i.imgur.com/WHOyL9W.png" width="150" alt="GemSorter Logo">
3
+ </p>
4
+
1
5
  # GemSorter
2
6
 
3
7
  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.
@@ -33,9 +37,32 @@ Example:
33
37
  ```bash
34
38
  rake gemfile:sort[true,true,true]
35
39
  ```
36
-
37
40
  This will sort your Gemfile, create a backup, and update comments and versions.
38
41
 
42
+ ### Options File
43
+ Create a file in the root of your project called `gem_sorter.yml`
44
+ * `backup`: Pass `true` to create a backup of your Gemfile as `Gemfile.old` before sorting.
45
+ * `update_comments`: Pass `true` to update the comments of the gems based on their descriptions.
46
+ * `update_versions`: Pass `true` to update the versions of the gems based on the lockfile.
47
+ * `ignore_gems`: Pass an array of GEMs you want to ignore versions and comments
48
+ * `ignore_gem_versions`: Pass an array of GEMs you want to ignore versions
49
+ * `ignore_gem_comments`: Pass an array of GEMs you want to ignore comments
50
+
51
+ Example:
52
+
53
+ ```yaml
54
+ backup: true
55
+ update_comments: true
56
+ update_versions: false
57
+ ignore_gems:
58
+ - byebug
59
+ - discard
60
+ ignore_gem_versions:
61
+ - rspec
62
+ ignore_gem_comments:
63
+ - otpor
64
+ ```
65
+ This will sort your Gemfile, create a backup, and update comments and versions.
39
66
  ## Example
40
67
  ### Input Gemfile
41
68
  ```ruby
@@ -1,3 +1,3 @@
1
1
  module GemSorter
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/gem_sorter.rb CHANGED
@@ -6,21 +6,22 @@ load File.expand_path('tasks/gem_sorter.rake', __dir__) if defined?(Rake)
6
6
 
7
7
  module GemSorter
8
8
  class Sorter
9
- def initialize(filepath)
10
- @filepath = filepath
11
- @content = File.read(filepath)
9
+ def initialize(task_config)
10
+ @config = task_config
11
+ @filepath = task_config.gemfile_path
12
+ @content = File.read(task_config.gemfile_path)
12
13
  @versions = nil
13
14
  end
14
15
 
15
- def sort(update_comments = false, update_versions = false)
16
+ def sort
16
17
  parts = @content.split(/^group/)
17
18
  main_section = parts.shift
18
19
  group_sections = parts
19
20
 
20
21
  source_line, gems = process_main_section(main_section)
21
22
 
22
- update_gem_summaries(gems) if update_comments
23
- update_version_text(gems) if update_versions
23
+ update_gem_summaries(gems) if @config.update_comments
24
+ update_version_text(gems) if @config.update_versions
24
25
 
25
26
  sorted_gems = sort_gem_blocks(gems)
26
27
 
@@ -32,8 +33,8 @@ module GemSorter
32
33
 
33
34
  group_sections.each do |section|
34
35
  group_gems = process_group_section(section)
35
- update_gem_summaries(group_gems) if update_comments
36
- update_version_text(group_gems) if update_versions
36
+ update_gem_summaries(group_gems) if @config.update_comments
37
+ update_version_text(group_gems) if @config.update_versions
37
38
  result << "group#{section.split("\n").first}"
38
39
  result.concat(sort_gem_blocks(group_gems).map { |line| " #{line}" })
39
40
  result << 'end'
@@ -49,6 +50,8 @@ module GemSorter
49
50
  @versions ||= fetch_versions_from_lockfile("#{@filepath}.lock")
50
51
  gems.each do |gem_block|
51
52
  gem_name = gem_block[:gem_line].match(/gem\s*"([^"]+)"/)[1]
53
+ next if @config.ignore_gems.include?(gem_name) || @config.ignore_gem_versions.include?(gem_name)
54
+
52
55
  version = @versions[gem_name]
53
56
  extra_params = extract_params(gem_block[:gem_line])
54
57
  base = version ? "#{fetch_gemfile_text(gem_name, version, gem_block[:gem_line])}" : gem_block[:gem_line]
@@ -73,6 +76,8 @@ module GemSorter
73
76
  def update_gem_summaries(gems)
74
77
  gems.each do |gem_block|
75
78
  gem_name = gem_block[:gem_line].match(/gem\s*"([^"]+)"/)[1]
79
+ next if @config.ignore_gems.include?(gem_name) || @config.ignore_gem_comments.include?(gem_name)
80
+
76
81
  if summary = get_summary(gem_name, false)
77
82
  gem_block[:comments] = ["# #{summary}"]
78
83
  end
@@ -0,0 +1,94 @@
1
+ require 'yaml'
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
26
+
27
+ def backup
28
+ @backup.nil? ? DEFAULT_CONFIG['backup'] : @backup
29
+ end
30
+
31
+ def update_comments
32
+ @update_comments.nil? ? DEFAULT_CONFIG['update_comments'] : @update_comments
33
+ end
34
+
35
+ def update_versions
36
+ @update_versions.nil? ? DEFAULT_CONFIG['update_versions'] : @update_versions
37
+ end
38
+
39
+ def ignore_gems
40
+ @ignore_gems.nil? ? DEFAULT_CONFIG['ignore_gems'] : @ignore_gems
41
+ end
42
+
43
+ def ignore_gem_versions
44
+ @ignore_gem_versions.nil? ? DEFAULT_CONFIG['ignore_gem_versions'] : @ignore_gem_versions
45
+ end
46
+
47
+ def ignore_gem_comments
48
+ @ignore_gem_comments.nil? ? DEFAULT_CONFIG['ignore_gem_comments'] : @ignore_gem_comments
49
+ end
50
+
51
+ def gemfile_path
52
+ @gemfile_path.nil? ? DEFAULT_CONFIG['gemfile_path'] : @gemfile_path
53
+ end
54
+
55
+ private
56
+
57
+ def load_config(args)
58
+ load_config_from_file
59
+ load_config_from_args(args)
60
+ end
61
+
62
+ def load_config_from_file
63
+ if File.exist?(gem_sorter_config_file_path)
64
+ task_config = YAML.load_file(gem_sorter_config_file_path)
65
+ @backup = task_config['backup'] if task_config.key?('backup')
66
+ @update_comments = task_config['update_comments'] if task_config.key?('update_comments')
67
+ @update_versions = task_config['update_versions'] if task_config.key?('update_versions')
68
+ @gemfile_path = task_config['gemfile_path'] if task_config.key?('gemfile_path')
69
+ @ignore_gems = task_config['ignore_gems'] if task_config.key?('ignore_gems')
70
+ @ignore_gem_versions = task_config['ignore_gem_versions'] if task_config.key?('ignore_gem_versions')
71
+ @ignore_gem_comments = task_config['ignore_gem_comments'] if task_config.key?('ignore_gem_comments')
72
+ end
73
+ end
74
+
75
+ def load_config_from_args(args)
76
+ return unless args
77
+
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
86
+
87
+ def parse_boolean(value)
88
+ value.to_s.downcase == 'true'
89
+ end
90
+
91
+ def gem_sorter_config_file_path
92
+ 'gem_sorter.yml'
93
+ end
94
+ end
@@ -1,26 +1,22 @@
1
1
  require 'gem_sorter'
2
+ require 'yaml'
3
+ require 'task_config'
2
4
 
3
5
  namespace :gemfile do
4
6
  desc 'Sort gems in Gemfile alphabetically. Options: [backup=true|false] [update_comments=true|false] [update_versions=true|false]'
5
7
  task :sort, [:backup, :update_comments, :update_versions] do |_t, args|
6
- args.with_defaults(
7
- backup: 'false',
8
- update_comments: 'false',
9
- update_versions: 'false'
10
- )
8
+ task_config = GemSorter::TaskConfig.new(args)
11
9
 
12
- gemfile_path = 'Gemfile'
13
-
14
- if File.exist?(gemfile_path)
15
- if args.backup.downcase == 'true'
16
- FileUtils.cp(gemfile_path, "#{gemfile_path}.old")
10
+ if File.exist?(task_config.gemfile_path)
11
+ if task_config.backup
12
+ FileUtils.cp(task_config.gemfile_path, "#{task_config.gemfile_path}.old")
17
13
  puts 'Original Gemfile backed up as Gemfile.old'
18
14
  end
19
15
 
20
- sorter = ::GemSorter::Sorter.new(gemfile_path)
21
- sorted_content = sorter.sort(args.update_comments.downcase == 'true', args.update_versions.downcase == 'true')
16
+ sorter = ::GemSorter::Sorter.new(task_config)
17
+ sorted_content = sorter.sort
22
18
 
23
- File.write(gemfile_path, sorted_content)
19
+ File.write(task_config.gemfile_path, sorted_content)
24
20
 
25
21
  puts 'Gemfile sorted successfully!'
26
22
  else
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renan Garcia
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-03 00:00:00.000000000 Z
10
+ date: 2025-01-06 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rake
@@ -48,6 +48,7 @@ files:
48
48
  - README.md
49
49
  - lib/gem_sorter.rb
50
50
  - lib/gem_sorter/version.rb
51
+ - lib/task_config.rb
51
52
  - lib/tasks/gem_sorter.rake
52
53
  homepage: https://github.com/renan-garcia/gem_sorter
53
54
  licenses: