Module: RakeScript::FileSystem
- Included in:
- RakeMethods
- Defined in:
- lib/rake_script/file_system.rb
Instance Method Summary collapse
- #append_file(filepath, string, verbose: false, before: nil, after: nil, safe: false) ⇒ Object
- #chdir(path) ⇒ Object
- #copy(from, to, options = {}) ⇒ Object
- #copy_r(from, to, options = {}) ⇒ Object
- #create_dir_p(*paths) ⇒ Object
- #failure!(safe = false, verbose = false, &block) ⇒ nil
-
#folder_exist?(path) ⇒ TruClass, FalseClass
Check if folder exists.
- #posix_flags(*flags) ⇒ Object
- #puts_verbose(msg, color: :red, style: :normal) ⇒ Object
- #remove_rf(*paths) ⇒ Object
- #replace_file(filepath, new_string, old:, verbose: false, safe: false) ⇒ Object
Instance Method Details
#append_file(filepath, string, verbose: false, before: nil, after: nil, safe: false) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rake_script/file_system.rb', line 57 def append_file(filepath, string, verbose: false, before: nil, after: nil, safe: false) raise ArgumentError, "can't use :before and :after in same time" if before && after unless File.exist?(filepath) return failure!(safe, verbose) { "can't find file at #{filepath}" } end after_index = nil if after || before content = File.read(filepath) after_index = content.index(after || before) return if after_index.nil? end after_index -= before.size if after_index && before File.open(filepath, 'a+') do |f| f.seek(after_index, IO::SEEK_SET) if after_index f.write(string) end puts_verbose("Append #{string.inspect} to #{filepath}") if verbose end |
#chdir(path) ⇒ Object
53 54 55 |
# File 'lib/rake_script/file_system.rb', line 53 def chdir(path) Dir.chdir(path) { yield } end |
#copy(from, to, options = {}) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/rake_script/file_system.rb', line 39 def copy(from, to, = {}) # from = from.include?('*') ? Dir.glob(from) : [from] # from.each { |f| FileUtils.cp(f, to, options) } flags = posix_flags('-v': !![:verbose]) cmd('cp', *flags, from, to) end |
#copy_r(from, to, options = {}) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/rake_script/file_system.rb', line 46 def copy_r(from, to, = {}) # from = from.include?('*') ? Dir.glob(from) : [from] # from.each { |f| FileUtils.cp_r(f, to, options) } flags = posix_flags('-r', '-v': !![:verbose]) cmd('cp', *flags, from, to) end |
#create_dir_p(*paths) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/rake_script/file_system.rb', line 32 def create_dir_p(*paths) = paths.last.is_a?(Hash) ? paths.pop : {} # FileUtils.mkdir_p(paths, options) flags = posix_flags('-p', '-v': !![:verbose]) cmd('mkdir', *flags, *paths) end |
#failure!(safe = false, verbose = false, &block) ⇒ nil
87 88 89 90 91 |
# File 'lib/rake_script/file_system.rb', line 87 def failure!(safe = false, verbose = false, &block) raise ArgumentError, block.call unless safe puts_verbose("[Error] #{block.call}") if verbose nil end |
#folder_exist?(path) ⇒ TruClass, FalseClass
Check if folder exists.
8 9 10 |
# File 'lib/rake_script/file_system.rb', line 8 def folder_exist?(path) File.directory?(path) end |
#posix_flags(*flags) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rake_script/file_system.rb', line 12 def posix_flags(*flags) result = [] flags.each do |flag| if flag.is_a?(Hash) result.concat( flag.reject { |_, v| !v }.map { |k, v| v.is_a?(TrueClass) ? k.to_s : "#{k}=#{v}" } ) else result.push(flag.to_s) end end result end |
#puts_verbose(msg, color: :red, style: :normal) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/rake_script/file_system.rb', line 93 def puts_verbose(msg, color: :red, style: :normal) if respond_to?(:puts_colored) puts_colored(msg, color: color, style: style) else puts(msg) end end |
#remove_rf(*paths) ⇒ Object
26 27 28 29 30 |
# File 'lib/rake_script/file_system.rb', line 26 def remove_rf(*paths) = paths.last.is_a?(Hash) ? paths.pop : {} flags = posix_flags('-rf', '-v': !![:verbose]) cmd('rm', *flags, *paths) end |
#replace_file(filepath, new_string, old:, verbose: false, safe: false) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rake_script/file_system.rb', line 101 def replace_file(filepath, new_string, old:, verbose: false, safe: false) unless File.exist?(filepath) return failure!(safe, verbose) { "can't find file at #{filepath}" } end content = File.read(filepath) index_start = content.index(old) if index_start.nil? return failure!(safe, verbose) { "can't find #{old.inspect} in #{filepath}" } end if old.is_a?(Regexp) old_string = old.match(content[index_start..-1]).to_a.first else old_string = old end index_end = index_start + old_string.size new_content = content[0..(index_start - 1)] + new_string + content[index_end..-1] # File.write(filepath, new_content, mode: 'w') File.open(filepath, 'w') do |f| f.sync = true f.write(new_content) end puts_verbose("Replace #{old.inspect} with #{new_string.inspect} in #{filepath}") if verbose end |