Class: Jekyll::Commands::PreCommit

Inherits:
Command
  • Object
show all
Defined in:
lib/jekyll-pre-commit/commands.rb

Class Method Summary collapse

Class Method Details

.init_with_program(prog) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jekyll-pre-commit/commands.rb', line 5

def init_with_program(prog)
  prog.command(:"pre-commit") do |c|
    c.syntax 'pre-commit SUBCOMMAND'
    c.description 'Runs SUBCOMMAND'
    c.option "force", "--force", "Overwrites an existing pre-commit if one already exists"

    c.action do |args, options|
      raise ArgumentError, "You must provide a subcommand" if args.empty?
      if args.include? "init"
        dest = "#{Dir.pwd}/.git/hooks/pre-commit"
        if (File.exist?(dest) && !options["force"])
          Jekyll.logger.abort_with "An existing pre-commit file already exists. If you'd like" \
            " to overwrite that file (e.g. you're upgrading) pass the --force flag."
        end

        if (File.exist?(dest))
          File.delete(dest)
        end

        # Is there a better way to resolve this path?
        src = `bundle show jekyll-pre-commit`
        src = src.strip + "/lib/jekyll-pre-commit/pre-commit"

        File.symlink src, dest
        puts "pre-commit hook file created!"
      end
    end
  end
end