Class: Hookify::Command::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/hookify/commands/base.rb

Direct Known Subclasses

PreCommit, PreRelease

Instance Method Summary collapse

Methods included from Helpers

#display, #error, #executable, #home_directory, #hooks_directory, #running_on_a_mac?, #running_on_windows?

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



5
6
7
# File 'lib/hookify/commands/base.rb', line 5

def initialize(args)
  
end

Instance Method Details

#command_nameObject



9
10
11
# File 'lib/hookify/commands/base.rb', line 9

def command_name
  self.class.to_s.split("::").last.underscore
end

#file_nameObject



13
14
15
# File 'lib/hookify/commands/base.rb', line 13

def file_name
  command_name.gsub("_", "-")
end

#file_pathObject



17
18
19
# File 'lib/hookify/commands/base.rb', line 17

def file_path
  File.join(hooks_directory, file_name)
end

#remove_fileObject



29
30
31
# File 'lib/hookify/commands/base.rb', line 29

def remove_file
  File.delete(file_path) if File.exists?(file_path)
end

#ruby_script_pathObject



21
22
23
# File 'lib/hookify/commands/base.rb', line 21

def ruby_script_path
  "config/hooks/#{command_name}.rb"
end

#runObject



25
26
27
# File 'lib/hookify/commands/base.rb', line 25

def run
  
end

#write_fileObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hookify/commands/base.rb', line 33

def write_file
  script         = IO.read(File.join(Hookify.root, "hooks/#{file_name}"))
  remove_file # temporary until I get symlinks working
  already_exists = File.exists?(file_path)
  
  # make the pre-x file, only if you haven't manually created one yourself
  unless already_exists
    FileUtils.mkdir_p(hooks_directory)
    File.new(file_path, "w")
  end
  
  FileUtils.mkdir_p(File.dirname(ruby_script_path))
  File.new(ruby_script_path, "w") unless File.exists?(ruby_script_path)
  
  display("Creating #{file_path}...\n")
  
  File.open(file_path, "r+") do |file|
    content = ""
    # if we want something like this, it needs to be more advanced
    #if already_exists
    #  content = file.read
    #  content << "\n"
    #end
    content << script
    file.pos = 0
    file.print content
    file.truncate(file.pos)
  end
  
  executable(file_path)
  
  display("Add a rake task named 'hookify:#{command_name}'")
end