Module: Hen::DSL

Extended by:
DSL, Rake::DSL
Included in:
DSL
Defined in:
lib/hen/dsl.rb

Overview

Some helper methods for use inside of a Hen definition.

Instance Method Summary collapse

Instance Method Details

#configObject

The Hen configuration.


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hen/dsl.rb', line 44

def config
  extend_object(Hen.config.dup) {
    # Always return a duplicate for a value,
    # hence making the configuration immutable
    def [](key)  # :nodoc:
      fetch(key).dup
    rescue IndexError
      {}
    end
  }
end

#execute(*commands) ⇒ Object

Find a command that is executable and run it. Intended for platform-dependent alternatives (Command A is not available? Then try B instead).


76
77
78
79
80
81
82
83
84
85
# File 'lib/hen/dsl.rb', line 76

def execute(*commands)
  if command = File.which_command(commands)
    sh(command) { |ok, res|
      warn "Error while executing command: #{command} " <<
           "(return code #{res.exitstatus})" unless ok
    }
  else
    warn "Command not found: #{commands.join('; ')}"
  end
end

#gitObject

Encapsulates tasks targeting at Git, skipping those if the current project is not controlled by Git. Yields a Git object via #init_git.


157
158
159
# File 'lib/hen/dsl.rb', line 157

def git
  have_git? ? yield(init_git) : skipping('Git')
end

#have_task?(t) ⇒ Boolean

Return true if task t is defined, false otherwise.

Returns:

  • (Boolean)

69
70
71
# File 'lib/hen/dsl.rb', line 69

def have_task?(t)
  tasks.key?(t.to_s)
end

#mangle_files!(*args) ⇒ Object

Clean up the file lists in args by removing duplicates and either deleting any files that are not managed by the source code management system (untracked files) or, if the project is not version-controlled or the SCM is not recognized, deleting any files that don't exist.

The return value indicates whether source control is in effect.

Currently supported SCM's (in that order): Git, SVN.


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hen/dsl.rb', line 96

def mangle_files!(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}

  managed_files = [:git, :svn].find { |scm|
    res = send(scm) { |scm_obj| scm_obj.managed_files }
    break res if res
  } if !options.key?(:managed) || options[:managed]

  args.compact.each { |files|
    files.uniq!

    if managed_files
      files.replace(files & managed_files)
    else
      files.delete_if { |file| !File.readable?(file) }
    end
  }

  !!managed_files
end

#map_options(options) ⇒ Object

Map options hash to array of command line arguments.


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/hen/dsl.rb', line 131

def map_options(options)
  options.map { |option, value|
    option = '--' << option.to_s.tr('_', '-')

    case value
      when Array  then value.map { |_value| [option, _value] }
      when String then [option, value]
      else value ? option : nil
    end
  }.compact.flatten
end

#rubygems(&block) ⇒ Object

Encapsulates tasks targeting at RubyGems.org, skipping those if RubyGem's 'push' command is not available. Yields an optional proc to obtain RubyGems (pseudo-)objects from (via call; reaching out to #init_rubygems).


147
148
149
150
151
152
153
# File 'lib/hen/dsl.rb', line 147

def rubygems(&block)
  if have_rubygems?
    call_block(block) { |*args| init_rubygems }
  else
    skipping 'RubyGems'
  end
end

#set_options(object, options, type = object.class) ⇒ Object

Set options on object by calling the corresponding setter method for each option; warns about illegal options. Optionally, use type to describe object (defaults to its class).


120
121
122
123
124
125
126
127
128
# File 'lib/hen/dsl.rb', line 120

def set_options(object, options, type = object.class)
  options.each { |option, value|
    if object.respond_to?(setter = "#{option}=")
      object.send(setter, value)
    else
      warn "Unknown #{type} option: #{option}"
    end
  }
end

#svnObject

Encapsulates tasks targeting at SVN, skipping those if the current project is not controlled by SVN. Yields an SVN object via #init_svn.


163
164
165
# File 'lib/hen/dsl.rb', line 163

def svn
  have_svn? ? yield(init_svn) : skipping('SVN')
end

#task!(t, *args, &block) ⇒ Object

Define task t, but overwrite any existing task of that name! (Rake usually just adds them up.)


63
64
65
66
# File 'lib/hen/dsl.rb', line 63

def task!(t, *args, &block)
  tasks.delete(t.to_s)
  task(t, *args, &block)
end

#tasksObject

Get a handle on the currently defined tasks.


57
58
59
# File 'lib/hen/dsl.rb', line 57

def tasks
  Rake.application.instance_variable_get(:@tasks)
end