Class: Troy::Cli

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = [], options = {}, config = {}) ⇒ Cli

Returns a new instance of Cli.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/troy/cli.rb', line 11

def initialize(args = [], options = {}, config = {})
  has_error =
    (config[:current_task] || config[:current_command]).name == "new" &&
    args.empty?

  if has_error
    raise Error, "The site path is required. For details run: troy help new"
  end

  super
end

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/troy/cli.rb', line 7

def self.exit_on_failure?
  true
end

Instance Method Details

#exportObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/troy/cli.rb', line 27

def export
  runner = lambda do
    if options[:assets]
      site.export_assets
      site.export_files
    end

    options[:file]&.each do |file|
      site.export_pages(file)
    end

    site.export if !options[:assets] && !options[:file]
  end

  if options[:benchmark]
    require "benchmark"
    elapsed = Benchmark.realtime(&runner)
    puts "=> Finished in #{elapsed.round(2)}s"
  else
    runner.call
  end
end

#new(path) ⇒ Object



51
52
53
54
55
# File 'lib/troy/cli.rb', line 51

def new(path)
  generator = Generator.new
  generator.destination_root = path
  generator.invoke_all
end

#serverObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/troy/cli.rb', line 66

def server
  begin
    handler = Rackup::Handler.pick(%i[puma thin webrick])
  rescue StandardError
    raise Error,
          "No Rack handler found. Install a Rack handler " \
          "(e.g. puma, thing, webrick)"
  end

  handler.run Troy::Server.new(File.join(Dir.pwd, "public")),
              Port: options[:port],
              Host: options[:host]
end

#versionObject



59
60
61
# File 'lib/troy/cli.rb', line 59

def version
  say "Troy #{Troy::VERSION}"
end