synvert-core 1.7.0 → 1.8.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: 4e0e8e77a7f85aa23300c28aaf44fca1d6b0a7ffad9177bc9925cfd7541f4e65
4
- data.tar.gz: eb1b9267dbf8264a9c5978dd019dc0c26fcc6613cf0413b767c8fdf4c763cb00
3
+ metadata.gz: 00661750a3522a9983d5eec1f064f62bf0d335e05e3b2a9cb7bb7222b04e20a2
4
+ data.tar.gz: 5a35b831ca678486f4a51db2ae8a57bed58617299a0c26c9cbc0c462351ffd3d
5
5
  SHA512:
6
- metadata.gz: 26a85edf49b07f5afc0688152bf63bf65966e72f15d58da615b8e4b6575e28a74978113a9dbbed78f0cf076e0d0a5d54d486ef83af6a46a0ce7e1b69547b33c1
7
- data.tar.gz: 21bb822480bd6ea79725d27eabb48c3306da71dec81fb817c8d94027b7457e5ae1ed3d78d9d5d870c103f173e2a3bc00e8b45273c66bbd4dfcb259e2ada254bc
6
+ metadata.gz: b18bcf0f9f344c2c8661d7e4edf1a4f768d67175f45b34d2bb37b321f38681840c29b8e9a0fa61ee295329909f3093de4b7703e6dec21066190336ee8ceb1b5a
7
+ data.tar.gz: 0f334dc34d82d15235381f3a9be7651707b5a7a0e5614e29f48310407c70384ac6f1594f2b8643dc97577e0e55a4b6165f3174071e2887f424a76ed4d5f3b3cb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.8.0 (2022-09-17)
4
+
5
+ * Rename config `path` to `root_path`
6
+ * Rename config `skip_files` to `skip_paths`
7
+ * Add config `only_paths`
8
+ * Change dir to `root_path`
9
+
3
10
  ## 1.7.0 (2022-09-16)
4
11
 
5
12
  * 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.0)
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.each 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.0'
6
6
  end
7
7
  end
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.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: 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