module RakeScript::FileSystem

Public Instance Methods

append_file(filepath, string, verbose: false, before: nil, after: nil, safe: false) click to toggle source
# 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) { || ... } click to toggle source
# File lib/rake_script/file_system.rb, line 53
def chdir(path)
  Dir.chdir(path) { yield }
end
copy(from, to, options = {}) click to toggle source
# File lib/rake_script/file_system.rb, line 39
def copy(from, to, options = {})
  # from = from.include?('*') ? Dir.glob(from) : [from]
  # from.each { |f| FileUtils.cp(f, to, options) }
  flags = posix_flags('-v': !!options[:verbose])
  cmd('cp', *flags, from, to)
end
copy_r(from, to, options = {}) click to toggle source
# File lib/rake_script/file_system.rb, line 46
def copy_r(from, to, options = {})
  # from = from.include?('*') ? Dir.glob(from) : [from]
  # from.each { |f| FileUtils.cp_r(f, to, options) }
  flags = posix_flags('-r', '-v': !!options[:verbose])
  cmd('cp', *flags, from, to)
end
create_dir_p(*paths) click to toggle source
# File lib/rake_script/file_system.rb, line 32
def create_dir_p(*paths)
  options = paths.last.is_a?(Hash) ? paths.pop : {}
  # FileUtils.mkdir_p(paths, options)
  flags = posix_flags('-p', '-v': !!options[:verbose])
  cmd('mkdir', *flags, *paths)
end
failure!(safe = false, verbose = false, &block) click to toggle source

@param safe [Boolean] @param verbose [Boolean] @raise [ArgumentError] @return [nil] @example

unless File.exist?(filepath)
  return failure!(true, true) { "can't find file at #{filepath}" }
end
# 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) click to toggle source

Check if folder exists. @param path [String] folder path. @return [TruClass,FalseClass] boolean

# File lib/rake_script/file_system.rb, line 8
def folder_exist?(path)
  File.directory?(path)
end
posix_flags(*flags) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/rake_script/file_system.rb, line 26
def remove_rf(*paths)
  options = paths.last.is_a?(Hash) ? paths.pop : {}
  flags = posix_flags('-rf', '-v': !!options[:verbose])
  cmd('rm', *flags, *paths)
end
replace_file(filepath, new_string, old:, verbose: false, safe: false) click to toggle source
# 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