2010-01-30 04:25:10 +00:00
|
|
|
require 'yaml'
|
|
|
|
|
2010-01-22 07:38:23 +00:00
|
|
|
module Hobo
|
|
|
|
class Env
|
2010-01-22 07:57:43 +00:00
|
|
|
HOME = File.expand_path('~/.hobo')
|
|
|
|
CONFIG = { File.join(HOME, 'config.yml') => '/config/default.yml' }
|
|
|
|
ENSURE = {
|
|
|
|
:files => CONFIG.merge({}), #additional files go mhia!
|
|
|
|
:dirs => [HOME] #additional dirs go mhia!
|
|
|
|
}
|
2010-01-30 08:46:56 +00:00
|
|
|
PATH_CHUNK_REGEX = /\/[^\/]+$/
|
2010-01-22 07:57:43 +00:00
|
|
|
|
2010-01-26 08:01:17 +00:00
|
|
|
class << self
|
2010-01-30 07:22:03 +00:00
|
|
|
def load!
|
|
|
|
load_config!
|
|
|
|
load_uuid!
|
|
|
|
end
|
|
|
|
|
2010-01-26 08:01:17 +00:00
|
|
|
def ensure_directories
|
|
|
|
ENSURE[:dirs].each do |name|
|
|
|
|
Dir.mkdir(name) unless File.exists?(name)
|
|
|
|
end
|
2010-01-22 07:38:23 +00:00
|
|
|
end
|
|
|
|
|
2010-01-26 08:01:17 +00:00
|
|
|
def ensure_files
|
|
|
|
ENSURE[:files].each do |target, default|
|
|
|
|
File.copy(File.join(PROJECT_ROOT, default), target) unless File.exists?(target)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-30 07:22:03 +00:00
|
|
|
def load_config!
|
2010-01-26 08:01:17 +00:00
|
|
|
ensure_directories
|
|
|
|
ensure_files
|
2010-01-30 04:38:36 +00:00
|
|
|
|
|
|
|
HOBO_LOGGER.info "Loading config from #{CONFIG.keys.first}"
|
2010-01-30 06:39:45 +00:00
|
|
|
parsed = YAML.load_file(CONFIG.keys.first)
|
2010-01-26 08:01:17 +00:00
|
|
|
Hobo.config!(parsed)
|
2010-01-22 07:38:23 +00:00
|
|
|
end
|
2010-01-30 07:22:03 +00:00
|
|
|
|
|
|
|
def load_uuid!
|
2010-01-30 08:46:56 +00:00
|
|
|
@@persisted_uuid = load_dotfile
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_dotfile(dir=Dir.pwd)
|
|
|
|
return nil if dir.empty?
|
|
|
|
|
|
|
|
file = "#{dir}/#{Hobo.config[:dotfile_name]}"
|
|
|
|
if File.exists?(file)
|
2010-01-30 07:48:05 +00:00
|
|
|
# TODO check multiple lines after the first for information
|
2010-01-30 08:46:56 +00:00
|
|
|
return File.open(file, 'r').first
|
2010-01-30 07:48:05 +00:00
|
|
|
end
|
2010-01-30 08:46:56 +00:00
|
|
|
|
|
|
|
load_dotfile(dir.sub(PATH_CHUNK_REGEX, ''))
|
2010-01-30 07:22:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def persisted_uuid
|
|
|
|
@@persisted_uuid
|
|
|
|
end
|
2010-01-22 07:38:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|