lib/hem/patches/rake.rb in hem-1.0.1.beta6 vs lib/hem/patches/rake.rb in hem-1.1.0

- old
+ new

@@ -11,29 +11,26 @@ task_name = task_name.to_s new_tasks = [new_tasks].flatten.compact old_task = Rake.application.instance_variable_get('@tasks').delete(task_name) Hem::Metadata.to_store task_name - task task_name => old_task.prerequisites do + task task_name => old_task.prerequisites | new_tasks do new_task.call unless new_task.nil? - new_tasks.each do |t| - Rake::Task[t].invoke - end old_task.invoke end end def after(task_name, new_tasks = nil, &new_task) task_name = task_name.to_s new_tasks = [new_tasks].flatten.compact - old_task = Rake.application.instance_variable_get('@tasks').delete(task_name) - Hem::Metadata.to_store task_name - task task_name => old_task.prerequisites do - old_task.invoke - new_tasks.each do |t| - Rake::Task[t].invoke + task = Rake::Task[task_name] + task.enhance do + new_tasks.each do |post_task| + post = Rake.application.lookup(post_task, task.scope) + raise ArgumentError, "Task #{post_task.inspect} not found" unless post + post.invoke end new_task.call unless new_task.nil? end end @@ -46,10 +43,29 @@ def invoke task, *args, &block Rake::Task[task].invoke(*args, &block) end + def argument name, options = {} + opts = { + optional: false, + as: String, + }.merge(options) + Hem::Metadata.store[:arg_list] ||= {} + + if Hem::Metadata.store[:arg_list].length > 0 + last_arg = Hem::Metadata.store[:arg_list].values.last + if last_arg[:optional] && !opts[:optional] + raise 'Cannot have mandatory arguments after optional arguments' + elsif last_arg[:as] == Array + raise 'Cannot add any arguments after an array argument' + end + end + + Hem::Metadata.store[:arg_list][name] = opts + end + def hidden value = true Hem::Metadata.store[:hidden] = value end def project_only @@ -57,15 +73,24 @@ end def task *args, &block name = args[0].is_a?(Hash) ? args[0].keys.first.to_s : args[0] scoped_name = Rake.application.current_scope.path_with_task_name(name).to_s + Hem::Metadata.store[:arg_list] ||= {} - [:opts, :desc, :long_desc, :hidden, :project_only].each do |meta| + args[1..-1].each do |name| + argument name, optional: true + end if args.length > 1 + + [:opts, :desc, :long_desc, :hidden, :project_only, :arg_list].each do |meta| Hem::Metadata.add scoped_name, meta end + if Hem::Metadata.store[:arg_list] + args = [args[0], *Hem::Metadata.store[:arg_list].keys] + end + Hem::Metadata.reset_store Hem::Logging.logger.debug("Added metadata to #{scoped_name} -- #{Hem::Metadata.metadata[scoped_name]}") task = Rake::Task.define_task(*args, &block) @@ -91,9 +116,15 @@ end Hem::Metadata.reset_store _old_namespace(name, &block) + end + + def plugins setup = true, &block + Hem.plugins.define &block if block_given? + Hem.plugins.setup if setup + Hem.plugins end end end Hem::Metadata.default :opts, []