1
1
mirror of https://github.com/NixOS/mobile-nixos.git synced 2024-12-18 13:31:36 +03:00
mobile-nixos/boot/init/lib/dependencies.rb

103 lines
2.1 KiB
Ruby
Raw Normal View History

2019-12-23 06:32:34 +03:00
module Dependencies
class BaseDependency
2019-12-23 22:46:06 +03:00
def fulfilled?()
2019-12-23 06:32:34 +03:00
true
end
def name()
self.class.name
end
def depends_on?(other)
raise "#{self.class.name} has to implement #depends_on?"
end
2019-12-23 06:32:34 +03:00
end
class Task < BaseDependency
def initialize(instance)
@instance = instance
end
2019-12-23 22:46:06 +03:00
def fulfilled?()
if @instance.ran
true
else
2019-12-23 22:46:06 +03:00
$logger.debug(" -> Dependency #{name} unfulfilled? (task #{@instance.inspect} hasn't run yet)")
false
end
end
def depends_on?(other)
@instance.depends_on?(other)
end
end
# When any of the dependencies given have been fulfilled, this dependency
# will be fulfilled.
class Any < BaseDependency
def initialize(*dependencies)
@dependencies = dependencies
end
def fulfilled?()
@dependencies.any? { |dependency| dependency.fulfilled? }
end
def depends_on?(other)
@dependencies.any? { |dependency| dependency.depends_on?(other) }
end
end
2019-12-23 06:32:34 +03:00
class Files < BaseDependency
def initialize(*patterns)
@patterns = *patterns
end
2019-12-23 22:46:06 +03:00
def fulfilled?()
if @patterns.all? { |pattern| Dir.glob(pattern).count > 0 }
true
else
$logger.debug do
patterns = @patterns.reject do |pattern|
Dir.glob(pattern).count > 0
end.join(", ")
2019-12-23 22:46:06 +03:00
" -> Dependency #{name} unfulfilled? (Pattern #{patterns} does not match paths)"
end
false
2019-12-23 06:32:34 +03:00
end
end
# It is unknown what creates the file.
# For mount points, prefer the +Mount+ dependency type.
def depends_on?(other)
false
end
end
# Checks in sysfs for the given network interface names.
class NetworkInterface < Files
def initialize(*names)
super(*names.map { |name| File.join("/sys/class/net", name) })
end
end
class Target < BaseDependency
def initialize(name)
@name = name.to_sym
end
def fulfilled?()
task.ran
end
def depends_on?(other)
task.depends_on?(other)
end
def task()
Targets[@name]
end
2019-12-23 06:32:34 +03:00
end
end