|
From: <luk...@us...> - 2006-09-06 07:26:41
|
Revision: 84
http://svn.sourceforge.net/asunit/?rev=84&view=rev
Author: lukebayes
Date: 2006-09-06 00:26:33 -0700 (Wed, 06 Sep 2006)
Log Message:
-----------
added exception handling to support batch creation with singular failures
Modified Paths:
--------------
trunk/ruby/src/asunit.rb
trunk/ruby/src/create_class.rb
Added Paths:
-----------
trunk/ruby/src/asunit_arguments.rb
Modified: trunk/ruby/src/asunit.rb
===================================================================
--- trunk/ruby/src/asunit.rb 2006-09-06 06:24:57 UTC (rev 83)
+++ trunk/ruby/src/asunit.rb 2006-09-06 07:26:33 UTC (rev 84)
@@ -46,6 +46,7 @@
require 'optparse'
require 'settings'
require 'create_class'
+ require 'asunit_arguments'
class Application
@@PROJECT_FILE_NAME = '.asunit'
@@ -57,19 +58,41 @@
prefs = YAML.load(project_file.read)
settings = AsUnit::Settings.new(prefs)
+ results = Array.new
arguments.classnames.each { |name|
if(name.ends_with? "Test")
- create_class(name, settings, TEST_TEMPLATE)
+ begin
+ create_class(name, settings, TEST_TEMPLATE, arguments.force?)
+ rescue Exception => e
+ results.push(e)
+ end
else
- create_class(name, settings, AsUnit::CLASS_TEMPLATE)
- create_class(name + "Test", settings, AsUnit::TEST_TEMPLATE)
+ begin
+ create_class(name, settings, AsUnit::CLASS_TEMPLATE, arguments.force?)
+ rescue Exception => e
+ results.push(e)
+ end
+ begin
+ create_class(name + "Test", settings, AsUnit::TEST_TEMPLATE, arguments.force?)
+ rescue Exception => e
+ results.push(e)
+ end
end
}
+ if(results.length > 0)
+ puts results.join("\n")
+ end
end
- def create_class(name, settings, template)
- created_class = AsUnit::CreateClass.new(name, settings, template)
- created_class.run
+ def create_class(name, settings, template, force)
+ begin
+ class_creator = AsUnit::CreateClass.new(name, settings, template)
+ class_creator.run(force)
+ puts 'File Created at: ' + class_creator.final_path
+ return nil
+ rescue Exception => e
+ raise e
+ end
end
def get_project_file(dir)
@@ -84,76 +107,6 @@
end
end
- class AsUnitArguments < Hash
-
- def initialize(args)
- super
- self[:classnames] = nil
- self[:display_object] = false
- self[:interfaces] = Array.new
- self[:project_file] = nil
- self[:superclass] = nil
-
- opts = OptionParser.new do |opts|
- opts.banner = "Usage: #$0 [options] CLASSNAME(s)"
-
- opts.on('-d', '--display-object', 'class created is a subclass of flash.display.DisplayObject') do
- self[:display_object] = true
- end
-
- opts.on('-p', '--project-file [FILE]', 'use this project file instead of looking for the nearest one [FILE]') do |file|
- if(file.nil?)
- raise '-p [--project-file] argument must be followed by a relative or absolute file target"'
- end
- self[:project_file] = (file || '$')
- end
-
- opts.on('-i', '--add-interface [STRING]', 'add an interface to this class [STRING]') do |inf|
- if(inf.nil?)
- raise '-i [--add-interface] argument must be followed by a fully-qualified interface name eg: "flash.events.IEventDispatcher"'
- end
- self[:interfaces].push(inf || '$')
- end
-
- opts.on('-s', '--superclass [STRING]', 'superclass of class being created [STRING]') do |superclass|
- if(superclass.nil?)
- raise '-s [--superclass] argument must be followed by a fully-qualified class name eg: "flash.display.DisplayObject"'
- end
- self[:superclass] = (superclass || '$')
- end
-
- opts.on_tail('-h', '--help', 'display this help and exit') do
- puts opts
- exit
- end
-
- if(args.length == 0)
- puts opts
- exit
- end
-
- opts.parse!(args)
- self[:classnames] = args
-
- end
- end
-
- def project_file
- return self[:project_file]
- end
-
- def interfaces
- return self[:interfaces]
- end
-
- def superclass
- return self[:superclass]
- end
-
- def classnames
- return self[:classnames]
- end
- end
end
class String
Added: trunk/ruby/src/asunit_arguments.rb
===================================================================
--- trunk/ruby/src/asunit_arguments.rb (rev 0)
+++ trunk/ruby/src/asunit_arguments.rb 2006-09-06 07:26:33 UTC (rev 84)
@@ -0,0 +1,84 @@
+
+require 'optparse'
+
+module AsUnit
+ class AsUnitArguments < Hash
+
+ def initialize(args)
+ super
+ self[:classnames] = nil
+ self[:display_object] = false
+ self[:force] = false
+ self[:interfaces] = Array.new
+ self[:project_file] = nil
+ self[:superclass] = nil
+
+ opts = OptionParser.new do |opts|
+ opts.banner = "Usage: #$0 [options] CLASSNAME(s)"
+
+ opts.on('-d', '--display-object', 'class created is a subclass of flash.display.DisplayObject') do
+ self[:display_object] = true
+ end
+
+ opts.on('-f', '--force', 'force overwrite even if files exist') do
+ self[:force] = true
+ end
+
+ opts.on('-p', '--project-file [FILE]', 'use this project file instead of looking for the nearest one [FILE]') do |file|
+ if(file.nil?)
+ raise '-p [--project-file] argument must be followed by a relative or absolute file target"'
+ end
+ self[:project_file] = (file || '$')
+ end
+
+ opts.on('-i', '--add-interface [STRING]', 'add an interface to this class [STRING]') do |inf|
+ if(inf.nil?)
+ raise '-i [--add-interface] argument must be followed by a fully-qualified interface name eg: "flash.events.IEventDispatcher"'
+ end
+ self[:interfaces].push(inf || '$')
+ end
+
+ opts.on('-s', '--superclass [STRING]', 'superclass of class being created [STRING]') do |superclass|
+ if(superclass.nil?)
+ raise '-s [--superclass] argument must be followed by a fully-qualified class name eg: "flash.display.DisplayObject"'
+ end
+ self[:superclass] = (superclass || '$')
+ end
+
+ opts.on_tail('-h', '--help', 'display this help and exit') do
+ puts opts
+ exit
+ end
+
+ if(args.length == 0)
+ puts opts
+ exit
+ end
+
+ opts.parse!(args)
+ self[:classnames] = args
+
+ end
+ end
+
+ def project_file
+ return self[:project_file]
+ end
+
+ def interfaces
+ return self[:interfaces]
+ end
+
+ def superclass
+ return self[:superclass]
+ end
+
+ def classnames
+ return self[:classnames]
+ end
+
+ def force?
+ return self[:force]
+ end
+ end
+end
\ No newline at end of file
Modified: trunk/ruby/src/create_class.rb
===================================================================
--- trunk/ruby/src/create_class.rb 2006-09-06 06:24:57 UTC (rev 83)
+++ trunk/ruby/src/create_class.rb 2006-09-06 07:26:33 UTC (rev 84)
@@ -3,39 +3,42 @@
module AsUnit
class CreateClass
- attr_accessor :settings, :template_name
+ attr_accessor :settings, :template_name
+ attr_reader :final_path
def initialize(name, settings, template)
@settings = settings
@template_name = template
@resolver = AsUnit::TemplateResolver.new name
+ @final_path = ''
end
- def run
+ def run(force=false)
src = Dir.pwd + File::SEPARATOR + settings.templates + File::SEPARATOR + template_name
- puts 'opening: ' + src
template = IO.read(src)
@resolver.template = template
parsed = @resolver.parse
- file = create_file(target_file(settings.src))
+ file = create_file(target_file(settings.src), force)
file.write(parsed)
end
- def create_file(relative)
+ def create_file(relative, force)
segments = relative.split(File::SEPARATOR)
file_name = segments.pop
current_path = ''
segments.each { |dir|
current_path << dir << File::SEPARATOR
- if(!File.exists? current_path)
+ if(!File.exists?(current_path))
Dir.mkdir(current_path)
end
}
current_path << file_name
- if(File.exists?(current_path))
- raise 'Requested File Exists at: ' + Dir.pwd + File::SEPARATOR + current_path
+ if(!force && File.exists?(current_path))
+ raise "\nRequested File Exists at: " + Dir.pwd + File::SEPARATOR + current_path + ". \n\nUse -f option to overwrite."
else
file = File.new(current_path, 'w')
+ @final_path = current_path
+ file
end
end
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|