isort 0.1.3 → 0.1.5

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: eddd4ac245d4454fae469ae1562f8236c9d273e3d0fbf2197503928267957816
4
- data.tar.gz: cc278de73effc6a9e8c69d21b6cb5d2fa06a85542e084d9f93affdedb5bfc169
3
+ metadata.gz: 6c121d3b6fba0a8fcf8a644eb26ebce0e35b11452cb1a3a644dfbee265a6bd1d
4
+ data.tar.gz: a80ae99d0f3d627407f7ccdd377365d10412ef9ade8b196b839285cee916a935
5
5
  SHA512:
6
- metadata.gz: 32a5b91883075031985e3145fee1a321b40e90c8e031288492f53b3faaed65db183b1d3ce1ef6caf8754e3360eeefa94dcfecc2229a4a4d9e96e9f19aec62e02
7
- data.tar.gz: 731ea5737a2e677a87b056441349d8fbadc406cefa577e5eeffd424d3a3f522dfb19eea2ca518edf6bedd1bb840d80e51fe4a64099b4e3df5db513ed4ff241ec
6
+ metadata.gz: 4a92208a0dfd4b76459832abb07ee6878976adfd8c40f8dd249bd330c1d2717e046bd4427a1078a43ff113c3d6adf82b994f3b4dcdf30471f1b1d17a1e54d3d1
7
+ data.tar.gz: 5ca8c19a71ed5c79135ed98b8f0ca74fe74a7b0ccb22d1de67d93e8fb75192d5f2f69957ab776a231989064a45b966ec2fb9f6a3cea02791acd5740939680635
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ## [Unreleased]
1
+ ## [Released]
2
2
 
3
3
  ## [0.1.3] - 2024-12-28
4
4
 
@@ -10,3 +10,10 @@
10
10
  - Support for require, require_relative, include, and extend statements
11
11
  - CLI interface
12
12
  - Preservation of code structure and spacing
13
+
14
+ ## [0.1.4] - 2024-12-29
15
+
16
+ - Second release
17
+
18
+ ### Added
19
+ - Import sorting functionality support for a whole directory
data/README.md CHANGED
@@ -14,6 +14,8 @@ gem install isort
14
14
 
15
15
  ```bash
16
16
  isort --file path/to/your/file.rb
17
+ or
18
+ isort -f path/to/your/file.rb
17
19
  ```
18
20
 
19
21
  ### In Ruby Code
@@ -35,7 +37,7 @@ sorter.sort_and_format_imports
35
37
 
36
38
  ## Contributing
37
39
 
38
- Bug reports and pull requests are welcome on GitHub at https://github.com/yourusername/isort.
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/abhinvv1/isort.
39
41
 
40
42
  ## License
41
43
 
data/lib/isort/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Isort
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/isort.rb CHANGED
@@ -1,11 +1,9 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "isort/version"
4
-
5
1
  require 'optparse'
2
+ require_relative "isort/version"
6
3
 
7
4
  module Isort
8
5
  class Error < StandardError; end
6
+
9
7
  class FileSorter
10
8
  def initialize(file_path)
11
9
  @file_path = file_path
@@ -13,7 +11,7 @@ module Isort
13
11
 
14
12
  def sort_imports
15
13
  # Read the file content
16
- lines = File.readlines(@file_path)
14
+ lines = File.readlines(@file_path, chomp: true).map { |line| line.gsub("\r", "") }
17
15
 
18
16
  # Separate import-related lines and other content
19
17
  imports = lines.select { |line| line =~ /^\s*(require|require_relative|include)\s/ }
@@ -34,12 +32,12 @@ module Isort
34
32
  lines = File.readlines(@file_path)
35
33
 
36
34
  # Separate and group lines
37
- requires = extract_lines(lines, /^require\s/)
38
- require_relatives = extract_lines(lines, /^require_relative\s/)
39
- includes = extract_lines(lines, /^include\s/)
40
- extends = extract_lines(lines, /^extend\s/)
41
- autoloads = extract_lines(lines, /^autoload\s/)
42
- usings = extract_lines(lines, /^using\s/)
35
+ requires = extract_lines_with_comments(lines, /^require\s/)
36
+ require_relatives = extract_lines_with_comments(lines, /^require_relative\s/)
37
+ includes = extract_lines_with_comments(lines, /^include\s/)
38
+ extends = extract_lines_with_comments(lines, /^extend\s/)
39
+ autoloads = extract_lines_with_comments(lines, /^autoload\s/)
40
+ usings = extract_lines_with_comments(lines, /^using\s/)
43
41
  others = lines.reject { |line| [requires, require_relatives, includes, extends, autoloads, usings].flatten.include?(line) }
44
42
 
45
43
  # Format and sort each group
@@ -51,11 +49,16 @@ module Isort
51
49
  formatted_imports << format_group("autoload", autoloads)
52
50
  formatted_imports << format_group("using", usings)
53
51
 
52
+ return if [requires, require_relatives, includes, extends, autoloads, usings].all?(&:empty?)
53
+
54
54
  # Combine formatted imports with the rest of the file
55
- sorted_content = (formatted_imports + others).join
55
+ sorted_content = "#{formatted_imports.reject(&:empty?).join("\n")}\n#{others.join}".strip
56
56
 
57
- # Write the sorted content back to the file
58
- File.write(@file_path, sorted_content)
57
+ # Add a trailing newline only if imports exist
58
+ sorted_content = "#{sorted_content.rstrip}\n" if !formatted_imports.empty? && !sorted_content.empty?
59
+
60
+ # Write the sorted content back to the file only if imports exist
61
+ File.write(@file_path, sorted_content) unless formatted_imports.empty? && sorted_content.empty?
59
62
  end
