Class: Hen

Inherits:
Object
  • Object
show all
Defined in:
lib/hen.rb,
lib/hen/cli.rb,
lib/hen/dsl.rb,
lib/hen/version.rb,
lib/hen/commands.rb

Overview

The class handling the program logic. This is what you use in your Rakefile. See the README for more information.

Defined Under Namespace

Modules: CLI, Commands, DSL, Version

Constant Summary collapse

HENDIRS =

The directories to search for hen files. Set environment variable HENPATH to add more.

[File.expand_path('../hens', __FILE__)]
HENS =

All hens found, mapped by their name.

Hash.new { |h, k| h[k] = [] }
RCDIRS =

Directories to search for .henrc files.

[ENV.user_home, '.']
HENRC_NAME =

The name of the .henrc file.

'.henrc'
VERSION =
Version.to_s

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, overwrite = false, &block) ⇒ Hen

call-seq:

new(args, overwrite = false) { ... }

Creates a new Hen instance of a certain name and optional dependencies; see #resolve_args for details on the args argument. Requires a definition block; see #lay! for details.

Adds itself to the global hen container via add_hen.


243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/hen.rb', line 243

def initialize(args, overwrite = false, &block)
  @name, @dependencies = resolve_args(args)

  @laid = false

  unless @block = block
    raise LocalJumpError, "#{@name}: no block given" if verbose
    return
  end

  self.class.add_hen(self, overwrite)
end

Class Attribute Details

.hensObject (readonly)

The global container for all loaded hens.


74
75
76
# File 'lib/hen.rb', line 74

def hens
  @hens
end

.verboseObject (readonly)

The verbosity concerning errors and warnings.


77
78
79
# File 'lib/hen.rb', line 77

def verbose
  @verbose
end

Instance Attribute Details

#blockObject (readonly)

The hen's definition block.


233
234
235
# File 'lib/hen.rb', line 233

def block
  @block
end

#dependenciesObject (readonly)

The list of the hen's dependencies.


230
231
232
# File 'lib/hen.rb', line 230

def dependencies
  @dependencies
end

#nameObject (readonly)

The hen's name.


227
228
229
# File 'lib/hen.rb', line 227

def name
  @name
end

Class Method Details

.[](hen) ⇒ Object

call-seq:

Hen[hen] => aHen

Get hen by name.


138
139
140
# File 'lib/hen.rb', line 138

def [](hen)
  @hens[hen]
end

.add_hen(hen, overwrite = false) ⇒ Object

call-seq:

add_hen(hen, overwrite = false)

Adds hen to the global container. Overwrites an existing hen only if overwrite is true.


126
127
128
129
130
131
132
# File 'lib/hen.rb', line 126

def add_hen(hen, overwrite = false)
  if overwrite
    @hens[hen.name]   = hen
  else
    @hens[hen.name] ||= hen
  end
end

.config(key = nil) ⇒ Object

call-seq:

config => aHash
config(key) => anObject

The configuration resulting from the user's .henrc. Takes optional key argument as “path” into the config hash, returning the thusly retrieved value.

Example:

config('a/b/c')  #=> @config[:a][:b][:c]

168
169
170
171
172
173
174
# File 'lib/hen.rb', line 168

def config(key = nil)
  @config ||= load_config
  return @config unless key

  key.split('/').inject(@config) { |value, k| value.fetch(k.to_sym) }
rescue IndexError, NoMethodError
end

.default_henrcObject

call-seq:

default_henrc => aString

The path to a suitable default .henrc location.


154
155
156
# File 'lib/hen.rb', line 154

def default_henrc
  find_henrc(false).first
end

.henrcObject

call-seq:

henrc => anArray

The paths to the user's .henrc files.


146
147
148
# File 'lib/hen.rb', line 146

def henrc
  @henrc ||= find_henrc
end

.lay!(*args) ⇒ Object

call-seq:

lay!
lay!(:some_hen, :some_other_hen)
lay!(exclude: [:some_hen, :some_other_hen])

Loads the hens, causing them to lay their eggs^H^H^Htasks. Either all, if no restrictions are specified, or the given hens, or all but those given in the :exclude option.


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/hen.rb', line 87

def lay!(*args)
  # Extract potential options hash
  options = args.last.is_a?(Hash) ? args.pop : {}

  @verbose = options[:verbose] if options.key?(:verbose)

  yield.each { |key, value| config[key].update(value) } if block_given?

  # Handle include/exclude requirements
  excl = options[:exclude]
  args, default = args.empty? ? [Array(excl), true] : [args, false]

  inclexcl = Hash.new(default)
  args.each { |arg| inclexcl[arg.to_s] = !default }

  # Load all available hens (as far as the
  # include/exclude conditions are met)
  load_hens { |hen| inclexcl[hen] }

  # Execute each hen definition
  hens.each { |name, hen|
    # Load any dependencies, in case they're not included yet
    begin
      load_hens(*hen.dependencies)
    rescue LoadError => err
      warn "#{name}: Required dependency missing: " <<
           File.basename(err.to_s, '.rake') if verbose
      next
    end

    hen.lay!
  }
end

Instance Method Details

#lay!Object

call-seq:

hen.lay!

Runs the definition block, exposing helper methods from the DSL.


260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/hen.rb', line 260

def lay!
  return if laid?

  @laid = true

  # Call dependencies first
  dependencies.each { |hen| self.class[hen].lay!  }

  block.bind(DSL).call
rescue => err
  trace = $DEBUG || Rake.application.options.trace

  warn "#{name}: #{err} (#{err.class})" if trace || verbose
  warn err.backtrace.join("\n  ") if trace
end