Class: Webby::Apps::Generator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/webby/apps/generator.rb

Overview

webby gen template site => creates the tmplate webby gen -h / –help webby gen => same as –help

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout, input = $stdin) ⇒ Generator

Initialize a new generator object.



27
28
29
30
31
32
# File 'lib/webby/apps/generator.rb', line 27

def initialize( output = $stdout, input = $stdin )
  @options = {}
  @site = @template = nil
  @output, @input = output, input
  @journal = journal
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/webby/apps/generator.rb', line 23

def options
  @options
end

#siteObject

Returns the value of attribute site.



22
23
24
# File 'lib/webby/apps/generator.rb', line 22

def site
  @site
end

#templateObject

Returns the value of attribute template.



22
23
24
# File 'lib/webby/apps/generator.rb', line 22

def template
  @template
end

Class Method Details

.run(args) ⇒ Object

Create a new Generator instance and run the webby application given the command line args.



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

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

Instance Method Details

#abort(msg) ⇒ Object

Prints an abort message to the screen and then exits the Ruby interpreter. A non-zero return code is used to indicate an error.



219
220
221
222
223
224
# File 'lib/webby/apps/generator.rb', line 219

def abort( msg )
  puts "\nAborting!"
  puts "    #{msg}"
  puts
  exit 1
end

#cp(file) ⇒ Object

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/webby/apps/generator.rb', line 189

def cp( file )
  src = template / file
  dst = site / file

  if test(?e, dst) 
    if identical?(src, dst)
      identical(dst)
      return
    end

    choice = case options[:collision]
      when :force then :force
      when :skip  then :skip
      else force_file_collision?( dst ) end

    case choice
      when :force then force(dst)
      when :skip  then skip(dst); return
      else raise "Invalid collision choice: #{choice.inspect}" end
  else
    create(dst)
  end
  return if pretend?

  FileUtils.cp(src, dst)
end

#create_siteObject

Create the site from the template specified by the user.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/webby/apps/generator.rb', line 145

def create_site
  files = site_files

  # in update mode we only want to update the tasks directory
  if options[:update]
    FileUtils.mkdir_p site unless pretend?
    mkdir 'tasks'
    files['tasks'].sort.each {|file| cp file}
  else
    dirs = files.keys.concat %w[content layouts lib tasks templates]
    dirs.sort!
    dirs.uniq!

    # create the directories first
    dirs.each do |dir|
      next if dir =~ %r/^output\/.*$/
      mkdir dir
    end

    # and the create the files under each directory
    dirs.each do |dir|
      next if dir =~ %r/^output(\/.*)?$/
      files[dir].sort.each {|file| cp file}
    end
  end
end

#force_file_collision?(dst) ⇒ Boolean

Ask the user what to do about the file collision.

Returns:

  • (Boolean)


260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/webby/apps/generator.rb', line 260

def force_file_collision?( dst )
  dst = dst.sub(%r/#{site}\/?/, '')
  print "overwrite #{dst}? [(Y)es (n)o (q)uit] "
  case gets
    when %r/q/i  then abort 'user asked to quit'
    when %r/n/i  then :skip
    when %r/y/i  then :force
    when %r/\s*/ then :force
    else force_file_collision?(dst) end
rescue
  retry
end

#getsObject

Reads a line text frim the input source.



53
54
55
# File 'lib/webby/apps/generator.rb', line 53

def gets
  @input.gets
end

#identical?(src, dst) ⇒ Boolean

Returns true if the source file is identical to the destination file. Returns false if this is not the case.

Returns:

  • (Boolean)


252
253
254
255
256
# File 'lib/webby/apps/generator.rb', line 252

def identical?( src, dst )
  source      = IO.read(src)
  destination = IO.read(dst)
  source == destination
end

#mkdir(dir) ⇒ Object

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



175
176
177
178
179
180
181
182
183
# File 'lib/webby/apps/generator.rb', line 175

def mkdir( dir )
  dir = dir.empty? ? site : site / dir
  if test ?d, dir
    exists dir
  else
    create dir
    FileUtils.mkdir_p dir unless pretend?
  end
end

#parse(args) ⇒ Object

Parse out the command line options found in the args array.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/webby/apps/generator.rb', line 67

def parse( args )
  opts = OptionParser.new
  opts.banner = 'Usage: webby-gen [options] template site'

  opts.separator ''
  opts.separator 'The webby-gen command is used to generate a site from a standard template.'
  opts.separator 'A new site can be created, or an existing site can be added to or updated.'

  opts.separator ''
  opts.on('-f', '--force',
          'overwrite files that already exist') {options[:collision] = :force}
  opts.on('-s', '--skip',
          'skip files that already exist') {options[:collision] = :skip}
  opts.on('-u', '--update',
          'update rake tasks for the site') {options[:update] = true}
  opts.on('-p', '--pretend',
          'run but do not make any changes') {options[:pretend] = true}

  opts.separator ''
  opts.on('-t', '--templates', 'list available templates') {
    ary = templates.map {|t| ::File.basename(t)}
    ary.delete 'webby'
    puts "\nAvailable Templates"
    puts "    #{ary.join(', ')}"
    puts
    exit
  }

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

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

  # parse the command line arguments
  opts.parse! args
  tmpl, @site = args

  # if no site was given, see if there is a Sitefile in the current
  # directory
  if site.nil?
    self.site = '.' if test(?f, 'Sitefile')
  end

  # exit if comand line args are missing
  if site.nil? or tmpl.nil?
    puts opts
    exit 1
  end

  templates.each {|t| self.template = t if t =~ %r/\/#{tmpl}$/}
  if template.nil?
    puts opts
    abort "Could not find template '#{tmpl}'"
  end

  nil
end

#pretend?Boolean

Returns true if we are only going to pretend to do something. All the output messages will be written, but no changes will be made on the disc.

Returns:

  • (Boolean)


133
134
135
# File 'lib/webby/apps/generator.rb', line 133

def pretend?
  options[:pretend] == true
end

Writes the given objects to the output destination.



47
48
49
# File 'lib/webby/apps/generator.rb', line 47

def print( *args )
  @output.print(*args)
end

#puts(*args) ⇒ Object

Writes the given objects to the output destination. Each object is followed by a newline unless the object is a string with a newline already at the end.



41
42
43
# File 'lib/webby/apps/generator.rb', line 41

def puts( *args )
  @output.puts(*args)
end

#run(args) ⇒ Object

Run the generator executing the commands specified by the user on the command line.



60
61
62
63
# File 'lib/webby/apps/generator.rb', line 60

def run( args )
  parse args
  create_site
end

#site_filesObject

Iterates over all the files in the template directory and stores them in a hash.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/webby/apps/generator.rb', line 229

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

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

  Find.find(template) 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

#templatesObject

Returns an array of available site templates.



139
140
141
# File 'lib/webby/apps/generator.rb', line 139

def templates
  Dir.glob(::Webby.path('examples') / '*').sort
end