geny 2.0.0 → 2.1.0

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
  SHA1:
3
- metadata.gz: beb9f0d7d2aa0eb72a51e348333886960c517a90
4
- data.tar.gz: 8d8abe00a79e1abb08cb9e01290979e8c0a50353
3
+ metadata.gz: 26e05f21ea0266a46ce4c2cdf05d3fd46be21cce
4
+ data.tar.gz: 5ab64929a864a15791f9a7a4604eda223619c7eb
5
5
  SHA512:
6
- metadata.gz: d29982e9852f6505962d7befe63b437d12ff8f800230d293e2206547d941558c6689c93218c78fa4360fafc85511d4981781e51079ce2fa2f98866f2b46d0398
7
- data.tar.gz: 733c66e1014488035c764630bdeb5eb71e07745d5e23bed939eeecfcb189f368f1e3115d16eb26ce74a740b48f80f7c71c7ad85e21918dc1be9f59208c81e459
6
+ metadata.gz: 312151d3e72663e279482d451a2e72400f8ee7d735a61bd063c35755af9967dc10bec11e0725744824378fab911d073831a2f52160ff4f76ad4888d763ddd0cb
7
+ data.tar.gz: fd10235c2db8e2bbdac6df3aff16a71a9c61dce8bb589d1d1af2b211b63be2f4edb98d92bfb07d0e9365d755660cb7cfb2959dec678a26b8c08c383f74e65de8
@@ -0,0 +1,72 @@
1
+ require "find"
2
+ require "fileutils"
3
+ require "tty-file"
4
+
5
+ module Geny
6
+ module Actions
7
+ class Find
8
+ # Replace the content of any file that matches the pattern with
9
+ # a replacement
10
+ # @param root [String] the path to search
11
+ # @param pattern [String,Regexp] a pattern to find
12
+ # @param replacement [String] the content to insert
13
+ # @param force [TrueClass,FalseClass] overwrite existing files
14
+ # @param excluding [Regexp] exclude any file matching this pattern
15
+ # @note This attempts to ignore binary files
16
+ #
17
+ # @example
18
+ # find.replace("src", "Boilerplate", "YourProject")
19
+ 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
35
+ end
36
+
37
+ # Replace any filename that matches the pattern with a replacement
38
+ # @param root [String] the path to search
39
+ # @param pattern [String,Regexp] a pattern to find
40
+ # @param replacement [String] the content to insert
41
+ # @param force [TrueClass,FalseClass] overwrite existing files
42
+ # @param excluding [Regexp] exclude any file matching this pattern
43
+ #
44
+ # @example
45
+ # find.rename("src", "Boilerplate", "YourProject")
46
+ 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
56
+
57
+ private
58
+
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
65
+
66
+ # Get the depth of a path
67
+ def depth(path)
68
+ path.split(File::SEPARATOR).length
69
+ end
70
+ end
71
+ end
72
+ end
@@ -5,6 +5,7 @@ require "geny/actions/geny"
5
5
  require "geny/actions/git"
6
6
  require "geny/actions/shell"
7
7
  require "geny/actions/files"
8
+ require "geny/actions/find"
8
9
  require "geny/actions/templates"
9
10
 
10
11
  module Geny
@@ -32,6 +33,12 @@ module Geny
32
33
  Actions::Files.new
33
34
  end
34
35
 
36
+ # A utility for bulk find-and-replace operations
37
+ # @return [Actions::Find]
38
+ def find
39
+ Actions::Find.new
40
+ end
41
+
35
42
  # A utility for running shell commands
36
43
  # @return [Actions::Shell]
37
44
  def shell
data/lib/geny/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Geny
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geny
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Zane
@@ -147,6 +147,7 @@ files:
147
147
  - lib/generators/new/templates/generator.rb.erb
148
148
  - lib/geny.rb
149
149
  - lib/geny/actions/files.rb
150
+ - lib/geny/actions/find.rb
150
151
  - lib/geny/actions/geny.rb
151
152
  - lib/geny/actions/git.rb
152
153
  - lib/geny/actions/shell.rb