class Webby::Apps::Main

Attributes

cmd_line_options[R]

Public Instance Methods

app() click to toggle source

Return the Rake application object.

# File lib/webby/apps/main.rb, line 151
def app
  Rake.application
end
capture_command_line_args(args) click to toggle source
# File lib/webby/apps/main.rb, line 188
def capture_command_line_args(args)
  args = OpenStruct.new(
    :raw  => args,
    :rake => ARGV.dup
  )

  if args.raw.size > 1
    ::Webby.deprecated "multiple arguments used for page title",
                       "please quote the page title"
  end

  dashed = args.raw.join('-').downcase
  spaced = args.raw.join(' ')
  dir = ::File.dirname(dashed)

  args.dir   = ('.' == dir ? '' : dir)
  args.slug  = ::Webby::Resources.basename(dashed).to_url
  args.title = ::Webby::Resources.basename(spaced).titlecase

  # page should be dir/slug without leading /
  args.page  = ::File.join(args.dir, args.slug).gsub(/^\//, '')

  ext = ::File.extname(dashed)
  args.page << ext unless ext.empty?

  ::Webby.site.args = args
  Object.const_set(:SITE, Webby.site)
  args
end
find_sitefile() click to toggle source

Search for the “Sitefile” starting in the current directory and working upwards through the filesystem until the root of the filesystem is reached. If a “Sitefile” is not found, a RuntimeError is raised.

# File lib/webby/apps/main.rb, line 165
def find_sitefile
  here = Dir.pwd
  while ! app.have_rakefile
    Dir.chdir("..")
    if Dir.pwd == here || options.nosearch
      fail "No Sitefile found"
    end
    here = Dir.pwd
  end
end
import_default_tasks() click to toggle source
# File lib/webby/apps/main.rb, line 176
def import_default_tasks
  Dir.glob(::Webby.libpath(%w[webby tasks *.rake])).sort.each {|fn| import fn}
end
import_website_tasks() click to toggle source
# File lib/webby/apps/main.rb, line 180
def import_website_tasks
  Dir.glob(::File.join(%w[tasks *.rake])).sort.each {|fn| import fn}
end
init( args ) click to toggle source

Initialize the Rake application object and load the core rake tasks, the site specific rake tasks, and the site specific ruby code. Any extra command line arguments are converted into a page name and directory that might get created (depending upon the task invoked).

# File lib/webby/apps/main.rb, line 96
def init( args )
  # Make sure we're in a folder with a Sitefile
  options = app.standard_rake_options
  [['--rakefile', 'Sitefile'],
   ['--no-search', nil],
   ['--silent', nil]].each {|opt, value| options.assoc(opt).last.call(value)}

  unless app.have_rakefile
    raise RuntimeError, "Sitefile not found"
  end

  import_default_tasks
  import_website_tasks
  require_lib_files
  capture_command_line_args(args)
  args
end
load_command_line_options() click to toggle source

Load options from the command line into the Webby.site struct

# File lib/webby/apps/main.rb, line 220
def load_command_line_options
  cmd_line_options.each do |key, value|
    ::Webby.site.__send__("#{key}=", value)
  end
end
options() click to toggle source

Returns the options hash from the Rake application object.

# File lib/webby/apps/main.rb, line 157
def options
  app.options
end
parse( args ) click to toggle source

Parse the command line args for options and commands to invoke.

# File lib/webby/apps/main.rb, line 39
def parse( args )
  opts = OptionParser.new
  opts.banner = 'Usage: webby [options] target [target args]'

  opts.separator ''

  desired_opts = %Q[--describe --prereqs --tasks --trace]
  app.standard_rake_options.each do |options|
    next unless desired_opts.include?(options.first)
    opts.on(*options)
  end
  opts.on('-o', '--options [PATTERN]',
          'Show configuration options (matching optional pattern), then exit.') { |value|
    @command = [:show_options, value]
  }

  opts.separator ''
  opts.separator 'autobuild options:'

  opts.on('--web-server', 'Start a local web server') {
    cmd_line_options[:use_web_server] = true
  }
  opts.on('--no-web-server', 'Do not start a local web server') {
    cmd_line_options[:use_web_server] = false
  }

  opts.separator ''
  opts.separator 'common options:'

  opts.on_tail( '-h', '--help', 'show this message' ) do
    @stdout.puts opts
    exit
  end
  opts.on_tail( '--version', 'show version' ) do
    @stdout.puts "Webby #{::Webby::VERSION}"
    exit
  end

  opts.parse! args

  ARGV.replace Array(args.shift)
  args.delete_if do |arg|
    if /^[A-Z_]+=/ =~ arg
      ARGV << arg
      next true
    end
    false
  end

  args
end
rake() click to toggle source

Execute the rake command.

# File lib/webby/apps/main.rb, line 116
def rake
  app.init 'webby'
  app.load_rakefile
  load_command_line_options
  app.top_level
end
require_lib_files() click to toggle source
# File lib/webby/apps/main.rb, line 184
def require_lib_files
  Dir.glob(::File.join(%w[lib ** *.rb])).sort.each {|fn| require fn}
end
run( args ) click to toggle source

Runs the main webby application. The command line arguments are passed in to this method as an array of strings. The command line arguments are parsed to figure out which rake task to invoke.

# File lib/webby/apps/main.rb, line 29
def run( args )
  args = args.dup

  parse args
  init args
  self.__send__(*@command)
end
show_options( attribute = nil ) click to toggle source

Print the available configuration options.

# File lib/webby/apps/main.rb, line 125
def show_options( attribute = nil )
  app.init 'webby'
  app.load_rakefile

  desc = <<-__
    The following options can be used to control Webby functionality.
    Options are configured in the 'Sitefile'. A few examples are shown below:
    |
    |   SITE.create_mode = 'directory'
    |   SITE.base        = 'http://www.example.com'
    |   SITE.uv.theme    = 'twilight'
    |
    =======< OPTIONS >=======
    |
  __
  
  @stdout.puts desc.gutter!
  help = Loquacious.help_for(
    :webby, :io => @stdout, :colorize => ENV.key?('TERM')
  )
  help.show attribute, :values => true
  @stdout.puts
end

Public Class Methods

new() click to toggle source

Create a new Main webby object for building websites.

# File lib/webby/apps/main.rb, line 19
def initialize
  @stdout = $stdout
  @cmd_line_options = {}
  @command = %w[rake]
end
run( args ) click to toggle source

Create a new instance of Main, and run the webby application given the command line args.

# File lib/webby/apps/main.rb, line 11
def self.run( args )
  self.new.run args
end