Class: Ufo::Command

Inherits:
Thor
  • Object
show all
Extended by:
Utils::Logging
Defined in:
lib/ufo/command.rb

Class Method Summary collapse

Methods included from Utils::Logging

logger

Class Method Details

.alter_command_description(command) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ufo/command.rb', line 134

def alter_command_description(command)
  return unless command

  # Add description to beginning of long_description
  long_desc = if command.long_description
      "#{command.description}\n\n#{command.long_description}"
    else
      command.description
    end

  # add reference url to end of the long_description
  unless website.empty?
    full_command = [command.ancestor_name, command.name].compact.join('-')
    url = "#{website}/reference/ufo-#{full_command}"
    long_desc += "\n\nHelp also available at: #{url}"
  end

  command.long_description = long_desc
end

.check_old_version_structure!(args) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ufo/command.rb', line 109

def check_old_version_structure!(args)
  return unless File.exist?('.ufo/settings.yml')
  puts "ERROR: Old .ufo configurations detected".color(:red)
  puts <<~EOL
    It looks like this project .ufo files for an older ufo version.
    The old .ufo structure does not work with this version of ufo.

    Current Installed UFO Version: 6.0.9

    Please upgrade.

    See: https://ufoships.com/docs/upgrading/upgrade6/
  EOL
  exit 1
end

.check_project!(args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ufo/command.rb', line 97

def check_project!(args)
  command_name = args.first
  return if subcommand?
  return if command_name.nil?
  return if help_flags.include?(args.last) # IE: -h help
  return if %w[-h -v --version central init version].include?(command_name)
  return if File.exist?("#{Ufo.root}/.ufo")

  logger.error "ERROR: It doesnt look like this is a ufo project. Are you sure you are in a ufo project?".color(:red)
  ENV['UFO_TEST'] ? raise : exit(1)
end

.command_help(shell, command_name) ⇒ Object

Override command_help to include the description at the top of the long_description.



127
128
129
130
131
132
# File 'lib/ufo/command.rb', line 127

def command_help(shell, command_name)
  meth = normalize_command_name(command_name)
  command = all_commands[meth]
  alter_command_description(command)
  super
end

.configure_dsl_evaluatorObject

Uses Ufo.logger and Ufo.root which loads Ufo.config. See comment where configure_dsl_evaluator is used about Ufo.role



79
80
81
82
83
84
85
86
# File 'lib/ufo/command.rb', line 79

def configure_dsl_evaluator
  DslEvaluator.configure do |config|
    config.backtrace.select_pattern = Ufo.root.to_s
    config.logger = Ufo.logger
    config.on_exception = :exit
    config.root = Ufo.root
  end
end

.dispatch(m, args, options, config) ⇒ Object



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
# File 'lib/ufo/command.rb', line 36

def dispatch(m, args, options, config)
  # Old note: Configuring the DslEvalulator requires Ufo.root and Ufo.logger which
  # loads Ufo.config and Ufo::Config#load_project_config
  # This requires Ufo.role.
  # So we set Ufo.role before triggering Ufo.config loading
  check_project!(args)
  check_old_version_structure!(args)
  # Special case for `ufo central` commands.
  # Dont want to call configure_dsl_evaluator
  # and trigger loading of config => .ufo/config.rb
  # Also, using ARGV instead of args because args is called by thor in multiple passes
  # For `ufo central update`:
  # * 1st pass: "central"
  # * 2nd pass: "update"
  configure_dsl_evaluator unless ARGV[0] == "central"

  # Allow calling for help via:
  #   ufo command help
  #   ufo command -h
  #   ufo command --help
  #   ufo command -D
  #
  # as well thor's normal way:
  #
  #   ufo help command
  if args.length > 1 && !(args & help_flags).empty?
    args -= help_flags
    args.insert(-2, "help")
  end

  #   ufo version
  #   ufo --version
  #   ufo -v
  version_flags = ["--version", "-v"]
  if args.length == 1 && !(args & version_flags).empty?
    args = ["version"]
  end

  super
end

.exit_on_failure?Boolean

github.com/erikhuda/thor/issues/244 Deprecation warning: Thor exit with status 0 on errors. To keep this behavior, you must define ‘exit_on_failure?` in `Lono::CLI` You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.

Returns:

  • (Boolean)


163
164
165
# File 'lib/ufo/command.rb', line 163

def exit_on_failure?
  true
end

.help_flagsObject



88
89
90
# File 'lib/ufo/command.rb', line 88

def help_flags
  Thor::HELP_MAPPINGS + ["help"]
end

.subcommand?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/ufo/command.rb', line 93

def subcommand?
  !!caller.detect { |l| l.include?('in subcommand') }
end

.websiteObject

meant to be overriden



156
157
158
# File 'lib/ufo/command.rb', line 156

def website
  "http://ufoships.com"
end