Class: Sunshine::DependencyLib
- Inherits:
-
Object
- Object
- Sunshine::DependencyLib
- Defined in:
- lib/sunshine/dependency_lib.rb
Overview
DependencyLib is a simple class for building and handling depenedencies. A dependency tree can be defined by inheriting the DependencyLib class, and dependencies can be defined through dependency instantiation methods:
dependency_lib.instance_eval do
yum 'ruby', :pkg => 'ruby-devel'
yum 'rubygems', :requires => 'ruby'
gem 'rdoc', :requires => 'rubygems'
gem 'ri', :requires => 'rubygems'
end
Calling the install for rdoc will then check and install all of its parent dependencies as well:
dependency_lib.install 'rdoc', 'ri'
Dependencies may also be generic and/or have custom bash scripts for installs, uninstalls, and presence checks:
dependency 'custom' do
requires 'yum', 'ruby'
install 'sudo yum install custom'
uninstall 'sudo yum remove custom'
check 'yum list installed custom'
end
See the Dependency class for more information.
Defined Under Namespace
Classes: MissingDependency
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
Class Method Summary collapse
-
.dependency_types ⇒ Object
Array of all dependency classes.
-
.sudo=(value) ⇒ Object
Define if sudo should be used.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns a dependency hash by type: DependencyLib #=> => <Yum…>, :apt => <Apt…>, ….
-
#add(dep) ⇒ Object
Add a dependency to the dependencies hash.
-
#exist?(key) ⇒ Boolean
Checks for the existance of a dependency by name.
-
#get(name, options = {}) ⇒ Object
Get a dependency object by name.
-
#initialize ⇒ DependencyLib
constructor
A new instance of DependencyLib.
-
#install(*deps) ⇒ Object
Install one or more dependencies:.
-
#send_each(method, *deps) ⇒ Object
Get and call method on each dependency passed.
-
#uninstall(*deps) ⇒ Object
Uninstall one or more dependencies:.
Constructor Details
#initialize ⇒ DependencyLib
Returns a new instance of DependencyLib.
52 53 54 |
# File 'lib/sunshine/dependency_lib.rb', line 52 def initialize @dependencies = Hash.new end |
Instance Attribute Details
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
50 51 52 |
# File 'lib/sunshine/dependency_lib.rb', line 50 def dependencies @dependencies end |
Class Method Details
.dependency_types ⇒ Object
Array of all dependency classes. Appended to automatically when DependencyLib::Dependency is inherited.
45 46 47 |
# File 'lib/sunshine/dependency_lib.rb', line 45 def self.dependency_types @dependency_types ||= [] end |
.sudo=(value) ⇒ Object
Define if sudo should be used
194 195 196 197 198 |
# File 'lib/sunshine/dependency_lib.rb', line 194 def self.sudo= value dependency_types.each do |dep_class| dep_class.sudo = value end end |
Instance Method Details
#[](key) ⇒ Object
Returns a dependency hash by type:
DependencyLib['name'] #=> {:yum => <Yum...>, :apt => <Apt...>, ...}
61 62 63 |
# File 'lib/sunshine/dependency_lib.rb', line 61 def [](key) @dependencies[key] end |
#add(dep) ⇒ Object
Add a dependency to the dependencies hash.
69 70 71 |
# File 'lib/sunshine/dependency_lib.rb', line 69 def add dep (@dependencies[dep.name] ||= []).unshift dep end |
#exist?(key) ⇒ Boolean
Checks for the existance of a dependency by name
77 78 79 |
# File 'lib/sunshine/dependency_lib.rb', line 77 def exist? key @dependencies.has_key? key end |
#get(name, options = {}) ⇒ Object
Get a dependency object by name. Supports passing :type => :pkg_manager if dependencies with the same name but different package managers exist:
dependencies.get 'daemon', :type => Gem
#=> <Gem @name="daemon"...>
For an ‘nginx’ dependency defined for both apt and yum, where the yum dependency object was added to the tree last. Returns nil if no matching dependency type is found:
dependencies.get 'nginx'
#=> <Yum @name="nginx"...>
dependencies.get 'nginx', :type => Apt
#=> <Apt @name="nginx"...>
Use the :prefer option if a certain dependency type is prefered but will fall back to whatever first dependency is available:
dependencies.yum 'my_dep'
dependencies.get 'my_dep', :prefer => Apt
#=> <Yum @name="my_dep"...>
Both the :type and the :prefer options support passing arrays to search from best to least acceptable candidate:
dependencies.yum 'my_dep'
dependencies.apt 'my_dep'
dependencies.get 'my_dep', :type => [Tpkg, Yum]
#=> <Yum @name="my_dep"...>
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sunshine/dependency_lib.rb', line 110 def get name, ={} return unless exist? name deps = @dependencies[name] dep_types = [*([:type] || [:prefer])].compact return deps.first if dep_types.empty? dep_types.each do |dep_type| deps.each do |dep| return dep if dep_type === dep end end return deps.first unless [:type] end |
#install(*deps) ⇒ Object
Install one or more dependencies:
dependencies.install 'dep1', 'dep2',
See DependencyLib#get and Dependency#install! for supported options.
Note: If a Dependency object is passed and the :type option is set, DependencyLib will attempt to find and install a dependency of class :type with the same name as the passed Dependency object:
my_dep = dependencies.yum "my_dep_yum_only"
dependencies.install my_dep, :type => Apt
#=> "No dependency 'my_dep' [Sunshine::Apt]"
142 143 144 |
# File 'lib/sunshine/dependency_lib.rb', line 142 def install(*deps) send_each(:install!, *deps) end |
#send_each(method, *deps) ⇒ Object
Get and call method on each dependency passed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/sunshine/dependency_lib.rb', line 162 def send_each(method, *deps) = Hash === deps.last ? deps.delete_at(-1).dup : {} #if options[:call].respond_to? :pkg_manager # options[:prefer] ||= options[:call].pkg_manager #end deps.each do |dep_name| dep = if Dependency === dep_name if [:type] && !([:type] === dep_name) get(dep_name.name, ) else dep_name end else get(dep_name, ) end raise MissingDependency, "No dependency '#{dep_name}' [#{[:type] || "any"}]" if !dep # Remove :type so dependencies of other types than dep can be installed .delete(:type) dep.send method, end end |
#uninstall(*deps) ⇒ Object
Uninstall one or more dependencies:
dependencies.uninstall 'dep1', 'dep2',
See DependencyLib#get and Dependency#uninstall! for supported options.
154 155 156 |
# File 'lib/sunshine/dependency_lib.rb', line 154 def uninstall(*deps) send_each(:uninstall!, *deps) end |