synvert-core 1.32.1 → 1.33.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e3c2e2aa7e3cb3eb9d99c5e2a17a92dafc5e0fe6772329e96fb6a80483a5abf5
4
- data.tar.gz: 92df973552a4554bb59d9d56296cca6fe8e57a3dabd1d424b3344fa10f20651c
3
+ metadata.gz: 279c659ed3a5da5cda7a67c95ef3cc74601d2f64d7dc70ae29431ef8ec4a9929
4
+ data.tar.gz: 65caaa8ccc16f8789137b8269ad5ec45c6fe1c9d9198dc0b940b09ad3e3e06cd
5
5
  SHA512:
6
- metadata.gz: 98be499a994210b2ccbd76d73b6d97a2fd9ea46724aef6d329694662e84186c4cf9ecb7beac15102d27881c5db4337fd8f485b54aff012a6e3eebcedc19c9766
7
- data.tar.gz: 1e2cf1d95019f79ac85e2344f00ef37aac814a2eaec3fbf508c962050d1cc2e64d9d553be4126688fb44978fae680c73de5b050e4f4fc78919fd28942cf4b011
6
+ metadata.gz: f45bf64e55621191bfe3a17c689d337d0b25fd356217dd8965876540514bb4b5d04cabb25a834b0567dfba538c93095b12ba281cc02c90a596ec34f8b3dc9231
7
+ data.tar.gz: 45c5c88b22be8e616cda46e518ee304333d3a8973f58b174182950eef2b958b16ed880311085cf63dc022fe79f8ec3e45fab3a32d23891db829dc5ba8066e075
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.33.0 (2024-02-18)
4
+
5
+ * Add `Configuration.respect_gitignore`
6
+ * Glob files with `git check-ignore` if `Configuration.respect_gitignore` is true
7
+
3
8
  ## 1.32.1 (2024-02-17)
4
9
 
5
10
  * Update `node_mutation` to 1.23.2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.32.1)
4
+ synvert-core (1.33.0)
5
5
  activesupport (< 7.0.0)
6
6
  node_mutation (>= 1.23.2)
7
7
  node_query (>= 1.15.1)
@@ -7,6 +7,7 @@ module Synvert::Core
7
7
  # @!attribute [w] root_path
8
8
  # @!attribute [w] skip_paths
9
9
  # @!attribute [w] only_paths
10
+ # @!attribute [w] respect_gitignore
10
11
  # @!attribute [w] show_run_process
11
12
  # @!attribute [w] number_of_workers
12
13
  # @!attribute [w] single_quote
@@ -16,6 +17,7 @@ module Synvert::Core
16
17
  attr_writer :root_path,
17
18
  :skip_paths,
18
19
  :only_paths,
20
+ :respect_gitignore,
19
21
  :show_run_process,
20
22
  :number_of_workers,
21
23
  :single_quote,
@@ -44,6 +46,13 @@ module Synvert::Core
44
46
  @only_paths || []
45
47
  end
46
48
 
49
+ # Check if respect .gitignore
50
+ #
51
+ # @return [Boolean] default is true
52
+ def respect_gitignore
53
+ @respect_gitignore.nil? ? true : @respect_gitignore
54
+ end
55
+
47
56
  # Check if show run process.
48
57
  #
49
58
  # @return [Boolean] default is false
@@ -3,6 +3,7 @@
3
3
  require 'net/http'
4
4
  require 'uri'
5
5
  require 'open-uri'
6
+ require 'open3'
6
7
 
7
8
  module Synvert::Core
8
9
  class Utils
@@ -35,11 +36,21 @@ module Synvert::Core
35
36
  # @return [Array<String>] file paths
36
37
  def glob(file_patterns)
37
38
  Dir.chdir(Configuration.root_path) do
38
- all_files =
39
- file_patterns.flat_map do |file_pattern|
40
- Dir.glob(file_pattern)
39
+ all_files = file_patterns.flat_map { |pattern| Dir.glob(pattern) }
40
+ ignored_files = []
41
+
42
+ if Configuration.respect_gitignore
43
+ Open3.popen3('git check-ignore --stdin') do |stdin, stdout, stderr, wait_thr|
44
+ stdin.puts(all_files.join("\n"))
45
+ stdin.close
46
+
47
+ ignored_files = stdout.read.split("\n")
41
48
  end
