Class: Filament::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/filament/target.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, &block) ⇒ Target

Returns a new instance of Target.



51
52
53
54
55
56
57
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
# File 'lib/filament/target.rb', line 51

def initialize(args, &block)
  if args.is_a? Hash
    h = args
    @name = h[:name]
  else
    @name = args
    h = {}
  end

  weak_deps = h[:weak_deps] || []
  deps = h[:deps] || []

  @weak_deps = TargetList.new(*weak_deps)
  @deps = TargetList.new(*deps)

  @package = h[:package] || $context[:this_package]
  @invoked = false
  @proc = Proc.new {}
  @outputs = {}
  @options = {}
  @deployables = Set.new
  @on_define_procs = []
  @on_init_procs = []

  @platform = Filament::Platform.find(:name => $target_platform)
  @tag = Filament::BlockObject.new do |sym, a, p| 
    tags = @platform.tags
    if tags.member?(sym)
      p.call unless p.nil?
    end
    
    tags.member?(sym)
  end

  raise 'targets must have a name' if @name.nil?
  raise 'targets must have a package' if @package.nil?
  
  @package << self
  init
  
  @proc = Proc.new do 
    instance_eval(&block)
    on_init
    on_define
  end
end

Instance Attribute Details

#deployablesObject (readonly)

Returns the value of attribute deployables.



49
50
51
# File 'lib/filament/target.rb', line 49

def deployables
  @deployables
end

#depsObject (readonly)

Returns the value of attribute deps.



49
50
51
# File 'lib/filament/target.rb', line 49

def deps
  @deps
end

#nameObject (readonly)

Returns the value of attribute name.



49
50
51
# File 'lib/filament/target.rb', line 49

def name
  @name
end

#packageObject (readonly)

Returns the value of attribute package.



49
50
51
# File 'lib/filament/target.rb', line 49

def package
  @package
end

#platformObject (readonly)

Returns the value of attribute platform.



49
50
51
# File 'lib/filament/target.rb', line 49

def platform
  @platform
end

#tagObject (readonly)

Returns the value of attribute tag.



49
50
51
# File 'lib/filament/target.rb', line 49

def tag
  @tag
end

#weak_depsObject (readonly)

Returns the value of attribute weak_deps.



49
50
51
# File 'lib/filament/target.rb', line 49

def weak_deps
  @weak_deps
end

Class Method Details

.on_define(target = nil, &block) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/filament/target.rb', line 117

def self.on_define(target=nil, &block)
  @@on_define_procs ||= {}
  @@on_define_procs[self] ||= []

  unless block.nil?
    raise "if a block is given, target cannot be specified" unless target.nil?
    @@on_define_procs[self] << block
  else
    raise "if a block is not given, target cannot be nil" if target.nil?
    superclass.on_define(target) if superclass.respond_to?(:on_define)
    @@on_define_procs[self].each{ |p| p.call(target) }
  end
end

.on_init(target = nil, &block) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/filament/target.rb', line 140

def self.on_init(target=nil, &block)
  @@on_init_procs ||= {}
  @@on_init_procs[self] ||= []

  unless block.nil?
    raise "if a block is given, target cannot be specified" unless target.nil?
    @@on_init_procs[self] << block
  else
    raise "if a block is not given, target cannot be nil" if target.nil?
    superclass.on_init(target) if superclass.respond_to?(:on_init)
    @@on_init_procs[self].each{ |p| p.call(target) }
  end
end

Instance Method Details

#<=>(other) ⇒ Object

for easy sorting



168
169
170
# File 'lib/filament/target.rb', line 168

def <=>(other)
  return (uri <=> other)
end

#[](tag) ⇒ Object

returns the value in the output hash for the given tag



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/filament/target.rb', line 216

def [](tag)
  invoke

  o = @outputs[tag]
  
  return nil if o.nil?

  tasks = o[:tasks]
  tasks.each {|t| t.invoke} unless tasks.nil?
  return o[:output]
