Class: Tetra::Subcommand

Inherits:
Clamp::Command
  • Object
show all
Includes:
Logging
Defined in:
lib/tetra/ui/subcommand.rb

Overview

implements common options and utility methods

Instance Method Summary collapse

Methods included from Logging

#log

Instance Method Details

#bypass_parsing(args) ⇒ Object

override default option parsing to pass options to other commands



40
41
42
43
44
45
46
# File 'lib/tetra/ui/subcommand.rb', line 40

def bypass_parsing(args)
  log.level = ::Logger::WARN if args.delete "--verbose"
  log.level = ::Logger::INFO if args.delete "--very-verbose"
  log.level = ::Logger::DEBUG if args.delete "--very-very-verbose"

  @options = args
end

#checking_exceptionsObject

handles most fatal exceptions



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tetra/ui/subcommand.rb', line 89

def checking_exceptions
  yield
rescue Errno::EACCES => e
  $stderr.puts e
rescue Errno::ENOENT => e
  $stderr.puts e
rescue Errno::EEXIST => e
  $stderr.puts e
rescue NoProjectDirectoryError => e
  $stderr.puts "#{e.directory} is not a tetra project directory, see \"tetra init\""
rescue GitAlreadyInitedError
  $stderr.puts "This directory is already a tetra project"
rescue ExecutionFailed => e
  $stderr.puts "Failed to run `#{e.commandline}` (exit status #{e.status})"
rescue Interrupt
  $stderr.puts "Execution interrupted by the user"
end

#configure_log_level(v, vv, vvv) ⇒ Object

maps verbosity options to log level



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tetra/ui/subcommand.rb', line 27

def configure_log_level(v, vv, vvv)
  if vvv
    log.level = ::Logger::DEBUG
  elsif vv
    log.level = ::Logger::INFO
  elsif v
    log.level = ::Logger::WARN
  else
    log.level = ::Logger::ERROR
  end
end

#ensure_dry_running(state, project) ⇒ Object

prints an error message and exits unless a dry-running condition is met. Conditions can be: :is_in_progress, :is_not_in_progress or :has_finished



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tetra/ui/subcommand.rb', line 51

def ensure_dry_running(state, project)
  dry_running = project.dry_running?
  has_finished = !project.version.nil?

  if (state == :is_in_progress && dry_running) ||
     (state == :is_not_in_progress && !dry_running) ||
     (state == :has_finished && !dry_running && has_finished)
    yield
  else
    if (state == :is_in_progress) ||
       (state == :has_finished && !dry_running && !has_finished)
      puts "Please start a dry-run first, use \"tetra dry-run\""
    elsif (state == :is_not_in_progress) ||
          (state == :has_finished && dry_running)
      puts "There is a dry-run in progress, please finish it (^D) or abort it (^C^D)"
    end
  end
end

#format_path(path, project) ⇒ Object

generates a version of path relative to the current directory



77
78
79
80
81
82
83
84
85
86
# File 'lib/tetra/ui/subcommand.rb', line 77

def format_path(path, project)
  full_path = (
    if Pathname.new(path).relative?
      File.join(project.full_path, path)
    else
      path
    end
  )
  Pathname.new(full_path).relative_path_from(Pathname.new(Dir.pwd))
end

outputs output of a file generation



71
72
73
74
# File 'lib/tetra/ui/subcommand.rb', line 71

def print_generation_result(project, result_path, conflict_count = 0)
  puts "#{format_path(result_path, project)} generated"
  puts "Warning: #{conflict_count} unresolved conflicts, please review and commit" if conflict_count > 0
end

#verbose=(flag) ⇒ Object



22
23
24
# File 'lib/tetra/ui/subcommand.rb', line 22

def verbose=(flag)
  configure_log_level(flag, very_verbose?, very_very_verbose?)
end

#very_verbose=(flag) ⇒ Object



18
19
20
# File 'lib/tetra/ui/subcommand.rb', line 18

def very_verbose=(flag)
  configure_log_level(verbose?, flag, very_very_verbose?)
end

#very_very_verbose=(flag) ⇒ Object

verbosity handlers



14
15
16
# File 'lib/tetra/ui/subcommand.rb', line 14

def very_very_verbose=(flag)
  configure_log_level(verbose?, very_verbose?, flag)
end