synvert-core 1.7.0 → 1.8.1

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: 4e0e8e77a7f85aa23300c28aaf44fca1d6b0a7ffad9177bc9925cfd7541f4e65
4
- data.tar.gz: eb1b9267dbf8264a9c5978dd019dc0c26fcc6613cf0413b767c8fdf4c763cb00
3
+ metadata.gz: 6007d28c9a4371057b72bfbd4e73c23163cf52c384e7cef0a3fcdcd71b384fcd
4
+ data.tar.gz: 5b43685d36948aaf52dd313fdf0e6198eefa73ff12813853d1f289577f58da07
5
5
  SHA512:
6
- metadata.gz: 26a85edf49b07f5afc0688152bf63bf65966e72f15d58da615b8e4b6575e28a74978113a9dbbed78f0cf076e0d0a5d54d486ef83af6a46a0ce7e1b69547b33c1
7
- data.tar.gz: 21bb822480bd6ea79725d27eabb48c3306da71dec81fb817c8d94027b7457e5ae1ed3d78d9d5d870c103f173e2a3bc00e8b45273c66bbd4dfcb259e2ada254bc
6
+ metadata.gz: bc762494a28bc325231e1903130fbd14056442646b463b2e336426e59830f2ee8216fb6caa29c50960fed82a75bc320d4196919b0bffb9ab2547674f19bc89ac
7
+ data.tar.gz: 60e6b882a9b039c42acecb27581fca198482eeef15628ac9123daf4e08dd0bc109ebff3a1e5c2c7ac62ad8fd4fc7e6fdf65871d83b98a3a90c32b71702ff4057
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.8.1 (2022-09-17)
4
+
5
+ * Fix test snippet, return test results
6
+
7
+ ## 1.8.0 (2022-09-17)
8
+
9
+ * Rename config `path` to `root_path`
10
+ * Rename config `skip_files` to `skip_paths`
11
+ * Add config `only_paths`
12
+ * Change dir to `root_path`
13
+
3
14
  ## 1.7.0 (2022-09-16)
4
15
 
5
16
  * Add `Rewriter#test`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.7.0)
4
+ synvert-core (1.8.1)
5
5
  activesupport (< 7.0.0)
6
6
  erubis
7
7
  node_mutation
@@ -4,23 +4,31 @@ module Synvert::Core
4
4
  # Synvert global configuration.
5
5
  class Configuration
6
6
  class << self
7
- # @!attribute [w] path
8
- # @!attribute [w] skip_files
7
+ # @!attribute [w] root_path
8
+ # @!attribute [w] skip_paths
9
+ # @!attribute [w] only_paths
9
10
  # @!attribute [w] show_run_process
10
- attr_writer :path, :skip_files, :show_run_process
11
+ attr_writer :root_path, :skip_paths, :only_paths, :show_run_process
11
12
 
12
13
  # Get the path.
13
14
  #
14
15
  # @return [String] default is '.'
15
- def path
16
- @path || '.'
16
+ def root_path
17
+ @root_path || '.'
17
18
  end
18
19
 
19
- # Get a list of skip files.
20
+ # Get a list of skip paths.
20
21
  #
21
22
  # @return [Array<String>] default is [].
22
- def skip_files
23
- @skip_files || []
23
+ def skip_paths
24
+ @skip_paths || []
25
+ end
26
+
27
+ # Get a list of only paths.
28
+ #
29
+ # @return [Array<String>] default is [].
30
+ def only_paths
31
+ @only_paths || []
24
32
  end
25
33
 
26
34
  # Check if show run process.
@@ -22,12 +22,12 @@ module Synvert::Core
22
22
  #
23
23
  # @return [Boolean] true if matches, otherwise false.
24
24
  def match?
25
- gemfile_lock_path = File.expand_path(File.join(Configuration.path, 'Gemfile.lock'))
25
+ gemfile_lock_path = File.expand_path(File.join(Configuration.root_path, 'Gemfile.lock'))
26
26
 
