How can I determine the original directory that an alias points to? - ruby

I'm working with a directory that's an alias on Mac OS, with Ruby. This is a folder that points to another folder. How can I determine the original directory that this alias points to?
In one of my Jenkins jobs, there is an alias called lastStable, which points to the latest stable build folder:
path = /Users/steve/.Jenkins/jobs/MyApp/lastStable
lastStable actually points to a folder called 2013-08-06_10_50_49.
How can I get this info dynamically in Ruby?

File.realpath resolves symlinks.
You could do:
File.realpath '/usr/bin/ruby'
#=> "/usr/bin/ruby1.9.3"

You can use the readlink method:
File.readlink(path)

Related

Get the full path of the symlink target in Ansible

How do I get the full path of a symlink target in Ansible? I'm trying to cleanup a folder--delete files older than x number of days but I don't want to remove the target of a particular file symlink. My plan is to get the symlink target and then use the value as an exclude target when using the find module.
The symlink target changes from time to time. I tried using the stat module but it doesn't solve my requirement.
stat module returns lnk_source and lnk_target which should do the trick.
Pay attention to confusing names:
lnk_source => Target of the symlink normalized for the remote filesystem
lnk_target => Target of the symlink. Note that relative paths remain relative
For the actual SOURCE of the symlink, well, it's just path.

Reduce file path when calling a file from terminal

I'm using Lua in interactive mode on a Mac (thanks to rudix.org).
When I want to load a file I do:
dofile("/my/long/path/to/my/directory/file.lua")
I want to do a different thing, that is:
put all my files in a desktop directory myDirectory;
then call the file from the terminal this way dofile("file.lua");
Is this possible? How?
If the path is fixed, you can just redefine dofile:
local _dofile=dofile
local path=("/my/long/path/to/my/directory/")
function dofile(x)
return _dofile(path..x)
end
You may put this (and other initializations) in a file and set the environment variable LUA_INIT to its location. After this, every invocation of lua will see the version of dofile redefined above and the users will be able to say simply dofile("foo.lua").
Alternatively, you can use require, which looks for modules in a list of paths in package.path or LUA_PATH.

Sending files made by `jar xf` to another directory

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.

RUBYLIB Environment Path

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.

Checking if a file exists in the user's home directory

How would I, say, determine if the file ~/.my_proj_config exists on any OS in Ruby?
A call to Dir.home is a OS independent way to get to the home directory for the user. You can then use it like
File.exists?(File.join(Dir.home, ".my_proj_config"))
This works in Ruby 1.9, but note that the call to expand_path is required on some systems (e.g. Windows):
File.exists?( File.expand_path "~/.my_proj_config" )
Use the class File and its method exist?.
Take a look at the Pathname class, specifically the realpath function - This will get you the full (expanded) path to your file.
http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/classes/Pathname.html#M001991
You then use the File class along with exists? method to find out if that exists. You shouldn't need to use realpath if you use this method, however.
http://www.ruby-doc.org/core/classes/File.html#M000045

Resources