Module: Tap::Declarations

Includes:
Support::ShellUtils
Included in:
Tap
Defined in:
lib/tap/declarations.rb

Overview

– more thought needs to go into extending Tap with Declarations and there should be some discussion on why include works at the top level (for main/Object) while extend should be used in all other cases.

Defined Under Namespace

Modules: Lazydoc, Rakish

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::ShellUtils

#capture_sh, #redirect_sh, #sh

Instance Attribute Details

#current_descObject



69
70
71
# File 'lib/tap/declarations.rb', line 69

def current_desc
  @current_desc ||= nil
end

#declaration_baseObject



63
64
65
# File 'lib/tap/declarations.rb', line 63

def declaration_base
  @declaration_base ||= ''
end

Class Method Details

.envObject



53
54
55
# File 'lib/tap/declarations.rb', line 53

def self.env
  @env ||= Tap::Env.instance_for(Dir.pwd)
end

.extended(base) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/tap/declarations.rb', line 42

def self.extended(base)
  declaration_base = base.to_s
  case declaration_base
  when "Object", "Tap", "main"
    declaration_base = ""
  end
  
  base.instance_variable_set(:@declaration_base, declaration_base.underscore)
  base.instance_variable_set(:@current_desc, nil)
end

Instance Method Details

#declaration_envObject



57
58
59
# File 'lib/tap/declarations.rb', line 57

def declaration_env
  @declaration_env ||= Declarations.env
end

#desc(str) ⇒ Object



112
113
114
# File 'lib/tap/declarations.rb', line 112

def desc(str)
  self.current_desc = str
end

#namespace(name, &block) ⇒ Object



105
106
107
108
109
110
# File 'lib/tap/declarations.rb', line 105

def namespace(name, &block)
  current_base = declaration_base
  @declaration_base = File.join(current_base, name.to_s.underscore)
  yield
  @declaration_base = current_base
end

#task(*args, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tap/declarations.rb', line 73

def task(*args, &block)
  const_name, configs, dependencies, arg_names = resolve_args(args)
  task_class = declare(const_name, configs, dependencies) do |*inputs|
    # collect inputs to make a rakish-args object
    args = {}
    arg_names.each do |arg_name|
      break if inputs.empty?
      args[arg_name] = inputs.shift
    end
    args = OpenStruct.new(args)
    
    # execute each block assciated with this task
    self.class::BLOCKS.each do |task_block|
      case task_block.arity
      when 0 then task_block.call()
      when 1 then task_block.call(self)
      else task_block.call(self, args)
      end
    end
    
    nil
  end
  register_doc(task_class, arg_names)
  
  # add the block to the task
  unless task_class.const_defined?(:BLOCKS)
    task_class.const_set(:BLOCKS, [])
  end
  task_class::BLOCKS << block unless block == nil
  task_class.instance
end