Class: Usmu::Ui::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/usmu/ui/console.rb

Overview

This is the CLI UI controller. This is initialised by the usmu binary to control the generation process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Console

Returns a new instance of Console.

Parameters:

  • args (Array<String>)

    Command line arguments, ie. ARGV.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/usmu/ui/console.rb', line 18

def initialize(args)
  @log = Logging.logger[self]
  initialize_logging args
  @log.debug 'Loading environment variables.'
  ::Dotenv.load
  Usmu.load_lazy_tilt_modules
  @commander = initialize_commander(args)

  Usmu.plugins.load_plugins
  Usmu.plugins.invoke :commands, self, @commander

  @commander.default_command :generate
  @commander.run!
end

Instance Attribute Details

#configurationUsmu::Configuration (readonly)

Do not access this till your command starts running, eg. in Hooks#commands, otherwise you may not get the right value for the configuration as option parsing may not have happened yet.

Returns:



13
14
15
# File 'lib/usmu/ui/console.rb', line 13

def configuration
  @configuration || load_configuration('usmu.yml')
end

Instance Method Details

#initialize_commander(args) ⇒ Commander::Runner (private)

Helper function to setup a Commander runner

Returns:

  • (Commander::Runner)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/usmu/ui/console.rb', line 79

def initialize_commander(args)
  commander = Commander::Runner.new args

  commander.program :version, Usmu::VERSION
  commander.program :description, 'Static site generator powered by Tilt'
  commander.program :int_message, 'Interrupt received, closing...'

  commander.global_option('--config STRING', String, &method(:load_configuration))
  # Logging options are manually processed in #initialize_logging, but included here for user documentations sake
  commander.global_option('-v', '--verbose')
  commander.global_option('-q', '--quiet')
  commander.global_option('--log STRING', String)

  commander
end

#initialize_logging(args) ⇒ void (private)



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/usmu/ui/console.rb', line 53

def initialize_logging(args)
  i = 0
  while i < args.length
    case args[i]
      when '-v', '--verbose'
        args.delete_at i
        Usmu.verbose_logging
      when '-q', '--quiet'
        args.delete_at i
        Usmu.quiet_logging
      when '--log'
        if args.length > i + 1
          args.delete_at i
          path = args.delete_at i
          Usmu.add_file_logger path
        else
          i += 1
        end
      else
        i += 1
    end
  end
end

#load_configuration(config) ⇒ Usmu::Configuration

Load a configuration from a file

Parameters:

  • config (String)

    Name of configuration file to load

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/usmu/ui/console.rb', line 37

def load_configuration(config)
  @log.info("Usmu v#{Usmu::VERSION}")
  @log.info('')

  if File.readable? config
    @configuration = Usmu::Configuration.from_file(config)
    @log.info("Configuration: #{config}")
  else
    @log.fatal("Unable to find configuration file at #{config}")
    raise
  end
  @configuration
end