Class: Cany::Recipe

Inherits:
Object
  • Object
show all
Includes:
Mixins::DependMixin
Defined in:
lib/cany/recipe.rb

Defined Under Namespace

Classes: DSL

Class Attribute Summary collapse

Instance Attribute Summary collapse

Recipe Steps - to be overridden in subclass collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::DependMixin

#create_dep

Constructor Details

#initialize(spec, &configure_block) ⇒ Recipe

Creates a new instance of this recipe



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cany/recipe.rb', line 33

def initialize(spec, &configure_block)
  @spec = spec
  @inner = nil
  @hooks = Hash[(self.class.defined_hooks || []).map do |name|
    [name, Cany.hash_with_array_as_default]
  end]
  @options = Hash[(self.class.defined_options || {}).map do |name, default|
    [name, default.dup]
  end]
  self.class.const_get(:DSL).new(self).exec(&configure_block) if configure_block
end

Class Attribute Details

.defined_hooksObject

Returns the value of attribute defined_hooks.



148
149
150
# File 'lib/cany/recipe.rb', line 148

def defined_hooks
  @defined_hooks
end

.defined_optionsObject

Returns the value of attribute defined_options.



148
149
150
# File 'lib/cany/recipe.rb', line 148

def defined_options
  @defined_options
end

Instance Attribute Details

#innerObject

Returns the value of attribute inner.



52
53
54
# File 'lib/cany/recipe.rb', line 52

def inner
  @inner
end

#specObject (readonly)

Returns the value of attribute spec.



52
53
54
# File 'lib/cany/recipe.rb', line 52

def spec
  @spec
end

Class Method Details

.from_name(name) ⇒ Cany::Recipe

Looks for the class registered for the given name

Raises:

  • UnknownRecipe if there is no recipe registered for this name.



26
27
28
29
# File 'lib/cany/recipe.rb', line 26

def self.from_name(name)
  raise UnknownRecipe.new(name) unless @@recipes[name]
  @@recipes[name]
end

.hook(name) ⇒ Object

Define a new hook



153
154
155
156
# File 'lib/cany/recipe.rb', line 153

def hook(name)
  @defined_hooks ||= []
  @defined_hooks  << name
end

.option(name, default = {}) ⇒ Object

Define a configure option. These kind of option are design for other recipes not for the user. See Recipe::DSL for this.



163
164
165
166
# File 'lib/cany/recipe.rb', line 163

def option(name, default={})
  @defined_options ||= {}
  @defined_options[name] = default
end

.register_as(name) ⇒ Object

This method should be call in subclasses to register new recipe instances. Cany ignores any recipe subclasses which does not call register_as. If multiple recipes register on the same name the later one will overwrite the earlier one and therefore used by Cany.



11
12
13
14
15
16
17
18
19
# File 'lib/cany/recipe.rb', line 11

def self.register_as(name)
  @@recipes ||= {}
  @@recipes[name] = self
  module_eval("    def name\n      :\#{name}\n    end\n    EOS\nend\n", __FILE__, __LINE__)

Instance Method Details

#binaryObject

create binary (package) version of this file (means make install)



255
256
257
# File 'lib/cany/recipe.rb', line 255

def binary
  inner.binary
end

#buildObject

build the program (means ./configure and make)



249
250
251
# File 'lib/cany/recipe.rb', line 249

def build
  inner.build
end

#cleanObject

clean the build directory from all temporary and created files



243
244
245
# File 'lib/cany/recipe.rb', line 243

def clean
  inner.clean
end

#configure(name, options) ⇒ Object

Configure an other recipe



187
188
189
# File 'lib/cany/recipe.rb', line 187

def configure(name, options)
  option(name).merge! options
end

#create(creator) ⇒ Object

This step is executed to create the distribution specific packages from canspec. The recipe can e.g. add additional dependencies or adjust the package meta data.



238
239
# File 'lib/cany/recipe.rb', line 238

def create(creator)
end

#depend(*args) ⇒ Object



220
221
222
# File 'lib/cany/recipe.rb', line 220

