From 990e352d622e3509907f4fbde5ffc5f8c1948053 Mon Sep 17 00:00:00 2001 From: Watson Date: Fri, 19 Apr 2013 23:02:49 +0900 Subject: [PATCH] add a feature which support the template --- bin/motion | 18 ++- lib/motion/project/app.rb | 153 +++++++++--------- lib/motion/template/gem/README.md.erb | 29 ++++ lib/motion/template/gem/Rakefile.erb | 9 ++ lib/motion/template/gem/app/app_delegate.rb | 5 + .../template/gem/lib/project/{name}.rb.erb | 16 ++ lib/motion/template/gem/lib/{name}.rb | 8 + .../gem/resources}/Default-568h@2x.png | Bin lib/motion/template/gem/spec/main_spec.rb.erb | 9 ++ lib/motion/template/gem/{name}.gemspec.erb | 23 +++ lib/motion/template/ios/.gitignore | 16 ++ lib/motion/template/ios/Rakefile.erb | 8 + lib/motion/template/ios/app/app_delegate.rb | 5 + .../ios/resources/Default-568h@2x.png | Bin 0 -> 2359 bytes lib/motion/template/ios/spec/main_spec.rb.erb | 9 ++ 15 files changed, 225 insertions(+), 83 deletions(-) create mode 100644 lib/motion/template/gem/README.md.erb create mode 100644 lib/motion/template/gem/Rakefile.erb create mode 100644 lib/motion/template/gem/app/app_delegate.rb create mode 100644 lib/motion/template/gem/lib/project/{name}.rb.erb create mode 100644 lib/motion/template/gem/lib/{name}.rb rename {resources => lib/motion/template/gem/resources}/Default-568h@2x.png (100%) create mode 100644 lib/motion/template/gem/spec/main_spec.rb.erb create mode 100644 lib/motion/template/gem/{name}.gemspec.erb create mode 100644 lib/motion/template/ios/.gitignore create mode 100644 lib/motion/template/ios/Rakefile.erb create mode 100644 lib/motion/template/ios/app/app_delegate.rb create mode 100644 lib/motion/template/ios/resources/Default-568h@2x.png create mode 100644 lib/motion/template/ios/spec/main_spec.rb.erb diff --git a/bin/motion b/bin/motion index 953d10a5..a44aca67 100644 --- a/bin/motion +++ b/bin/motion @@ -48,12 +48,22 @@ class CreateCommand < Command self.help = 'Create a new project' def self.run(args) - if args.size != 1 - die "Usage: motion create " + if args.size < 1 || args.size > 2 + die "Usage: motion create [--template=]" end - app_name = args.shift - Motion::Project::App.create(app_name) + app_name = '' + template_name = 'ios' + args.each do |a| + case a + when /--template=(.+)/ + template_name = $1.to_s + else + app_name = a + end + end + + Motion::Project::App.create(app_name, template_name) end end diff --git a/lib/motion/project/app.rb b/lib/motion/project/app.rb index 9fc103ad..f9d88fc2 100644 --- a/lib/motion/project/app.rb +++ b/lib/motion/project/app.rb @@ -21,6 +21,8 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +require 'erb' + module Motion; module Project class App VERBOSE = @@ -80,85 +82,8 @@ module Motion; module Project builder.codesign(config, platform) end - def create(app_name) - unless app_name.match(/^[\w\s-]+$/) - fail "Invalid app name" - end - - if File.exist?(app_name) - fail "Directory `#{app_name}' already exists" - end - - App.log 'Create', app_name - Dir.mkdir(app_name) - Dir.chdir(app_name) do - App.log 'Create', File.join(app_name, '.gitignore') - File.open('.gitignore', 'w') do |io| - io.puts ".repl_history" - io.puts "build" - io.puts "tags" - io.puts "app/pixate_code.rb" - io.puts "resources/*.nib" - io.puts "resources/*.momd" - io.puts "resources/*.storyboardc" - io.puts ".DS_Store" - io.puts "nbproject" - io.puts ".redcar" - io.puts "#*#" - io.puts "*~" - io.puts "*.sw[po]" - io.puts ".eprj" - io.puts ".sass-cache" - io.puts ".idea" - end - App.log 'Create', File.join(app_name, 'Rakefile') - File.open('Rakefile', 'w') do |io| - io.puts < 0 + end + end + + def create_files + Dir.glob(File.join(template_directory, "**/*"), File::FNM_DOTMATCH).each do |src| + dest = src.sub("#{template_directory}/", '') + next if File.directory?(src) + next if dest.include?(".DS_Store") + + dest = replace_file_name(dest) + if dest =~ /(.+)\.erb$/ + App.log 'Create', "#{@app_name}/#{$1}" + File.open($1, "w") { |io| + io.print ERB.new(File.read(src)).result(binding) + } + else + App.log 'Create', "#{@app_name}/#{dest}" + FileUtils.cp(src, dest) + end + end + end + + def replace_file_name(file_name) + file_name = file_name.sub("{name}", "#{@name}") + file_name + end + end end end; end diff --git a/lib/motion/template/gem/README.md.erb b/lib/motion/template/gem/README.md.erb new file mode 100644 index 00000000..5454c741 --- /dev/null +++ b/lib/motion/template/gem/README.md.erb @@ -0,0 +1,29 @@ +# <%= name %> + +TODO: Write a gem description + +## Installation + +Add this line to your application's Gemfile: + + gem '<%= name %>' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install <%= name %> + +## Usage + +TODO: Write usage instructions here + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/lib/motion/template/gem/Rakefile.erb b/lib/motion/template/gem/Rakefile.erb new file mode 100644 index 00000000..2c941b81 --- /dev/null +++ b/lib/motion/template/gem/Rakefile.erb @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- +$:.unshift("/Library/RubyMotion/lib") +require 'motion/project' +require './lib/<%= name %>' + +Motion::Project::App.setup do |app| + # Use `rake config' to see complete project settings. + app.name = '<%= name %>' +end diff --git a/lib/motion/template/gem/app/app_delegate.rb b/lib/motion/template/gem/app/app_delegate.rb new file mode 100644 index 00000000..43b2aa9e --- /dev/null +++ b/lib/motion/template/gem/app/app_delegate.rb @@ -0,0 +1,5 @@ +class AppDelegate + def application(application, didFinishLaunchingWithOptions:launchOptions) + true + end +end diff --git a/lib/motion/template/gem/lib/project/{name}.rb.erb b/lib/motion/template/gem/lib/project/{name}.rb.erb new file mode 100644 index 00000000..28b3bd92 --- /dev/null +++ b/lib/motion/template/gem/lib/project/{name}.rb.erb @@ -0,0 +1,16 @@ +<%= +indent = 0 +string = "" +module_names = name.split(/[-_]/).map{ |x| x.capitalize } +module_names.each do |m| + string.concat(" " * indent + "class #{m}\n") + indent += 2 +end +string = string + "\n" +module_names.size.times do + indent -= 2 + string.concat(" " * indent + "end\n") +end + +string +%> diff --git a/lib/motion/template/gem/lib/{name}.rb b/lib/motion/template/gem/lib/{name}.rb new file mode 100644 index 00000000..4ba8c83d --- /dev/null +++ b/lib/motion/template/gem/lib/{name}.rb @@ -0,0 +1,8 @@ +unless defined?(Motion::Project::Config) + raise "This file must be required within a RubyMotion project Rakefile." +end + +lib_dir_path = File.dirname(File.expand_path(__FILE__)) +Motion::Project::App.setup do |app| + app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb"))) +end diff --git a/resources/Default-568h@2x.png b/lib/motion/template/gem/resources/Default-568h@2x.png similarity index 100% rename from resources/Default-568h@2x.png rename to lib/motion/template/gem/resources/Default-568h@2x.png diff --git a/lib/motion/template/gem/spec/main_spec.rb.erb b/lib/motion/template/gem/spec/main_spec.rb.erb new file mode 100644 index 00000000..a638776e --- /dev/null +++ b/lib/motion/template/gem/spec/main_spec.rb.erb @@ -0,0 +1,9 @@ +describe "Application '<%= name %>'" do + before do + @app = UIApplication.sharedApplication + end + + it "has one window" do + @app.windows.size.should == 1 + end +end diff --git a/lib/motion/template/gem/{name}.gemspec.erb b/lib/motion/template/gem/{name}.gemspec.erb new file mode 100644 index 00000000..b86fa8c1 --- /dev/null +++ b/lib/motion/template/gem/{name}.gemspec.erb @@ -0,0 +1,23 @@ +# -*- encoding: utf-8 -*- +VERSION = "1.0" + +Gem::Specification.new do |spec| + spec.name = "<%= name %>" + spec.version = VERSION + spec.authors = ["<%= `git config --get user.name`.strip %>"] + spec.email = ["<%= `git config --get user.email`.strip %>"] + spec.description = %q{TODO: Write a gem description} + spec.summary = %q{TODO: Write a gem summary} + spec.homepage = "" + spec.license = "" + + files = [] + files << 'README.md' + files.concat(Dir.glob('lib/**/*.rb')) + spec.files = files + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_development_dependency "rake" +end diff --git a/lib/motion/template/ios/.gitignore b/lib/motion/template/ios/.gitignore new file mode 100644 index 00000000..c3576622 --- /dev/null +++ b/lib/motion/template/ios/.gitignore @@ -0,0 +1,16 @@ +.repl_history +build +tags +app/pixate_code.rb +resources/*.nib +resources/*.momd +resources/*.storyboardc +.DS_Store +nbproject +.redcar +#*# +*~ +*.sw[po] +.eprj +.sass-cache +.idea diff --git a/lib/motion/template/ios/Rakefile.erb b/lib/motion/template/ios/Rakefile.erb new file mode 100644 index 00000000..bbf93e3f --- /dev/null +++ b/lib/motion/template/ios/Rakefile.erb @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +$:.unshift("/Library/RubyMotion/lib") +require 'motion/project' + +Motion::Project::App.setup do |app| + # Use `rake config' to see complete project settings. + app.name = '<%= name %>' +end diff --git a/lib/motion/template/ios/app/app_delegate.rb b/lib/motion/template/ios/app/app_delegate.rb new file mode 100644 index 00000000..43b2aa9e --- /dev/null +++ b/lib/motion/template/ios/app/app_delegate.rb @@ -0,0 +1,5 @@ +class AppDelegate + def application(application, didFinishLaunchingWithOptions:launchOptions) + true + end +end diff --git a/lib/motion/template/ios/resources/Default-568h@2x.png b/lib/motion/template/ios/resources/Default-568h@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..0f9e292bfba72151806eeb71a9079db00ccfd8e6 GIT binary patch literal 2359 zcmeAS@N?(olHy`uVBq!ia0y~yU}|7sU@72W28#FwiW&hamUKs7M+SzC{oH>NS%G}x z0G|+7AWbS*%<6U$s7IhA$S-(*&FL$7cXwQ9tNQ%m@6RXy_T7?y#lXNZ+0(@_q~g}w zD<|`s4MZFQjazT_|Nnn$S={j-m6C@HjnvWspC!jvA>;X}Ox2F#~Ar-TOs>+t1j)$Bp9{!X9$#cMK?=H>fe&gIMex~ZX& p_^#b>Yv^6ww1fYnckRmB&ybkZ{yek&A1?zCc)I$ztaD0e0svn$yix!F literal 0 HcmV?d00001 diff --git a/lib/motion/template/ios/spec/main_spec.rb.erb b/lib/motion/template/ios/spec/main_spec.rb.erb new file mode 100644 index 00000000..a638776e --- /dev/null +++ b/lib/motion/template/ios/spec/main_spec.rb.erb @@ -0,0 +1,9 @@ +describe "Application '<%= name %>'" do + before do + @app = UIApplication.sharedApplication + end + + it "has one window" do + @app.windows.size.should == 1 + end +end