|
| 1 | +require "optparse" |
1 | 2 | require "git_tracker/prepare_commit_message" |
2 | 3 | require "git_tracker/hook" |
3 | 4 | require "git_tracker/repository" |
4 | 5 | require "git_tracker/version" |
5 | 6 |
|
6 | 7 | module GitTracker |
7 | | - module Runner |
8 | | - def self.call(cmd_arg = "help", *args) |
9 | | - command = cmd_arg.tr("-", "_") |
| 8 | + class Runner |
| 9 | + def self.call(*args, io: $stdout) |
| 10 | + args << "--help" if args.empty? |
| 11 | + options = {} |
10 | 12 |
|
11 | | - abort("[git_tracker] command: '#{cmd_arg}' does not exist.") unless respond_to?(command) |
| 13 | + OptionParser.new { |optparse| |
| 14 | + optparse.banner = <<~BANNER |
| 15 | + git-tracker is a Git hook used during the normal lifecycle of committing, |
| 16 | + rebasing, merging, etc… This hook must be initialized into each repository |
| 17 | + in which you wish to use it. |
12 | 18 |
|
13 | | - send(command, *args) |
| 19 | + usage: git-tracker init |
| 20 | + BANNER |
| 21 | + |
| 22 | + optparse.on("-h", "--help", "Prints this help") do |
| 23 | + io.puts(optparse) |
| 24 | + options[:exit] = true |
| 25 | + end |
| 26 | + |
| 27 | + optparse.on("-v", "--version", "Prints the git-tracker version number") do |
| 28 | + io.puts("git-tracker #{VERSION}") |
| 29 | + options[:exit] = true |
| 30 | + end |
| 31 | + }.parse!(args) |
| 32 | + |
| 33 | + return if options.fetch(:exit, false) |
| 34 | + |
| 35 | + command, *others = args |
| 36 | + |
| 37 | + new(command: command, arguments: others, options: options).call |
| 38 | + end |
| 39 | + |
| 40 | + def initialize(command:, arguments:, options:) |
| 41 | + @command = command |
| 42 | + @arguments = arguments |
| 43 | + @options = options |
14 | 44 | end |
15 | 45 |
|
16 | | - def self.prepare_commit_msg(*args) |
17 | | - PrepareCommitMessage.call(*args) |
| 46 | + def call |
| 47 | + abort("[git_tracker] command: '#{command}' does not exist.") unless sub_command |
| 48 | + |
| 49 | + send(sub_command) |
18 | 50 | end |
19 | 51 |
|
20 | | - def self.init |
| 52 | + private |
| 53 | + |
| 54 | + SUB_COMMANDS = { |
| 55 | + init: :init, |
| 56 | + install: :install, |
| 57 | + "prepare-commit-msg": :prepare_commit_msg |
| 58 | + }.freeze |
| 59 | + private_constant :SUB_COMMANDS |
| 60 | + |
| 61 | + attr_reader :arguments, :command, :options |
| 62 | + |
| 63 | + def init |
21 | 64 | Hook.init(at: Repository.root) |
22 | 65 | end |
23 | 66 |
|
24 | | - def self.install |
25 | | - warn("`git-tracker install` is deprecated. Please use `git-tracker init`", uplevel: 1) |
| 67 | + def install |
| 68 | + warn("`git-tracker install` is deprecated. Please use `git-tracker init`.") |
26 | 69 |
|
27 | 70 | init |
28 | 71 | end |
29 | 72 |
|
30 | | - def self.help |
31 | | - puts <<~HELP |
32 | | - git-tracker #{VERSION} is installed. |
| 73 | + def prepare_commit_msg |
| 74 | + PrepareCommitMessage.call(*arguments) |
| 75 | + end |
33 | 76 |
|
34 | | - Remember, git-tracker is a hook which Git interacts with during its normal |
35 | | - lifecycle of committing, rebasing, merging, etc. You need to initialize this |
36 | | - hook by running `git-tracker init` from each repository in which you wish to |
37 | | - use it. Cheers! |
38 | | - HELP |
| 77 | + def sub_command |
| 78 | + @sub_command ||= SUB_COMMANDS.fetch(command.intern, false) |
39 | 79 | end |
40 | 80 | end |
41 | 81 | end |
0 commit comments