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_compact(opts) ⇒ Object



107
108
109
110
111
# File 'lib/tinyci/cli.rb', line 107

def self.do_compact(opts)
  logger = MultiLogger.new(quiet: opts[:quiet])
  
  TinyCI::Compactor.new(logger: logger, working_dir: opts[:dir], num_builds_to_leave: opts[:num_builds_to_leave], builds_to_leave: opts[:builds_to_leave]).compact!
end

.do_install(opts) ⇒ Object



101
102
103
104
105
# File 'lib/tinyci/cli.rb', line 101

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tinyci/cli.rb', line 82

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



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
72
73
74
75
76
77
78
79
80
# File 'lib/tinyci/cli.rb', line 16

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,
    'compact' => OptionParser.new do |o|
      o.banner = "Usage: compact [options]"
      o.on("-n", "--num-builds-to-leave <NUM>", "number of builds to leave in place, starting from the most recent") {|n| opts[:num_builds_to_leave] = n}
      o.on("-b", "--builds-to-leave <BUILDS>", "specific build directories to leave in place, comma-separated") {|b| opts[:builds_to_leave] = b.split(',')}
      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\ncompact  compress old build artifacts\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



113
114
115
116
117
# File 'lib/tinyci/cli.rb', line 113

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