60
63
 
61
64
  private
@@ -64,13 +67,41 @@ module Isort
64
67
  lines.select { |line| line =~ regex }
65
68
  end
66
69
 
67
- def format_group(type, lines)
68
- return [] if lines.empty?
70
+ def extract_lines_with_comments(lines, regex)
71
+ grouped_lines = []
72
+ buffer = []
73
+
74
+ lines.each do |line|
75
+ if line.strip.start_with?("#") || line.strip.empty?
76
+ # If the line is a comment or blank, add it to the buffer
77
+ buffer << line
78
+ elsif line =~ regex
79
+ # If it's an import line matching the regex, attach the buffer as comments
80
+ grouped_lines << (buffer + [line])
81
+ buffer = [] # Reset buffer
82
+ else
83
+ # If it's a non-matching line, reset the buffer
84
+ buffer = []
85
+ end
86
+ end
87
+
88
+ grouped_lines
89
+ end
90
+
91
+ def format_group(type, grouped_lines)
92
+ return [] if grouped_lines.empty?
69
93
 
70
- # Remove duplicates and sort
71
- lines.uniq.sort
94
+ # Flatten and sort each group by the import line
95
+ grouped_lines
96
+ .sort_by { |lines| lines.last.strip } # Sort by the actual import statement
97
+ .map { |lines| lines.join } # Combine comments with the import
98
+ .map(&:strip) # Remove trailing newlines
99
+ .join("\n") # Join all imports in the group with newlines
100
+ .concat("\n") # Add a newline between groups
72
101
  end
102
+
73
103
  end
104
+
74
105
  class CLI
75
106
  def self.start
76
107
  options = {}
@@ -80,12 +111,23 @@ module Isort
80
111
  opts.on("-fFILE", "--file=FILE", "File to sort") do |file|
81
112
  options[:file] = file
82
113
  end
114
+ opts.on("-dDIRECTORY", "--directory=DIRECTORY", "Specify a directory to sort") do |dir|
115
+ options[:directory] = dir
116
+ end
83
117
  end.parse!
84
118
 
85
119
  if options[:file]
86
120
  sorter = FileSorter.new(options[:file])
87
121
  sorter.sort_and_format_imports
88
122
  puts "Imports sorted in #{options[:file]}"
123
+ elsif options[:directory]
124
+ count = 0
125
+ Dir.glob("#{options[:directory]}/**/*.rb").each do |file|
126
+ count += 1
127
+ sorter = FileSorter.new(file)
128
+ sorter.sort_and_format_imports
129
+ end
130
+ puts "Sorted imports in #{count} files in directory: #{options[:directory]}"
89
131
  else
90
132
  puts "Please specify a file using -f or --file"
91
133
  end
data/sample.rb CHANGED
@@ -1,20 +1,4 @@
1
- require 'csv'
2
- require 'json'
3
- require_relative 'a_file'
4
- require_relative 'b_file'
5
- include SomeModule
6
- extend AnotherModule
7
- autoload :CSV, 'csv'
8
- using SomeRefinement
9
- class A
10
-
11
- end
12
-
13
-
14
-
15
-
16
-
17
-
18
- class B
19
-
20
- end
1
+ require_relative "aa"
2
+ require_relative "aaaa"
3
+ require_relative "aaaaac"
4
+ require_relative "abc"
@@ -0,0 +1,11 @@
1
+ module Isort
2
+ class FileSorter
3
+ @file_path: untyped
4
+
5
+ def sort_and_format_imports: -> untyped
6
+
7
+ def sort_directory: -> untyped
8
+
9
+ def sort_imports: -> untyped
10
+ end
11
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - abhinvv1
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-28 00:00:00.000000000 Z
11
+ date: 2024-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: optparse
@@ -67,7 +67,10 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
69
  description: Isort automatically sorts and organizes your Ruby imports, including
70
- require, require_relative, include, and extend statements
70
+ require, require_relative, include, using, and extend statements.Has the ability
71
+ to group different types of imports together. It can process single files or entire
72
+ directories directly throughcommand-line interface, and preserve the code comments
73
+ and inline documentation
71
74
  email:
72
75
  - abhinav.p@browserstack.com
73
76
  executables:
@@ -82,18 +85,18 @@ files:
82
85
  - README.md
83
86
  - Rakefile
84
87
  - exe/isort
85
- - isort-0.1.0.gem
86
88
  - lib/isort.rb
87
89
  - lib/isort/version.rb
88
90
  - sample.rb
89
91
  - sig/isort.rbs
90
- homepage: https://github.com/abhinvv1/sort
92
+ - sig/isort/file_sorter.rbs
93
+ homepage: https://github.com/abhinvv1/isort
91
94
  licenses:
92
95
  - MIT
93
96
  metadata:
94
- homepage_uri: https://github.com/abhinvv1/sort
95
- source_code_uri: https://github.com/abhinvv1/sort
96
- changelog_uri: https://github.com/abhinvv1/sort
97
+ homepage_uri: https://github.com/abhinvv1/isort
98
+ source_code_uri: https://github.com/abhinvv1/isort
99
+ changelog_uri: https://github.com/abhinvv1/isort
97
100
  post_install_message:
98
101
  rdoc_options: []
99
102
  require_paths:
data/isort-0.1.0.gem DELETED
File without changes