lib/roll/environment.rb in roll-1.1.0 vs lib/roll/environment.rb in roll-1.2.0
- old
+ new
@@ -1,9 +1,10 @@
+#require File.dirname(__FILE__) + '/config.rb'
+require 'roll/config'
+
require 'yaml'
require 'fileutils'
-#require 'roll/xdg'
-require 'roll/config'
module Roll
# An Environment represents a set of libraries.
#
@@ -14,24 +15,52 @@
# Location of environment files.
#--
# Perhaps combine all enrtries instead?
#++
- DIRS = ::Config.find_config('roll')
+ DIRS = ::Config.find_config('roll', 'environments')
+ #
+ HOME_ENV_DIR = File.join(::Config::CONFIG_HOME, 'roll', 'environments')
+
+ # File that stores the name of the current environment.
+ CURRENT_FILE = File.join(::Config::CONFIG_HOME, 'roll', 'current')
+
# Current environment name.
def self.current
- ENV['RUBYENV'] || DEFAULT
+ @current ||= (
+ if File.exist?(CURRENT_FILE)
+ env = File.read(CURRENT_FILE).strip
+ else
+ env = ENV['RUBYENV'] || DEFAULT
+ end
+ #warn "#{env} is not a valid environment" unless list.include?(env)
+ env
+ )
end
# List of available environments.
def self.list
- Dir[File.join(DIR, '*')].map do |file|
+ Dir[File.join('{'+DIRS.join(',')+'}', '*')].map do |file|
File.basename(file)
end
end
+ # Change environment to given +name+.
+ #
+ # TODO: should only last a long as the shell session,
+ # not change it perminently.
+ #
+ def self.save(name)
+ if name == 'system'
+ FileUtils.rm(CURRENT_FILE)
+ else
+ File.open(CURRENT_FILE,'w'){|f| f << name.to_s}
+ end
+ CURRENT_FILE
+ end
+
# Environment name.
attr :name
# Instantiate environment.
def initialize(name=nil)
@@ -57,11 +86,13 @@
def save
index.save
end
#
- def each(&block) ; index.each(&block) ; end
+ def each(&block)
+ index.each(&block)
+ end
#
def size ; index.size ; end
#
@@ -71,11 +102,10 @@
str << "#{path} #{depth}\n"
end
str
end
-
# Index tracks the name and location of each library
# in an environment.
#--
# TODO: Using a hash table means un-order, fix?
#++
@@ -94,11 +124,11 @@
@name
end
# Environment file (full-path).
def file
- @file ||= ::Config.find_config('roll', name, 'index').first
+ @file ||= ::Config.find_config('roll', 'environments', name, 'index').first
end
# Load the environment file.
def reload
if file && File.exist?(file)
@@ -131,18 +161,18 @@
@table.dup
end
#
def to_s
- out = ""
+ out = []
max = @table.map{ |name, paths| name.size }.max
@table.map do |name, paths|
paths.each do |path|
- out << "%-#{max}s %s\n" % [name, path]
+ out << "%-#{max}s %s" % [name, path]
end
end
- out
+ out.sort.join("\n")
end
# Save environment file.
def save
out = to_s
@@ -150,11 +180,11 @@
#@table.map do |name, paths|
# paths.each do |path|
# out << "%-#{max}s %s\n" % [name, path]
# end
#end
- file = File.join(::Config::CONFIG_HOME, 'roll', name, 'index')
+ file = File.join(HOME_ENV_DIR, name, 'index')
if File.exist?(file)
data = File.read(file)
if out != data
File.open(file, 'w'){ |f| f << out }
#puts "updated: #{name}"
@@ -171,27 +201,26 @@
true
end
@file = file
end
-=begin
- # Get library version.
- # TODO: handle VERSION file
- def load_version(path)
- file = Dir[File.join(path, '{,.}meta', 'version')].first
- if file
- File.read(file).strip # TODO: handle YAML ?
- end
+ # Get library version.
+ # TODO: handle VERSION file
+ #def load_version(path)
+ # file = Dir[File.join(path, '{,.}meta', 'version')].first
+ # if file
+ # File.read(file).strip # TODO: handle YAML ?
+ # end
+ #end
end
-=end
- end
-
# The Lookup class provides a table of paths which
# make it easy to quickly populate and refresh the
# environment index.
-
+ #
+ # TODO: Provide a way to specifically exclude a location.
+ # Probaby recognize a path with a '-' prefix.
class Lookup
include Enumerable
#
#DIR = ::Config.find_config('roll').first
@@ -207,11 +236,11 @@
@name
end
#
def file
- @file ||= ::Config.find_config('roll', name, 'lookup').first
+ @file ||= ::Config.find_config('roll', 'environments', name, 'lookup').first
end
#
def reload
t = []
@@ -254,61 +283,74 @@
@table.reject!{ |p,d| path == p }
end
#
def save
- file = File.join(::Config::CONFIG_HOME, 'roll', name, 'lookup')
+ file = File.join(HOME_ENV_DIR, name, 'lookup')
out = @table.map do |(path, depth)|
"#{path} #{depth}"
- end
+ end.sort
dir = File.dirname(file)
FileUtils.mkdir_p(dir) unless File.exist?(dir)
File.open(file, 'w') do |f|
f << out.join("\n")
end
@file = file
end
# Generate index from lookup list.
def index
- set = Hash.new{ |h,k| h[k] = [] }
+ set = Hash.new{|h,k| h[k]=[]}
locate.each do |path|
- name = load_name(path)
+ name = libname(path)
+ next if name == 'roll' # NEVER INCLUDE ROLL ITSELF!!!
#vers = load_version(path)
if name #&& vers
set[name] << path
+ else
+ warn "omitting: #{path}"
end
end
set
end
- #
+ # Locate projects.
def locate
locs = []
each do |dir, depth|
locs << find_projects(dir, depth)
end
locs.flatten
end
- # Search a given directory for projects upto a given depth.
- # Projects directories are determined by containing a
- # 'meta' or '.meta' directory.
+ # Search a given directory for projects upto a given depth. Projects
+ # directories are determined by containing a lib/*.rb file.
def find_projects(dir, depth=3)
depth = Integer(depth || 3)
depth = (0...depth).map{ |i| (["*"] * i).join('/') }.join(',')
- glob = File.join(dir, "{#{depth}}", "{.meta,meta}")
- meta_locations = Dir[glob]
- meta_locations.map{ |d| d.chomp('/meta').chomp('/.meta') }
+ find = File.join(dir, "{#{depth}}", "lib/*.rb")
+ locals = Dir.glob(find)
+ locals.map{|d| File.dirname(File.dirname(d)) }.uniq
end
- # Get library name.
- def load_name(path)
- file = Dir[File.join(path, '{,.}meta', 'name')].first
- if file
- File.read(file).strip # TODO: handle YAML
- end
+ #
+ def metadata(path)
+ @metadata ||= {}
+ @metadata[path] ||= Metadata.new(path)
end
+
+ #
+ def libname(path)
+ metadata(path).name
+ end
+
+ ## Get library name.
+ #def load_name(path)
+ # file = Dir[File.join(path, '{,.}meta', 'name')].first
+ # if file
+ # File.read(file).strip # TODO: handle YAML
+ # end
+ #end
end#class Lookup
end#class Environment