Can someone explain me exactly (better if with examples) the meaning of the dereference_root option in FileUtils.cp_r and in other class method of the same class?
Thank you in advance.
It applies only if your source file/directory is symbolic link. If it is and you specify this option, then FileUtils.cp_r will follow this link and copy the original files otherwise you will get just a copy of the symlink.
But this doesn't apply recursively. So if you will have other symlinks inside your source folder they will be just copied as is, without 'dereferencing'.
Related
How to check if the file exists in all subdirectories within chef recipe (not_if guard).
There is no specific answer other than "write some Ruby code to check what you want". You'll probably want to do a Dir glob and then compare the various outputs but maybe you just need an all? and File.exist?.
FileUtils.cp_r does exactly what I need, but it's not able to filter out files by some pattern.
So, I would like to copy recursively a complex folder/files structure to another directory, but I'd like to only include files with txt extension. How would I do that?
FileUtils.cp_r Dir['**/*.txt'], target_dir
I have a JAR with a bunch of configs. I'd like to send them to the correct directory without cd'ing there.
Something like jar xf config.jar --MAGIC-PARAM PATH/TO/DIRECTORY
Is there such a thing? If it helps, this will be called by a Buildr extension (Ruby).
From the API documentation: http://buildr.apache.org/rdoc/classes/Buildr/Unzip.html
unzip(dir => zip_file).target.invoke
Alex's answer is good. If there's some special magic that jar xf does that makes you prefer it to unzipping (I'm not aware of any), here's another option:
FileUtils.cd('PATH/TO/DIRECTORY') do
system("jar xf '#{_('config.jar')'")
end
It does involve cd'ing, but when you use cd with a block, the original directory is restored after the block. You will need to use either an absolute path or a path relative to the directory you changed to; I'm using buildr's _ method to get an absolute path for a project-relative file.
So currently I have included the following in my .bashrc file.
export RUBYLIB=/home/git/project/app/helpers
I am trying to run rspec with a spec that has
require 'output_helper'
This file is in the helpers directory. My question is that when I change the export line to:
export RUBYLIB=/home/git/project/
It no longer finds the helper file. I thought that ruby should search the entire path I supply, and not just the outermost directory supplied? Is this the correct way to think about it? And if not, how can I make it so RUBY will search through all subdirectories and their subdirectories, etc?
Thanks,
Robin
Similar to PATH, you need to explicitly name the directory under which to look for libraries. However, this will not include any child directories within, so you will need to list any child sub-directories as well, delimiting them with a colon.
For example:
export RUBYLIB=/home/git/project:/home/git/project/app/helpers
As buruzaemon mentions, Ruby does not search subdirectories, so you need to include all the directories you want in your search path. However, what you probably want to do is:
require 'app/helpers/output_helper'
This way you aren't depending on the RUBYLIB environment variable being set a certain way. When you're deploying code to production, or collaborating with others, these little dependencies can make for annoying debugging sessions.
Also as a side note, you can specify . as a search path, rather than using machine-specific absolute paths.
I'd like to delete a directory that may or may not contain files or other directories. Looking in the Ruby docs I found Dir.rmdir but it won't delete non-empty dir. Is there a convenience method that let's me do this? Or do I need to write a recursive method to examine everything below the directory?
require 'fileutils'
FileUtils.rm_rf(dir)
A pure Ruby way:
require 'fileutils'
FileUtils.rm_rf("/directory/to/go")
If you need thread safety: (warning, changes working directory)
FileUtils.rm_rf("directory/to/go", :secure=>true)
If some looking for answer on #ferventcoder comment -
Just a note, I found that with Windows and Ruby 1.9.3 (at least) FileUtils.rm_rf will follow links (symlinks in this case) and delete those files and folders as well. This was found based on creating a symlink with CreateSymbolicLinkW and then running FileUtils.rm_rf against a parent directory the symlinks are in. Not exactly expected behavior.
– ferventcoder
Safest way is to iterate path and delete it manually.
def rec_del(path):
if path is file call FileUtils.rm_rf - it is safe to delete even in windows
if dir call Dir.rmdir which will succeed for empty dir and dir symlink. else will get ENOTEMPTY for regular non empty dir.
iterate the dir and call rec_del for each item.
now call again Dir.rmdir which will succeed
The laziest way is:
def delete_all(path)
`rm -rf "#{path}"`
end