Class: Tap::Tasks::List

Inherits:
Dump show all
Defined in:
lib/tap/tasks/list.rb

Overview

:startdoc::task list resources

Prints a list of resources registered with the application env. Any of the resources may be used in a workflow. A list of filters may be used to limit the output; each is converted to a regexp and can match any part of the resource (path, class, desc).

% tap list join gate
join:
  gate                 # collects results before the join

The configurations can be used to switch the resource description. By default env only lists resources registered as a task, join, or middleware.

% tap list join gate --class --full
join:
  /tap/joins/gate      # Tap::Joins::Gate

Instance Attribute Summary

Attributes inherited from Tap::Task

#joins

Attributes inherited from App::Api

#app

Instance Method Summary collapse

Methods inherited from Dump

#dump, #process, #to_spec

Methods inherited from Tap::Task

#associations, #enq, #exe, #initialize, #log, #on_complete, parser, #process

Methods inherited from App::Api

#associations, build, help, inherited, #initialize, #inspect, parse, parse!, parser, #to_spec

Methods included from Signals

#sig, #signal, #signal?, #signals

Methods included from Signals::ModuleMethods

included

Constructor Details

This class inherits a constructor from Tap::Task

Instance Method Details

#basisObject



42
43
44
# File 'lib/tap/tasks/list.rb', line 42

def basis
  app.env.constants
end

#call(input) ⇒ Object



38
39
40
# File 'lib/tap/tasks/list.rb', line 38

def call(input)
  process manifest(*input).join("\n")
end

#describe(constant, type) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/tap/tasks/list.rb', line 155

def describe(constant, type)
  case
  when clas 
    constant.const_name
    
  when path
    require_paths = constant.require_paths
    require_paths = require_paths.collect do |path|
      File.join(load_path(path), path)
    end if full
    require_paths.join(',')
    
  else 
    constant.types[type]
  end
end

#filter(constants, filters) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/tap/tasks/list.rb', line 100

def filter(constants, filters)
  return constants if filters.empty?
  
  filters.collect! {|filter| Regexp.new(filter) }
  constants = constants.select do |constant|
    filters.all? do |filter|
      constant.path =~ filter
    end
  end
end

#fullmap(constants) ⇒ Object



111
112
113
114
115
# File 'lib/tap/tasks/list.rb', line 111

def fullmap(constants)
  paths = {}
  constants.each {|constant| paths[constant] = constant.path }
  paths
end

#load_path(path) ⇒ Object



172
173
174
175
176
# File 'lib/tap/tasks/list.rb', line 172

def load_path(path)
  $:.find do |load_path|
    File.exists?(File.join(load_path, path))
  end || '?'
end

#manifest(*filters) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
# File 'lib/tap/tasks/list.rb', line 58

def manifest(*filters)
  constants = filter(basis, filters)
  
  paths = full ? fullmap(constants) : minimap(constants)
  constants = constants.sort_by {|constant| paths[constant] }
  
  descriptions = {}
  selected_paths = []
  selected_types = types
  
  selected_types.each do |type|
    lines = []
    constants.each do |constant|
      next unless constant.types.include?(type)
      
      path = paths[constant]
      selected_paths << path
      lines << [path, describe(constant, type)]
    end
    
    descriptions[type] = lines unless lines.empty?
  end
  
  format = "  %-#{max_width(selected_paths)}s # %s"
  
  lines = []
  selected_types.each do |type|
    next unless descriptions.has_key?(type)
    
    lines << "#{type}:"
    descriptions[type].each do |description|
      lines << (format % description)
    end
  end
  
  if lines.empty?
    lines << "(no constants match criteria)"
  end
  
  lines
end

#max_width(paths) ⇒ Object



178
179
180
181
# File 'lib/tap/tasks/list.rb', line 178

def max_width(paths)
  max = paths.collect {|path| path.length }.max
  max.nil? || max < 20 ? 20 : max
end

#minimap(constants) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/tap/tasks/list.rb', line 117

def minimap(constants)
  paths = {}
  constants.each do |constant|
    paths[constant] = split(constant.path)
  end
  
  minimap = {}
  queue = constants.dup
  while !queue.empty?
    next_queue = []
    queue.each do |constant|
      path = paths[constant].shift
      
      if current = minimap[path]
        next_queue << current unless current == :skip
        next_queue << constant
        minimap[path] = :skip
      else
        minimap[path] = constant
      end
    end
    
    queue = next_queue
  end
  
  minimap.delete_if {|path, constant| constant == :skip }.invert
end

#split(path) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/tap/tasks/list.rb', line 145

def split(path)
  splits = []
  current = nil
  path.split('/').reverse_each do |split|
    current = current ? File.join(split, current) : split
    splits << current
  end
  splits
end

#typesObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tap/tasks/list.rb', line 46

def types
  return @types unless all
  
  types = []
  app.env.constants.each do |constant|
    types.concat constant.types.keys
  end
  types.uniq!
  types.sort!
  types
end