Class: Nanoc::CLI::Commands::CreateLayout

Inherits:
Nanoc::CLI::CommandRunner show all
Defined in:
lib/nanoc/cli/commands/create-layout.rb

Instance Method Summary (collapse)

Methods inherited from Nanoc::CLI::CommandRunner

#call, call, #debug?, #in_site_dir?, #load_site, #require_site, #site

Instance Method Details

- (Object) run



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
61
62
63
64
# File 'lib/nanoc/cli/commands/create-layout.rb', line 13

def run
  # Check arguments
  if arguments.length != 1
    raise Nanoc::Errors::GenericTrivial, "usage: #{command.usage}"
  end

  # Extract arguments
  identifier = arguments[0].cleaned_identifier

  # Make sure we are in a nanoc site directory
  require_site

  # Set VCS if possible
  set_vcs(options[:vcs])

  # Check whether layout is unique
  unless site.layouts.find { |l| l.identifier == identifier }.nil?
    raise Nanoc::Errors::GenericTrivial,
      "A layout already exists at #{identifier}. Please " \
      'pick a unique name for the layout you are creating.'
  end

  # Check whether layout is not at /
  if identifier == '/'
    raise Nanoc::Errors::GenericTrivial,
      "There cannot be a layout with the identifier '/'; " \
      'please pick a different identifier for this layout.'
  end

  # Setup notifications
  Nanoc::NotificationCenter.on(:file_created) do |file_path|
    Nanoc::CLI::Logger.instance.file(:high, :create, file_path)
  end

  # Create layout
  data_source = site.data_sources[0]
  data_source.create_layout(
    "<html>\n" \
    "  <head>\n" \
    "    <title><%= @item[:title] %></title>\n" \
    "  </head>\n" \
    "  <body>\n" \
    "    <p>Hi, I'm a new layout. Please customize me!</p>\n" \
    "<%= yield %>\n" \
    "  </body>\n" \
    "</html>\n",
    {},
    identifier
  )

  puts "A layout has been created at #{identifier}."
end