Class: Webby::Apps::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/webby/apps/main.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

Create a new Main webby object for building websites.



19
20
21
22
# File 'lib/webby/apps/main.rb', line 19

def initialize
  @stdout = $stdout
  @cmd_line_options = {}
end

Instance Attribute Details

#cmd_line_optionsObject (readonly)

Returns the value of attribute cmd_line_options.



15
16
17
# File 'lib/webby/apps/main.rb', line 15

def cmd_line_options
  @cmd_line_options
end

Class Method Details

.run(args) ⇒ Object

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



11
12
13
# File 'lib/webby/apps/main.rb', line 11

def self.run( args )
  self.new.run args
end

Instance Method Details

#appObject

Return the Rake application object.



120
121
122
# File 'lib/webby/apps/main.rb', line 120

def app
  Rake.application
end

#capture_command_line_args(args) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/webby/apps/main.rb', line 157

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_sitefileObject

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.



134
135
136
137
138
139
140
141
142
143
# File 'lib/webby/apps/main.rb', line 134

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_tasksObject



145
146
147
# File 'lib/webby/apps/main.rb', line 145

def import_default_tasks
  Dir.glob(::Webby.libpath(%w[webby tasks *.rake])).sort.each {|fn| import fn}
end

#import_website_tasksObject



149
150
151
# File 'lib/webby/apps/main.rb', line 149

def import_website_tasks
  Dir.glob(::File.join(%w[tasks *.rake])).sort.each {|fn| import fn}
end

#init(args) ⇒ Object

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).



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/webby/apps/main.rb', line 91

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_optionsObject

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



189
190
191
192
193
# File 'lib/webby/apps/main.rb', line 189

def load_command_line_options
  cmd_line_options.each do |key, value|
    ::Webby.site.__send__("#{key}=", value)
  end
end

#optionsObject

Returns the options hash from the Rake application object.



126
127
128
# File 'lib/webby/apps/main.rb', line 126

def options
  app.options
end

#parse(args) ⇒ Object

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



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/webby/apps/main.rb', line 38

def parse( args )
  opts = OptionParser.new
  opts.banner = 'Usage: webby [options] target [target args]'

  opts.separator ''

  desired_opts = %[--describe --prereqs --tasks --trace]
  app.standard_rake_options.each do |options|
    next unless desired_opts.include?(options.first)
    opts.on(*options)
  end

  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 %r/^[A-Z_]+=/ =~ arg
      ARGV << arg
      next true
    end
    false
  end

  args
end

#rakeObject

Execute the rake command.



111
112
113
114
115
116
# File 'lib/webby/apps/main.rb', line 111

def rake
  app.init 'webby'
  app.load_rakefile
  load_command_line_options
  app.top_level
end

#require_lib_filesObject



153
154
155
# File 'lib/webby/apps/main.rb', line 153

def require_lib_files
  Dir.glob(::File.join(%w[lib ** *.rb])).sort.each {|fn| require fn}
end

#run(args) ⇒ Object

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.



28
29
30
31
32
33
34
# File 'lib/webby/apps/main.rb', line 28

def run( args )
  args = args.dup

  parse args
  init args
  rake
end