Class: Webby::Main

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

Overview

The Webby::Main class contains all the functionality needed by the webby command line application.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Directory where the prototype Webby website can be found



18
19
20
# File 'lib/webby/main.rb', line 18

def data
  @data
end

#siteObject

Directory where the Webby website will be created



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

def site
  @site
end

#updateObject

Flag used to update an existing website



21
22
23
# File 'lib/webby/main.rb', line 21

def update
  @update
end

Class Method Details

.run(args) ⇒ Object

call-seq:

Main.run( args )    => nil

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



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

def self.run( args )
  m = self.new
  m.parse args

  if m.update then m.update_site
              else m.create_site end
end

Instance Method Details

#abort(msg) ⇒ Object

call-seq:

abort( msg )   => nil

Prints an abort msg to the screen and then exits the Ruby interpreter.



164
165
166
167
168
# File 'lib/webby/main.rb', line 164

def abort( msg )
  puts msg
  puts "Aborting!"
  exit 1
end

#cp(file) ⇒ Object

call-seq:

cp( file )    => nil

Copy a file from the Webby prototype website location to the user specified site location. A message will be displayed to the screen indicating tha the file is being created.



132
133
134
135
136
137
# File 'lib/webby/main.rb', line 132

def cp( file )
  src = ::File.join(data, file)
  dst = ::File.join(site, file)
  test(?e, dst) ? updating(dst) : creating(dst)
  FileUtils.cp src, dst
end

#create_siteObject

call-seq:

create_site    => nil

Create a new website.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/webby/main.rb', line 79

def create_site
  # see if the site already exists
  abort "'#{site}' already exists" if test ?e, site

  # copy over files from the data directory
  files = site_files

  files.keys.sort.each do |dir|
    mkdir dir
    files[dir].sort.each {|file| cp file}
  end
  nil
end

#creating(msg) ⇒ Object

call-seq:

creating( msg )   => nil

Prints a “creating msg” to the screen.



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

def creating( msg )
  print "creating "
  puts msg
end

#mkdir(dir) ⇒ Object

call-seq:

mkdir( dir )    => nil

Make a directory in the user specified site location. A message will be displayed to the screen indicating tha the directory is being created.



117
118
119
120
121
122
123
# File 'lib/webby/main.rb', line 117

def mkdir( dir )
  dir = dir.empty? ? site : ::File.join(site, dir)
  unless test ?d, dir
    creating dir
    FileUtils.mkdir_p dir
  end
end

#parse(args) ⇒ Object

call-seq:

parse( args )   => nil

Parse the command line arguments and store the values for later use by the create_site and update_site methods.



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
# File 'lib/webby/main.rb', line 43

def parse( args )
  self.data = File.join(::Webby::PATH, 'data')
  self.update = false

  opts = OptionParser.new
  opts.banner << ' site'

  opts.separator ''
  opts.on('-u', '--update',
          'update the rake tasks for the site') {self.update = true}

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

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

  # parse the command line arguments
  opts.parse! args
  self.site = args.shift

  if site.nil?
    puts opts
    ::Kernel.abort
  end
  nil
end

#site_filesObject

call-seq:

site_files   => hash

Iterates over all the files in the Webby prototype website directory and stores them in a hash.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/webby/main.rb', line 176

def site_files
  exclude = %r/tmp$|bak$|~$|CVS|\.svn/o

  rgxp = %r/\A#{data}\/?/o
  paths = Hash.new {|h,k| h[k] = []}

  Find.find(data) do |p|
    next if exclude =~ p

    if test(?d, p)
      paths[p.sub(rgxp, '')]
      next
    end
    dir = ::File.dirname(p).sub(rgxp, '')
    paths[dir] << p.sub(rgxp, '')
  end

  paths
end

#update_siteObject

call-seq:

update_site    => nil

Update the rake tasks for an existing website.



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/webby/main.rb', line 98

def update_site
  # ensure the site already exists
  abort "'#{site}' does not exist" unless test ?d, site

  # copy over files from the data/tasks directory
  files = site_files

  mkdir 'tasks'
  files['tasks'].sort.each {|file| cp file}

  nil
end

#updating(msg) ⇒ Object

call-seq:

updating( msg )   => nil

Prints a “updating msg” to the screen.



154
155
156
157
# File 'lib/webby/main.rb', line 154

def updating( msg )
  print "updating "
  puts msg
end