Skip to content

Instantly share code, notes, and snippets.

@richo
Created August 31, 2012 04:58
Show Gist options
  • Select an option

  • Save richo/3549203 to your computer and use it in GitHub Desktop.

Select an option

Save richo/3549203 to your computer and use it in GitHub Desktop.
require 'fileutils'
module RelativePaths extend self
PREFIX_FILE="~/.babuska/cache/install_prefix".p
PREFIX_ENV_VAR="BABUSHKA_PREFIX".freeze
module Mixin
# A helper for generating paths relative to a given path, specified in the
# environment but cached from thenceforth
def path(p)
RelativePaths.prefix.p / p.p
end
end
def prefix
@prefix || get_prefix
end
def get_prefix
prefix_from_env ||
prefix_from_file ||
"~"
end
def prefix_from_env
if ENV.include? PREFIX_ENV_VAR
write_prefix(ENV[PREFIX_ENV_VAR])
ENV[PREFIX_ENV_VAR]
end
end
def prefix_from_File
File.read(PREFIX_FILE).chomp rescue nil
end
def write_prefix(p)
FileUtils.mkdir_p(File.basename(PREFIX_FILE))
File.open(PREFIX_FILE) do |f|
f.write(p)
end
end
end
Object.send(:include, RelativePaths::Mixin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment