Class: Lifer::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/lifer/cli.rb

Overview

This class is the entrypoint for Lifer’s commandline interface.

Constant Summary collapse

File.read("%s/lib/lifer/templates/cli.txt.erb" % Lifer.gem_root)
SUBCOMMANDS =

This constant tracks the supported Lifer CLI subcommands.

Key: name
Value: description
{
   build: I18n.t("cli.subcommands.build"),
   help: I18n.t("cli.subcommands.help"),
   serve: I18n.t("cli.subcommands.serve")
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



35
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
# File 'lib/lifer/cli.rb', line 35

def initialize
  @subcommand, @args = user_input
  @parser =
    OptionParser.new do |parser|
      parser.banner = ERB.new(BANNER_ERB).result

      parser.on "-cCONFIG", "--config=CONFIG", topt(:config) do |config|
        @config_file = config
      end

      parser.on "-d", "--dump-default-config", topt(:dump_default_config) do |_|
        puts File.read(Lifer::Config::DEFAULT_CONFIG_FILE)
        exit
      end

      parser.on "-pPORT", "--port=PORT", topt(:port) do |port|
        @dev_server_port = Integer port
      rescue => exception
        raise I18n.t("cli.bad_port", exception:)
      end

      parser.on "-rROOT", "--root=ROOT", topt(:root) do |root|
        @root = root
      end
    end
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



33
34
35
# File 'lib/lifer/cli.rb', line 33

def args
  @args
end

#parserObject

Returns the value of attribute parser.



33
34
35
# File 'lib/lifer/cli.rb', line 33

def parser
  @parser
end

#subcommandObject

Returns the value of attribute subcommand.



33
34
35
# File 'lib/lifer/cli.rb', line 33

def subcommand
  @subcommand
end

Class Method Details

.start!void

This method returns an undefined value.

This method parses the given CLI subcommands and arguments, and then starts the Lifer program as requested.



30
# File 'lib/lifer/cli.rb', line 30

def start! = self.new.start!

Instance Method Details

#start!void

This method returns an undefined value.

Parses the user-provided CLI arguments and handles any requested commands.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lifer/cli.rb', line 65

def start!
  parser.parse! args

  brain = Lifer.brain(**{root: @root, config_file: @config_file}.compact)
  brain.require_user_provided_ruby_files!

  case subcommand
  when :build then Lifer.build!
  when :help then parser.parse!(["--help"])
  when :serve then Lifer::Dev::Server.start!(port: @dev_server_port)
  else
    puts I18n.t(
      "cli.no_subcommand",
      subcommand: Lifer::Utilities.bold_text(subcommand),
      default_command: Lifer::Utilities.bold_text("lifer build")
    )

    parser.parse!(args) && Lifer.build!
  end
end