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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gjp/commands/base.rb', line 71

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
  $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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gjp/commands/base.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 there is a dry-run in progress



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

def ensure_dry_running(state, project)
  if project.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



59
60
61
62
63
64
65
66
67
68
# File 'lib/gjp/commands/base.rb', line 59

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



53
54
55
56
# File 'lib/gjp/commands/base.rb', line 53

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

#verbose=(flag) ⇒ Object



22
23
24
# File 'lib/gjp/commands/base.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/gjp/commands/base.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/gjp/commands/base.rb', line 14

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