$:.push(File.dirname(__FILE__)) require 'asclass_arguments' require 'test_suite_generator' module AsProject class AsClass < AsProjectBase @@TEMPLATE_TYPE = 'asclass' @@CLASS_TEMPLATE = 'Class.as' @@TEST_CASE_TEMPLATE = 'TestCase.as' @@TEST_SUITE_TEMPLATE = 'TestSuite.as' @@INTERFACE_TEMPLATE = 'Interface.as' @@COMPONENT_TEMPLATE = 'Component.as' @@MXML_TEMPLATE = 'Component.mxml' def initialize(args=nil) # begin @created_files = [] @arguments = parse_args(args) @project = get_project(@arguments.execution_dir) if(!@arguments.name.nil?) configure_project(@arguments.name) end if(@arguments.force) @project.replace_all = true end execute finish # rescue Exception => e # puts e.to_s # exit # end end def parse_args(args=nil) if(args.nil?) args = AsClassArguments.new args.execution_dir = Dir.pwd args.parse!(ARGV) end return args end def execute name = @arguments.name # Only build class and test case if a name # was provided... if(!name.nil?) is_test_case = false is_interface = false template = '' if(@arguments.mxml) @project['display_object'] = true template = mxml_template elsif(@arguments.component) template = component_template elsif(name.match(/Test$/)) is_test_case = true template = test_case_template elsif(name.match(/^I[A-Z]/) || name.match(/able$/)) is_interface = true template = interface_template else template = class_template end if(!is_test_case) create_class(template) end if(!is_interface && @arguments.build_test_case || is_test_case) template = test_case_template create_test_case(template) end end if(@arguments.build_test_suites) create_test_suites end end def create_class(template_file) suffix = '.' + template_file.split(File::SEPARATOR).pop.split('.').pop target_name = capitalize(@project['class_name'] + suffix) create_file(template_file, @project.source_dir, target_name) end def create_test_case(template_file) suffix = '.' + template_file.split(File::SEPARATOR).pop.split('.').pop target_name = capitalize(@project['class_name'] + 'Test' + suffix) create_file(template_file, @project.test_dir, target_name) end def create_file(template_file, target_dir, target_name) template_base = select_template_base @arguments.verify_template(template_base) source_location = @project.get_template(template_type, File.join(template_base, template_file)) package_dirs = @project['package_name'].split('.').join(File::SEPARATOR) target_location = File.join(target_dir, package_dirs, target_name) @created_files << @project.copy_file(source_location, target_location, true) end def select_template_base if(@arguments.template.nil?) return @project.default_templates[0] else return @arguments.template end end def create_test_suites template_base = select_template_base template = @project.get_template(template_type, File.join(template_base, 'TestSuite.as')) content = IO.read(template) name = 'AllTests.as' suite_generator = TestSuiteGenerator.new(@project.test_dir, name, content) suite_generator.created_files.each do |suite| @created_files << suite end end # package_name # class_name # full_class_name # test_case_name # full_test_case_name # instance_name # add_child_line # remove_child_line def configure_project(name) @project['display_object'] = false @project['package_name'] = get_package_name(name) @project['class_name'] = get_class_name(name) @project['full_class_name'] = @project['package_name'] + '.' + @project['class_name'] @project['test_case_name'] = get_test_case_name(name) @project['full_test_case_name'] = @project['package_name'] + '.' + @project['test_case_name'] @project['instance_name'] = get_instance_name(name) end def get_package_name(name) parts = name.split('.') parts.pop return parts.join('.') end def get_class_name(name) name = name.sub(/Test$/, '') parts = name.split('.') return parts.pop end def get_class_location(name) pkg = get_package_name(name) return pkg.split('.').join(File::SEPARATOR) end def get_test_case_name(name) return get_class_name(name) + 'Test' end def get_instance_name(name) name = get_class_name(name) chars = name.split('') chars[0] = chars[0].downcase return chars.join('') end def template_type return @@TEMPLATE_TYPE end def class_template return @@CLASS_TEMPLATE end def interface_template return @@INTERFACE_TEMPLATE end def test_case_template return @@TEST_CASE_TEMPLATE end def component_template return @@COMPONENT_TEMPLATE end def mxml_template return @@MXML_TEMPLATE end def get_project(dir) if(File.exists?(File.join(dir, '.project'))) return AsClassProject.new(dir) end raise ProjectError.new('AsClass could not find a .project file in the current directory: ' + dir) end end # TODO: Refactor EclipseProject out so that # Project just instantiates a parser as needed # and this class extends the more abstract Project class AsClassProject < EclipseProject def package_name return self['package_name'] end def class_name return self['class_name'] end def full_class_name return self['full_class_name'] end def test_case_name return self['test_case_name'] end def full_test_case_name self['full_test_case_name'] end def instance_name self['instance_name'] end def display_object? self['display_object'] end def add_child_line if(display_object?) return "\n addChild(#{instance_name});" else return "" end end def remove_child_line if(display_object?) return "removeChild(#{instance_name});\n " else return "" end end end end