Class: Usmu::Plugin::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/usmu/plugin/core.rb

Overview

Usmu Core's plugin. We dog-food several plugin integration points.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCore

Basic constructor.



10
11
12
# File 'lib/usmu/plugin/core.rb', line 10

def initialize
  @log = Logging.logger[self]
end

Instance Attribute Details

#uivoid (private)

Returns the value of attribute ui.



95
96
97
# File 'lib/usmu/plugin/core.rb', line 95

def ui
  @ui
end

Instance Method Details

#command_generate(args, options) ⇒ void

Command to generate a website.

Parameters:

  • args (Array<String>)

    arguments passed by the user.

  • options (Hash)

    options parsed by Commander



45
46
47
48
49
50
# File 'lib/usmu/plugin/core.rb', line 45

def command_generate(args, options)
  raise 'This command does not take arguments' unless args.empty?
  raise 'Invalid options' unless options.inspect.start_with? '<Commander::Command::Options '

  @ui.configuration.generator.generate
end

#command_init(args, options) ⇒ void

Command to initialise a new website.

Parameters:

  • args (Array<String>)

    arguments passed by the user.

  • options (Hash)

    options parsed by Commander



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/usmu/plugin/core.rb', line 56

def command_init(args, options)
  @log.info("Usmu v#{Usmu::VERSION}")
  @log.info('')

  raise 'Invalid options' unless options.inspect.start_with? '<Commander::Command::Options '
  if args.length > 1
    @log.fatal('Only one path allowed to be initialised at a time.')
    raise
  end

  path = args.length == 1 ? args.shift : '.'
  from = File.realpath(File.join(File.dirname(__FILE__), '../../../share/init-site'))

  @log.info("Copying #{from} -> #{path}")
  Dir["#{from}/**/{*,.??*}"].each {|f| init_copy_file(from, path, f) }
end

#command_serve(args, options) ⇒ void



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/usmu/plugin/core.rb', line 73

def command_serve(args, options)
  raise 'This command does not take arguments' unless args.empty?
  raise 'Invalid options' unless options.inspect.start_with? '<Commander::Command::Options '

  configuration = @ui.configuration
  @log.info('Starting webserver...')

  server = Ui::RackServer.new(configuration)
  trap :INT do
    Rack::Handler::WEBrick.shutdown
    server.shutdown
  end
  host = configuration['serve', 'host', default: 'localhost']
  port = (ENV['PORT'] || configuration['serve', 'port', default: 8080]).to_i
  Rack::Handler::WEBrick.run server,
                             Host: host,
                             Port: port
  @log.info('Stopped webserver.')
end

#commands(ui, c) ⇒ void

We add two commands: generate and init [path].



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/usmu/plugin/core.rb', line 19

def commands(ui, c)
  @log.debug('Adding core console commands...')
  @ui = ui
  c.command(:generate) do |command|
    command.syntax = 'usmu generate'
    command.description = 'Generates your website using the configuration specified.'
    command.action &method(:command_generate)
  end

  c.command(:init) do |command|
    command.syntax = 'usmu init [path]'
    command.description = 'Initialise a new website in the given path, or the current directory if none given.'
    command.action &method(:command_init)
  end

  c.command(:serve) do |command|
    command.syntax = 'usmu serve'
    command.description = 'Serve files processed files directly. This won\'t update files on disk, but you will be able to view your website as rendered.'
    command.action &method(:command_serve)
  end
end

#init_copy_file(from, to, file) ⇒ void (private)

Helper to copy a file.

Parameters:

  • from (String)
  • to (String)
  • file (String)


102
103
104
105
106
107
108
109
110
# File 'lib/usmu/plugin/core.rb', line 102

def init_copy_file(from, to, file)
  output_name = file[(from.length + 1)..file.length]
  @log.success "Creating #{output_name}..."
  unless File.directory? file
    output_path = "#{to}/#{output_name}"
    FileUtils.mkdir_p File.dirname(output_path) unless File.directory? File.dirname(output_path)
    FileUtils.copy(file, output_path)
  end
end