Module: FileUtils
- Defined in:
- lib/file_utils/version.rb,
lib/file_utils/file_utils.rb
Constant Summary collapse
- VERSION =
"1.0.7"
Instance Method Summary collapse
- #copy_files(from_dir, to_dir, pattern) ⇒ Object
- #create_directory(dir) ⇒ Object
- #delete_directory(dir) ⇒ Object
- #execute(command) ⇒ Object
- #execute_command(command) ⇒ Object
- #execute_template(file_name, binding) ⇒ Object
- #pattern_to_files(dir, pattern) ⇒ Object
- #read_file(file_name) ⇒ Object
- #with_dir(dir, &code) ⇒ Object
- #write_content_to_file(content, to) ⇒ Object
- #write_to_file(from, to) ⇒ Object
Instance Method Details
#copy_files(from_dir, to_dir, pattern) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/file_utils/file_utils.rb', line 29 def copy_files from_dir, to_dir, pattern create_directory to_dir files = pattern_to_files from_dir, pattern FileUtils.cp_r files, to_dir end |
#create_directory(dir) ⇒ Object
5 6 7 |
# File 'lib/file_utils/file_utils.rb', line 5 def create_directory dir FileUtils.mkdir_p dir unless File.exist? dir end |
#delete_directory(dir) ⇒ Object
9 10 11 |
# File 'lib/file_utils/file_utils.rb', line 9 def delete_directory dir FileUtils.rm_rf dir end |
#execute(command) ⇒ Object
55 56 57 58 59 |
# File 'lib/file_utils/file_utils.rb', line 55 def execute command IO.popen(command).each_line do |line| puts line end end |
#execute_command(command) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/file_utils/file_utils.rb', line 61 def execute_command(command) output = nil error = nil status = nil Open3.popen3(command) do |_, stdout, stderr| output = stdout.readlines error = stderr.readlines status = $? end [output, error, status] end |
#execute_template(file_name, binding) ⇒ Object
49 50 51 52 53 |
# File 'lib/file_utils/file_utils.rb', line 49 def execute_template file_name, binding template = ERB.new read_file(file_name) template.result(binding) end |
#pattern_to_files(dir, pattern) ⇒ Object
37 38 39 |
# File 'lib/file_utils/file_utils.rb', line 37 def pattern_to_files dir, pattern Dir.glob("#{dir}/#{pattern}") end |
#read_file(file_name) ⇒ Object
25 26 27 |
# File 'lib/file_utils/file_utils.rb', line 25 def read_file file_name File.open(file_name).read end |
#with_dir(dir, &code) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/file_utils/file_utils.rb', line 41 def with_dir dir, &code Dir.foreach(dir) do |entry_name| next if entry_name == '.' or entry_name == '..' code.call(entry_name) end end |
#write_content_to_file(content, to) ⇒ Object
19 20 21 22 23 |
# File 'lib/file_utils/file_utils.rb', line 19 def write_content_to_file content, to File.open(to, "w") do |file| file.write(content) end end |
#write_to_file(from, to) ⇒ Object
13 14 15 16 17 |
# File 'lib/file_utils/file_utils.rb', line 13 def write_to_file from, to File.open(to, "w") do |file| file.write(read_file(from)) end end |