rake_script 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rake_script/file_system.rb +55 -2
- data/lib/rake_script/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb8f2fb9d883a772838e6a3ea624031319db197ed8a7909b881b3f5abb5ae390
|
4
|
+
data.tar.gz: 1dcac5081bb2f0e7dc07beb2d1a5646700eb21510a3ccb17102e9f7940a494d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
data/lib/rake_script/version.rb
CHANGED