diff --git a/lib/motion/project/command/create.rb b/lib/motion/project/command/create.rb index 477fa1ba..51df7880 100644 --- a/lib/motion/project/command/create.rb +++ b/lib/motion/project/command/create.rb @@ -22,15 +22,18 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. require 'motion/project/app' +require 'motion/project/template' module Motion; module Project class CreateCommand < Command self.name = 'create' self.help = 'Create a new project' - + + DefaultTemplate = 'ios' + def run(args) app_name = nil - template_name = 'ios' + template_name = DefaultTemplate args.each do |a| case a when /--([^=]+)=(.+)/ @@ -53,7 +56,9 @@ module Motion; module Project end unless app_name - die "Usage: motion create [--template=] " + $stderr.puts "Usage: motion create [--template=] " + $stderr.puts "Available templates: " + Motion::Project::Template.all_templates.keys.map { |x| x == DefaultTemplate ? "#{x} (default)" : x }.join(', ') + exit 1 end Motion::Project::App.create(app_name, template_name) diff --git a/lib/motion/project/template.rb b/lib/motion/project/template.rb index 62e82b72..46014121 100644 --- a/lib/motion/project/template.rb +++ b/lib/motion/project/template.rb @@ -33,13 +33,26 @@ module Motion; module Project File.expand_path(File.join(ENV['HOME'], 'Library/RubyMotion/template')) ] + def self.all_templates + @all_templates ||= begin + h = {} + Paths.map { |path| Dir.glob(path + '/*') }.flatten.select { |x| !x.match(/^\./) and File.directory?(x) }.each do |template_path| + h[File.basename(template_path)] = template_path + end + h + end + end + + Templates = Paths.map { |path| Dir.glob(path + '/*') }.flatten.select { |x| !x.match(/^\./) and File.directory?(x) }.map { |x| File.basename(x) } + def initialize(app_name, template_name) @name = @app_name = app_name @template_name = template_name.to_s - @template_directory = Paths.map { |x| File.join(x, @template_name) }.find { |x| File.exist?(x) } + @template_directory = self.class.all_templates[@template_name] unless @template_directory $stderr.puts "Cannot find template `#{@template_name}' in #{Paths.join(' or ')}" + $stderr.puts "Available templates: " + self.class.all_templates.keys.join(', ') exit 1 end