42
- filter_only_paths(all_files) - get_skip_files
49
+ end
50
+
51
+ filtered_files = filter_only_paths(all_files - ignored_files)
52
+
53
+ filtered_files -= get_explicitly_skipped_files
43
54
  end
44
55
  end
45
56
 
@@ -85,16 +96,16 @@ module Synvert::Core
85
96
  # Filter only paths with `Configuration.only_paths`.
86
97
  # @return [Array<String>] filtered file paths
87
98
  def filter_only_paths(all_files)
88
- return all_files if Configuration.only_paths.size == 0
99
+ return all_files if Configuration.only_paths.empty?
89
100
 
90
101
  Configuration.only_paths.flat_map do |only_path|
91
102
  all_files.filter { |file_path| file_path.starts_with?(only_path) }
92
103
  end
93
104
  end
94
105
 
95
- # Get skip files.
96
- # @return [Array<String>] skip files
97
- def get_skip_files
106
+ # Get skipped files.
107
+ # @return [Array<String>] skipped files
108
+ def get_explicitly_skipped_files
98
109
  Configuration.skip_paths.flat_map do |skip_path|
99
110
  if File.directory?(skip_path)
100
111
  Dir.glob(File.join(skip_path, "**/*"))
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.32.1'
5
+ VERSION = '1.33.0'
6
6
  end
7
7
  end
@@ -58,16 +58,42 @@ module Synvert::Core
58
58
  before do
59
59
  Configuration.only_paths = []
60
60
  Configuration.skip_paths = []
61
+ Configuration.respect_gitignore = false
61
62
  end
62
63
 
63
- it 'gets all files' do
64
- expect(Dir).to receive(:glob).with('**/*.rb').and_return(
65
- [
66
- 'app/models/post.rb',
67
- 'app/controllers/posts_controller.rb'
68
- ]
69
- )
70
- expect(described_class.glob(['**/*.rb'])).to eq(['app/models/post.rb', 'app/controllers/posts_controller.rb'])
64
+ context 'Configuration.respect_gitignore is false' do
65
+ it 'gets all files' do
66
+ expect(Dir).to receive(:glob).with('**/*.rb').and_return(
67
+ [
68
+ 'app/models/post.rb',
69
+ 'app/controllers/posts_controller.rb'
70
+ ]
71
+ )
72
+ expect(described_class.glob(['**/*.rb'])).to eq(['app/models/post.rb', 'app/controllers/posts_controller.rb'])
73
+ end
74
+ end
75
+
76
+ context 'Configuration.respect_gitignore is true' do
77
+ before do
78
+ Configuration.respect_gitignore = true
79
+ end
80
+
81
+ it 'correctly filters out ignored files' do
82
+ file_patterns = ["**/*.rb"]
83
+ all_files = ["app/models/post.rb", "app/controllers/posts_controller.rb", "app/controllers/temp.tmp"]
84
+ not_ignored_files = ["app/models/post.rb", "app/controllers/posts_controller.rb"]
85
+ command_output = "app/controllers/temp.tmp\n"
86
+
87
+ allow(Open3).to receive(:popen3).with('git check-ignore --stdin').and_yield(
88
+ instance_double(IO, puts: nil, close: nil),
89
+ StringIO.new(command_output),
90
+ StringIO.new(''),
91
+ instance_double(Process::Waiter, value: instance_double(Process::Status, success?: true))
92
+ )
93
+ allow(Dir).to receive(:glob).and_return(all_files)
94
+
95
+ expect(described_class.glob(file_patterns)).to match_array(not_ignored_files)
96
+ end
71
97
  end
72
98
 
73
99
  it 'filters only paths' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.32.1
4
+ version: 1.33.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-17 00:00:00.000000000 Z
11
+ date: 2024-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport