command/joybox_generate_command.rb in joybox-1.0.0 vs command/joybox_generate_command.rb in joybox-1.1.0

- old
+ new

@@ -1,114 +1,68 @@ -class String +require 'optparse' +require File.expand_path('../command/string.rb', File.readlink(__FILE__)) +require File.expand_path('../command/template.rb', File.readlink(__FILE__)) - def underscore - gsub(/::/, '/'). - gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). - gsub(/([a-z\d])([A-Z])/,'\1_\2'). - tr("-", "_"). - downcase - end +module Motion + module Project + class JoyboxGenerateCommand < Command - def titleize - gsub(/([A-Z])/, '_\1').split(/_/).map(&:capitalize).join - end + self.name = 'joybox:generate' + self.help = 'Joybox: Class Generator' -end + @@description = <<-EOF + Generates a new class according to the class type and name, those + parameters can be sended either CamelCased or snake_cased. Also a + test class will be generated and placed inside the spec folder. + Supported Classes: Sprite, Layer, Scene + EOF -module Motion; module Project - class JoyboxGenerateCommand < Command + @@example = <<-EOF + 'motion jb:generate layer game' - self.name = 'joybox:generate' - self.help = 'Joybox: Class Generator' + This will generate a Layer class named 'GameLayer' with the + following files: + class: 'app/layers/game_layer.rb' + spec: 'spec/layers/game_layer_spec.rb' + + The list of posible paths according to the class type: + Sprite: app/sprites/ - spec/sprites/ + Layer: app/layers/ - spec/layers/ + Scene: app/scenes/ - spec/scenes/ + EOF - attr_reader :name + def run(args) + opt_parser = OptionParser.new do |opt| + opt.banner = "Usage:" + opt.separator " motion joybox:generate <class-type> <file-name>" + opt.separator "" + opt.separator "Options:" - def run(args) + opt.on('-h', '--help', 'Shows this screen') - if args.empty? or args.size != 2 - show_help - end + opt.separator "" + opt.separator "Description:" + opt.separator @@description + opt.separator "" + opt.separator "Example:" + opt.separator @@example + end - klass = args[0] - name = args[1] + opt_parser.parse! + return puts opt_parser if args.size != 2 - template_class_file = File.expand_path("../joybox/templates/#{klass}.erb", __FILE__) - template_spec_file = File.expand_path("../joybox/templates/#{klass}_spec.erb", __FILE__) + klass = args[0] + name = args[1] - unless File.exists?(template_class_file) - show_help + case klass + when /(\bsprite\b|\blayer\b|\bscene\b)/i + template = Joybox::Command::Template.new(name, klass) + template.save + else + puts opt_parser + end end - begin - app_directory = find_app_directory - spec_directory = find_spec_directory - rescue - die 'The command needs to run inside of a RubyMotion project' - end - - class_file = File.join(app_directory, "#{klass}s/#{name.underscore}_#{klass}.rb") - test_file = File.join(spec_directory, "#{klass}s/#{name.underscore}_#{klass}_spec.rb") - - if File.exists?(class_file) or File.exists?(test_file) - App.log 'Duplicate', "#{class_file} exists" if File.exists?(class_file) - App.log 'Duplicate', "#{test_file} exists" if File.exists?(test_file) - exit 1 - end - - create_directory(File.dirname(class_file)) - create_directory(File.dirname(test_file)) - - @name = name.titleize - create_file(class_file, template_class_file) - create_file(test_file, template_spec_file) end - - - def find_app_directory(directory = 'app') - - finded_directories = Dir.glob(directory) - - if finded_directories.size != 1 - - find_app_directory(File.join('..', directory)) - else - - finded_directories[0] - end - end - - - def find_spec_directory(directory = 'spec') - - finded_directories = Dir.glob(directory) - - if finded_directories.size != 1 - - find_app_directory(File.join('..', directory)) - else - - finded_directories[0] - end - end - - - def create_directory(directory) - FileUtils.mkdir_p(directory) unless File.directory?(directory) - end - - - def create_file(file_path, template_path) - File.open(file_path, "w") { |io| - io.print ERB.new(File.read(template_path)).result(binding) - } - App.log 'Create', "#{file_path}" - end - - - def show_help - usage_file = File.expand_path("../joybox/USAGE", __FILE__) - die File.read(usage_file) - end - end -end; end \ No newline at end of file +end