helpers/io_helper.rb in simple_commander-0.5.1 vs helpers/io_helper.rb in simple_commander-0.6.0
- old
+ new
@@ -1,74 +1,90 @@
+require 'colorize'
require 'erb'
module IO_helper
- def run_cmd(path)
+ def run_cmd(command)
+ puts "running command: #{command}".colorize(:light_green)
+ puts `#{command}`
+ end
+
+ def run_in(path)
FileUtils.cd(path) do
yield
end
end
def mkdir(dest)
if(File.directory?(dest))
- puts "#{dest} already exist!"
+ puts "mkdir ERROR: #{dest} already exist!".colorize(:red)
return 0
end
+ puts "Creating folder #{dest}".colorize(:blue)
FileUtils.mkdir(dest)
end
def copy(src, dest)
+ puts "Copying from #{src} to #{dest}".colorize(:green)
FileUtils.cp(src, dest)
end
def copy_dir(src, dest)
+ puts "Copying from #{src} to #{dest}".colorize(:green)
FileUtils.copy_entry(src, dest)
end
- def template(src, dest, replace=false)
+ def template(src, dest, context, replace=true)
if(File.file?(dest) and !replace)
- puts "#{dest} already exist!"
+ puts "template ERROR: #{dest} already exist!".colorize(:red)
return 0
end
+ puts "template from #{src} to #{dest} replace: #{replace}".colorize(:magenta)
template = File.open(src, 'r')
file_contents = template.read
template.close
renderer = ERB.new(file_contents)
- result = renderer.result(binding)
+ result = renderer.result(context)
output = File.open(dest, 'w+')
output.write(result)
output.close
end
def rm_file(path)
+ puts "rm file #{path}".colorize(:light_red)
FileUtils.remove_file path
end
+ # remove folder recursively
def rm_dir(path)
- FileUtils.rmdir path
+ puts "rm dir #{path}".colorize(:light_red)
+ FileUtils.rm_rf path
end
def in_file?(string, path)
regexp, content = Regexp.new string
File.open(path, 'r'){|f| content = f.read }
content =~ regexp
end
def write_start(string, path)
+ puts "Trying to write at the start #{string[0...8]} ... on the file #{path}".colorize(:magenta)
lines = []
File.open(path, 'r'){|f| lines = f.readlines }
lines.unshift("#{string}\n")
File.open(path, 'w+'){|f| f.write(lines.join)}
end
def write_end(string, path)
+ puts "Trying to write at the end #{string[0...8]} ... on the file #{path}".colorize(:magenta)
lines = []
File.open(path, 'r'){|f| lines = f.readlines }
lines << "#{string}\n"
File.open(path, 'w+'){|f| f.write(lines.join)}
end
def write_after(id, string, path)
+ puts "Trying to write after #{id} the string #{string[0...8]} ... on the file #{path}".colorize(:magenta)
lines = []
File.open(path, 'r'){|f| lines = f.readlines }
new_lines = lines.inject([]) do |result, value|
if value.include? id
result << value
@@ -79,17 +95,19 @@
end
File.open(path, 'w+'){|f| f.write(new_lines.join)}
end
def rm_string(string, path)
+ puts "Removing #{string[0...8]} from #{path}".colorize(:light_red)
regexp = Regexp.new string
lines = []
File.open(path, 'r'){|f| lines = f.readlines }
new_lines = lines.reject{|line| line =~ regexp }
File.open(path, 'w+'){|f| f.write(new_lines.join)}
end
def rm_block(start_id, end_id, path)
+ puts "Removing string block from #{start_id} to #{end_id} #{path}".colorize(:light_red)
start_regexp = Regexp.new start_id
end_regexp = Regexp.new end_id
lines, new_lines, result = [], [], true
File.open(path, 'r'){|f| lines = f.readlines }
lines.chunk { |line|