views:

270

answers:

4

I am working on writing a rake build scrip which will work cross platform ( Mac OSX, Linux , Windows ). The build script will be consumed by a CI server [1]

I want the logic of my script to be as follows:

  1. If the path is determined to be relative, make it absolute by making *output_path = FOO_HOME + user_supplied_relative_path*
  2. If the path is determined to be absolute, take it as-is

I'm currently using Pathname.new(location).absolute? but it's not working correctly on windows.

What approach would you suggest for this?

[1] = http://www.thoughtworks-studios.com/cruise-release-management

+1  A: 

The method you're looking for is realpath.

Essentially you do this:

absolute_path = Pathname.new(path).realpath

N.B.: The Pathname module states that usage is experimental on machines that do not have unix like pathnames. So it's implementation dependent. Looks like JRuby should work on Windows.

EmFi
`Pathname#realpath` assumes your relative path is relative to the current working directory, which might not be the users home.
johannes
A: 

Pathname can do all that for you

require "pathname"
home= Pathname.new("/home/foo")

home + Pathname.new("/bin") # => #<Pathname:/bin>
home + Pathname.new("documents") # => #<Pathname:/home/foo/documents>

I am not sure about this on windows though.

johannes
A: 

You could also use File.expand_path if the relative directory is relative to the current working directory.

I checked on Linux and windows and didn't have any issues.

Assuming FOO_HOME is the working directory, the code would be:

output_path = File.expand_path user_supplied_relative_path
BaroqueBobcat
A: 
require 'pathname'
(Pathname.new "/foo").absolute? # => true
(Pathname.new "foo").absolute? # => false
evanmeng