Class: Bridgetown::Commands::Application

Inherits:
Bridgetown::Command show all
Defined in:
bridgetown-core/lib/bridgetown-core/commands/application.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Bridgetown::Command

command_line, start, summary

Class Method Details

.desc(name, description) ⇒ Object



32
33
34
35
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 32

def self.desc(name, description)
  @next_cmd_name = name.split.first
  @next_cmd_description = description
end

.register(klass, name) ⇒ Object



26
27
28
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 26

def self.register(klass, name, *)
  registrations[name.to_s] = klass
end

.registrationsObject



24
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 24

def self.registrations = @registrations ||= {}

.subcommand(name, klass) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 37

def self.subcommand(name, klass)
  return unless @next_cmd_name == name

  table[:command].commands[name] = klass
  klass.description = @next_cmd_description

  @next_cmd_name = nil
  @next_cmd_description = nil
end

Instance Method Details

#callObject



62
63
64
65
66
67
68
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 62

def call
  if @command
    @command.()
  else
    print_usage
  end
end

#display_rake_tasks(rake, previous_max_length) ⇒ Object

Parameters:

  • rake (Rake::Application)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 168

def display_rake_tasks(rake, previous_max_length)
  # based in part on `Rake::Application#display_tasks_and_comments`
  displayable_tasks = rake.tasks.to_h do |t|
    [t.name_with_args, t.comment]
  end

  name_max_length = [previous_max_length, displayable_tasks.keys.map(&:length).max].max
  displayable_tasks.find do |name, comment|
    next if name == "default"

    spaces = " " * (name_max_length - name.length)
    puts "  #{"bridgetown".cyan} #{name.bold.cyan}  #{spaces}#{comment}"
  end
end

#load_rake_tasks(rake) ⇒ Object

Parameters:

  • rake (Rake::Application)


184
185
186
187
188
189
190
191
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 184

def load_rake_tasks(rake)
  rake.load_rakefile
  tasks = rake.instance_variable_get(:@tasks)
  rake.instance_variable_set(:@tasks, tasks.reject do |_k, v|
    v.locations.first&.include?("/lib/rails/tasks/") ||
      v.locations.first&.include?("/lib/rake/dsl_definition")
  end)
end

#locate_command(input) ⇒ Object

Parameters:

  • input (String)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 110

def locate_command(input)
  token = input.first

  case token
  when "--help", "-help", "-h"
    input[0] = "help"
    input
  when "c"
    input[0] = "console"
    input
  when "s"
    input[0] = "start"
    input
  else
    self.class.table[:command].commands.find do |name, _command|
      next unless name.start_with?(token)

      input[0] = name
      input
    end
  end
end

#locate_rake_task(input) ⇒ Object

Parameters:

  • input (String)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 134

def locate_rake_task(input) # rubocop:todo Metrics
  require "rake"
  Rake::TaskManager. = true
  cmd = input.first

  Rake.with_application do |rake|
    rake.standard_exception_handling do
      rakefile, _location = rake.find_rakefile_location
      unless rakefile
        puts "No Rakefile found (searching: #{rake.class::DEFAULT_RAKEFILES.join(", ")})\n\n"
        new.invoke("help")
        return # rubocop:disable Lint/NonLocalExitFromIterator
      end
      rake.init("bridgetown")
      load_rake_tasks(rake)
    end

    # either return a command proc or nothing, #call will handle either case
    if Rake::Task.task_defined?(cmd.split("[")[0])
      @command = -> { rake.top_level }
    else
      puts "#{"Unknown token:".bold.red} #{cmd.split("[")[0].yellow}\n\n"
    end
  rescue RuntimeError => e
    # re-raise error unless it's an error through Minitest
    raise e unless e.message.include?("ruby -Ilib:test")

    Bridgetown.logger.error "test aborted!"
    Bridgetown.logger.error e.message
    exit(false)
  end
end

#parse(input) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 47

def parse(input)
  Registrations.load_registrations self.class
  self.class.table.merged.parse(input, self)

  return self if input.empty?

  new_input = locate_command(input)
  unless new_input
    locate_rake_task(input)
    return self
  end

  parse(new_input)
end

rubocop:disable Metrics



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'bridgetown-core/lib/bridgetown-core/commands/application.rb', line 70

def print_usage # rubocop:disable Metrics
  puts "Bridgetown v#{Bridgetown::VERSION.magenta} \"#{Bridgetown::CODE_NAME.yellow}\" " \
       "is a #{self.class.description}"
  puts ""
  puts "Usage:".bold.green + " #{"bridgetown".bold.cyan} #{"<command> [options]".cyan}\n\n"
  puts "Commands:".bold.green

  commands = self.class.table[:command].commands.to_h do |name, command|
    inputs = command.table.each.find do |item|
      item.is_a?(Samovar::Many) || item.is_a?(Samovar::One) || item.is_a?(Samovar::Nested)
    end
    name = name.bold.cyan
    name = "#{name} #{"<#{inputs.key.upcase}>".cyan}" if inputs
    [name, command]
  end

  name_max_length = commands.keys.map { Foundation::Packages::Ansi.strip(_1).length }.max
  commands.find do |name, command|
    spaces = " " * (name_max_length - Foundation::Packages::Ansi.strip(name).length)
    puts "  #{"bridgetown".cyan} #{name}  #{spaces}#{command.description}"
  end

  puts ""

  require "rake"
  Rake::TaskManager. = true
  Rake.with_application do |rake|
    rake.instance_variable_set(:@name, "  bridgetown")
    rake.standard_exception_handling do
      rakefile, _location = rake.find_rakefile_location
      return unless rakefile # rubocop:disable Lint/NonLocalExitFromIterator

      load_rake_tasks(rake)
      puts "Available Rake Tasks:".bold.green
      display_rake_tasks(rake, name_max_length)
    end
  end
end