lib/reap/project.rb in reap-9.3.5 vs lib/reap/project.rb in reap-9.4.0

- old
+ new

@@ -1,19 +1,22 @@ module Reap require 'yaml' require 'rbconfig' require 'reap/utilities' + require 'reap/hosts' # = Project # # The Project class is the main class of Reap. It provides the tools for # working with a project. The CLI Application class delegates to this class, # for instance. class Project require 'reap/metadata' require 'reap/settings' + require 'reap/defaults' + require 'reap/runmodes' # Load up the tools require "reap/project/announce.rb" require "reap/project/check.rb" require "reap/project/clean.rb" @@ -28,78 +31,90 @@ require "reap/project/scaffold.rb" require "reap/project/site.rb" require "reap/project/spec.rb" require "reap/project/stats.rb" require "reap/project/scm.rb" - require "reap/project/svn.rb" require "reap/project/test.rb" require "reap/project/version.rb" include Utilities # New Project. def initialize(options=nil) @options = options || {} - begin - @location = locate - raise LoadError, "no .reap configuration file" unless @location - @metadata = Metadata.read(location) - @settings = Settings.read(location, @metadata) - rescue LoadError => e - abort e.message.capitalize + '.' - end + @location = locate + + raise LoadError, "no .reap configuration file" unless @location + + @metadata = Metadata.read(location) + @settings = Settings.read(location, @metadata) + @defaults = Defaults.new(@metadata) + @runmodes = RunModes.new(options) end # Location of project. def location ; @location ; end # Project metadata. def metadata ; @metadata ; end - # Configuration data. + # Task defaults. + def defaults ; @defaults ; end + + # Task user settings. + def settings ; @settings ; end - alias_method :configuration, :settings + # Run modes. - #alias_method :config, :configuration + def runmodes ; @runmodes ; end # Common options. def options ; @options ; end + #alias_method :init_options, :options # TODO: Improve me! (see stamp.rb) - def dryrun? ; options['dryrun'] ; end - def trace? ; options['trace'] ; end - def force? ; options['force'] ; end - def verbose? ; options['verbose'] ; end - def debug? ; options['debug'] ; end + def dryrun? ; runmodes.dryrun? ; end + def trace? ; runmodes.trace? ; end + def force? ; runmodes.force? ; end + def verbose? ; runmodes.verbose? ; end + def debug? ; runmodes.debug? ; end - def dryrun=(x) ; options['dryrun'] = x ; end - def trace=(x) ; options['trace'] = x ; end - def force=(x) ; options['force'] = x ; end - def verbose=(x) ; options['verbose'] = x ; end - def debug=(x) ; options['debug'] = x ; end + def dryrun=(x) ; runmodes.dryrun = x ; end + def trace=(x) ; runmodes.trace = x ; end + def force=(x) ; runmodes.force = x ; end + def verbose=(x) ; runmodes.verbose = x ; end + def debug=(x) ; runmodes.debug = x ; end alias_method :noharm?, :dryrun? alias_method :noharm=, :dryrun= # Invoke a tool. def invoke(command, *args) - #display_location - meth = method(command) - chdir_to_project do - case meth.arity - when 0 - meth.call + begin + #display_location + meth = method(command) + chdir_to_project do + case meth.arity + when 0 + meth.call + else + meth.call(*args) + end + end + rescue LoadError => e + if trace? + raise e else - meth.call(*args) + abort e.message.capitalize + '.' end end end # Display the project's root location. @@ -135,14 +150,31 @@ def introspect(options) args = options['arguments'] if args args.each do |field| - puts metadata.send(field) + case field + when 'defaults' + y defaults + when 'settings' + y settings + when 'metadata' + y metadata + else + puts metadata.send(field) + end end else - y self + puts + puts "#{metadata.title} #{metadata.version}" + puts metadata.brief + puts metadata.homepage + puts + puts metadata.description + puts + puts metadata.copyright + puts end end # Project manifest file. @@ -170,26 +202,54 @@ end end return loc end - # + # Tasks use this to automatically combine commandline options, + # user settings, defualts. def configure_options(options, *entries) + config = {} + + entries.each do |entry| + config.update(defaults[entry] || {}) + end + + entries.each do |entry| + config.update(settings[entry] || {}) + end + options = (options || {}).rekey(&:to_s) - entries.inject(options) do |memo, entry| - (settings[entry] || {}).merge(memo) + config.update(options) + + return config + end + + # Access to selected hosts. + + def hosts(select=nil) + @hosts ||= {} + select ||= metadata.hosts.keys + result = [] + metadata.hosts.each do |key, options| + next unless select.include?(key) + host_class = options['type'] ? Hosts.registry[options['type']] : Hosts.registry[key] + next unless host_class + # cache hosts + @hosts[key] ||= host_class.from_project(self, options) + result |= [@hosts[key]] end + return result end - # Helper method for cleaning list options. - # This will split the option on ':' or ';' - # if it is a string, rather than an array. - # And it will make sure there are no nil elements. + # Access to project's scource control management system. - def list_option(option) - option = option.to_s.split(/[:;,]/) unless Array===option - [option].compact.flatten + def scm + @scm ||= ( + if system = Systems.current + system.from_project(self, {}) #TODO: metadata.scm is a hash? + end + ) end end end