Class: Rusky::Hook

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/rusky/hook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook_name, cwd, setting = nil) ⇒ Hook

Returns a new instance of Hook.



18
19
20
21
22
23
# File 'lib/rusky/hook.rb', line 18

def initialize(hook_name, cwd, setting=nil)
  @hook_name = hook_name
  @cwd = cwd
  @filename = File.join(@cwd, '.git', 'hooks', hook_name)
  @setting = setting
end

Instance Attribute Details

#cwdObject (readonly)

Returns the value of attribute cwd.



16
17
18
# File 'lib/rusky/hook.rb', line 16

def cwd
  @cwd
end

#filenameObject (readonly)

Returns the value of attribute filename.



16
17
18
# File 'lib/rusky/hook.rb', line 16

def filename
  @filename
end

#hook_nameObject (readonly)

Returns the value of attribute hook_name.



16
17
18
# File 'lib/rusky/hook.rb', line 16

def hook_name
  @hook_name
end

#settingObject (readonly)

Returns the value of attribute setting.



16
17
18
# File 'lib/rusky/hook.rb', line 16

def setting
  @setting
end

Instance Method Details

#createObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rusky/hook.rb', line 25

def create
  if !exists?
    puts "rusky > creating #{hook_name} hook script..."
    write
  else
    if is_rusky_hook?
      puts "rusky > overwriting #{hook_name} hook script because also existing one is created by rusky."
      write
    else
      puts "rusky > skip creating #{hook_name} hook script because existing one is created by you or other tool."
    end
  end
  self
end

#define_taskObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rusky/hook.rb', line 54

def define_task
  # prioritize existing user hook
  if Rake::Task.task_defined? rake_task_name
    puts "rusky > skip creating a new rake task since the task for #{hook_name} is already defined."
    return
  end

  commands = setting&.commands_for hook_name

  task "#{rake_task_name}" do
    if commands.empty?
      puts "rusky > No command for #{hook_name} is defined in .rusky file"
      next
    end

    commands.each do |command|
      puts "rusky > #{command}"
      system(command) || raise("rusky > #{command} failed")
    end
  end
end

#deleteObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rusky/hook.rb', line 40

def delete
  if !exists?
    puts "rusky > skip deleting #{hook_name} hook script since there is no git hook file."
  else
    if is_rusky_hook?
      puts "rusky > removing #{hook_name} hook script..."
      File.delete(filename)
    else
      puts "rusky > skip deleting #{hook_name} hook script because existing one is created by you or other tool."
    end
  end
  self
end