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

Constructor Details

#initializeMain

call-seq:

Main.new

Create a new Main webby object for building websites.



42
43
44
# File 'lib/webby/main.rb', line 42

def initialize
  @log = Logging::Logger[self]
end

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.



171
172
173
174
# File 'lib/webby/main.rb', line 171

def abort( msg )
  @log.fatal msg
  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.



141
142
143
144
145
146
# File 'lib/webby/main.rb', line 141

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.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/webby/main.rb', line 88

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.



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

def creating( msg )
  @log.info "creating #{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.



126
127
128
129
130
131
132
# File 'lib/webby/main.rb', line 126

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.



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

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.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/webby/main.rb', line 182

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.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/webby/main.rb', line 107

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.



162
163
164
# File 'lib/webby/main.rb', line 162

def updating( msg )
  @log.info "updating #{msg}"
end