27
27
  # if Gemfile.lock does not exist, just ignore this check
28
28
  return true unless File.exist?(gemfile_lock_path)
29
29
 
30
- ENV['BUNDLE_GEMFILE'] = Configuration.path # make sure bundler reads Gemfile.lock in the correct path
30
+ ENV['BUNDLE_GEMFILE'] = Configuration.root_path # make sure bundler reads Gemfile.lock in the correct path
31
31
  parser = Bundler::LockfileParser.new(File.read(gemfile_lock_path))
32
32
  parser.specs.any? { |spec| Gem::Dependency.new(@name, @version).match?(spec) }
33
33
  end
@@ -32,12 +32,8 @@ module Synvert::Core
32
32
  # It finds specified files, for each file, it executes the block code, rewrites the original code,
33
33
  # then writes the code back to the original file.
34
34
  def process
35
- @file_patterns.each do |file_pattern|
36
- Dir.glob(File.join(Configuration.path, file_pattern)).each do |file_path|
37
- next if Configuration.skip_files.include?(file_path)
38
-
39
- process_file(file_path)
40
- end
35
+ get_file_paths.each do |file_path|
36
+ process_file(file_path)
41
37
  end
42
38
  end
43
39
 
@@ -45,13 +41,7 @@ module Synvert::Core
45
41
  # It finds specified files, for each file, it executes the block code, tests the original code,
46
42
  # then returns the actions.
47
43
  def test
48
- paths = @file_patterns.flat_map do |file_pattern|
49
- Dir.glob(File.join(Configuration.path, file_pattern))
50
- end
51
-
52
- paths.uniq.map do |file_path|
53
- next if Configuration.skip_files.include?(file_path)
54
-
44
+ get_file_paths.map do |file_path|
55
45
  test_file(file_path)
56
46
  end
57
47
  end
@@ -424,6 +414,36 @@ module Synvert::Core
424
414
  end
425
415
  end
426
416
 
417
+ # Get file paths.
418
+ # @return [Array<String>] file paths
419
+ def get_file_paths
420
+ Dir.chdir(Configuration.root_path) do
421
+ only_paths = Configuration.only_paths.size > 0 ? Configuration.only_paths : ["."]
422
+ only_paths.flat_map do |only_path|
423
+ @file_patterns.flat_map do |file_pattern|
424
+ pattern = only_path == "." ? file_pattern : File.join(only_path, file_pattern)
425
+ Dir.glob(pattern) - get_skip_files
426
+ end
427
+ end
428
+ end
429
+ end
430
+
431
+ # Get skip files.
432
+ # @return [Array<String>] skip files
433
+ def get_skip_files
434
+ @skip_files ||= Configuration.skip_paths.flat_map do |skip_path|
435
+ if File.directory?(skip_path)
436
+ Dir.glob(File.join(skip_path, "**/*"))
437
+ elsif File.file?(skip_path)
438
+ [skip_path]
439
+ elsif skip_path.end_with?("**") || skip_path.end_with?("**/")
440
+ Dir.glob(File.join(skip_path, "*"))
441
+ else
442
+ Dir.glob(skip_path)
443
+ end
444
+ end
445
+ end
446
+
427
447
  # Read file source.
428
448
  # @param file_path [String] file path
429
449
  # @return [String] file source
@@ -16,14 +16,14 @@ module Synvert::Core
16
16
  #
17
17
  # @return [Boolean] true if matches, otherwise false.
18
18
  def match?
19
- if File.exist?(File.join(Configuration.path, '.ruby-version'))
19
+ if File.exist?(File.join(Configuration.root_path, '.ruby-version'))
20
20
  version_file = '.ruby-version'
21
- elsif File.exist?(File.join(Configuration.path, '.rvmrc'))
21
+ elsif File.exist?(File.join(Configuration.root_path, '.rvmrc'))
22
22
  version_file = '.rvmrc'
23
23
  end
24
24
  return true unless version_file
25
25
 
