lib/stack.rb in stack-0.0.1 vs lib/stack.rb in stack-0.0.2

- old
+ new

@@ -1,28 +1,82 @@ # rubygems require 'rubygems' +require 'optparse' +require 'core_ext/hash' + +require 'stack/configuration' +require 'stack/generator' +require 'stack/runner' + module Stack - # Default options used by stack, overridden from the command line or stack configration file. + # Default options used by stack, overridden from the command line or YML configration file. DEFAULTS = { - + :source => '.', + :target => File.join('.', '_stack') }.freeze + # Array of valid commands stack can use + COMMANDS = %w(create generate server) + + class << self + attr_accessor :runner + end + + # Parses the options configuration from the command line + def self.parse!(argv) + config = { } + + parser = OptionParser.new do |opts| + opts.banner = "Usage: stack [options] #{Stack::COMMANDS.join('|')}" + + opts.on("-s", "--source [DIR]", "Directory to use as the source for generating a stack") do |l| config[:source] = l unless l.nil? end + opts.on("-t", "--target [DIR]", "Directory to use as the target directory") do |l| config[:target] = l unless l.nil? end + + opts.on_tail("-h", "--help", "Show this message") { puts opts; exit } + opts.on_tail("-v", "--version" "Show version") do puts "stack #{Stack::version}"; exit; end + end + + parser.parse! argv + + options = Stack::configuration(config) + + options + end + + # Merges the configuration from YAML file, command line and defaults def self.configuration(override) config = { } + source = File.join(override['source'] || Stack::DEFAULTS[:source]) + config_file = File.join(source, "_stack.yml") + begin - #config = YAML.load_file(config_file) - rescue => error + yml = YAML.load_file(config_file) + raise "Invalid configuration - #{config_file}" if !config.is_a?(Hash) + + yml.each { |key, value| config[key.to_sym] = value } + + STDOUT.puts "Configuration loaded from #{config_file}" + rescue => error + STDERR.puts "WARNING: Could not read configuration. Using defaults (and options)." + STDERR.puts "\t" + error.to_s end # Merge configuration with defaults - Stack::DEFAULTS.deep_merge(config).deep_merge(override) + Stack::DEFAULTS.deep_merge(override).deep_merge(config) end # Stacks current version def self.version - yml = YAML.load(File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION.yml]))) - "#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}" + version_file = File.join(File.dirname(__FILE__), *%w[.. VERSION.yml]) + + if File.exist?(version_file) + yml = YAML.load(File.read(version_file)) + + "#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}" + else + "0.0.0" + end end end \ No newline at end of file