geny 2.1.0 → 2.1.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
  SHA1:
3
- metadata.gz: 26e05f21ea0266a46ce4c2cdf05d3fd46be21cce
4
- data.tar.gz: 5ab64929a864a15791f9a7a4604eda223619c7eb
3
+ metadata.gz: 558b6cc3a2985012f76f27a23c8a1a338aef4bdf
4
+ data.tar.gz: a859475b06d94fdba962beb25b0ffece71eb1119
5
5
  SHA512:
6
- metadata.gz: 312151d3e72663e279482d451a2e72400f8ee7d735a61bd063c35755af9967dc10bec11e0725744824378fab911d073831a2f52160ff4f76ad4888d763ddd0cb
7
- data.tar.gz: fd10235c2db8e2bbdac6df3aff16a71a9c61dce8bb589d1d1af2b211b63be2f4edb98d92bfb07d0e9365d755660cb7cfb2959dec678a26b8c08c383f74e65de8
6
+ metadata.gz: cd59935ac74a3cdb86e94e93c61e2154a5a62db930c0f699aef8ff5d9d825d2dc78da99e2a5e94cd103c410b12ac5aac9aac1ff480d2ff5ab4f2ba79b546d9dc
7
+ data.tar.gz: 0c4465488a3d5920d51659e399cbed3d972179ea7d528c79e9ab92e4f3fffca154565858e78b2f99c674fbf5cd72696ecf2970b6b9ae1dc4c8a8fc803f965f19
@@ -4,6 +4,7 @@ require "tty-file"
4
4
 
5
5
  module Geny
6
6
  module Actions
7
+ # Utilities for manipulating files in bulk
7
8
  class Find
8
9
  # Replace the content of any file that matches the pattern with
9
10
  # a replacement
@@ -17,21 +18,27 @@ module Geny
17
18
  # @example
18
19
  # find.replace("src", "Boilerplate", "YourProject")
19
20
  def replace(root, pattern, replacement, force: false, excluding: nil)
20
- ::Find.find(root)
21
- .lazy
22
- .reject { |path| path == root }
23
- .reject { |path| excluding && path.match?(excluding) }
24
- .reject { |path| File.directory?(path) }
25
- .reject { |path| TTY::File.binary?(path) }
26
- .each do |path|
27
- TTY::File.replace_in_file(
28
- path,
29
- pattern,
30
- replacement,
31
- verbose: false,
32
- force: force
33
- )
34
- end
21
+ ::Find.find(root) do |path|
22
+ # The first emitted path will be the root directory
23
+ next if path == root
24
+
25
+ # Don't look any further into this directory
26
+ ::Find.prune if excluding && excluding.match?(excluding)
27
+
28
+ # We don't care about directories
29
+ next if File.directory?(path)
30
+
31
+ # We can't replace binary files
32
+ next if TTY::File.binary?(path)
33
+
34
+ TTY::File.replace_in_file(
35
+ path,
36
+ pattern,
37
+ replacement,
38
+ verbose: false,
39
+ force: force
40
+ )
41
+ end
35
42
  end
36
43
 
37
44
  # Replace any filename that matches the pattern with a replacement
@@ -44,28 +51,32 @@ module Geny
44
51
  # @example
45
52
  # find.rename("src", "Boilerplate", "YourProject")
46
53
  def rename(root, pattern, replacement, force: false, excluding: nil)
47
- ::Find.find(root)
48
- .lazy
49
- .reject { |path| path == root }
50
- .reject { |path| excluding && path.match?(excluding) }
51
- .select { |path| path.match?(pattern) }
52
- .sort { |path| depth(path) }
53
- .map { |path| [path, rsub(path, pattern, replacement)] }
54
- .each { |source, dest| FileUtils.mv(source, dest) }
55
- end
54
+ matches = []
55
+ options = {force: force, excluding: nil}
56
56
 
57
- private
57
+ ::Find.find(root) do |path|
58
+ # The first emitted path will be the root directory
59
+ next if path == root
58
60
 
59
- # Replace the last match of a string
60
- def rsub(str, pattern, replacement)
61
- prefix, match, suffix = str.rpartition(pattern)
62
- return str if prefix.empty? and match.empty?
63
- "#{prefix}#{replacement}#{suffix}"
64
- end
61
+ # Don't look any further into this directory
62
+ ::Find.prune if excluding && excluding.match?(excluding)
63
+
64
+ # The path doesn't match, keep searching
65
+ next unless path.match?(pattern)
66
+
67
+ # Record the match
68
+ matches << path
69
+
70
+ # Don't search the children of the match, because it will
71
+ # be renamed. We'll have re-run against the renamed path
72
+ ::Find.prune
73
+ end
65
74
 
66
- # Get the depth of a path
67
- def depth(path)
68
- path.split(File::SEPARATOR).length
75
+ matches.each do |source|
76
+ dest = source.sub(pattern, replacement)
77
+ FileUtils.mv(source, dest, force: force)
78
+ rename(dest, pattern, replacement, options) if File.directory?(dest)
79
+ end
69
80
  end
70
81
  end
71
82
  end
data/lib/geny/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Geny
2
- VERSION = "2.1.0"
2
+ VERSION = "2.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geny
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Zane
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-13 00:00:00.000000000 Z
11
+ date: 2019-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: argy