Class: Herodot::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/herodot/commands.rb

Constant Summary collapse

SCRIPT =
"#!/bin/sh\nherodot track $(pwd)".freeze
DEFAULT_RANGE =
'this week'.freeze

Class Method Summary collapse

Class Method Details

.init(path, config) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/herodot/commands.rb', line 21

def self.init(path, config)
  path = '.' if path.nil?
  puts "Start tracking of `#{File.expand_path(path)}` into `#{config.worklog_file}`."
  hooks = "#{path}/.git/hooks"
  abort('Path is not a git repository.') unless File.exist?(hooks)
  %w[post-checkout post-commit].each do |name|
    File.open("#{hooks}/#{name}", 'w') { |file| file.write(SCRIPT) }
    File.chmod(0o755, "#{hooks}/#{name}")
    FileUtils.touch(config.worklog_file)
  end
end


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/herodot/commands.rb', line 43

def self.link(path)
  path = '.' if path.nil?
  choose do |menu|
    menu.prompt = 'What tracker do you want to link to?'
    menu.choice(:jira) { link_jira(path) }
    menu.choice(:github) { link_github(path) }
    menu.choice(:gitlab) { link_gitlab(path) }
    menu.choices(:other) { link_other(path) }
    menu.default = :other
  end
end


61
62
63
64
# File 'lib/herodot/commands.rb', line 61

def self.link_github(path)
  handle = ask('Github handle (something/something for https://github.com/something/something)?')
  ProjectLink.link(path, "https://github.com/#{handle}/issues/", '\\d+')
end


66
67
68
69
# File 'lib/herodot/commands.rb', line 66

def self.link_gitlab(path)
  handle = ask('GitLab handle (something/something for https://gitlab.com/something/something)?')
  ProjectLink.link(path, "https://gitlab.com/#{handle}/issues/", '\\d+')
end


55
56
57
58
59
# File 'lib/herodot/commands.rb', line 55

def self.link_jira(path)
  prefix = ask('Jira URL prefix (something for https://something.atlassian.net)?')
  pattern = ask('Ticket prefix (ABCD for tickets like ABCD-123)')
  ProjectLink.link(path, "http://#{prefix}.atlassian.net/browse/", "#{pattern}-\\d+")
end


71
72
73
74
75
# File 'lib/herodot/commands.rb', line 71

def self.link_other(path)
  url = ask('URL to issue tracker:')
  pattern = ask('Ticket regex pattern (ruby):')
  ProjectLink.link(path, url, pattern)
end

.show(args, config, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/herodot/commands.rb', line 11

def self.show(args, config, opts = {})
  subject = args.empty? ? DEFAULT_RANGE : args.join(' ')
  range = Chronic.parse(subject, guess: false, context: :past)
  abort "Date not parsable: #{args.join(' ')}" unless range
  worklog = Parser.parse(range, config)
  decorated_worklog = ProjectLink.new(worklog)
  output = Output.print(decorated_worklog.totals, opts)
  puts output
end

.track(path, config) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/herodot/commands.rb', line 33

def self.track(path, config)
  puts 'Logging into worklog'
  File.open(config.worklog_file, 'a') do |worklog|
    datestr = Time.now.strftime('%a %b %e %H:%M:%S %z %Y')
    branch = `(cd #{path} && git rev-parse --abbrev-ref HEAD)`.strip
    line = [datestr, path, branch].join(';')
    worklog.puts(line)
  end
end