26
- version = File.read(File.join(Configuration.path, version_file))
26
+ version = File.read(File.join(Configuration.root_path, version_file))
27
27
  Gem::Version.new(version) >= Gem::Version.new(@version)
28
28
  end
29
29
  end
@@ -76,7 +76,7 @@ module Synvert::Core
76
76
  # @option options [Boolean] :run_instance (true) process the instance.
77
77
  # @return [Synvert::Core::Rewriter] the registered rewriter.
78
78
  # @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
79
- def call(group, name, options = {})
79
+ def call(group, name, options = { run_instance: true })
80
80
  rewriter = fetch(group, name)
81
81
  if options[:run_instance]
82
82
  rewriter.process
@@ -274,7 +274,7 @@ module Synvert::Core
274
274
  def add_file(filename, content)
275
275
  return unless @options[:run_instance]
276
276
 
277
- filepath = File.join(Configuration.path, filename)
277
+ filepath = File.join(Configuration.root_path, filename)
278
278
  if File.exist?(filepath)
279
279
  puts "File #{filepath} already exists."
280
280
  return
@@ -293,7 +293,7 @@ module Synvert::Core
293
293
  def remove_file(filename)
294
294
  return unless @options[:run_instance]
295
295
 
296
- file_path = File.join(Configuration.path, filename)
296
+ file_path = File.join(Configuration.root_path, filename)
297
297
  File.delete(file_path) if File.exist?(file_path)
298
298
  end
299
299
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.7.0'
5
+ VERSION = '1.8.1'
6
6
  end
7
7
  end
@@ -205,7 +205,7 @@ module Synvert::Core
205
205
  assert post.valid?
206
206
  end
207
207
  EOS
208
- expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb'])
208
+ expect(Dir).to receive(:glob).with('spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb'])
209
209
  expect(File).to receive(:read).with('spec/models/post_spec.rb', encoding: 'UTF-8').and_return(input)
210
210
  expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
211
211
  instance.process
@@ -230,7 +230,7 @@ module Synvert::Core
230
230
  config.include FactoryGirl::Syntax::Methods
231
231
  end
232
232
  EOS
233
- expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
233
+ expect(Dir).to receive(:glob).with('spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
234
234
  expect(File).to receive(:read).with('spec/spec_helper.rb', encoding: 'UTF-8').and_return(input)
235
235
  expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
236
236
  instance.process
@@ -257,7 +257,7 @@ module Synvert::Core
257
257
  assert post.valid?
258
258
  end
259
259
  EOS
260
- expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb']).twice
260
+ expect(Dir).to receive(:glob).with('spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb']).twice
261
261
  expect(File).to receive(:read).with('spec/models/post_spec.rb', encoding: 'UTF-8').and_return(input)
262
262
  expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
263
263
  expect(File).to receive(:read).with('spec/models/post_spec.rb', encoding: 'UTF-8').and_return(output)
@@ -284,7 +284,7 @@ module Synvert::Core
284
284
  assert post.valid?
285
285
  end
286
286
  EOS
287
- expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb'])
287
+ expect(Dir).to receive(:glob).with('spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb'])
288
288
  expect(File).to receive(:read).with('spec/models/post_spec.rb', encoding: 'UTF-8').and_return(input)
289
289
  results = instance.test
290
290
  expect(results[0].file_path).to eq 'spec/models/post_spec.rb'
@@ -308,7 +308,7 @@ module Synvert::Core
308
308
  config.include FactoryGirl::Syntax::Methods
309
309
  end
310
310
  EOS
311
- expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
311
+ expect(Dir).to receive(:glob).with('spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
312
312
  expect(File).to receive(:read).with('spec/spec_helper.rb', encoding: 'UTF-8').and_return(input)
313
313
  result = instance.test
314
314
  expect(result[0].file_path).to eq 'spec/spec_helper.rb'
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.7.0
4
+ version: 1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-16 00:00:00.000000000 Z
11
+ date: 2022-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport