Class: Gjp::BaseCommand

Inherits:
Clamp::Command
  • Object
show all
Includes:
Logging
Defined in:
lib/gjp/commands/base.rb

Overview

implements common options and utility methods

Instance Method Summary collapse

Methods included from Logging

#log

Instance Method Details

#checking_exceptionsObject

handles most fatal exceptions



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gjp/commands/base.rb', line 75

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 gjp project directory, see gjp init"
rescue NoPackageDirectoryError => e
  $stderr.puts "#{e.directory} is not a gjp package directory, see README"
rescue GitAlreadyInitedError => e
  $stderr.puts "This directory is already a gjp project"
rescue ExecutableNotFoundError => e
  $stderr.puts "Executable #{e.executable} not found in kit/ or any of its subdirectories"
end

#configure_log_level(v, vv, vvv) ⇒ Object

maps verbosity options to log level



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gjp/commands/base.rb', line 29

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 there is a dry-run in progress



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

def ensure_dry_running(state, project)
  if project.is_dry_running == state
    yield
  else
    if state == true
      puts "Please start a dry-run first, use \"gjp dry-run\""
    else
      puts "Please finish or abort this dry-run first, use \"gjp finish\" or \"gjp finish --abort\""
    end
  end
end

#format_path(path, project) ⇒ Object

generates a version of path relative to the current directory



63
64
65
66
67
68
69
70
71
72
# File 'lib/gjp/commands/base.rb', line 63

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



55
56
57
58
59
60
# File 'lib/gjp/commands/base.rb', line 55

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

#verbose=(flag) ⇒ Object



24
25
26
# File 'lib/gjp/commands/base.rb', line 24

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

#very_verbose=(flag) ⇒ Object



20
21
22
# File 'lib/gjp/commands/base.rb', line 20

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

#very_very_verbose=(flag) ⇒ Object

verbosity handlers



16
17
18
# File 'lib/gjp/commands/base.rb', line 16

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