rake_script 0.3.0 → 0.4.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
  SHA256:
3
- metadata.gz: 8cc103a638ff21e24588737201910ec53ed682abb285567f0cd6e21bd5e6b66e
4
- data.tar.gz: b11cbc1465ce68e498321533470f423764e171e4d5e3addcad1f9f25c3790ae7
3
+ metadata.gz: bb8f2fb9d883a772838e6a3ea624031319db197ed8a7909b881b3f5abb5ae390
4
+ data.tar.gz: 1dcac5081bb2f0e7dc07beb2d1a5646700eb21510a3ccb17102e9f7940a494d7
5
5
  SHA512:
6
- metadata.gz: c0ea2e5bc75e86bf9b7445873493e9613bc32249260eaec5714f2a66077c0e5dbbf421dac0036aacc8e020b6aa1ac01c6cfeed0e336e120e31d6bc51e208328b
7
- data.tar.gz: 3e1325f937068bf7afa760c79bc41d8ee1b305f07973dcaaad8c7aa79c061b0146506f6430548d49d7c39c2f190e99108627afd1783ac532a0a48a6f374f955b
6
+ metadata.gz: a64af8382fa822d6cc9f01bea71a8f41dcf2e4a805759304027fe3767a750e36620c174e924edd360365aeb1c62612efa4dae5c6c878da866fbffebf94506230
7
+ data.tar.gz: a70b64a07a0b68373f64f055d211bc7db3e78b05f91f091212b3af0cbeab25623e2d677408adf291f36f644c3c70f38ecfd394df7d53d09a3c19d504c0530a74
@@ -54,9 +54,13 @@ module RakeScript
54
54
  Dir.chdir(path) { yield }
55
55
  end
56
56
 
57
- def append_file(filepath, string, verbose: false, before: nil, after: nil)
57
+ def append_file(filepath, string, verbose: false, before: nil, after: nil, safe: false)
58
58
  raise ArgumentError, "can't use :before and :after in same time" if before && after
59
59
 
60
+ unless File.exist?(filepath)
61
+ return failure!(safe, verbose) { "can't find file at #{filepath}" }
62
+ end
63
+
60
64
  after_index = nil
61
65
  if after || before
62
66
  content = File.read(filepath)
@@ -69,7 +73,56 @@ module RakeScript
69
73
  f.seek(after_index, IO::SEEK_SET) if after_index
70
74
  f.write(string)
71
75
  end
72
- puts("Append #{string.inspect} to #{filepath}") if verbose
76
+ puts_verbose("Append #{string.inspect} to #{filepath}") if verbose
77
+ end
78
+
79
+ # @param safe [Boolean]
80
+ # @param verbose [Boolean]
81
+ # @raise [ArgumentError]
82
+ # @return [nil]
83
+ # @example
84
+ # unless File.exist?(filepath)
85
+ # return failure!(true, true) { "can't find file at #{filepath}" }
86
+ # end
87
+ def failure!(safe = false, verbose = false, &block)
88
+ raise ArgumentError, block.call unless safe
89
+ puts_verbose("[Error] #{block.call}") if verbose
90
+ nil
91
+ end
92
+
93
+ def puts_verbose(msg, color: :red, style: :normal)
94
+ if respond_to?(:puts_colored)
95
+ puts_colored(msg, color: color, style: style)
96
+ else
97
+ puts(msg)
98
+ end
99
+ end
100
+
101
+ def replace_file(filepath, new_string, old:, verbose: false, safe: false)
102
+ unless File.exist?(filepath)
103
+ return failure!(safe, verbose) { "can't find file at #{filepath}" }
104
+ end
105
+
106
+ content = File.read(filepath)
107
+ index_start = content.index(old)
108
+ if index_start.nil?
109
+ return failure!(safe, verbose) { "can't find #{old.inspect} in #{filepath}" }
110
+ end
111
+
112
+ if old.is_a?(Regexp)
113
+ old_string = old.match(content[index_start..-1]).to_a.first
114
+ else
115
+ old_string = old
116
+ end
117
+ index_end = index_start + old_string.size
118
+ new_content = content[0..(index_start - 1)] + new_string + content[index_end..-1]
119
+ # File.write(filepath, new_content, mode: 'w')
120
+ File.open(filepath, 'w') do |f|
121
+ f.sync = true
122
+ f.write(new_content)
123
+ end
124
+
125
+ puts_verbose("Replace #{old.inspect} with #{new_string.inspect} in #{filepath}") if verbose
73
126
  end
74
127
  end
75
128
  end
@@ -1,3 +1,3 @@
1
1
  module RakeScript
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rake_script
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Talakevich