unison-watch/lib/unison/profile.rb

55 lines
1.2 KiB
Ruby
Raw Normal View History

2012-05-07 10:44:25 +00:00
module Unison
class Profile
2012-05-07 22:21:03 +00:00
PROFILE_DIR = File.expand_path('~/.unison')
2012-05-07 10:44:25 +00:00
def self.process(profiles)
profiles.collect { |profile| new(profile) }
end
2012-05-07 22:21:03 +00:00
def self.available
Dir[File.join(PROFILE_DIR, '*.prf')].collect { |file| File.basename(file).gsub('.prf', '') }.sort
end
2012-05-07 10:44:25 +00:00
def initialize(which)
@which = which
end
def local_root
roots.find { |root| root[%r{^/}] }
end
def roots
@roots ||= lines.find_all { |line| line[%r{^root}] }.collect { |line| value_of(line) }
end
def lines
return @lines if @lines
@lines = File.readlines(File.expand_path("~/.unison/#{@which}.prf"))
includes = []
@lines.each do |line|
if file = line[%r{^include (.*)}, 1]
includes += File.readlines(File.expand_path("~/.unison/#{file}"))
end
end
@lines += includes
end
def paths
@paths ||= lines.find_all { |line| line[%r{^path}] }.collect { |line| value_of(line) }
end
def paths_with_local_root
paths.collect { |path| File.join(local_root, path) }
end
def value_of(line)
line[%r{=(.*)$}, 1].strip
end
end
end