Class: TinyCI::CLI

Inherits:
Object
  • Object
show all
Extended by:
GitUtils
Defined in:
lib/tinyci/cli.rb

Overview

Defines the CLI interface. Uses OptionParser.

Constant Summary collapse

LOGO =
File.read(File.expand_path('logo.txt', __dir__))

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GitUtils

git_cmd, git_directory_path, inside_bare_repo?, inside_git_directory?, inside_repository?, inside_work_tree?, repo_root

Class Method Details

.do_install(opts) ⇒ Object



92
93
94
95
96
# File 'lib/tinyci/cli.rb', line 92

def self.do_install(opts)
  logger = MultiLogger.new(quiet: opts[:quiet])

  TinyCI::Installer.new(logger: logger, working_dir: opts[:dir]).write!
end

.do_run(opts) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tinyci/cli.rb', line 73

def self.do_run(opts)
  if PidFile.running?
    puts 'TinyCI is already running!' unless opts[:quiet]
    return false
  end
  
  opts.delete(:commit) if opts[:all]
  
  if !opts[:commit] && !opts[:all]
    puts "You must pass either --commit or --all, or try --help" unless opts[:quiet]
    return false
  end
        
  logger = MultiLogger.new(quiet: opts[:quiet])
  result = Scheduler.new(commit: opts[:commit], logger: logger, working_dir: opts[:dir]).run!
  
  result
end

.parse!(argv = ARGV) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
66
67
68
69
70
71
# File 'lib/tinyci/cli.rb', line 14

def self.parse!(argv = ARGV)
  opts = {}
  
  global = OptionParser.new do |o|
    o.banner = ''
    o.on("-q", "--[no-]quiet", "surpress output") {|q| opts[:quiet] = q}
    o.on("-D <DIR>", "--dir <DIR>", "specify repository location") {|d| opts[:dir] = d}
  end
  
  subcommands = { 
    'run' => OptionParser.new do |o|
      o.banner = "#{LOGO % TinyCI::VERSION}\nGlobal options:\n  #{global.help.slice(3..-1)}\nrun options:"
      o.on("-c <SHA>", "--commit <SHA>", "run against a specific commit") {|c| opts[:commit] = c}
      o.on("-a", "--all", "run against all commits which have not been run against before") {|a| opts[:all] = a}
    end,
    'install' => OptionParser.new do |o|
      o.banner = "Usage: install [options]"
      o.on("-q", "--[no-]quiet", "quietly run") {|v| opts[:quiet] = v}
    end
  }
  
      banner = "\#{LOGO % TinyCI::VERSION}\nGlobal options:\n\#{global.help.strip}\n\nAvailable commands:\nrun      build and test the repo\ninstall  install the git hook into the current repository\nversion  print the TinyCI version number\n"
  if argv[0] == '--help'
    puts banner
    return false
  end
  
  global.order!(argv)
  command = argv.shift
  
  if command.nil? || subcommands[command].nil?
    puts banner
    return false
  end
  
  subcommands[command].order!(argv)
  
  opts[:dir] ||= begin
    repo_root
  rescue TinyCI::Subprocesses::SubprocessError => e
    if e.message == '`git rev-parse --is-inside-git-dir` failed with code 32768'
      exit 1
    else
      raise e
    end
  end
  
  send "do_#{command}", opts
end

Instance Method Details

#do_version(opts) ⇒ Object



98
99
100
101
102
# File 'lib/tinyci/cli.rb', line 98

def do_version(opts)
  puts TinyCI::VERSION
  
  true
end