lib/bones/main.rb in bones-1.3.3 vs lib/bones/main.rb in bones-1.3.4

- old
+ new

@@ -1,16 +1,16 @@ -# $Id: main.rb 536 2008-02-18 23:07:49Z tim_pease $ +# $Id: main.rb 543 2008-02-20 04:36:46Z tim_pease $ require 'fileutils' require 'optparse' require 'erb' module Bones class Main attr_writer :update - attr_accessor :name, :data, :verbose + attr_accessor :name, :data, :verbose, :output_dir # Create a new instance of Main, and run the +bones+ application given # the command line _args_. # def self.run( args ) @@ -36,10 +36,12 @@ opts.separator '' opts.on('-u', '--update', 'update the rake tasks for the project') {self.update = true} opts.on('-v', '--verbose', 'enable verbose output') {self.verbose = true} + opts.on('-d', '--directory DIRECTORY', String, + 'project directory to create') {|dir| self.output_dir = dir} opts.separator '' opts.on('--freeze', 'freeze the project skeleton') {freeze; exit} opts.on('--unfreeze', 'use the standard project skeleton') {unfreeze; exit} opts.on('-i', '--info', @@ -65,10 +67,12 @@ if name.nil? puts opts ::Kernel.abort end + + self.output_dir = name if output_dir.nil? nil end # Returns +true+ if we are updating an already existing project. Returns # +false+ if we are creating a new project. @@ -86,38 +90,43 @@ # Create a new project from the bones/data project template. # def create # see if the directory already exists - abort "'#{name}' already exists" if test ?e, name + abort "'#{output_dir}' already exists" if test ?e, output_dir begin files_to_copy.each {|fn| cp fn} pwd = File.expand_path(FileUtils.pwd) begin - FileUtils.cd name + FileUtils.cd output_dir system "rake manifest:create 2>&1 > #{::Bones::DEV_NULL}" - STDOUT.puts "created '#{name}'\nnow you need to fix these files" + msg = "created '#{name}'" + msg << " in directory '#{output_dir}'" if name != output_dir + msg << "\nnow you need to fix these files" + STDOUT.puts msg system "rake notes" ensure FileUtils.cd pwd end rescue Exception => err - FileUtils.rm_rf name - abort "could not create '#{name}'" + FileUtils.rm_rf output_dir + msg = "could not create '#{name}'" + msg << " in directory '#{output_dir}'" if name != output_dir + abort msg end end # Archive any existing tasks in the project's tasks folder, and then # copy in new tasks from the bones/data directory. # def update - abort "'#{name}' does not exist" unless test ?e, name + abort "'#{output_dir}' does not exist" unless test ?e, output_dir - task_dir = File.join(name, 'tasks') - abort "no tasks directory found in '#{name}'" unless test ?d, task_dir + task_dir = File.join(output_dir, 'tasks') + abort "no tasks directory found in '#{output_dir}'" unless test ?d, task_dir archive_dir = File.join(task_dir, 'archive') FileUtils.rm_rf archive_dir FileUtils.mkdir archive_dir @@ -129,11 +138,13 @@ files_to_copy.each do |fn| next unless %r/^tasks\// =~ fn cp fn end - STDOUT.puts "updated tasks for '#{name}'" + msg = "updated tasks for '#{name}'" + msg << " in directory '#{output_dir}'" if name != output_dir + STDOUT.puts msg end # Freeze the project skeleton to the current Mr Bones gem. If the project # skeleton has already been frozen, then it will be archived before being # overwritten by the current Mr Bones skeleton. @@ -209,10 +220,10 @@ # specified project location. A message will be displayed to the screen # indicating that the file is being created. # def cp( file ) dir = File.dirname(file).sub('NAME', name) - dir = (dir == '.' ? name : File.join(name, dir)) + dir = (dir == '.' ? output_dir : File.join(output_dir, dir)) dst = File.join(dir, File.basename(file, '.erb').sub('NAME', name)) src = File.join(data, file) puts (test(?e, dst) ? "updating #{dst}" : "creating #{dst}") if verbose FileUtils.mkdir_p(dir)