Class: Chap::Hook

Inherits:
Object
  • Object
show all
Includes:
SpecialMethods
Defined in:
lib/chap/hook.rb

Constant Summary

Constants included from SpecialMethods

SpecialMethods::SPECIAL_METHODS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SpecialMethods

included

Constructor Details

#initialize(path, config) ⇒ Hook

Returns a new instance of Hook.



6
7
8
9
10
# File 'lib/chap/hook.rb', line 6

def initialize(path, config)
  @path = path
  @config = config
  @with = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/chap/hook.rb', line 5

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/chap/hook.rb', line 5

def options
  @options
end

Instance Method Details

#evaluateObject



12
13
14
# File 'lib/chap/hook.rb', line 12

def evaluate
  instance_eval(File.read(@path), @path)
end

#rm_rvmrcObject



31
32
33
34
35
36
# File 'lib/chap/hook.rb', line 31

def rm_rvmrc
  %w[.rvmrc .ruby-version].each do |file|
    path = "#{release_path}/#{file}"
    run "rm -f #{path}" if File.exist?(path)
  end
end

#run(cmd) ⇒ Object



44
45
46
47
# File 'lib/chap/hook.rb', line 44

def run(cmd)
  cmd = "#{@with} #{cmd}"
  config.run(cmd)
end

hook helper methods



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/chap/hook.rb', line 17

def symlink_configs
  paths = Dir.glob("#{shared_path}/config/**/*").
            select {|p| File.file?(p) }
  paths.each do |src|
    relative_path = src.gsub(%r{.*config/},'config/')
    dest = "#{release_path}/#{relative_path}"
    # make sure the directory exist for symlink creation
    dirname = File.dirname(dest)
    FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
    FileUtils.rm_rf(dest) if File.exist?(dest)
    FileUtils.ln_s(src,dest)
  end
end

#with(prepend) ⇒ Object



38
39
40
41
42
# File 'lib/chap/hook.rb', line 38

def with(prepend)
  prev, @with = @with, prepend
  yield
  @with = prev
end