end

#assert_tag(*tags) ⇒ Object



98
99
100
101
# File 'lib/filament/target.rb', line 98

def assert_tag(*tags)
  tags -= platform.tags
  raise "target #{uri} requires platform tags: #{tags.join(', ')}" unless tags.empty?
end

#build(&block) ⇒ Object

builds this specific target (and all dependencies) after building, it executes the provided block in the build context



230
231
232
233
234
235
# File 'lib/filament/target.rb', line 230

def build(&block)
  @package.build_context.execute do
    invoke
    block.call unless block.nil?
  end
end

#collect_outputs(tag) ⇒ Object



189
190
191
192
193
194
# File 'lib/filament/target.rb', line 189

def collect_outputs(tag)
  o = flattened_deps.collect{|dep| dep[tag]}
 o << self[tag]
 o.compact!
  return o
end

#done?Boolean

returns true if the target has completed

Returns:

  • (Boolean)


238
239
240
# File 'lib/filament/target.rb', line 238

def done?
  return @invoked
end

#flattened_deps(include_weak = false) ⇒ Object

walks tree of dependencies, returns a complete list



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/filament/target.rb', line 173

def flattened_deps(include_weak=false)
  all = @deps.to_a.clone
  all += @weak_deps.to_a.clone if include_weak
  
  fd = []
  until all.empty?
    d = all.pop
    unless fd.include?(d)
      fd << d
      all += d.deps
      all += d.weak_deps if include_weak
    end
  end
  return fd
end

#invokeObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/filament/target.rb', line 242

def invoke
  return if done?
  @invoked = true
  
  @package.execute do
    @weak_deps.each {|t| t.invoke}
    @deps.each {|t| t.invoke}

    instance_eval(&@proc)
    
    puts ">>> BUILDING #{uri} (#{@package.pathname}, #{self})"
    @outputs.values.select{|h| h.key?(:tasks)}.collect{|h| h[:tasks]}.each do |tasks|
      tasks.each do |t|
        t.invoke
      end
    end
  end
end

#on_define(&block) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/filament/target.rb', line 108

def on_define(&block)
  unless block.nil?
    @on_define_procs << block
  else
    @on_define_procs.each { |p| p.call(self) }
    self.class.on_define(self)
  end
end

#on_init(&block) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/filament/target.rb', line 131

def on_init(&block)
  unless block.nil?
    @on_init_procs << block
  else
    @on_init_procs.each { |p| p.call(self) }
    self.class.on_init(self)
  end
end

#options(h = nil) ⇒ Object



103
104
105
106
# File 'lib/filament/target.rb', line 103

def options(h=nil)
  return @options if h.nil?
  @options.merge!(h)
end

#output(h, &block) ⇒ Object

adds some :output to the given :tag if a block is given, assume that’s the output if :deployable is true, also adds :output to deployables



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/filament/target.rb', line 199

def output(h, &block)
  tag = h[:tag]
  h[:output] ||= block
  if tag.respond_to?(:to_ary)
    tag.to_ary.each do |t|
      h2 = h.clone
      h2[:tag] = t
      output(h2)
    end
  else 
    @outputs[tag] = h 
  end
  
  @deployables << h[:output] if h[:deployable]
end

#output_dirObject



159
160
161
# File 'lib/filament/target.rb', line 159

def output_dir
  return "#{$context[:output_dir]}/#{package.path}"
end

#uriObject



163
164
165
# File 'lib/filament/target.rb', line 163

def uri
  return "#{@package.uri}:#{@name}"
end

#working_dir(subdir = nil) ⇒ Object



154
155
156
157
# File 'lib/filament/target.rb', line 154

def working_dir(subdir=nil)
  base = "#{$context[:working_dir]}/#{package.path}/#{name}"
  return subdir.nil? ? base : "#{base}/#{subdir.to_s}"
end