Module: IO_helper
- Included in:
- SimpleCommander::CLI
- Defined in:
- lib/simple_commander/helpers/io.rb
Instance Method Summary collapse
- #cli_exist?(path, cli) ⇒ Boolean
- #command_exist?(path, command) ⇒ Boolean
- #copy(src, dest) ⇒ Object
- #copy_dir(src, dest) ⇒ Object
- #have_subcommands?(path, cli, command) ⇒ Boolean
- #in_file?(string, path) ⇒ Boolean
- #mkdir(dest) ⇒ Object
- #rm_block(start_id, end_id, path) ⇒ Object
- #rm_dir(path) ⇒ Object
- #rm_file(path) ⇒ Object
- #rm_string(string, path) ⇒ Object
- #run_cmd(path) ⇒ Object
- #subcommand_exist?(path, command, subcommand) ⇒ Boolean
- #template(src, dest, replace = false) ⇒ Object
- #write_after(id, string, path) ⇒ Object
- #write_end(string, path) ⇒ Object
- #write_start(string, path) ⇒ Object
Instance Method Details
#cli_exist?(path, cli) ⇒ Boolean
112 113 114 |
# File 'lib/simple_commander/helpers/io.rb', line 112 def cli_exist?(path, cli) File.directory?("#{path}/#{cli}") end |
#command_exist?(path, command) ⇒ Boolean
116 117 118 |
# File 'lib/simple_commander/helpers/io.rb', line 116 def command_exist?(path, command) File.file?("#{path}/subcommands/#{command}.rb") end |
#copy(src, dest) ⇒ Object
18 19 20 |
# File 'lib/simple_commander/helpers/io.rb', line 18 def copy(src, dest) FileUtils.cp(src, dest) end |
#copy_dir(src, dest) ⇒ Object
22 23 24 |
# File 'lib/simple_commander/helpers/io.rb', line 22 def copy_dir(src, dest) FileUtils.copy_entry(src, dest) end |
#have_subcommands?(path, cli, command) ⇒ Boolean
124 125 126 127 |
# File 'lib/simple_commander/helpers/io.rb', line 124 def have_subcommands?(path, cli, command) in_file? /register/, "#{path}/#{cli}/subcommands/#{command}.rb" end |
#in_file?(string, path) ⇒ Boolean
49 50 51 52 53 |
# File 'lib/simple_commander/helpers/io.rb', line 49 def in_file?(string, path) regexp, content = Regexp.new string File.open(path, 'r'){|f| content = f.read } content =~ regexp end |
#mkdir(dest) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/simple_commander/helpers/io.rb', line 10 def mkdir(dest) if(File.directory?(dest)) puts "#{dest} already exist!" return 0 end FileUtils.mkdir(dest) end |
#rm_block(start_id, end_id, path) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/simple_commander/helpers/io.rb', line 91 def rm_block(start_id, end_id, path) 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| if line =~ start_regexp result = false elsif line =~ end_regexp result = true end result }.each{ |result, arr| if result arr[0] =~ end_regexp ? arr.shift : arr new_lines << arr end } File.open(path, 'w+'){|f| f.write(new_lines.join)} end |
#rm_dir(path) ⇒ Object
45 46 47 |
# File 'lib/simple_commander/helpers/io.rb', line 45 def rm_dir(path) FileUtils.rmdir path end |
#rm_file(path) ⇒ Object
41 42 43 |
# File 'lib/simple_commander/helpers/io.rb', line 41 def rm_file(path) FileUtils.remove_file path end |
#rm_string(string, path) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/simple_commander/helpers/io.rb', line 83 def rm_string(string, path) 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 |
#run_cmd(path) ⇒ Object
4 5 6 7 8 |
# File 'lib/simple_commander/helpers/io.rb', line 4 def run_cmd(path) FileUtils.cd(path) do yield end end |
#subcommand_exist?(path, command, subcommand) ⇒ Boolean
120 121 122 |
# File 'lib/simple_commander/helpers/io.rb', line 120 def subcommand_exist?(path, command, subcommand) File.file?("#{path}/subcommands/#{command}/#{command}_#{subcommand}.rb") end |
#template(src, dest, replace = false) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/simple_commander/helpers/io.rb', line 26 def template(src, dest, replace=false) if(File.file?(dest) and !replace) puts "#{dest} already exist!" return 0 end template = File.open(src, 'r') file_contents = template.read template.close renderer = ERB.new(file_contents) result = renderer.result(binding) output = File.open(dest, 'w+') output.write(result) output.close end |
#write_after(id, string, path) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/simple_commander/helpers/io.rb', line 69 def write_after(id, string, path) lines = [] File.open(path, 'r'){|f| lines = f.readlines } new_lines = lines.inject([]) do |result, value| if value.include? id result << value result << "#{string}\n" else result << value end end File.open(path, 'w+'){|f| f.write(new_lines.join)} end |
#write_end(string, path) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/simple_commander/helpers/io.rb', line 62 def write_end(string, path) lines = [] File.open(path, 'r'){|f| lines = f.readlines } lines << "#{string}\n" File.open(path, 'w+'){|f| f.write(lines.join)} end |
#write_start(string, path) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/simple_commander/helpers/io.rb', line 55 def write_start(string, path) lines = [] File.open(path, 'r'){|f| lines = f.readlines } lines.unshift("#{string}\n") File.open(path, 'w+'){|f| f.write(lines.join)} end |