def depend(*args)
  @spec.dependencies << create_dep(*args)
end

#exec(*args) ⇒ Object Also known as: exec_

Run a command inside the build directory. In most cases it is not needed to call this method directly. Look at the other helper methods.

The method expects as arguments the program name and additional parameters for the program. The arguments can be group with arguments, but must not:

Examples:

exec 'echo', %w(a b)
exec ['echo', 'a', 'b']
exec 'echo', 'a', 'b'

Raises:



69
70
71
72
73
74
75
# File 'lib/cany/recipe.rb', line 69

def exec(*args)
  args.flatten!
  Cany.logger.info args.join(' ')
  unless system(*args)
    raise CommandExecutionFailed.new args
  end
end

#hook(name) ⇒ Object



169
170
171
172
173
# File 'lib/cany/recipe.rb', line 169

def hook(name)
  @hooks[name].tap do |hook|
    raise UnknownHook.new name unless hook
  end
end

#install(source, destination) ⇒ Object

Install files or directory from the build directory



94
95
96
# File 'lib/cany/recipe.rb', line 94

def install(source, destination)
  exec 'dh_install', source, destination
end

#install_content(filename, content) ⇒ Object

Install a file. The content is passed as argument. This method is designed to be used by recipes to create files dynamically.



103
104
105
106
107
108
# File 'lib/cany/recipe.rb', line 103

def install_content(filename, content)
  FileUtils.mkdir_p File.dirname File.join('debian', spec.name, filename)
  File.open File.join('debian', spec.name, filename), 'w' do |f|
    f.write content
  end
end

#install_dir(path) ⇒ Object

Installs/creates an empty directory



113
114
115
# File 'lib/cany/recipe.rb', line 113

def install_dir(path)
  exec 'dh_installdirs', path
end

Create a file named destination as a link to a file named source



119
120
121
# File 'lib/cany/recipe.rb', line 119

def install_link(source, destination)
  exec 'dh_link', source, destination
end

#install_service(*args) ⇒ Object

Specify a command call (program + args) that should be installed as service and started automatically. This method should be only call inside the binary step.



134
135
136
# File 'lib/cany/recipe.rb', line 134

def install_service(*args)
  recipe(:system).install_service(*args)
end

#option(name) ⇒ Object

Ask for the current values for a defined option



177
178
179
180
181
# File 'lib/cany/recipe.rb', line 177

def option(name)
  @options[name].tap do |option|
    raise UnknownOption.new name unless option
  end
end

#prepareObject

Prepares the recipes to run things. This is call exactly once for all recipes before recipes actions are executed.



232
233
# File 'lib/cany/recipe.rb', line 232

def prepare
end

#recipe(name) ⇒ Object

Access the recipe instance from another loaded recipe of this specification



206
207
208
209
210
211
212
213
# File 'lib/cany/recipe.rb', line 206

def recipe(name)
  return spec.system_recipe if name == :system
  recipe_class = Recipe.from_name(name)
  @spec.recipes.each do |one_recipe|
    return one_recipe if one_recipe.instance_of? recipe_class
  end
  raise UnloadedRecipe.new name
end

#rmtree(*args) ⇒ Object

Ensure that the given files or directories are no present. Directories are removed recursively.



141
142
143
144
145
# File 'lib/cany/recipe.rb', line 141

def rmtree(*args)
  args.flatten.each do |path|
    ::FileUtils.remove_entry path if File.exists? path
  end
end

#ruby_bin(*args) ⇒ Object

Run a ruby task (like gem, bundle, rake …)

The method expects as arguments the program name and additional parameters for the program. See exec for more examples



84
85
86
# File 'lib/cany/recipe.rb', line 84

def ruby_bin(*args)
  exec RbConfig.ruby, '-S', *args
end

#run_hook(name, state) ⇒ Object

Run defined actions for a hook



195
196
197
198
199
200
# File 'lib/cany/recipe.rb', line 195

def run_hook(name, state)
  hook(name)[state].each do |block|
    Cany.logger.info  "run #{block} for hook #{name} in state #{state} ..."
    instance_eval(&block)
  end
end