Class: Filament::Package
- Inherits:
-
Object
- Object
- Filament::Package
- Defined in:
- lib/filament/package.rb
Constant Summary collapse
- @@package_scms =
[]
- @@orig_system =
Kernel.method :system
Instance Attribute Summary collapse
-
#build_context ⇒ Object
readonly
Returns the value of attribute build_context.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#package_resolver ⇒ Object
readonly
Returns the value of attribute package_resolver.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#realpath ⇒ Object
(also: #pathname)
readonly
Returns the value of attribute realpath.
-
#target_resolver ⇒ Object
readonly
Returns the value of attribute target_resolver.
-
#workspace ⇒ Object
readonly
Returns the value of attribute workspace.
Class Method Summary collapse
Instance Method Summary collapse
-
#+(subpackage_name) ⇒ Object
returns a Package object for the subpackage with the given name NOTE: a Package is returned, even when this package is not a valid parent or this package doesn’t exist.
- #<<(target) ⇒ Object
- #[](target_name) ⇒ Object
- #clobber ⇒ Object
- #create ⇒ Object
- #descriptor ⇒ Object
- #disabled? ⇒ Boolean
-
#execute(&block) ⇒ Object
executes cmd with this packages as the wd, and with $this_package set to self.
- #exist? ⇒ Boolean
-
#has_valid_parent? ⇒ Boolean
returns true if: 1.
-
#has_valid_root? ⇒ Boolean
returns true if this is root package or if one of its parents is.
-
#initialize(h) ⇒ Package
constructor
A new instance of Package.
-
#list ⇒ Object
Recursive package operations.
- #load(fail_on_missing = false) ⇒ Object
-
#loaded? ⇒ Boolean
Target-related stuff.
-
#make_parent! ⇒ Object
if this package is not yet a parent, it will mark it as one.
- #make_root! ⇒ Object
- #make_valid! ⇒ Object
- #output_dir ⇒ Object
-
#parent? ⇒ Boolean
returns true if this package is a parent package a parent package is one that is a root or has a .parent file.
-
#path ⇒ Object
returns the path of the package (from the root_dir).
-
#root? ⇒ Boolean
returns true if this package is a root package.
-
#scm ⇒ Object
returns the scm for this package.
-
#subpackage(subpackage_name) ⇒ Object
returns nil if there is no such subpackage or the subpackage with the given name.
-
#subpackages(include_disabled = false) ⇒ Object
returns a list of subpackages.
-
#system(cmd) ⇒ Object
executes cmd with this package as the wd.
- #target(name, klass, &block) ⇒ Object
- #target_dir(subdir = nil) ⇒ Object
- #targets ⇒ Object
-
#uri ⇒ Object
(also: #full_name)
returns the full name of the package.
-
#valid? ⇒ Boolean
returns true if: 1.
- #working_dir ⇒ Object
- #workspace? ⇒ Boolean
- #youngest_root_package ⇒ Object
Constructor Details
#initialize(h) ⇒ Package
Returns a new instance of Package.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 144 145 146 147 148 |
# File 'lib/filament/package.rb', line 100 def initialize(h) @parent = h[:parent] @workspace = h[:workspace] if @workspace.nil? unless @parent.nil? @workspace = @parent.workspace else raise "all packages must belong to a workspace" end end @package_resolver = h[:package_resolver] || PackageResolver.new(@workspace.package_resolver) @name = h[:name] @realpath = h[:realpath] @subpackage_cache = {} if @realpath.nil? raise "package must have a name." if @name.nil? raise "package must have a parent." if @parent.nil? @realpath = @parent.realpath + @name else raise "package must not have a name." unless @name.nil? raise "package must not have a parent." unless @parent.nil? @realpath = Pathname.new(@realpath) raise "realpath must be absolute" unless @realpath.absolute? end @descriptor_loaded = false @targets = {} @target_resolver = TargetResolver.new(self) # add the "cache" directory for this package @package_resolver << youngest_root_package.subpackage('cache') # add the root directory for the current package to the list of search directories @package_resolver << youngest_root_package # add the "vendor" directory for this package @package_resolver << subpackage('vendor') @execution_context = ExecutionContext.new(self) @build_context = BuildContext.new(self) @scm = SCM::CompoundSCM.new end |
Instance Attribute Details
#build_context ⇒ Object (readonly)
Returns the value of attribute build_context.
97 98 99 |
# File 'lib/filament/package.rb', line 97 def build_context @build_context end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
97 98 99 |
# File 'lib/filament/package.rb', line 97 def name @name end |
#package_resolver ⇒ Object (readonly)
Returns the value of attribute package_resolver.
97 98 99 |
# File 'lib/filament/package.rb', line 97 def package_resolver @package_resolver end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
97 98 99 |
# File 'lib/filament/package.rb', line 97 def parent @parent end |
#realpath ⇒ Object (readonly) Also known as: pathname
Returns the value of attribute realpath.
97 98 99 |
# File 'lib/filament/package.rb', line 97 def realpath @realpath end |
#target_resolver ⇒ Object (readonly)
Returns the value of attribute target_resolver.
97 98 99 |
# File 'lib/filament/package.rb', line 97 def target_resolver @target_resolver end |
#workspace ⇒ Object (readonly)
Returns the value of attribute workspace.
97 98 99 |
# File 'lib/filament/package.rb', line 97 def workspace @workspace end |
Class Method Details
.orig_system(*args) ⇒ Object
357 |
# File 'lib/filament/package.rb', line 357 def self.orig_system(*args); @@orig_system.call(*args); end |
.scm(klass) ⇒ Object
87 88 89 |
# File 'lib/filament/package.rb', line 87 def self.scm(klass) @@package_scms << klass end |
Instance Method Details
#+(subpackage_name) ⇒ Object
returns a Package object for the subpackage with the given name NOTE: a Package is returned, even when this package is not a valid parent or this package doesn’t exist
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/filament/package.rb', line 287 def +(subpackage_name) if subpackage_name.respond_to?(:to_ary) name_stack = subpackage_name.to_ary elsif subpackage_name.respond_to?(:to_s) name_stack = subpackage_name.to_s.split(/\//).reverse else raise "illegal argument. only string or array is allowed here." end sub_name = name_stack.pop if @subpackage_cache.key?(sub_name) sub = @subpackage_cache[sub_name] else sub = Package.new(:name => sub_name, :parent => self) @subpackage_cache[sub_name] = sub end if name_stack.empty? return sub else return sub + name_stack end end |
#<<(target) ⇒ Object
375 376 377 378 |
# File 'lib/filament/package.rb', line 375 def << (target) @targets[target.name.to_sym] = target return self end |
#[](target_name) ⇒ Object
419 420 421 422 |
# File 'lib/filament/package.rb', line 419 def [](target_name) load return @targets[target_name.to_sym] end |
#clobber ⇒ Object
414 415 416 417 |
# File 'lib/filament/package.rb', line 414 def clobber rm_rf(working_dir) rm_rf(output_dir) end |
#create ⇒ Object
247 248 249 250 |
# File 'lib/filament/package.rb', line 247 def create raise "directory #{@realpath} does not exist" unless exist? make_valid! end |
#descriptor ⇒ Object
371 372 373 |
# File 'lib/filament/package.rb', line 371 def descriptor return "#{@realpath}/package.rb" end |
#disabled? ⇒ Boolean
211 212 213 214 215 216 217 218 219 |
# File 'lib/filament/package.rb', line 211 def disabled? p = @realpath + '.disabled' return true if p.exist? if root? return false else return @parent.disabled? end end |
#execute(&block) ⇒ Object
executes cmd with this packages as the wd, and with $this_package set to self
352 353 354 |
# File 'lib/filament/package.rb', line 352 def execute(&block) @execution_context.execute(&block) end |
#exist? ⇒ Boolean
150 151 152 |
# File 'lib/filament/package.rb', line 150 def exist? return @realpath.exist? end |
#has_valid_parent? ⇒ Boolean
returns true if:
-
its parent thinks its a parent
-
its parent has a valid root
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/filament/package.rb', line 189 def has_valid_parent? # should have a valid root return false unless has_valid_root? # roots shouldn't have parents return @parent.nil? if root? # everything else should return false unless @parent.parent? # walk up return @parent.has_valid_parent? end |
#has_valid_root? ⇒ Boolean
returns true if this is root package or if one of its parents is
168 169 170 171 |
# File 'lib/filament/package.rb', line 168 def has_valid_root? return true if root? return @parent.has_valid_root? end |
#list ⇒ Object
Recursive package operations
329 330 331 332 333 |
# File 'lib/filament/package.rb', line 329 def list subpackages.each do |s| puts s.uri end end |
#load(fail_on_missing = false) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/filament/package.rb', line 380 def load(fail_on_missing=false) return if loaded? @descriptor_loaded = true pn = Pathname.new(descriptor) unless pn.exist? unless parent? or not fail_on_missing raise "package descriptor for #{@realpath} does not exist '#{pn}'" end return end @execution_context.execute { instance_eval(pn.read, pn.realpath) } end |
#loaded? ⇒ Boolean
Target-related stuff
367 368 369 |
# File 'lib/filament/package.rb', line 367 def loaded? return @descriptor_loaded end |
#make_parent! ⇒ Object
if this package is not yet a parent, it will mark it as one
228 229 230 231 232 |
# File 'lib/filament/package.rb', line 228 def make_parent! return if parent? return make_root! if @parent.nil? touch(@realpath + '.parent') end |
#make_root! ⇒ Object
221 222 223 224 225 |
# File 'lib/filament/package.rb', line 221 def make_root! return if root? raise "won't convert parent to root" if parent? touch(@realpath + '.root') end |
#make_valid! ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/filament/package.rb', line 234 def make_valid! return if valid? p = @parent until p.nil? p.make_parent! p = p.parent end mkdir(@realpath) unless exist? return nil end |
#output_dir ⇒ Object
410 411 412 |
# File 'lib/filament/package.rb', line 410 def output_dir target_dir('obj') end |
#parent? ⇒ Boolean
returns true if this package is a parent package a parent package is one that is a root or has a .parent file
180 181 182 183 184 |
# File 'lib/filament/package.rb', line 180 def parent? return true if root? p = @realpath + '.parent' return p.exist? end |
#path ⇒ Object
returns the path of the package (from the root_dir)
259 260 261 262 263 |
# File 'lib/filament/package.rb', line 259 def path return '' if workspace? return @name if @parent.workspace? return @parent.path + '/' + @name end |
#root? ⇒ Boolean
returns true if this package is a root package. a root package is one that has a .root file.
161 162 163 164 165 |
# File 'lib/filament/package.rb', line 161 def root? return true if workspace? p = @realpath + '.root' return p.exist? end |
#scm ⇒ Object
returns the scm for this package
336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/filament/package.rb', line 336 def scm load return @scm unless @scm.empty? @scm << Filament::SCM::PackageSCM.new(self) @@package_scms.each do |scm| @scm << scm.new(@realpath) if scm.applies?(self) end # default to the simplest scm return @scm end |
#subpackage(subpackage_name) ⇒ Object
returns nil if there is no such subpackage or the subpackage with the given name
314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/filament/package.rb', line 314 def subpackage(subpackage_name) return nil unless parent? child = @realpath + subpackage_name if child.exist? # log "child found: #{subpackage_name} (#{child}) in #{full_name}" return self + subpackage_name else # log "child not found: #{subpackage_name} (#{child}) in #{full_name}" return nil end end |
#subpackages(include_disabled = false) ⇒ Object
returns a list of subpackages. [] is returned if there are none.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/filament/package.rb', line 266 def subpackages(include_disabled = false) return [] unless parent? children = @realpath.children.reject do |child| not child.directory? or child.basename.to_s =~ /^\./ end children.collect! do |child| subpackage(child.basename.to_s) end unless include_disabled children.reject! { |child| child.disabled? } end return children end |
#system(cmd) ⇒ Object
executes cmd with this package as the wd
360 361 362 363 364 |
# File 'lib/filament/package.rb', line 360 def system(cmd) execute do |dir| raise "error executing '#{cmd}' in #{dir}" unless Package.orig_system(cmd) end end |
#target(name, klass, &block) ⇒ Object
91 92 93 |
# File 'lib/filament/package.rb', line 91 def target(name, klass, &block) klass.new(name, &block) end |
#target_dir(subdir = nil) ⇒ Object
396 397 398 399 400 401 402 403 404 |
# File 'lib/filament/package.rb', line 396 def target_dir(subdir=nil) pn = @realpath + 'target' unless subdir.nil? pn += "#{subdir}/#{$build_dir_prefix}" end return pn.to_s end |
#targets ⇒ Object
424 425 426 427 |
# File 'lib/filament/package.rb', line 424 def targets load return @targets.values end |
#uri ⇒ Object Also known as: full_name
returns the full name of the package
253 254 255 |
# File 'lib/filament/package.rb', line 253 def uri return '//' + path end |
#valid? ⇒ Boolean
returns true if:
-
this package refers to a directory that exists
-
this package has a valid parent
206 207 208 209 |
# File 'lib/filament/package.rb', line 206 def valid? return false unless exist? and @realpath.directory? return has_valid_parent? end |
#working_dir ⇒ Object
406 407 408 |
# File 'lib/filament/package.rb', line 406 def working_dir target_dir('tmp') end |
#workspace? ⇒ Boolean
154 155 156 157 |
# File 'lib/filament/package.rb', line 154 def workspace? p = @realpath + '.workspace' return p.exist? end |
#youngest_root_package ⇒ Object
173 174 175 176 |
# File 'lib/filament/package.rb', line 173 def youngest_root_package return self if root? return @parent.youngest_root_package end |