# file:: $Name$ # author:: $Author$ # version:: $Revision$ # date:: $Date$ # # This source code copyright (C) 2006 by Ralph M. Churchill # All rights reserved. # # Released under the terms of the GNU General Public License # See LICENSE file for additional information. require 'yaml' require 'fileutils' require 'optparse' require 'ostruct' $:.unshift 'lib' unless $:.include?('lib') require 'models/game' require 'models/region' class Scaffold def Scaffold.create_empty_module(root) empty_list = Proc.new {|f| YAML::dump([],f)} empty_file([root,"accounts.yaml"], &empty_list) empty_file([root,"game.yaml"]){|f| YAML::dump(Game.new,f)} mkdir [root,"players"] mkdir [root,"regions","Start"] do |start| empty_file([start,"Start.yaml"]) do |f| r = Region.new r.name="Start" YAML::dump(r,f) end empty_file([start,"characters.yaml"], &empty_list) empty_file([start,"items.yaml"], &empty_list) empty_file([start,"portals.yaml"], &empty_list) empty_file([start,"rooms.yaml"], &empty_list) end mkdir [root,"templates"] do |templates| empty_file([templates,"characters.yaml"], &empty_list) empty_file([templates,"items.yaml"], &empty_list) end mkdir [root,'lib','commands'] mkdir [root,'lib','logic'] end private # won't clobber existing files def self.empty_file(path=[],&blk) file = path.pop dir = FileUtils.mkdir_p path.join(File::SEPARATOR) unless path.empty? new_file = [dir,file].join(File::SEPARATOR) File.open(new_file,'w') do |f| yield f end if FileTest.zero?(new_file) end def self.mkdir(path=[]) dir = FileUtils.mkdir_p path.join(File::SEPARATOR) unless path.empty? yield dir if block_given? end end def parse_args(argv) options = OpenStruct.new opts = OptionParser.new do |opts| opts.banner = "Usage: scaffold.rb [options]" opts.separator "" opts.separator "Specific options:" opts.on("-m","--module MODULE", "Indicate which Module to create") do |module_name| options.module = module_name end opts.separator "" opts.separator "Common options:" opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end begin opts.parse(argv) raise "Error: Module Required." if options.module.nil? rescue => e $stderr.puts e puts opts exit end options end if $0 == __FILE__ options = parse_args(ARGV) Scaffold.create_empty_module(options.module) end