Class: Autumn::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/autumn/generator.rb

Overview

Generates the files for Autumn templates such as leaves and seasons. The contents of these template files are populated by an LeafCoder instance.

Constant Summary collapse

SEASON_FILES =

The names of the required files in a season’s directory, and example for each file.

{
  "leaves.yml" => {
    'Scorekeeper' => {
      'class' => 'Scorekeeper'
    },
    'Insulter' => {
      'class' => 'Insulter'
    },
    'Administrator' => {
      'class' => 'Administrator',
      'authentication' => {
        'type' => 'op'
      }
    }
  },
  "stems.yml" => {
    'Example' => {
      'server' => 'irc.yourircserver.com',
      'nick' => 'MyIRCBot',
      'channel' => '#yourchannel',
      'rejoin' => true,
      'leaves' => [ 'Administrator', 'Scorekeeper', 'Insulter' ]
    }
  },
  "season.yml" => {
    'logging' => 'debug'
  },
  'database.yml' => {
    'Example' => {
      'adapter' => 'mysql',
      'host' => 'localhost',
      'username' => 'root',
      'password' => '',
      'database' => 'example_database'
    }
  }
}

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Creates a new instance.



55
56
57
# File 'lib/autumn/generator.rb', line 55

def initialize
  @coder = Autumn::TemplateCoder.new
end

Instance Method Details

#leaf(name, options = {}) ⇒ Object

Generates the files for a new leaf with the given name. Options:

verbose

Print to standard output every action that is taken.

vcs

The version control system used by this project. The files and directories created by this method will be added to the project’s VCS.



66
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
# File 'lib/autumn/generator.rb', line 66

def leaf(name, options={})
  lpath = "leaves/#{name.snakecase}"
  if File.directory? lpath then
    exists lpath, options
    return
  elsif File.exist? lpath then
    raise "There is a file named #{lpath} in the way."
  else
    Dir.mkdir lpath
    created lpath, options
  end
  
  cname = "leaves/#{name.snakecase}/controller.rb"
  if File.exist? cname then
    exists cname, options
  else
    @coder.leaf(name)
    File.open(cname, 'w') { |file| file.puts @coder.output }
    created cname, options
  end
  
  dpath = "leaves/#{name.snakecase}/data"
  if File.directory? dpath then
    exists dpath, options
  else
    Dir.mkdir dpath
    created dpath, options
  end
  
  [ 'helpers', 'models', 'tasks', 'views' ].each do |dir|
    path = "leaves/#{name.snakecase}/#{dir}"
    if File.directory? path then
      exists path, options
    elsif File.exist? path then
      raise "There is a file named #{path} in the way."
    else
      Dir.mkdir path
      created path, options
    end
  end
  
  vname = "leaves/#{name.snakecase}/views/about.txt.erb"
  if File.exist? vname then
    exists cname, options
  else
    File.open(vname, 'w') { |file| file.puts "Insert your about string here!" }
    created vname, options
  end
  
  rname = "leaves/#{name.snakecase}/README"
  if File.exist? rname then
    exists rname, options
  else
    File.open(rname, 'w') { |file| file.puts "This is the read-me for your leaf." }
    created rname, options
  end
end

#season(name, options = {}) ⇒ Object

Generates the files and directories for a new season with the given name. Options:

verbose

Print to standard output every action that is taken.

vcs

The version control system used by this project. The files and directories created by this method will be added to the project’s VCS.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/autumn/generator.rb', line 161

def season(name, options={})
  dname = "config/seasons/#{name.snakecase}"
  if File.directory? dname then
    raise "The directory #{dname} already exists."
  elsif File.exist? dname then
    raise "There is a file named #{dname} in the way."
  else
    Dir.mkdir dname
    created dname, options
    SEASON_FILES.each do |fname, content|
      fpath = File.join(dname, fname)
      if File.exist? fpath then
        exists fpath, options
      else
        File.open(fpath, 'w') { |file| file.puts content.to_yaml }
        created fpath, options
      end
    end
  end
end

#unleaf(name, options = {}) ⇒ Object

Removes the files for a new leaf with the given name. Options:

verbose

Print to standard output every action that is taken.

vcs

The version control system used by this project. The files and directories removed by this method will be removed from the project’s VCS.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/autumn/generator.rb', line 131

def unleaf(name, options={})
  lpath = "leaves/#{name.snakecase}"
  if not File.directory? lpath then
    raise "The directory #{lpath} doesn't exist."
  end
  
  if File.directory? "#{lpath}/data" and Dir.entries("#{lpath}/data").size > 2 then
    print "\a" # ring the bell
    puts "WARNING: Files exist in this leaf's data directory!"
    puts "Type Ctrl-C in the next ten seconds if you don't want these files to be deleted..."
    (0..9).each do |num|
      print "#{10 - num}... "
      $stdout.flush
      sleep 1
    end
    print "\n"
  end
  
  FileUtils.remove_dir lpath
  deleted lpath, options
end

#unseason(name, options = {}) ⇒ Object

Removes the files and directories for a season with the given name. Options:

verbose

Print to standard output every action that is taken.

vcs

The version control system used by this project. The files and directories removed by this method will be removed from the project’s VCS.



190
191
192
193
194
195
196
197
198
# File 'lib/autumn/generator.rb', line 190

def unseason(name, options={})
  dname = "config/seasons/#{name.snakecase}"
  if not File.directory? dname then
    raise "The directory #{dname} doesn't exist."
  end
  
  FileUtils.remove_dir dname
  deleted dname, options
end