Sending files made by `jar xf` to another directory - ruby

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.

Related

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

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)

Ruby - FileUtils - dereference_root option

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'.

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.

Directory dependencies with rake

I'm using rake to copy a directory as so:
file copied_directory => original_directory do
#copy directory
end
This works fine, except when something inside of original_directory changes. The problem is that the mod date doesn't change on the enclosing directory, so rake doesn't know to copy the directory again. Is there any way to handle this? Unfortunately my current setup does not allow me to set up individual dependencies for each individual file inside of original_directory.
You could use rsync to keep the 2 directories in sync as shown here: http://asciicasts.com/episodes/149-rails-engines
You don't need to know the files to depend on them:
file copied_directory => FileList[original_directory, original_directory + "/**/*"]

RubyZip - files from different directories have path in zip

I'm trying to use RubyZip to package up some files. At the moment I have a method which happily zips on particular directory and sub-directories.
def zip_directory(zipfile)
Dir["#{#directory_to_zip}/**/**"].reject{|f| reject_file(f)}.each do |file_path|
file_name = file_path.sub(#directory_to_zip+'/','');
zipfile.add(file_name, file_path)
end
end
However, I want to include a file from a completely different folder. I have a the following method to solve this:
def zip_additional(zipfile)
additional_files.reject{|f| reject_file(f)}.each do |file_path|
file_name = file_path.split('\\').last
zipfile.add(file_name, file_path)
end
end
While the file is added, it also copies the directory structure instead of placing the file at the root of the folder. This is really annoying and makes it more difficult to work with.
How can I get around this?
Thanks
Ben
there is setting to include (or exclude) the full path for zip libraries, check that setting
Turns out it was because the filename had the pull path in. My split didn't work as the path used a / instead of a . With the path removed from the filename it just worked.

Resources